From e5cdcb8b124f5b7d59950429787e760e46388f72 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 3 May 2021 17:08:09 +0300 Subject: Add a way to resolve certain assists --- crates/ide/src/diagnostics/fixes.rs | 51 ++++++++++++++++++++++++----- crates/ide/src/diagnostics/unlinked_file.rs | 7 +++- 2 files changed, 48 insertions(+), 10 deletions(-) (limited to 'crates/ide/src/diagnostics') diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs index 7be8b3459..f23064eac 100644 --- a/crates/ide/src/diagnostics/fixes.rs +++ b/crates/ide/src/diagnostics/fixes.rs @@ -8,6 +8,7 @@ use hir::{ }, HasSource, HirDisplay, InFile, Semantics, VariantDef, }; +use ide_assists::AssistResolveStrategy; use ide_db::{ base_db::{AnchoredPathBuf, FileId}, source_change::{FileSystemEdit, SourceChange}, @@ -35,11 +36,19 @@ pub(crate) trait DiagnosticWithFix: Diagnostic { /// /// If `resolve` is false, the edit will be computed later, on demand, and /// can be omitted. - fn fix(&self, sema: &Semantics, _resolve: bool) -> Option; + fn fix( + &self, + sema: &Semantics, + _resolve: AssistResolveStrategy, + ) -> Option; } impl DiagnosticWithFix for UnresolvedModule { - fn fix(&self, sema: &Semantics, _resolve: bool) -> Option { + fn fix( + &self, + sema: &Semantics, + _resolve: AssistResolveStrategy, + ) -> Option { let root = sema.db.parse_or_expand(self.file)?; let unresolved_module = self.decl.to_node(&root); Some(fix( @@ -59,7 +68,11 @@ impl DiagnosticWithFix for UnresolvedModule { } impl DiagnosticWithFix for NoSuchField { - fn fix(&self, sema: &Semantics, _resolve: bool) -> Option { + fn fix( + &self, + sema: &Semantics, + _resolve: AssistResolveStrategy, + ) -> Option { let root = sema.db.parse_or_expand(self.file)?; missing_record_expr_field_fix( &sema, @@ -70,7 +83,11 @@ impl DiagnosticWithFix for NoSuchField { } impl DiagnosticWithFix for MissingFields { - fn fix(&self, sema: &Semantics, _resolve: bool) -> Option { + fn fix( + &self, + sema: &Semantics, + _resolve: AssistResolveStrategy, + ) -> Option { // Note that although we could add a diagnostics to // fill the missing tuple field, e.g : // `struct A(usize);` @@ -106,7 +123,11 @@ impl DiagnosticWithFix for MissingFields { } impl DiagnosticWithFix for MissingOkOrSomeInTailExpr { - fn fix(&self, sema: &Semantics, _resolve: bool) -> Option { + fn fix( + &self, + sema: &Semantics, + _resolve: AssistResolveStrategy, + ) -> Option { let root = sema.db.parse_or_expand(self.file)?; let tail_expr = self.expr.to_node(&root); let tail_expr_range = tail_expr.syntax().text_range(); @@ -119,7 +140,11 @@ impl DiagnosticWithFix for MissingOkOrSomeInTailExpr { } impl DiagnosticWithFix for RemoveThisSemicolon { - fn fix(&self, sema: &Semantics, _resolve: bool) -> Option { + fn fix( + &self, + sema: &Semantics, + _resolve: AssistResolveStrategy, + ) -> Option { let root = sema.db.parse_or_expand(self.file)?; let semicolon = self @@ -139,7 +164,11 @@ impl DiagnosticWithFix for RemoveThisSemicolon { } impl DiagnosticWithFix for IncorrectCase { - fn fix(&self, sema: &Semantics, resolve: bool) -> Option { + fn fix( + &self, + sema: &Semantics, + resolve: AssistResolveStrategy, + ) -> Option { let root = sema.db.parse_or_expand(self.file)?; let name_node = self.ident.to_node(&root); @@ -149,7 +178,7 @@ impl DiagnosticWithFix for IncorrectCase { let label = format!("Rename to {}", self.suggested_text); let mut res = unresolved_fix("change_case", &label, frange.range); - if resolve { + if resolve.should_resolve(&res.id) { let source_change = rename_with_semantics(sema, file_position, &self.suggested_text); res.source_change = Some(source_change.ok().unwrap_or_default()); } @@ -159,7 +188,11 @@ impl DiagnosticWithFix for IncorrectCase { } impl DiagnosticWithFix for ReplaceFilterMapNextWithFindMap { - fn fix(&self, sema: &Semantics, _resolve: bool) -> Option { + fn fix( + &self, + sema: &Semantics, + _resolve: AssistResolveStrategy, + ) -> Option { let root = sema.db.parse_or_expand(self.file)?; let next_expr = self.next_expr.to_node(&root); let next_call = ast::MethodCallExpr::cast(next_expr.syntax().clone())?; diff --git a/crates/ide/src/diagnostics/unlinked_file.rs b/crates/ide/src/diagnostics/unlinked_file.rs index 7d39f4fbe..e48528bed 100644 --- a/crates/ide/src/diagnostics/unlinked_file.rs +++ b/crates/ide/src/diagnostics/unlinked_file.rs @@ -5,6 +5,7 @@ use hir::{ diagnostics::{Diagnostic, DiagnosticCode}, InFile, }; +use ide_assists::AssistResolveStrategy; use ide_db::{ base_db::{FileId, FileLoader, SourceDatabase, SourceDatabaseExt}, source_change::SourceChange, @@ -50,7 +51,11 @@ impl Diagnostic for UnlinkedFile { } impl DiagnosticWithFix for UnlinkedFile { - fn fix(&self, sema: &hir::Semantics, _resolve: bool) -> Option { + fn fix( + &self, + sema: &hir::Semantics, + _resolve: AssistResolveStrategy, + ) -> Option { // If there's an existing module that could add a `mod` item to include the unlinked file, // suggest that as a fix. -- cgit v1.2.3 From 1679a376f30c5ad8971c0f855074a3f489fee5fa Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 3 May 2021 18:03:28 +0300 Subject: Resolve single assist only --- crates/ide/src/diagnostics/fixes.rs | 16 ++++++++-------- crates/ide/src/diagnostics/unlinked_file.rs | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'crates/ide/src/diagnostics') diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs index f23064eac..15821500f 100644 --- a/crates/ide/src/diagnostics/fixes.rs +++ b/crates/ide/src/diagnostics/fixes.rs @@ -39,7 +39,7 @@ pub(crate) trait DiagnosticWithFix: Diagnostic { fn fix( &self, sema: &Semantics, - _resolve: AssistResolveStrategy, + _resolve: &AssistResolveStrategy, ) -> Option; } @@ -47,7 +47,7 @@ impl DiagnosticWithFix for UnresolvedModule { fn fix( &self, sema: &Semantics, - _resolve: AssistResolveStrategy, + _resolve: &AssistResolveStrategy, ) -> Option { let root = sema.db.parse_or_expand(self.file)?; let unresolved_module = self.decl.to_node(&root); @@ -71,7 +71,7 @@ impl DiagnosticWithFix for NoSuchField { fn fix( &self, sema: &Semantics, - _resolve: AssistResolveStrategy, + _resolve: &AssistResolveStrategy, ) -> Option { let root = sema.db.parse_or_expand(self.file)?; missing_record_expr_field_fix( @@ -86,7 +86,7 @@ impl DiagnosticWithFix for MissingFields { fn fix( &self, sema: &Semantics, - _resolve: AssistResolveStrategy, + _resolve: &AssistResolveStrategy, ) -> Option { // Note that although we could add a diagnostics to // fill the missing tuple field, e.g : @@ -126,7 +126,7 @@ impl DiagnosticWithFix for MissingOkOrSomeInTailExpr { fn fix( &self, sema: &Semantics, - _resolve: AssistResolveStrategy, + _resolve: &AssistResolveStrategy, ) -> Option { let root = sema.db.parse_or_expand(self.file)?; let tail_expr = self.expr.to_node(&root); @@ -143,7 +143,7 @@ impl DiagnosticWithFix for RemoveThisSemicolon { fn fix( &self, sema: &Semantics, - _resolve: AssistResolveStrategy, + _resolve: &AssistResolveStrategy, ) -> Option { let root = sema.db.parse_or_expand(self.file)?; @@ -167,7 +167,7 @@ impl DiagnosticWithFix for IncorrectCase { fn fix( &self, sema: &Semantics, - resolve: AssistResolveStrategy, + resolve: &AssistResolveStrategy, ) -> Option { let root = sema.db.parse_or_expand(self.file)?; let name_node = self.ident.to_node(&root); @@ -191,7 +191,7 @@ impl DiagnosticWithFix for ReplaceFilterMapNextWithFindMap { fn fix( &self, sema: &Semantics, - _resolve: AssistResolveStrategy, + _resolve: &AssistResolveStrategy, ) -> Option { let root = sema.db.parse_or_expand(self.file)?; let next_expr = self.next_expr.to_node(&root); diff --git a/crates/ide/src/diagnostics/unlinked_file.rs b/crates/ide/src/diagnostics/unlinked_file.rs index e48528bed..93fd25dea 100644 --- a/crates/ide/src/diagnostics/unlinked_file.rs +++ b/crates/ide/src/diagnostics/unlinked_file.rs @@ -54,7 +54,7 @@ impl DiagnosticWithFix for UnlinkedFile { fn fix( &self, sema: &hir::Semantics, - _resolve: AssistResolveStrategy, + _resolve: &AssistResolveStrategy, ) -> Option { // If there's an existing module that could add a `mod` item to include the unlinked file, // suggest that as a fix. -- cgit v1.2.3