From 790788d5f4013d8d92f110bc12a581d18cf4b6ae Mon Sep 17 00:00:00 2001 From: Emil Lauridsen Date: Fri, 31 Jan 2020 19:23:25 +0100 Subject: Rework how we send diagnostics to client. The previous way of sending from the thread pool suffered from stale diagnostics due to being canceled before we could clear the old ones. The key change is moving to sending diagnostics from the main loop thread, but doing all the hard work in the thread pool. This should provide the best of both worlds, with little to no of the downsides. This should hopefully fix a lot of issues, but we'll need testing in each individual issue to be sure. --- crates/ra_cargo_watch/src/conv.rs | 66 ++++++++--------------- crates/ra_cargo_watch/src/lib.rs | 110 ++++++-------------------------------- 2 files changed, 38 insertions(+), 138 deletions(-) (limited to 'crates/ra_cargo_watch') diff --git a/crates/ra_cargo_watch/src/conv.rs b/crates/ra_cargo_watch/src/conv.rs index 8fba400ae..506370535 100644 --- a/crates/ra_cargo_watch/src/conv.rs +++ b/crates/ra_cargo_watch/src/conv.rs @@ -1,12 +1,11 @@ //! This module provides the functionality needed to convert diagnostics from //! `cargo check` json format to the LSP diagnostic format. use cargo_metadata::diagnostic::{ - Applicability, Diagnostic as RustDiagnostic, DiagnosticLevel, DiagnosticSpan, - DiagnosticSpanMacroExpansion, + Diagnostic as RustDiagnostic, DiagnosticLevel, DiagnosticSpan, DiagnosticSpanMacroExpansion, }; use lsp_types::{ - Diagnostic, DiagnosticRelatedInformation, DiagnosticSeverity, DiagnosticTag, Location, - NumberOrString, Position, Range, Url, + CodeAction, Diagnostic, DiagnosticRelatedInformation, DiagnosticSeverity, DiagnosticTag, + Location, NumberOrString, Position, Range, TextEdit, Url, WorkspaceEdit, }; use std::{ fmt::Write, @@ -117,38 +116,9 @@ fn is_deprecated(rd: &RustDiagnostic) -> bool { } } -#[derive(Clone, Debug)] -pub struct SuggestedFix { - pub title: String, - pub location: Location, - pub replacement: String, - pub applicability: Applicability, - pub diagnostics: Vec, -} - -impl std::cmp::PartialEq for SuggestedFix { - fn eq(&self, other: &SuggestedFix) -> bool { - if self.title == other.title - && self.location == other.location - && self.replacement == other.replacement - { - // Applicability doesn't impl PartialEq... - match (&self.applicability, &other.applicability) { - (Applicability::MachineApplicable, Applicability::MachineApplicable) => true, - (Applicability::HasPlaceholders, Applicability::HasPlaceholders) => true, - (Applicability::MaybeIncorrect, Applicability::MaybeIncorrect) => true, - (Applicability::Unspecified, Applicability::Unspecified) => true, - _ => false, - } - } else { - false - } - } -} - enum MappedRustChildDiagnostic { Related(DiagnosticRelatedInformation), - SuggestedFix(SuggestedFix), + SuggestedFix(CodeAction), MessageLine(String), } @@ -176,12 +146,20 @@ fn map_rust_child_diagnostic( rd.message.clone() }; - MappedRustChildDiagnostic::SuggestedFix(SuggestedFix { + let edit = { + let edits = vec![TextEdit::new(location.range, suggested_replacement.clone())]; + let mut edit_map = std::collections::HashMap::new(); + edit_map.insert(location.uri, edits); + WorkspaceEdit::new(edit_map) + }; + + MappedRustChildDiagnostic::SuggestedFix(CodeAction { title, - location, - replacement: suggested_replacement.clone(), - applicability: span.suggestion_applicability.clone().unwrap_or(Applicability::Unknown), - diagnostics: vec![], + kind: Some("quickfix".to_string()), + diagnostics: None, + edit: Some(edit), + command: None, + is_preferred: None, }) } else { MappedRustChildDiagnostic::Related(DiagnosticRelatedInformation { @@ -195,7 +173,7 @@ fn map_rust_child_diagnostic( pub(crate) struct MappedRustDiagnostic { pub location: Location, pub diagnostic: Diagnostic, - pub suggested_fixes: Vec, + pub fixes: Vec, } /// Converts a Rust root diagnostic to LSP form @@ -250,15 +228,13 @@ pub(crate) fn map_rust_diagnostic_to_lsp( } } - let mut suggested_fixes = vec![]; + let mut fixes = vec![]; let mut message = rd.message.clone(); for child in &rd.children { let child = map_rust_child_diagnostic(&child, workspace_root); match child { MappedRustChildDiagnostic::Related(related) => related_information.push(related), - MappedRustChildDiagnostic::SuggestedFix(suggested_fix) => { - suggested_fixes.push(suggested_fix) - } + MappedRustChildDiagnostic::SuggestedFix(code_action) => fixes.push(code_action.into()), MappedRustChildDiagnostic::MessageLine(message_line) => { write!(&mut message, "\n{}", message_line).unwrap(); @@ -295,7 +271,7 @@ pub(crate) fn map_rust_diagnostic_to_lsp( tags: if !tags.is_empty() { Some(tags) } else { None }, }; - Some(MappedRustDiagnostic { location, diagnostic, suggested_fixes }) + Some(MappedRustDiagnostic { location, diagnostic, fixes }) } /// Returns a `Url` object from a given path, will lowercase drive letters if present. diff --git a/crates/ra_cargo_watch/src/lib.rs b/crates/ra_cargo_watch/src/lib.rs index a718a5e52..f07c34549 100644 --- a/crates/ra_cargo_watch/src/lib.rs +++ b/crates/ra_cargo_watch/src/lib.rs @@ -4,22 +4,20 @@ use cargo_metadata::Message; use crossbeam_channel::{never, select, unbounded, Receiver, RecvError, Sender}; use lsp_types::{ - Diagnostic, Url, WorkDoneProgress, WorkDoneProgressBegin, WorkDoneProgressEnd, - WorkDoneProgressReport, + CodeAction, CodeActionOrCommand, Diagnostic, Url, WorkDoneProgress, WorkDoneProgressBegin, + WorkDoneProgressEnd, WorkDoneProgressReport, }; use std::{ - collections::HashMap, io::{BufRead, BufReader}, path::PathBuf, process::{Command, Stdio}, - sync::Arc, thread::JoinHandle, time::Instant, }; mod conv; -use crate::conv::{map_rust_diagnostic_to_lsp, MappedRustDiagnostic, SuggestedFix}; +use crate::conv::{map_rust_diagnostic_to_lsp, MappedRustDiagnostic}; pub use crate::conv::url_from_path_with_drive_lowercasing; @@ -38,7 +36,6 @@ pub struct CheckOptions { #[derive(Debug)] pub struct CheckWatcher { pub task_recv: Receiver, - pub state: Arc, cmd_send: Option>, handle: Option>, } @@ -46,7 +43,6 @@ pub struct CheckWatcher { impl CheckWatcher { pub fn new(options: &CheckOptions, workspace_root: PathBuf) -> CheckWatcher { let options = options.clone(); - let state = Arc::new(CheckState::new()); let (task_send, task_recv) = unbounded::(); let (cmd_send, cmd_recv) = unbounded::(); @@ -54,13 +50,12 @@ impl CheckWatcher { let mut check = CheckWatcherThread::new(options, workspace_root); check.run(&task_send, &cmd_recv); }); - CheckWatcher { task_recv, cmd_send: Some(cmd_send), handle: Some(handle), state } + CheckWatcher { task_recv, cmd_send: Some(cmd_send), handle: Some(handle) } } /// Returns a CheckWatcher that doesn't actually do anything pub fn dummy() -> CheckWatcher { - let state = Arc::new(CheckState::new()); - CheckWatcher { task_recv: never(), cmd_send: None, handle: None, state } + CheckWatcher { task_recv: never(), cmd_send: None, handle: None } } /// Schedule a re-start of the cargo check worker. @@ -87,84 +82,13 @@ impl std::ops::Drop for CheckWatcher { } } -#[derive(Clone, Debug)] -pub struct CheckState { - diagnostic_collection: HashMap>, - suggested_fix_collection: HashMap>, -} - -impl CheckState { - fn new() -> CheckState { - CheckState { - diagnostic_collection: HashMap::new(), - suggested_fix_collection: HashMap::new(), - } - } - - /// Clear the cached diagnostics, and schedule updating diagnostics by the - /// server, to clear stale results. - pub fn clear(&mut self) -> Vec { - let cleared_files: Vec = self.diagnostic_collection.keys().cloned().collect(); - self.diagnostic_collection.clear(); - self.suggested_fix_collection.clear(); - cleared_files - } - - pub fn diagnostics_for(&self, uri: &Url) -> Option<&[Diagnostic]> { - self.diagnostic_collection.get(uri).map(|d| d.as_slice()) - } - - pub fn fixes_for(&self, uri: &Url) -> Option<&[SuggestedFix]> { - self.suggested_fix_collection.get(uri).map(|d| d.as_slice()) - } - - pub fn add_diagnostic_with_fixes(&mut self, file_uri: Url, diagnostic: DiagnosticWithFixes) { - for fix in diagnostic.suggested_fixes { - self.add_suggested_fix_for_diagnostic(fix, &diagnostic.diagnostic); - } - self.add_diagnostic(file_uri, diagnostic.diagnostic); - } - - fn add_diagnostic(&mut self, file_uri: Url, diagnostic: Diagnostic) { - let diagnostics = self.diagnostic_collection.entry(file_uri).or_default(); - - // If we're building multiple targets it's possible we've already seen this diagnostic - let is_duplicate = diagnostics.iter().any(|d| are_diagnostics_equal(d, &diagnostic)); - if is_duplicate { - return; - } - - diagnostics.push(diagnostic); - } - - fn add_suggested_fix_for_diagnostic( - &mut self, - mut suggested_fix: SuggestedFix, - diagnostic: &Diagnostic, - ) { - let file_uri = suggested_fix.location.uri.clone(); - let file_suggestions = self.suggested_fix_collection.entry(file_uri).or_default(); - - let existing_suggestion: Option<&mut SuggestedFix> = - file_suggestions.iter_mut().find(|s| s == &&suggested_fix); - if let Some(existing_suggestion) = existing_suggestion { - // The existing suggestion also applies to this new diagnostic - existing_suggestion.diagnostics.push(diagnostic.clone()); - } else { - // We haven't seen this suggestion before - suggested_fix.diagnostics.push(diagnostic.clone()); - file_suggestions.push(suggested_fix); - } - } -} - #[derive(Debug)] pub enum CheckTask { /// Request a clearing of all cached diagnostics from the check watcher ClearDiagnostics, /// Request adding a diagnostic with fixes included to a file - AddDiagnostic(Url, DiagnosticWithFixes), + AddDiagnostic { url: Url, diagnostic: Diagnostic, fixes: Vec }, /// Request check progress notification to client Status(WorkDoneProgress), @@ -279,10 +203,17 @@ impl CheckWatcherThread { None => return, }; - let MappedRustDiagnostic { location, diagnostic, suggested_fixes } = map_result; + let MappedRustDiagnostic { location, diagnostic, fixes } = map_result; + let fixes = fixes + .into_iter() + .map(|fix| { + CodeAction { diagnostics: Some(vec![diagnostic.clone()]), ..fix }.into() + }) + .collect(); - let diagnostic = DiagnosticWithFixes { diagnostic, suggested_fixes }; - task_send.send(CheckTask::AddDiagnostic(location.uri, diagnostic)).unwrap(); + task_send + .send(CheckTask::AddDiagnostic { url: location.uri, diagnostic, fixes }) + .unwrap(); } CheckEvent::Msg(Message::BuildScriptExecuted(_msg)) => {} @@ -294,7 +225,7 @@ impl CheckWatcherThread { #[derive(Debug)] pub struct DiagnosticWithFixes { diagnostic: Diagnostic, - suggested_fixes: Vec, + fixes: Vec, } /// WatchThread exists to wrap around the communication needed to be able to @@ -429,10 +360,3 @@ impl std::ops::Drop for WatchThread { } } } - -fn are_diagnostics_equal(left: &Diagnostic, right: &Diagnostic) -> bool { - left.source == right.source - && left.severity == right.severity - && left.range == right.range - && left.message == right.message -} -- cgit v1.2.3 From 9f70f443a35bc12caf37e2548abf7987926c3875 Mon Sep 17 00:00:00 2001 From: Emil Lauridsen Date: Mon, 3 Feb 2020 12:24:57 +0100 Subject: Update snapshot tests due to removed SuggestedFix --- ...watch__conv__test__snap_clippy_pass_by_ref.snap | 48 ++++++++++++++-------- ...h__conv__test__snap_handles_macro_location.snap | 2 +- ...tch__conv__test__snap_macro_compiler_error.snap | 2 +- ...st__snap_rustc_incompatible_type_for_trait.snap | 2 +- ...ch__conv__test__snap_rustc_mismatched_type.snap | 2 +- ...ch__conv__test__snap_rustc_unused_variable.snap | 48 ++++++++++++++-------- ...est__snap_rustc_wrong_number_of_parameters.snap | 2 +- 7 files changed, 67 insertions(+), 39 deletions(-) (limited to 'crates/ra_cargo_watch') diff --git a/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_clippy_pass_by_ref.snap b/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_clippy_pass_by_ref.snap index cb0920914..95ca163dc 100644 --- a/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_clippy_pass_by_ref.snap +++ b/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_clippy_pass_by_ref.snap @@ -61,25 +61,39 @@ MappedRustDiagnostic { ), tags: None, }, - suggested_fixes: [ - SuggestedFix { + fixes: [ + CodeAction { title: "consider passing by value instead: \'self\'", - location: Location { - uri: "file:///test/compiler/mir/tagset.rs", - range: Range { - start: Position { - line: 41, - character: 23, - }, - end: Position { - line: 41, - character: 28, - }, + kind: Some( + "quickfix", + ), + diagnostics: None, + edit: Some( + WorkspaceEdit { + changes: Some( + { + "file:///test/compiler/mir/tagset.rs": [ + TextEdit { + range: Range { + start: Position { + line: 41, + character: 23, + }, + end: Position { + line: 41, + character: 28, + }, + }, + new_text: "self", + }, + ], + }, + ), + document_changes: None, }, - }, - replacement: "self", - applicability: Unspecified, - diagnostics: [], + ), + command: None, + is_preferred: None, }, ], } diff --git a/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_handles_macro_location.snap b/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_handles_macro_location.snap index 19510ecc1..12eb32df4 100644 --- a/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_handles_macro_location.snap +++ b/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_handles_macro_location.snap @@ -42,5 +42,5 @@ MappedRustDiagnostic { related_information: None, tags: None, }, - suggested_fixes: [], + fixes: [], } diff --git a/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_macro_compiler_error.snap b/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_macro_compiler_error.snap index 92f7eec05..7b83a7cd0 100644 --- a/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_macro_compiler_error.snap +++ b/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_macro_compiler_error.snap @@ -57,5 +57,5 @@ MappedRustDiagnostic { ), tags: None, }, - suggested_fixes: [], + fixes: [], } diff --git a/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_rustc_incompatible_type_for_trait.snap b/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_rustc_incompatible_type_for_trait.snap index cf683e4b6..54679c5db 100644 --- a/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_rustc_incompatible_type_for_trait.snap +++ b/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_rustc_incompatible_type_for_trait.snap @@ -42,5 +42,5 @@ MappedRustDiagnostic { related_information: None, tags: None, }, - suggested_fixes: [], + fixes: [], } diff --git a/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_rustc_mismatched_type.snap b/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_rustc_mismatched_type.snap index 8c1483c74..57df4ceaf 100644 --- a/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_rustc_mismatched_type.snap +++ b/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_rustc_mismatched_type.snap @@ -42,5 +42,5 @@ MappedRustDiagnostic { related_information: None, tags: None, }, - suggested_fixes: [], + fixes: [], } diff --git a/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_rustc_unused_variable.snap b/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_rustc_unused_variable.snap index eb5a2247b..3e1fe736c 100644 --- a/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_rustc_unused_variable.snap +++ b/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_rustc_unused_variable.snap @@ -46,25 +46,39 @@ MappedRustDiagnostic { ], ), }, - suggested_fixes: [ - SuggestedFix { + fixes: [ + CodeAction { title: "consider prefixing with an underscore: \'_foo\'", - location: Location { - uri: "file:///test/driver/subcommand/repl.rs", - range: Range { - start: Position { - line: 290, - character: 8, - }, - end: Position { - line: 290, - character: 11, - }, + kind: Some( + "quickfix", + ), + diagnostics: None, + edit: Some( + WorkspaceEdit { + changes: Some( + { + "file:///test/driver/subcommand/repl.rs": [ + TextEdit { + range: Range { + start: Position { + line: 290, + character: 8, + }, + end: Position { + line: 290, + character: 11, + }, + }, + new_text: "_foo", + }, + ], + }, + ), + document_changes: None, }, - }, - replacement: "_foo", - applicability: MachineApplicable, - diagnostics: [], + ), + command: None, + is_preferred: None, }, ], } diff --git a/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_rustc_wrong_number_of_parameters.snap b/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_rustc_wrong_number_of_parameters.snap index 2f4518931..69301078d 100644 --- a/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_rustc_wrong_number_of_parameters.snap +++ b/crates/ra_cargo_watch/src/conv/snapshots/ra_cargo_watch__conv__test__snap_rustc_wrong_number_of_parameters.snap @@ -61,5 +61,5 @@ MappedRustDiagnostic { ), tags: None, }, - suggested_fixes: [], + fixes: [], } -- cgit v1.2.3