Skip to content

Commit

Permalink
Publish error and warning diagnostics when the project cannot be pars…
Browse files Browse the repository at this point in the history
…ed (#2809)

LSP now displays errors and warnings to the user in the editor.
  • Loading branch information
JoshuaBatty authored Sep 26, 2022
1 parent 184f914 commit d5115ae
Showing 1 changed file with 22 additions and 35 deletions.
57 changes: 22 additions & 35 deletions sway-lsp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::capabilities;
use crate::core::{
document::{DocumentError, TextDocument},
session::Session,
token::TokenMap,
};
use crate::utils::debug::{self, DebugFlags};
use forc_util::find_manifest_dir;
Expand Down Expand Up @@ -34,10 +33,6 @@ impl Backend {
self.client.log_message(MessageType::INFO, message).await;
}

async fn log_error_message(&self, message: &str) {
self.client.log_message(MessageType::ERROR, message).await;
}

async fn parse_and_store_sway_files(&self) -> Result<(), DocumentError> {
let curr_dir = std::env::current_dir().unwrap();

Expand All @@ -54,6 +49,20 @@ impl Backend {

Ok(())
}

async fn parse_project(&self, uri: &Url) {
let diagnostics = match self.session.parse_project(uri) {
Ok(diagnostics) => diagnostics,
Err(err) => {
if let DocumentError::FailedToParse(diagnostics) = err {
diagnostics
} else {
vec![]
}
}
};
self.publish_diagnostics(uri, diagnostics).await;
}
}

fn capabilities() -> ServerCapabilities {
Expand Down Expand Up @@ -86,20 +95,17 @@ fn capabilities() -> ServerCapabilities {
}

impl Backend {
async fn publish_diagnostics(
&self,
uri: &Url,
diagnostics: Vec<Diagnostic>,
token_map: &TokenMap,
) {
async fn publish_diagnostics(&self, uri: &Url, diagnostics: Vec<Diagnostic>) {
match &self.config.collected_tokens_as_warnings {
Some(s) => {
let token_map = self.session.tokens_for_file(uri);

// If collected_tokens_as_warnings is Some, take over the normal error and warning display behavior
// and instead show the either the parsed or typed tokens as warnings.
// This is useful for debugging the lsp parser.
let diagnostics = match s.as_str() {
"parsed" => Some(debug::generate_warnings_for_parsed_tokens(token_map)),
"typed" => Some(debug::generate_warnings_for_typed_tokens(token_map)),
"parsed" => Some(debug::generate_warnings_for_parsed_tokens(&token_map)),
"typed" => Some(debug::generate_warnings_for_typed_tokens(&token_map)),
_ => None,
};
if let Some(diagnostics) = diagnostics {
Expand Down Expand Up @@ -151,38 +157,19 @@ impl LanguageServer for Backend {
async fn did_open(&self, params: DidOpenTextDocumentParams) {
let uri = params.text_document.uri.clone();
self.session.handle_open_file(&uri);

match self.session.parse_project(&uri) {
Ok(diagnostics) => {
let tokens = self.session.tokens_for_file(&uri);
self.publish_diagnostics(&uri, diagnostics, &tokens).await
}
Err(_) => self.log_error_message("Unable to Parse Project!").await,
}
self.parse_project(&uri).await;
}

async fn did_change(&self, params: DidChangeTextDocumentParams) {
let uri = params.text_document.uri.clone();
self.session
.update_text_document(&uri, params.content_changes);
match self.session.parse_project(&uri) {
Ok(diagnostics) => {
let tokens = self.session.tokens_for_file(&uri);
self.publish_diagnostics(&uri, diagnostics, &tokens).await
}
Err(_) => self.log_error_message("Unable to Parse Project!").await,
}
self.parse_project(&uri).await;
}

async fn did_save(&self, params: DidSaveTextDocumentParams) {
let uri = params.text_document.uri.clone();
match self.session.parse_project(&uri) {
Ok(diagnostics) => {
let tokens = self.session.tokens_for_file(&uri);
self.publish_diagnostics(&uri, diagnostics, &tokens).await
}
Err(_) => self.log_error_message("Unable to Parse Project!").await,
}
self.parse_project(&uri).await;
}

async fn did_change_watched_files(&self, params: DidChangeWatchedFilesParams) {
Expand Down

0 comments on commit d5115ae

Please sign in to comment.