diff options
author | Phil Ellison <[email protected]> | 2020-12-30 15:46:05 +0000 |
---|---|---|
committer | Phil Ellison <[email protected]> | 2021-01-23 07:40:25 +0000 |
commit | 1ff860b93c972e0f8d3a8ee582c255fa59e9b284 (patch) | |
tree | 0533465ecb7329b6185c9259fb0f9c2b5ab0f7b0 /crates/ide | |
parent | 1316422a7c2ef26e9da78fa23f170407b1cb39bb (diff) |
Implement fix, add tests
Diffstat (limited to 'crates/ide')
-rw-r--r-- | crates/ide/src/diagnostics/fixes.rs | 29 |
1 files changed, 11 insertions, 18 deletions
diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs index eafbac43a..7bbf1d8c7 100644 --- a/crates/ide/src/diagnostics/fixes.rs +++ b/crates/ide/src/diagnostics/fixes.rs | |||
@@ -1,5 +1,6 @@ | |||
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. |
3 | use ast::MethodCallExpr; | ||
3 | use hir::{ | 4 | use hir::{ |
4 | db::AstDatabase, | 5 | db::AstDatabase, |
5 | diagnostics::{ | 6 | diagnostics::{ |
@@ -13,11 +14,7 @@ use ide_db::{ | |||
13 | source_change::{FileSystemEdit, SourceChange}, | 14 | source_change::{FileSystemEdit, SourceChange}, |
14 | RootDatabase, | 15 | RootDatabase, |
15 | }; | 16 | }; |
16 | use syntax::{ | 17 | use syntax::{AstNode, TextRange, algo, ast::{self, ArgList, edit::IndentLevel, make}}; |
17 | algo, | ||
18 | ast::{self, edit::IndentLevel, make}, | ||
19 | AstNode, | ||
20 | }; | ||
21 | use text_edit::TextEdit; | 18 | use text_edit::TextEdit; |
22 | 19 | ||
23 | use crate::{diagnostics::Fix, references::rename::rename_with_semantics, FilePosition}; | 20 | use crate::{diagnostics::Fix, references::rename::rename_with_semantics, FilePosition}; |
@@ -144,25 +141,21 @@ impl DiagnosticWithFix for IncorrectCase { | |||
144 | } | 141 | } |
145 | } | 142 | } |
146 | 143 | ||
147 | // Bugs: | ||
148 | // * Action is applicable for both iter() and filter_map() rows | ||
149 | // * Action deletes the entire method chain | ||
150 | impl DiagnosticWithFix for ReplaceFilterMapNextWithFindMap { | 144 | impl DiagnosticWithFix for ReplaceFilterMapNextWithFindMap { |
151 | fn fix(&self, sema: &Semantics<RootDatabase>) -> Option<Fix> { | 145 | fn fix(&self, sema: &Semantics<RootDatabase>) -> Option<Fix> { |
152 | let root = sema.db.parse_or_expand(self.file)?; | 146 | let root = sema.db.parse_or_expand(self.file)?; |
153 | |||
154 | let next_expr = self.next_expr.to_node(&root); | 147 | let next_expr = self.next_expr.to_node(&root); |
155 | let next_expr_range = next_expr.syntax().text_range(); | 148 | let next_call = MethodCallExpr::cast(next_expr.syntax().clone())?; |
156 | 149 | ||
157 | let filter_map_expr = self.filter_map_expr.to_node(&root); | 150 | let filter_map_call = MethodCallExpr::cast(next_call.receiver()?.syntax().clone())?; |
158 | let filter_map_expr_range = filter_map_expr.syntax().text_range(); | 151 | let filter_map_name_range = filter_map_call.name_ref()?.ident_token()?.text_range(); |
152 | let filter_map_args = filter_map_call.syntax().children().find_map(ArgList::cast)?; | ||
159 | 153 | ||
160 | let edit = TextEdit::delete(next_expr_range); | 154 | let range_to_replace = TextRange::new(filter_map_name_range.start(), next_expr.syntax().text_range().end()); |
155 | let replacement = format!("find_map{}", filter_map_args.syntax().text()); | ||
156 | let trigger_range = next_expr.syntax().text_range(); | ||
161 | 157 | ||
162 | // This is the entire method chain, including the array literal | 158 | let edit = TextEdit::replace(range_to_replace, replacement); |
163 | eprintln!("NEXT EXPR: {:#?}", next_expr); | ||
164 | // This is the entire method chain except for the final next() | ||
165 | eprintln!("FILTER MAP EXPR: {:#?}", filter_map_expr); | ||
166 | 159 | ||
167 | let source_change = | 160 | let source_change = |
168 | SourceFileEdit { file_id: self.file.original_file(sema.db), edit }.into(); | 161 | SourceFileEdit { file_id: self.file.original_file(sema.db), edit }.into(); |
@@ -170,7 +163,7 @@ impl DiagnosticWithFix for ReplaceFilterMapNextWithFindMap { | |||
170 | Some(Fix::new( | 163 | Some(Fix::new( |
171 | "Replace filter_map(..).next() with find_map()", | 164 | "Replace filter_map(..).next() with find_map()", |
172 | source_change, | 165 | source_change, |
173 | filter_map_expr_range, | 166 | trigger_range, |
174 | )) | 167 | )) |
175 | } | 168 | } |
176 | } | 169 | } |