From 426d098bd6a032cb03e61d4b3d091caeaecbd4d0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 12 Apr 2021 17:58:01 +0300 Subject: internal: prepare for lazy diagnostics --- crates/ide/src/diagnostics.rs | 83 +++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 39 deletions(-) (limited to 'crates/ide/src/diagnostics.rs') diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index 0ace80a1e..4f0b4a62e 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs @@ -25,7 +25,7 @@ use syntax::{ use text_edit::TextEdit; use unlinked_file::UnlinkedFile; -use crate::{FileId, Label, SourceChange}; +use crate::{Assist, AssistId, AssistKind, FileId, Label, SourceChange}; use self::fixes::DiagnosticWithFix; @@ -35,7 +35,7 @@ pub struct Diagnostic { pub message: String, pub range: TextRange, pub severity: Severity, - pub fix: Option, + pub fix: Option, pub unused: bool, pub code: Option, } @@ -56,7 +56,7 @@ impl Diagnostic { } } - fn with_fix(self, fix: Option) -> Self { + fn with_fix(self, fix: Option) -> Self { Self { fix, ..self } } @@ -69,21 +69,6 @@ impl Diagnostic { } } -#[derive(Debug)] -pub struct Fix { - pub label: Label, - pub source_change: SourceChange, - /// Allows to trigger the fix only when the caret is in the range given - pub fix_trigger_range: TextRange, -} - -impl Fix { - fn new(label: &str, source_change: SourceChange, fix_trigger_range: TextRange) -> Self { - let label = Label::new(label); - Self { label, source_change, fix_trigger_range } - } -} - #[derive(Debug, Copy, Clone)] pub enum Severity { Error, @@ -261,7 +246,8 @@ fn check_unnecessary_braces_in_use_statement( acc.push( Diagnostic::hint(use_range, "Unnecessary braces in use statement".to_string()) - .with_fix(Some(Fix::new( + .with_fix(Some(fix( + "remove_braces", "Remove unnecessary braces", SourceChange::from_text_edit(file_id, edit), use_range, @@ -284,6 +270,17 @@ fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement( None } +fn fix(id: &'static str, label: &str, source_change: SourceChange, target: TextRange) -> Assist { + assert!(!id.contains(' ')); + Assist { + id: AssistId(id, AssistKind::QuickFix), + label: Label::new(label), + group: None, + target, + source_change: Some(source_change), + } +} + #[cfg(test)] mod tests { use expect_test::{expect, Expect}; @@ -308,10 +305,11 @@ mod tests { .unwrap(); let fix = diagnostic.fix.unwrap(); let actual = { - let file_id = *fix.source_change.source_file_edits.keys().next().unwrap(); + let source_change = fix.source_change.unwrap(); + let file_id = *source_change.source_file_edits.keys().next().unwrap(); let mut actual = analysis.file_text(file_id).unwrap().to_string(); - for edit in fix.source_change.source_file_edits.values() { + for edit in source_change.source_file_edits.values() { edit.apply(&mut actual); } actual @@ -319,9 +317,9 @@ mod tests { assert_eq_text!(&after, &actual); assert!( - fix.fix_trigger_range.contains_inclusive(file_position.offset), + fix.target.contains_inclusive(file_position.offset), "diagnostic fix range {:?} does not touch cursor position {:?}", - fix.fix_trigger_range, + fix.target, file_position.offset ); } @@ -665,24 +663,31 @@ fn test_fn() { range: 0..8, severity: Error, fix: Some( - Fix { + Assist { + id: AssistId( + "create_module", + QuickFix, + ), label: "Create module", - source_change: SourceChange { - source_file_edits: {}, - file_system_edits: [ - CreateFile { - dst: AnchoredPathBuf { - anchor: FileId( - 0, - ), - path: "foo.rs", + group: None, + target: 0..8, + source_change: Some( + SourceChange { + source_file_edits: {}, + file_system_edits: [ + CreateFile { + dst: AnchoredPathBuf { + anchor: FileId( + 0, + ), + path: "foo.rs", + }, + initial_contents: "", }, - initial_contents: "", - }, - ], - is_snippet: false, - }, - fix_trigger_range: 0..8, + ], + is_snippet: false, + }, + ), }, ), unused: false, -- cgit v1.2.3