working lsp

This commit is contained in:
2024-06-06 17:42:28 +02:00
parent 7699809a28
commit ed60ab7af4
4 changed files with 146 additions and 22 deletions
+98 -19
View File
@@ -1,40 +1,119 @@
use crate::lspcom::{request_methods, LspServer};
use lsp_types::*;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::{self, Value};
use serde_json::{self, json, Value};
use std::{
collections::HashMap,
fs::{self, File},
io::{stdin, Read, Write},
io::{stdin, stdout, Read, Write},
path::Path,
};
pub fn run_method<F>(func: F) {}
#[derive(Debug)]
pub struct Server {
documents: HashMap<String, String>,
}
impl LspServer for Server {
fn initialize(&mut self) -> InitializeResult {
InitializeResult {
capabilities: ServerCapabilities {
completion_provider: Some(CompletionOptions {
trigger_characters: Some(vec![".".to_string()]),
..Default::default()
}),
..Default::default()
},
..Default::default()
}
}
fn completion(&mut self) -> CompletionResponse {
CompletionResponse::Array(vec![CompletionItem::new_simple(
"mytest".to_string(),
"mydetail".to_string(),
)])
}
}
pub fn run_lsp_server() {
if Path::new("/Users/klestiselimaj/work/wyst/log.txt").exists() {
fs::remove_file("/Users/klestiselimaj/work/wyst/log.txt");
if Path::new("/home/leo/work/wyst/log.txt").exists() {
fs::remove_file("/home/leo/work/wyst/log.txt").unwrap();
}
let mut file = File::create("/Users/klestiselimaj/work/wyst/log.txt").expect("err_log");
let mut file = File::create("/home/leo/work/wyst/log.txt").expect("err_log");
let clpattern = Lazy::new(|| Regex::new(r"^Content-Length: (\d+)").unwrap());
let mut reader = stdin();
// let mut sout = stdout();
writeln!(file, "It's running!");
let mut server = Server {
documents: HashMap::new(),
};
writeln!(file, "It's running!").unwrap();
loop {
writeln!(file, "waiting").unwrap();
let mut input = String::new();
let _ = reader.read_line(&mut input);
writeln!(file, "{}", &input);
if reader.read_line(&mut input).is_err() {
writeln!(file, "Error reading line").unwrap();
continue;
}
writeln!(file, "{}", input).unwrap();
let stdout = stdout();
let mut handle = stdout.lock();
if let Some(caps) = clpattern.captures(&input) {
let conent_len = *(&caps[1].parse::<usize>().unwrap()) + 2;
writeln!(file, "ok {}", conent_len);
let mut buf = vec![0u8; conent_len];
let _ = reader.read_exact(&mut buf);
let content_len = caps[1].parse::<usize>().unwrap() + 2;
writeln!(file, "ok {}", content_len).unwrap();
let mut buf = vec![0u8; content_len];
if reader.read_exact(&mut buf).is_err() {
writeln!(file, "Error reading content").unwrap();
continue;
}
let json_string = String::from_utf8(buf).expect("Our bytes should be valid utf8");
writeln!(file, "{}", &json_string);
writeln!(file, "{}", &json_string).unwrap();
let client_json: Value = serde_json::from_str(&json_string).expect("err_json");
writeln!(file, "bruh {}", &client_json["method"].as_str().unwrap());
let response = match client_json["method"].as_str().unwrap() {
request_methods::INITIALIZE => {
writeln!(file, "test").unwrap();
serde_json::to_string(&json!({
"jsonrpc": "2.0",
"id": client_json["id"].as_u64().unwrap(),
"result": server.initialize()
}))
.unwrap()
}
request_methods::COMPLETION => serde_json::to_string(&json!({
"jsonrpc": "2.0",
"id": client_json["id"].as_u64().unwrap(),
"result": server.completion()
}))
.unwrap(),
request_methods::INITIALIZED => "None".to_string(),
request_methods::SHUTDOWN => {
return;
}
_ => {
writeln!(
file,
"other request {}",
client_json["method"].as_str().unwrap()
)
.unwrap();
continue;
}
};
if response != "None" {
let header = format!(
"Content-Length: {}\r\n\r\n{}",
response.trim().len(),
response.trim()
);
handle
.write_all(header.as_bytes())
.expect("err_write_stdin");
handle.flush().expect("err_flush_stdin");
writeln!(file, "{}", header).unwrap();
}
} else {
writeln!(file, "not match").unwrap();
}
}
}