get_completion works but lsp doesn't

This commit is contained in:
2024-06-04 22:57:57 +02:00
parent 35653cff1f
commit a6a5b46a01
5 changed files with 181 additions and 56 deletions
+74 -17
View File
@@ -1,17 +1,41 @@
// use std::collections::HashMap;
// use std::fs;
use std::collections::HashMap;
use std::vec;
use lspower::{self, LspService, Server};
use lspower::jsonrpc::Result;
use lspower::lsp::{self, *};
use lspower::{Client, LanguageServer};
use rand::{thread_rng, Rng};
use crate::parser::{new_vars, Variable};
use crate::transpiler::Transpiler;
#[derive(Debug)]
struct Backend {
client: Client,
client: Client
}
#[derive(Debug)]
struct DocumentManager {
documents: HashMap<String, String>, // Key: Document URI, Value: Document content
}
impl DocumentManager {
fn new() -> DocumentManager {
DocumentManager {
documents: HashMap::new()
}
}
fn update_document(&mut self, params: DidChangeTextDocumentParams) {
let uri = params.text_document.uri.to_string(); // Convert URI to string
let content = params.content_changes.last().unwrap().text.clone(); // Access latest content
self.documents.insert(uri, content);
}
fn get_document(&self, uri: &str) -> Option<&str> {
self.documents.get(uri).map(|x| x.as_str())
}
}
#[lspower::async_trait]
@@ -38,9 +62,23 @@ impl LanguageServer for Backend {
async fn completion(
&self,
_params: lsp::CompletionParams,
params: lsp::CompletionParams,
) -> lspower::jsonrpc::Result<Option<lsp::CompletionResponse>> {
let completion_items: Vec<CompletionItem> = Vec::new();
// 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)))
}
@@ -54,21 +92,40 @@ pub async fn run_lsp_server() {
let stdin = tokio::io::stdin();
let stdout = tokio::io::stdout();
let (service, messages) = LspService::new(|client| Backend { client });
let (service, messages) = LspService::new(|client| Backend {client});
Server::new(stdin, stdout)
.interleave(messages)
.serve(service)
.await;
}
// #[tokio::main]
// pub async fn run_lsp_server() {
// let mut transpiler = Transpiler {
// var_match: String::from("x"),
// var_state: LexerState { line: 6, column: 9 },
// ..Default::default()
// };
// let file_content = fs::read_to_string("main.wt").expect("Error reading file");
// transpiler.transpile(file_content, 0, HashMap::new());
// println!("{:?}", transpiler.matched_vars);
// }
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
}