From a8829889e353179e004fe2014cceaa69ee3141ca Mon Sep 17 00:00:00 2001 From: rvcas Date: Mon, 20 Feb 2023 12:55:41 -0500 Subject: [PATCH] feat(lsp): properly recompile project is files change --- crates/aiken-lsp/src/server.rs | 46 +++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/crates/aiken-lsp/src/server.rs b/crates/aiken-lsp/src/server.rs index d07eaf22..c7974e40 100644 --- a/crates/aiken-lsp/src/server.rs +++ b/crates/aiken-lsp/src/server.rs @@ -5,23 +5,26 @@ use std::{ }; use aiken_lang::{ - ast::{Located, ModuleKind, Span}, + ast::{Definition, Located, ModuleKind, Span, Use}, parser, tipo::pretty::Printer, }; use aiken_project::{ - config, + config::{self, Config}, error::{Error as ProjectError, GetSource}, module::CheckedModule, }; use indoc::formatdoc; +use itertools::Itertools; use lsp_server::{Connection, Message}; use lsp_types::{ notification::{ - DidChangeTextDocument, DidSaveTextDocument, Notification, Progress, PublishDiagnostics, - ShowMessage, + DidChangeTextDocument, DidChangeWatchedFiles, DidCloseTextDocument, DidSaveTextDocument, + Notification, Progress, PublishDiagnostics, ShowMessage, + }, + request::{ + Completion, Formatting, GotoDefinition, HoverRequest, Request, WorkDoneProgressCreate, }, - request::{Formatting, GotoDefinition, HoverRequest, Request, WorkDoneProgressCreate}, DocumentFormattingParams, InitializeParams, TextEdit, }; use miette::Diagnostic; @@ -175,7 +178,7 @@ impl Server { fn handle_notification( &mut self, - _connection: &lsp_server::Connection, + connection: &lsp_server::Connection, notification: lsp_server::Notification, ) -> Result<(), ServerError> { match notification.method.as_str() { @@ -184,8 +187,13 @@ impl Server { self.edited.remove(params.text_document.uri.path()); + self.compile(connection)?; + + self.publish_stored_diagnostics(connection)?; + Ok(()) } + DidChangeTextDocument::METHOD => { let params = cast_notification::(notification)?; @@ -198,6 +206,32 @@ impl Server { Ok(()) } + + DidCloseTextDocument::METHOD => { + let params = cast_notification::(notification)?; + + self.edited.remove(params.text_document.uri.path()); + + Ok(()) + } + + DidChangeWatchedFiles::METHOD => { + if let Ok(config) = Config::load(&self.root) { + self.config = Some(config); + self.create_new_compiler(); + self.compile(connection)?; + } else { + self.stored_messages.push(lsp_types::ShowMessageParams { + typ: lsp_types::MessageType::ERROR, + message: "Failed to reload aiken.toml".to_string(), + }); + } + + self.publish_stored_diagnostics(connection)?; + + Ok(()) + } + _ => Ok(()), } }