From c7aacfe75659c1b03545a28ce5a6f543adfba967 Mon Sep 17 00:00:00 2001 From: sofia Date: Tue, 29 Jul 2025 21:56:50 +0300 Subject: [PATCH] Refactor code a little bit --- reid-lsp/src/main.rs | 126 +++++++++++++++++++++---------------------- 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/reid-lsp/src/main.rs b/reid-lsp/src/main.rs index 1b0bd40..6b2e221 100644 --- a/reid-lsp/src/main.rs +++ b/reid-lsp/src/main.rs @@ -3,12 +3,12 @@ use std::path::PathBuf; use dashmap::DashMap; use reid::ast::lexer::{FullToken, Position}; +use reid::error_raporting::{ErrorModules, ReidError}; use reid::mir::{ self, Context, FunctionCall, FunctionDefinition, FunctionParam, IfExpression, SourceModuleId, StructType, TypeKind, WhileStatement, }; use reid::{compile_module, parse_module, perform_all_passes}; -use tower_lsp::jsonrpc::Result; use tower_lsp::lsp_types::{ self, CompletionItem, CompletionOptions, CompletionParams, CompletionResponse, Diagnostic, DiagnosticSeverity, DidChangeTextDocumentParams, DidOpenTextDocumentParams, Hover, HoverContents, HoverParams, HoverProviderCapability, @@ -16,7 +16,7 @@ use tower_lsp::lsp_types::{ TextDocumentItem, TextDocumentSyncCapability, TextDocumentSyncKind, TextDocumentSyncOptions, WorkspaceFoldersServerCapabilities, WorkspaceServerCapabilities, }; -use tower_lsp::{Client, LanguageServer, LspService, Server}; +use tower_lsp::{Client, LanguageServer, LspService, Server, jsonrpc}; #[derive(Debug)] struct Backend { @@ -28,7 +28,7 @@ struct Backend { #[tower_lsp::async_trait] impl LanguageServer for Backend { - async fn initialize(&self, _: InitializeParams) -> Result { + async fn initialize(&self, _: InitializeParams) -> jsonrpc::Result { self.client .log_message(MessageType::INFO, "Initializing Reid Language Server") .await; @@ -64,18 +64,18 @@ impl LanguageServer for Backend { .await; } - async fn shutdown(&self) -> Result<()> { + async fn shutdown(&self) -> jsonrpc::Result<()> { Ok(()) } - async fn completion(&self, _: CompletionParams) -> Result> { + async fn completion(&self, _: CompletionParams) -> jsonrpc::Result> { Ok(Some(CompletionResponse::Array(vec![ CompletionItem::new_simple("Hello".to_string(), "Some detail".to_string()), CompletionItem::new_simple("Bye".to_string(), "More detail".to_string()), ]))) } - async fn hover(&self, params: HoverParams) -> Result> { + async fn hover(&self, params: HoverParams) -> jsonrpc::Result> { let path = PathBuf::from(params.text_document_position_params.text_document.uri.path()); let file_name = path.file_name().unwrap().to_str().unwrap().to_owned(); let tokens = self.tokens.get(&file_name); @@ -134,55 +134,24 @@ impl LanguageServer for Backend { impl Backend { async fn recompile(&self, params: TextDocumentItem) { - let mut map = Default::default(); let path = PathBuf::from(params.uri.clone().path()); let file_name = path.file_name().unwrap().to_str().unwrap().to_owned(); - let mut reid_error = None; - let mut tokens = None; - let mut token_types_opt = None; + let mut map = Default::default(); + let parse_res = parse(¶ms.text, path.clone(), &mut map); + let (tokens, result) = match parse_res { + Ok((module_id, tokens)) => (tokens.clone(), compile(module_id, tokens, path, &mut map)), + Err(e) => (Vec::new(), Err(e)), + }; - match parse_module(¶ms.text, file_name.clone(), &mut map) { - Ok(module) => { - self.client - .log_message(MessageType::INFO, format!("successfully parsed!")) - .await; - tokens = Some(module.1.clone()); - match compile_module(module.0, module.1, &mut map, Some(path.clone()), true) { - Ok(module) => { - let module_id = module.module_id; - let mut context = Context::from(vec![module], path.parent().unwrap().to_owned()); - match perform_all_passes(&mut context, &mut map) { - Ok(_) => { - for module in context.modules.values() { - if module.module_id != module_id { - continue; - } - let token_types = DashMap::new(); - for (idx, token) in module.tokens.iter().enumerate() { - token_types.insert(token.clone(), find_type_in_context(&module, idx)); - } - token_types_opt = Some(token_types); - } - } - Err(e) => { - reid_error = Some(e); - } - } - } - Err(e) => { - reid_error = Some(e); - } - } + let mut diagnostics = Vec::new(); + match result { + Ok(Some(result)) => { + self.tokens.insert(file_name.clone(), result.tokens); + self.types.insert(file_name.clone(), result.types); } - Err(e) => { - reid_error = Some(e); - } - } - - if let Some(tokens) = &tokens { - if let Some(mut reid_error) = reid_error { - let mut diagnostics = Vec::new(); + Ok(_) => {} + Err(mut reid_error) => { reid_error.errors.dedup(); for error in reid_error.errors { let meta = error.get_meta(); @@ -217,25 +186,56 @@ impl Backend { }); self.client.log_message(MessageType::INFO, format!("{}", error)).await; } - self.client - .publish_diagnostics(params.uri.clone(), diagnostics, Some(params.version)) - .await; - } else { - self.client - .publish_diagnostics(params.uri.clone(), Vec::new(), Some(params.version)) - .await; } } - if let Some(tokens) = tokens.take() { - self.tokens.insert(file_name.clone(), tokens); - } - if let Some(token_types) = token_types_opt { - self.types.insert(file_name.clone(), token_types); - } + self.client + .publish_diagnostics(params.uri.clone(), diagnostics, Some(params.version)) + .await; } } +struct CompileResult { + tokens: Vec, + types: DashMap>, +} + +fn parse(source: &str, path: PathBuf, map: &mut ErrorModules) -> Result<(SourceModuleId, Vec), ReidError> { + let file_name = path.file_name().unwrap().to_str().unwrap().to_owned(); + + Ok(parse_module(source, file_name.clone(), map)?) +} + +fn compile( + module_id: SourceModuleId, + tokens: Vec, + path: PathBuf, + map: &mut ErrorModules, +) -> Result, ReidError> { + let token_types = DashMap::new(); + + let module = compile_module(module_id, tokens, map, Some(path.clone()), true)?; + + let module_id = module.module_id; + let mut context = Context::from(vec![module], path.parent().unwrap().to_owned()); + perform_all_passes(&mut context, map)?; + + for module in context.modules.into_values() { + if module.module_id != module_id { + continue; + } + for (idx, token) in module.tokens.iter().enumerate() { + token_types.insert(token.clone(), find_type_in_context(&module, idx)); + } + + return Ok(Some(CompileResult { + tokens: module.tokens, + types: token_types, + })); + } + return Ok(None); +} + #[tokio::main] async fn main() { let stdin = tokio::io::stdin();