attempt lsp from scratch

This commit is contained in:
2024-06-05 17:43:05 +02:00
parent c5331279da
commit 57d99b9526
3 changed files with 20 additions and 109 deletions
+1
View File
@@ -14,3 +14,4 @@ tokio = { version = "1.38.0", features = ["full"] }
tokio-macros = "2.3.0" tokio-macros = "2.3.0"
rand = "0.8.5" rand = "0.8.5"
serde_json = "1.0" serde_json = "1.0"
lsp-types = "0.97.0"
+17 -108
View File
@@ -1,115 +1,24 @@
// use std::collections::HashMap; use lsp_types;
// use std::fs;
use crate::parser::{new_vars, Variable};
use crate::transpiler::Transpiler;
use lspower::jsonrpc::Result;
use lspower::lsp::{self, *};
use lspower::{self, LspService, Server};
use lspower::{Client, LanguageServer};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use rand::{thread_rng, Rng}; use regex::Regex;
use std::collections::HashMap; use std::io::{stdin, stdout, Read};
use std::vec;
#[derive(Debug)] pub fn run_lsp_server() {
struct Backend { let clpattern = Lazy::new(|| Regex::new(r"Content-Length: (\d+)\r\n").unwrap());
client: Client, let mut sin = stdin();
} let mut sout = stdout();
static DOCUMENTS: Lazy<HashMap<Url, String>> = Lazy::new(|| HashMap::default()); println!("ok");
#[lspower::async_trait] loop {
impl LanguageServer for Backend { let mut input = String::new();
async fn initialize(&self, _: InitializeParams) -> Result<InitializeResult> { let _ = sin.read_line(&mut input);
// Ok(InitializeResult::default()) if let Some(caps) = clpattern.captures(&input) {
Ok(InitializeResult { let x = 30;
capabilities: ServerCapabilities { let mut buf = vec![0u8; x];
completion_provider: Some(CompletionOptions { sin.read_exact(&mut buf);
trigger_characters: vec!["::".to_string()].into(),
..CompletionOptions::default() println!("{}", &caps[1])
}),
..ServerCapabilities::default()
},
..InitializeResult::default()
})
} }
async fn initialized(&self, _: InitializedParams) {
self.client
.log_message(MessageType::INFO, "Wyst LSP initialized!")
.await;
}
async fn did_change(&self, params: lsp::DidChangeTextDocumentParams) {
// DOCUMENTS.insert(params.text_document.uri, d);
}
async fn completion(
&self,
params: lsp::CompletionParams,
) -> lspower::jsonrpc::Result<Option<lsp::CompletionResponse>> {
// let document_manager = &self.document;
// let uri = params.text_document_position.text_document.uri.to_string();
// let document_content = document_manager.get_document(&uri).unwrap_or_default(); // Handle missing document
// let variables = get_completion(
// document_content.to_string(),
// 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, _) in variables {
// completion_items.push(CompletionItem::new_simple(name, "".to_string()));
// }
Ok(Some(CompletionResponse::Array(completion_items)))
}
async fn shutdown(&self) -> Result<()> {
Ok(())
} }
} }
#[tokio::main]
pub async fn run_lsp_server() {
let stdin = tokio::io::stdin();
let stdout = tokio::io::stdout();
let (service, messages) = LspService::new(|client| Backend { client });
Server::new(stdin, stdout)
.interleave(messages)
.serve(service)
.await;
}
pub fn place_at(input: String, in2: String, line_goal: usize, column_goal: usize) -> String {
let mut line: usize = 1;
let mut column: usize = 0;
let mut out = String::new();
for c in input.chars() {
out += c.to_string().as_str();
column += 1;
if c == '\n' {
line += 1;
column = 0;
} else if line == line_goal && column == column_goal {
out += in2.as_str();
column += 1
}
}
out
}
pub fn get_completion(input: String, line: usize, column: usize) -> HashMap<String, Variable> {
let rand_id: u32 = thread_rng().gen();
let rand_id: String = rand_id.to_string();
let mut transpiler = Transpiler {
peek: rand_id.clone(),
..Default::default()
};
transpiler.transpile(place_at(input, rand_id, line, column), 0, new_vars());
transpiler.matched_vars
}
+1
View File
@@ -7,6 +7,7 @@ use clap::Parser;
use parser::new_vars; use parser::new_vars;
use std::{fs, path::Path}; use std::{fs, path::Path};
use transpiler::Transpiler; use transpiler::Transpiler;
mod lspcom;
use crate::lsp::run_lsp_server; use crate::lsp::run_lsp_server;