feat(lsp): properly recompile project is files change

This commit is contained in:
rvcas 2023-02-20 12:55:41 -05:00 committed by Lucas
parent c033cab79e
commit a8829889e3
1 changed files with 40 additions and 6 deletions

View File

@ -5,23 +5,26 @@ use std::{
}; };
use aiken_lang::{ use aiken_lang::{
ast::{Located, ModuleKind, Span}, ast::{Definition, Located, ModuleKind, Span, Use},
parser, parser,
tipo::pretty::Printer, tipo::pretty::Printer,
}; };
use aiken_project::{ use aiken_project::{
config, config::{self, Config},
error::{Error as ProjectError, GetSource}, error::{Error as ProjectError, GetSource},
module::CheckedModule, module::CheckedModule,
}; };
use indoc::formatdoc; use indoc::formatdoc;
use itertools::Itertools;
use lsp_server::{Connection, Message}; use lsp_server::{Connection, Message};
use lsp_types::{ use lsp_types::{
notification::{ notification::{
DidChangeTextDocument, DidSaveTextDocument, Notification, Progress, PublishDiagnostics, DidChangeTextDocument, DidChangeWatchedFiles, DidCloseTextDocument, DidSaveTextDocument,
ShowMessage, Notification, Progress, PublishDiagnostics, ShowMessage,
},
request::{
Completion, Formatting, GotoDefinition, HoverRequest, Request, WorkDoneProgressCreate,
}, },
request::{Formatting, GotoDefinition, HoverRequest, Request, WorkDoneProgressCreate},
DocumentFormattingParams, InitializeParams, TextEdit, DocumentFormattingParams, InitializeParams, TextEdit,
}; };
use miette::Diagnostic; use miette::Diagnostic;
@ -175,7 +178,7 @@ impl Server {
fn handle_notification( fn handle_notification(
&mut self, &mut self,
_connection: &lsp_server::Connection, connection: &lsp_server::Connection,
notification: lsp_server::Notification, notification: lsp_server::Notification,
) -> Result<(), ServerError> { ) -> Result<(), ServerError> {
match notification.method.as_str() { match notification.method.as_str() {
@ -184,8 +187,13 @@ impl Server {
self.edited.remove(params.text_document.uri.path()); self.edited.remove(params.text_document.uri.path());
self.compile(connection)?;
self.publish_stored_diagnostics(connection)?;
Ok(()) Ok(())
} }
DidChangeTextDocument::METHOD => { DidChangeTextDocument::METHOD => {
let params = cast_notification::<DidChangeTextDocument>(notification)?; let params = cast_notification::<DidChangeTextDocument>(notification)?;
@ -198,6 +206,32 @@ impl Server {
Ok(()) Ok(())
} }
DidCloseTextDocument::METHOD => {
let params = cast_notification::<DidCloseTextDocument>(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(()), _ => Ok(()),
} }
} }