From c5331279da543b38157fab2fe327d44d8f34c9dd Mon Sep 17 00:00:00 2001 From: Leo dev Date: Wed, 5 Jun 2024 15:58:17 +0200 Subject: [PATCH] plan b --- Cargo.toml | 1 + src/lsp.rs | 64 ++++++++-------------- src/transpiler.rs | 137 ++++++++++++++++++++++++++++++++-------------- 3 files changed, 120 insertions(+), 82 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4e703a6..46d0e8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,4 @@ lspower = "1.5.0" tokio = { version = "1.38.0", features = ["full"] } tokio-macros = "2.3.0" rand = "0.8.5" +serde_json = "1.0" diff --git a/src/lsp.rs b/src/lsp.rs index 3d860b9..ba267f7 100644 --- a/src/lsp.rs +++ b/src/lsp.rs @@ -1,42 +1,23 @@ // 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; +use lspower::jsonrpc::Result; +use lspower::lsp::{self, *}; +use lspower::{self, LspService, Server}; +use lspower::{Client, LanguageServer}; +use once_cell::sync::Lazy; +use rand::{thread_rng, Rng}; +use std::collections::HashMap; +use std::vec; #[derive(Debug)] struct Backend { - client: Client + client: Client, } -#[derive(Debug)] -struct DocumentManager { - documents: HashMap, // 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()) - } -} +static DOCUMENTS: Lazy> = Lazy::new(|| HashMap::default()); #[lspower::async_trait] impl LanguageServer for Backend { @@ -60,6 +41,11 @@ impl LanguageServer for Backend { .await; } + async fn did_change(&self, params: lsp::DidChangeTextDocumentParams) { + // DOCUMENTS.insert(params.text_document.uri, d); + + } + async fn completion( &self, params: lsp::CompletionParams, @@ -67,18 +53,18 @@ impl LanguageServer for Backend { // 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 = Vec::new(); // for (name, _) in variables { - // completion_items.push(CompletionItem::new_simple(name, "".to_string())); + // completion_items.push(CompletionItem::new_simple(name, "".to_string())); // } - + Ok(Some(CompletionResponse::Array(completion_items))) } @@ -92,7 +78,7 @@ 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) @@ -104,14 +90,14 @@ pub fn place_at(input: String, in2: String, line_goal: usize, column_goal: usize let mut column: usize = 0; let mut out = String::new(); for c in input.chars() { - out+=c.to_string().as_str(); + 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 + column += 1 } } out @@ -124,8 +110,6 @@ pub fn get_completion(input: String, line: usize, column: usize) -> HashMap) -> String { + pub fn transpile( + &mut self, + input: String, + indent: u32, + variables: HashMap, + ) -> String { let mut variables = variables; let mut result = String::new(); if indent == 0 { @@ -50,11 +55,13 @@ impl Transpiler { }; for ast in full_ast.parse() { variables = full_ast.variables.clone(); - if ast.ast_type == AstType::Other && ast.tokens[0].token_type == TokenType::Identifier - && ast.tokens[0].value.contains(&self.peek) && self.peek!="" { - + if ast.ast_type == AstType::Other + && ast.tokens[0].token_type == TokenType::Identifier + && ast.tokens[0].value.contains(&self.peek) + && self.peek != "" + { let ctoken = &ast.tokens[0]; - let pname = ctoken.value.split(&self.peek).next().unwrap(); + // let pname = ctoken.value.split(&self.peek).next().unwrap(); for (name, var) in variables.clone() { if ctoken.line > var.state.line || var.vtype != VariableType::Var { self.matched_vars.insert(name, var); @@ -93,7 +100,11 @@ impl Transpiler { ast.tokens[1].value, self.transpile_round(ast.tokens[2].value.clone(), variables.clone()), ast.tokens[0].value, - self.transpile(ast.tokens[3].value.clone(), indent + 1, variables.clone()) + self.transpile( + ast.tokens[3].value.clone(), + indent + 1, + variables.clone() + ) ) .as_str(); } else if ast.ast_type == AstType::VoidFunctionDeceleration { @@ -104,7 +115,11 @@ impl Transpiler { "fn {}({}) {}", ast.tokens[1].value, self.transpile_round(ast.tokens[2].value.clone(), variables.clone()), - self.transpile(ast.tokens[3].value.clone(), indent + 1, variables.clone()) + self.transpile( + ast.tokens[3].value.clone(), + indent + 1, + variables.clone() + ) ) .as_str(); } else if ast.ast_type == AstType::StructDeceleration { @@ -142,9 +157,11 @@ impl Transpiler { } else if ast.ast_type == AstType::Other && ast.tokens[0].token_type == TokenType::Round { - result += - format!("({})", self.transpile_round(ast.tokens[0].value.clone(), variables.clone())) - .as_str(); + result += format!( + "({})", + self.transpile_round(ast.tokens[0].value.clone(), variables.clone()) + ) + .as_str(); } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square { result += format!( @@ -154,7 +171,8 @@ impl Transpiler { LexerState { line: ast.tokens[0].line, column: ast.tokens[0].column - }, variables.clone() + }, + variables.clone() ) ) .as_str(); @@ -176,7 +194,8 @@ impl Transpiler { LexerState { line: ast.tokens[0].line, column: ast.tokens[0].column, - }, variables.clone(), + }, + variables.clone(), ) .as_str(); // } @@ -221,14 +240,22 @@ impl Transpiler { "{} {} {}", ast.tokens[0].value.clone(), self.transpile_round(ast.tokens[1].value.clone(), variables.clone()), - self.transpile(ast.tokens[2].value.clone(), indent + 1, variables.clone()), + self.transpile( + ast.tokens[2].value.clone(), + indent + 1, + variables.clone() + ), ) .as_str(); } else if ast.ast_type == AstType::State2 { result += format!( "{} {}", ast.tokens[0].value.clone(), - self.transpile(ast.tokens[1].value.clone(), indent + 1, variables.clone()), + self.transpile( + ast.tokens[1].value.clone(), + indent + 1, + variables.clone() + ), ) .as_str(); } else if ast.ast_type == AstType::Namespace { @@ -300,16 +327,16 @@ impl Transpiler { result = result.trim_end().to_string(); // if self.var_match != "" && (self.state.line + input.len()) > self.var_state.line { - // for (name, var) in variables { - // if (self.var_state.line > var.line - // || (self.var_state.line == var.line - // && self.var_state.column > var.column)) - // && name.to_lowercase().starts_with(&self.var_match) - // { - // self.matched_vars.insert(name, var); - // } - // } - // self.var_match = String::new(); + // for (name, var) in variables { + // if (self.var_state.line > var.line + // || (self.var_state.line == var.line + // && self.var_state.column > var.column)) + // && name.to_lowercase().starts_with(&self.var_match) + // { + // self.matched_vars.insert(name, var); + // } + // } + // self.var_match = String::new(); // } if indent > 0 { @@ -341,7 +368,11 @@ impl Transpiler { .expect("Error writing file"); modname } - pub fn transpile_round(&mut self, input: String, variables: HashMap) -> String { + pub fn transpile_round( + &mut self, + input: String, + variables: HashMap, + ) -> String { let mut result = String::new(); let lexer_out = lex(input.as_str(), false, self.state); match lexer_out { @@ -383,9 +414,11 @@ impl Transpiler { .as_str(); } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round { - result += - format!("({})", self.transpile_round(ast.tokens[0].value.clone(), variables.clone())) - .as_str(); + result += format!( + "({})", + self.transpile_round(ast.tokens[0].value.clone(), variables.clone()) + ) + .as_str(); } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square { result += format!( @@ -395,7 +428,8 @@ impl Transpiler { LexerState { line: ast.tokens[0].line, column: ast.tokens[0].column - }, variables.clone() + }, + variables.clone() ) ) .as_str(); @@ -409,7 +443,8 @@ impl Transpiler { LexerState { line: ast.tokens[0].line, column: ast.tokens[0].column, - }, variables.clone(), + }, + variables.clone(), ) .as_str(); } else if ast.ast_type == AstType::StructCall { @@ -436,7 +471,12 @@ impl Transpiler { } } - pub fn transpile_square(&mut self, input: String, state: LexerState, variables: HashMap) -> String { + pub fn transpile_square( + &mut self, + input: String, + state: LexerState, + variables: HashMap, + ) -> String { let mut result = String::new(); let lexer_out = lex(input.as_str(), false, state); match lexer_out { @@ -476,9 +516,11 @@ impl Transpiler { .as_str(); } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round { - result += - format!("({})", self.transpile_round(ast.tokens[0].value.clone(), variables.clone())) - .as_str(); + result += format!( + "({})", + self.transpile_round(ast.tokens[0].value.clone(), variables.clone()) + ) + .as_str(); } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square { result += format!( @@ -488,7 +530,8 @@ impl Transpiler { LexerState { line: ast.tokens[0].line, column: ast.tokens[0].column - }, variables.clone() + }, + variables.clone() ) ) .as_str(); @@ -502,7 +545,8 @@ impl Transpiler { LexerState { line: ast.tokens[0].line, column: ast.tokens[0].column, - }, variables.clone(), + }, + variables.clone(), ) .as_str(); } else if ast.ast_type == AstType::StructCall { @@ -526,7 +570,12 @@ impl Transpiler { } } - pub fn transpile_json(&mut self, input: String, state: LexerState, variables: HashMap) -> String { + pub fn transpile_json( + &mut self, + input: String, + state: LexerState, + variables: HashMap, + ) -> String { let mut result = String::new(); let lexer_out = lex(input.as_str(), false, state); match lexer_out { @@ -568,9 +617,11 @@ impl Transpiler { result += ")"; } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round { - result += - format!("({})", self.transpile_round(ast.tokens[0].value.clone(), variables.clone())) - .as_str(); + result += format!( + "({})", + self.transpile_round(ast.tokens[0].value.clone(), variables.clone()) + ) + .as_str(); } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square { result += format!( @@ -580,7 +631,8 @@ impl Transpiler { LexerState { line: ast.tokens[0].line, column: ast.tokens[0].column - }, variables.clone() + }, + variables.clone() ) ) .as_str(); @@ -592,7 +644,8 @@ impl Transpiler { LexerState { line: ast.tokens[0].line, column: ast.tokens[0].column, - }, variables.clone(), + }, + variables.clone(), ) .as_str(); } else {