aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/diagnostics
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/diagnostics')
-rw-r--r--crates/ide/src/diagnostics/fixes.rs1
-rw-r--r--crates/ide/src/diagnostics/incorrect_case.rs (renamed from crates/ide/src/diagnostics/fixes/change_case.rs)61
2 files changed, 36 insertions, 26 deletions
diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs
index e4bd90c3f..d763dca93 100644
--- a/crates/ide/src/diagnostics/fixes.rs
+++ b/crates/ide/src/diagnostics/fixes.rs
@@ -1,6 +1,5 @@
1//! Provides a way to attach fixes to the diagnostics. 1//! Provides a way to attach fixes to the diagnostics.
2//! The same module also has all curret custom fixes for the diagnostics implemented. 2//! The same module also has all curret custom fixes for the diagnostics implemented.
3mod change_case;
4 3
5use hir::{diagnostics::Diagnostic, Semantics}; 4use hir::{diagnostics::Diagnostic, Semantics};
6use ide_assists::AssistResolveStrategy; 5use ide_assists::AssistResolveStrategy;
diff --git a/crates/ide/src/diagnostics/fixes/change_case.rs b/crates/ide/src/diagnostics/incorrect_case.rs
index db1a37cd6..56283b58b 100644
--- a/crates/ide/src/diagnostics/fixes/change_case.rs
+++ b/crates/ide/src/diagnostics/incorrect_case.rs
@@ -1,35 +1,46 @@
1use hir::{db::AstDatabase, diagnostics::IncorrectCase, InFile, Semantics}; 1use hir::{db::AstDatabase, InFile};
2use ide_assists::{Assist, AssistResolveStrategy}; 2use ide_assists::Assist;
3use ide_db::{base_db::FilePosition, RootDatabase}; 3use ide_db::base_db::FilePosition;
4use syntax::AstNode; 4use syntax::AstNode;
5 5
6use crate::{ 6use crate::{
7 diagnostics::{unresolved_fix, DiagnosticWithFixes}, 7 diagnostics::{unresolved_fix, Diagnostic, DiagnosticsContext},
8 references::rename::rename_with_semantics, 8 references::rename::rename_with_semantics,
9 Severity,
9}; 10};
10 11
11impl DiagnosticWithFixes for IncorrectCase { 12// Diagnostic: incorrect-ident-case
12 fn fixes( 13//
13 &self, 14// This diagnostic is triggered if an item name doesn't follow https://doc.rust-lang.org/1.0.0/style/style/naming/README.html[Rust naming convention].
14 sema: &Semantics<RootDatabase>, 15pub(super) fn incorrect_case(ctx: &DiagnosticsContext<'_>, d: &hir::IncorrectCase) -> Diagnostic {
15 resolve: &AssistResolveStrategy, 16 Diagnostic::new(
16 ) -> Option<Vec<Assist>> { 17 "incorrect-ident-case",
17 let root = sema.db.parse_or_expand(self.file)?; 18 format!(
18 let name_node = self.ident.to_node(&root); 19 "{} `{}` should have {} name, e.g. `{}`",
19 20 d.ident_type, d.ident_text, d.expected_case, d.suggested_text
20 let name_node = InFile::new(self.file, name_node.syntax()); 21 ),
21 let frange = name_node.original_file_range(sema.db); 22 ctx.sema.diagnostics_display_range(InFile::new(d.file, d.ident.clone().into())).range,
22 let file_position = FilePosition { file_id: frange.file_id, offset: frange.range.start() }; 23 )
23 24 .severity(Severity::WeakWarning)
24 let label = format!("Rename to {}", self.suggested_text); 25 .with_fixes(fixes(ctx, d))
25 let mut res = unresolved_fix("change_case", &label, frange.range); 26}
26 if resolve.should_resolve(&res.id) { 27
27 let source_change = rename_with_semantics(sema, file_position, &self.suggested_text); 28fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::IncorrectCase) -> Option<Vec<Assist>> {
28 res.source_change = Some(source_change.ok().unwrap_or_default()); 29 let root = ctx.sema.db.parse_or_expand(d.file)?;
29 } 30 let name_node = d.ident.to_node(&root);
30 31
31 Some(vec![res]) 32 let name_node = InFile::new(d.file, name_node.syntax());
33 let frange = name_node.original_file_range(ctx.sema.db);
34 let file_position = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
35
36 let label = format!("Rename to {}", d.suggested_text);
37 let mut res = unresolved_fix("change_case", &label, frange.range);
38 if ctx.resolve.should_resolve(&res.id) {
39 let source_change = rename_with_semantics(&ctx.sema, file_position, &d.suggested_text);
40 res.source_change = Some(source_change.ok().unwrap_or_default());
32 } 41 }
42
43 Some(vec![res])
33} 44}
34 45
35#[cfg(test)] 46#[cfg(test)]