Got the lsp working

This commit is contained in:
2024-06-06 23:13:48 +02:00
parent ed60ab7af4
commit 8348c36394
2 changed files with 61 additions and 32 deletions
+41 -25
View File
@@ -1,4 +1,7 @@
use crate::lspcom::{request_methods, LspServer}; use crate::{
lspcom::{get_completion, request_methods, LspServer, TextDocumentChangeParams},
parser::VariableType,
};
use lsp_types::*; use lsp_types::*;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
@@ -13,6 +16,7 @@ use std::{
#[derive(Debug)] #[derive(Debug)]
pub struct Server { pub struct Server {
documents: HashMap<String, String>, documents: HashMap<String, String>,
file: File,
} }
impl LspServer for Server { impl LspServer for Server {
@@ -28,11 +32,36 @@ impl LspServer for Server {
..Default::default() ..Default::default()
} }
} }
fn completion(&mut self) -> CompletionResponse { fn completion(&mut self, params: CompletionParams) -> CompletionResponse {
CompletionResponse::Array(vec![CompletionItem::new_simple( let text = self
"mytest".to_string(), .documents
"mydetail".to_string(), .get(params.text_document_position.text_document.uri.as_str())
)]) .expect("err_textdoc");
let items = get_completion(
text.clone(),
params.text_document_position.position.line as usize + 1,
params.text_document_position.position.character as usize,
);
let mut completion_items: Vec<CompletionItem> = Vec::new();
for (name, var) in items {
let mut item = CompletionItem::new_simple(name, "mydetail".to_string());
match var.vtype {
VariableType::Func => {
item.kind = Some(CompletionItemKind::FUNCTION);
}
VariableType::Var => {
item.kind = Some(CompletionItemKind::VARIABLE);
}
VariableType::Keyword => {
item.kind = Some(CompletionItemKind::KEYWORD);
}
}
completion_items.push(item);
}
CompletionResponse::Array(completion_items)
}
fn did_change(&mut self, params: TextDocumentChangeParams) {
self.documents.insert(params.uri, params.text);
} }
} }
@@ -40,39 +69,31 @@ pub fn run_lsp_server() {
if Path::new("/home/leo/work/wyst/log.txt").exists() { if Path::new("/home/leo/work/wyst/log.txt").exists() {
fs::remove_file("/home/leo/work/wyst/log.txt").unwrap(); fs::remove_file("/home/leo/work/wyst/log.txt").unwrap();
} }
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 clpattern = Lazy::new(|| Regex::new(r"^Content-Length: (\d+)").unwrap());
let mut reader = stdin(); let mut reader = stdin();
let mut server = Server { let mut server = Server {
documents: HashMap::new(), documents: HashMap::new(),
file: File::create("/home/leo/work/wyst/log.txt").expect("err_log"),
}; };
writeln!(file, "It's running!").unwrap(); writeln!(server.file, "It's running!").unwrap();
loop { loop {
writeln!(file, "waiting").unwrap();
let mut input = String::new(); let mut input = String::new();
if reader.read_line(&mut input).is_err() { if reader.read_line(&mut input).is_err() {
writeln!(file, "Error reading line").unwrap();
continue; continue;
} }
writeln!(file, "{}", input).unwrap();
let stdout = stdout(); let stdout = stdout();
let mut handle = stdout.lock(); let mut handle = stdout.lock();
if let Some(caps) = clpattern.captures(&input) { if let Some(caps) = clpattern.captures(&input) {
let content_len = caps[1].parse::<usize>().unwrap() + 2; let content_len = caps[1].parse::<usize>().unwrap() + 2;
writeln!(file, "ok {}", content_len).unwrap();
let mut buf = vec![0u8; content_len]; let mut buf = vec![0u8; content_len];
if reader.read_exact(&mut buf).is_err() { if reader.read_exact(&mut buf).is_err() {
writeln!(file, "Error reading content").unwrap();
continue; continue;
} }
let json_string = String::from_utf8(buf).expect("Our bytes should be valid utf8"); let json_string = String::from_utf8(buf).expect("Our bytes should be valid utf8");
writeln!(file, "{}", &json_string).unwrap();
let client_json: Value = serde_json::from_str(&json_string).expect("err_json"); let client_json: Value = serde_json::from_str(&json_string).expect("err_json");
let response = match client_json["method"].as_str().unwrap() { let response = match client_json["method"].as_str().unwrap() {
request_methods::INITIALIZE => { request_methods::INITIALIZE => {
writeln!(file, "test").unwrap();
serde_json::to_string(&json!({ serde_json::to_string(&json!({
"jsonrpc": "2.0", "jsonrpc": "2.0",
"id": client_json["id"].as_u64().unwrap(), "id": client_json["id"].as_u64().unwrap(),
@@ -83,20 +104,17 @@ pub fn run_lsp_server() {
request_methods::COMPLETION => serde_json::to_string(&json!({ request_methods::COMPLETION => serde_json::to_string(&json!({
"jsonrpc": "2.0", "jsonrpc": "2.0",
"id": client_json["id"].as_u64().unwrap(), "id": client_json["id"].as_u64().unwrap(),
"result": server.completion() "result": server.completion(serde_json::from_value(serde_json::to_value(client_json["params"].as_object()).expect("err_pars2")).unwrap())
})) }))
.unwrap(), .unwrap(),
request_methods::DID_CHANGE =>{
server.did_change(serde_json::from_value(serde_json::to_value(client_json["params"].as_object()).expect("err_pars2")).expect("err_pars3"));
"None".to_string()},
request_methods::INITIALIZED => "None".to_string(), request_methods::INITIALIZED => "None".to_string(),
request_methods::SHUTDOWN => { request_methods::SHUTDOWN => {
return; return;
} }
_ => { _ => {
writeln!(
file,
"other request {}",
client_json["method"].as_str().unwrap()
)
.unwrap();
continue; continue;
} }
}; };
@@ -110,10 +128,8 @@ pub fn run_lsp_server() {
.write_all(header.as_bytes()) .write_all(header.as_bytes())
.expect("err_write_stdin"); .expect("err_write_stdin");
handle.flush().expect("err_flush_stdin"); handle.flush().expect("err_flush_stdin");
writeln!(file, "{}", header).unwrap();
} }
} else { } else {
writeln!(file, "not match").unwrap();
} }
} }
} }
+20 -7
View File
@@ -1,8 +1,9 @@
use std::collections::HashMap;
use crate::transpiler::Transpiler;
use lsp_types::{CompletionResponse, InitializeResult};
use rand::{thread_rng, Rng};
use crate::parser::{new_vars, Variable}; use crate::parser::{new_vars, Variable};
use crate::transpiler::Transpiler;
use lsp_types::{CompletionParams, CompletionResponse, InitializeResult};
use rand::{thread_rng, Rng};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub fn place_at(input: String, in2: String, line_goal: usize, column_goal: usize) -> String { pub fn place_at(input: String, in2: String, line_goal: usize, column_goal: usize) -> String {
let mut line: usize = 1; let mut line: usize = 1;
@@ -38,9 +39,21 @@ pub mod request_methods {
pub const COMPLETION: &str = "textDocument/completion"; pub const COMPLETION: &str = "textDocument/completion";
pub const INITIALIZED: &str = "initialized"; pub const INITIALIZED: &str = "initialized";
pub const SHUTDOWN: &str = "shutdown"; pub const SHUTDOWN: &str = "shutdown";
pub const DID_CHANGE: &str = "textDocument/didChange";
} }
pub trait LspServer { pub trait LspServer {
fn completion(&mut self) -> CompletionResponse { CompletionResponse::Array(vec![]) } fn did_change(&mut self, _params: TextDocumentChangeParams) {}
fn initialize(&mut self) -> InitializeResult { InitializeResult::default() } fn completion(&mut self, _params: CompletionParams) -> CompletionResponse {
} CompletionResponse::Array(vec![])
}
fn initialize(&mut self) -> InitializeResult {
InitializeResult::default()
}
}
#[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
pub struct TextDocumentChangeParams {
pub uri: String,
pub text: String,
}