From e24e22f288eba33928a9e579f13653d6f04fcdfa Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Sat, 3 Oct 2020 17:34:52 +0300 Subject: Add fix for incorrect case diagnostic --- crates/ide/src/diagnostics/fixes.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'crates/ide/src/diagnostics') diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs index 68ae1c239..286ef0785 100644 --- a/crates/ide/src/diagnostics/fixes.rs +++ b/crates/ide/src/diagnostics/fixes.rs @@ -3,7 +3,10 @@ use base_db::FileId; use hir::{ db::AstDatabase, - diagnostics::{Diagnostic, MissingFields, MissingOkInTailExpr, NoSuchField, UnresolvedModule}, + diagnostics::{ + Diagnostic, IncorrectCase, MissingFields, MissingOkInTailExpr, NoSuchField, + UnresolvedModule, + }, HasSource, HirDisplay, Semantics, VariantDef, }; use ide_db::{ @@ -17,7 +20,7 @@ use syntax::{ }; use text_edit::TextEdit; -use crate::diagnostics::Fix; +use crate::{diagnostics::Fix, references::rename::rename_with_semantics, FilePosition}; /// A [Diagnostic] that potentially has a fix available. /// @@ -99,6 +102,19 @@ impl DiagnosticWithFix for MissingOkInTailExpr { } } +impl DiagnosticWithFix for IncorrectCase { + fn fix(&self, sema: &Semantics) -> Option { + let file_id = self.file.original_file(sema.db); + let offset = self.ident.text_range().start(); + let file_position = FilePosition { file_id, offset }; + + let rename_changes = rename_with_semantics(sema, file_position, &self.suggested_text)?; + + let label = format!("Rename to {}", self.suggested_text); + Some(Fix::new(&label, rename_changes.info, rename_changes.range)) + } +} + fn missing_record_expr_field_fix( sema: &Semantics, usage_file_id: FileId, -- cgit v1.2.3 From fb0ab9f7456018ff0bac628e05366f976c5af1a7 Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Thu, 8 Oct 2020 09:27:38 +0300 Subject: Keep SyntaxNodePtr::range private --- crates/ide/src/diagnostics/fixes.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'crates/ide/src/diagnostics') diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs index 286ef0785..b47fe0469 100644 --- a/crates/ide/src/diagnostics/fixes.rs +++ b/crates/ide/src/diagnostics/fixes.rs @@ -104,8 +104,11 @@ impl DiagnosticWithFix for MissingOkInTailExpr { impl DiagnosticWithFix for IncorrectCase { fn fix(&self, sema: &Semantics) -> Option { + let root = sema.db.parse_or_expand(self.file)?; + let name_node = self.ident.to_node(&root); + let file_id = self.file.original_file(sema.db); - let offset = self.ident.text_range().start(); + let offset = name_node.syntax().text_range().start(); let file_position = FilePosition { file_id, offset }; let rename_changes = rename_with_semantics(sema, file_position, &self.suggested_text)?; -- cgit v1.2.3 From 991d0190968662f23220d8aefaf28bd03b1dbe41 Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Thu, 8 Oct 2020 09:30:01 +0300 Subject: Use TextRange::contains_inclusive in fixes check --- crates/ide/src/diagnostics/fixes.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'crates/ide/src/diagnostics') diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs index b47fe0469..0c75e50b0 100644 --- a/crates/ide/src/diagnostics/fixes.rs +++ b/crates/ide/src/diagnostics/fixes.rs @@ -111,7 +111,8 @@ impl DiagnosticWithFix for IncorrectCase { let offset = name_node.syntax().text_range().start(); let file_position = FilePosition { file_id, offset }; - let rename_changes = rename_with_semantics(sema, file_position, &self.suggested_text)?; + let rename_changes = + rename_with_semantics(sema, file_position, &self.suggested_text).ok()?; let label = format!("Rename to {}", self.suggested_text); Some(Fix::new(&label, rename_changes.info, rename_changes.range)) -- cgit v1.2.3