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/field_shorthand.rs15
-rw-r--r--crates/ide/src/diagnostics/fixes.rs36
2 files changed, 23 insertions, 28 deletions
diff --git a/crates/ide/src/diagnostics/field_shorthand.rs b/crates/ide/src/diagnostics/field_shorthand.rs
index f41bcd619..5c89e2170 100644
--- a/crates/ide/src/diagnostics/field_shorthand.rs
+++ b/crates/ide/src/diagnostics/field_shorthand.rs
@@ -1,8 +1,7 @@
1//! Suggests shortening `Foo { field: field }` to `Foo { field }` in both 1//! Suggests shortening `Foo { field: field }` to `Foo { field }` in both
2//! expressions and patterns. 2//! expressions and patterns.
3 3
4use ide_db::base_db::FileId; 4use ide_db::{base_db::FileId, source_change::SourceChange};
5use ide_db::source_change::SourceFileEdit;
6use syntax::{ast, match_ast, AstNode, SyntaxNode}; 5use syntax::{ast, match_ast, AstNode, SyntaxNode};
7use text_edit::TextEdit; 6use text_edit::TextEdit;
8 7
@@ -50,7 +49,7 @@ fn check_expr_field_shorthand(
50 Diagnostic::hint(field_range, "Shorthand struct initialization".to_string()).with_fix( 49 Diagnostic::hint(field_range, "Shorthand struct initialization".to_string()).with_fix(
51 Some(Fix::new( 50 Some(Fix::new(
52 "Use struct shorthand initialization", 51 "Use struct shorthand initialization",
53 SourceFileEdit { file_id, edit }.into(), 52 SourceChange::from_text_edit(file_id, edit),
54 field_range, 53 field_range,
55 )), 54 )),
56 ), 55 ),
@@ -89,7 +88,7 @@ fn check_pat_field_shorthand(
89 acc.push(Diagnostic::hint(field_range, "Shorthand struct pattern".to_string()).with_fix( 88 acc.push(Diagnostic::hint(field_range, "Shorthand struct pattern".to_string()).with_fix(
90 Some(Fix::new( 89 Some(Fix::new(
91 "Use struct field shorthand", 90 "Use struct field shorthand",
92 SourceFileEdit { file_id, edit }.into(), 91 SourceChange::from_text_edit(file_id, edit),
93 field_range, 92 field_range,
94 )), 93 )),
95 )); 94 ));
@@ -120,7 +119,7 @@ fn main() { A { 0: 0 } }
120struct A { a: &'static str } 119struct A { a: &'static str }
121fn main() { 120fn main() {
122 let a = "haha"; 121 let a = "haha";
123 A { a<|>: a } 122 A { a$0: a }
124} 123}
125"#, 124"#,
126 r#" 125 r#"
@@ -138,7 +137,7 @@ struct A { a: &'static str, b: &'static str }
138fn main() { 137fn main() {
139 let a = "haha"; 138 let a = "haha";
140 let b = "bb"; 139 let b = "bb";
141 A { a<|>: a, b } 140 A { a$0: a, b }
142} 141}
143"#, 142"#,
144 r#" 143 r#"
@@ -171,7 +170,7 @@ fn f(a: A) { let A { 0: 0 } = a; }
171 r#" 170 r#"
172struct A { a: &'static str } 171struct A { a: &'static str }
173fn f(a: A) { 172fn f(a: A) {
174 let A { a<|>: a } = a; 173 let A { a$0: a } = a;
175} 174}
176"#, 175"#,
177 r#" 176 r#"
@@ -186,7 +185,7 @@ fn f(a: A) {
186 r#" 185 r#"
187struct A { a: &'static str, b: &'static str } 186struct A { a: &'static str, b: &'static str }
188fn f(a: A) { 187fn f(a: A) {
189 let A { a<|>: a, b } = a; 188 let A { a$0: a, b } = a;
190} 189}
191"#, 190"#,
192 r#" 191 r#"
diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs
index 71ec4df92..e4335119b 100644
--- a/crates/ide/src/diagnostics/fixes.rs
+++ b/crates/ide/src/diagnostics/fixes.rs
@@ -3,14 +3,14 @@
3use hir::{ 3use hir::{
4 db::AstDatabase, 4 db::AstDatabase,
5 diagnostics::{ 5 diagnostics::{
6 Diagnostic, IncorrectCase, MissingFields, MissingOkInTailExpr, NoSuchField, 6 Diagnostic, IncorrectCase, MissingFields, MissingOkOrSomeInTailExpr, NoSuchField,
7 RemoveThisSemicolon, UnresolvedModule, 7 RemoveThisSemicolon, UnresolvedModule,
8 }, 8 },
9 HasSource, HirDisplay, InFile, Semantics, VariantDef, 9 HasSource, HirDisplay, InFile, Semantics, VariantDef,
10}; 10};
11use ide_db::base_db::{AnchoredPathBuf, FileId};
12use ide_db::{ 11use ide_db::{
13 source_change::{FileSystemEdit, SourceFileEdit}, 12 base_db::{AnchoredPathBuf, FileId},
13 source_change::{FileSystemEdit, SourceChange},
14 RootDatabase, 14 RootDatabase,
15}; 15};
16use syntax::{ 16use syntax::{
@@ -88,21 +88,22 @@ impl DiagnosticWithFix for MissingFields {
88 }; 88 };
89 Some(Fix::new( 89 Some(Fix::new(
90 "Fill struct fields", 90 "Fill struct fields",
91 SourceFileEdit { file_id: self.file.original_file(sema.db), edit }.into(), 91 SourceChange::from_text_edit(self.file.original_file(sema.db), edit),
92 sema.original_range(&field_list_parent.syntax()).range, 92 sema.original_range(&field_list_parent.syntax()).range,
93 )) 93 ))
94 } 94 }
95} 95}
96 96
97impl DiagnosticWithFix for MissingOkInTailExpr { 97impl DiagnosticWithFix for MissingOkOrSomeInTailExpr {
98 fn fix(&self, sema: &Semantics<RootDatabase>) -> Option<Fix> { 98 fn fix(&self, sema: &Semantics<RootDatabase>) -> Option<Fix> {
99 let root = sema.db.parse_or_expand(self.file)?; 99 let root = sema.db.parse_or_expand(self.file)?;
100 let tail_expr = self.expr.to_node(&root); 100 let tail_expr = self.expr.to_node(&root);
101 let tail_expr_range = tail_expr.syntax().text_range(); 101 let tail_expr_range = tail_expr.syntax().text_range();
102 let edit = TextEdit::replace(tail_expr_range, format!("Ok({})", tail_expr.syntax())); 102 let replacement = format!("{}({})", self.required, tail_expr.syntax());
103 let source_change = 103 let edit = TextEdit::replace(tail_expr_range, replacement);
104 SourceFileEdit { file_id: self.file.original_file(sema.db), edit }.into(); 104 let source_change = SourceChange::from_text_edit(self.file.original_file(sema.db), edit);
105 Some(Fix::new("Wrap with ok", source_change, tail_expr_range)) 105 let name = if self.required == "Ok" { "Wrap with Ok" } else { "Wrap with Some" };
106 Some(Fix::new(name, source_change, tail_expr_range))
106 } 107 }
107} 108}
108 109
@@ -120,8 +121,7 @@ impl DiagnosticWithFix for RemoveThisSemicolon {
120 .text_range(); 121 .text_range();
121 122
122 let edit = TextEdit::delete(semicolon); 123 let edit = TextEdit::delete(semicolon);
123 let source_change = 124 let source_change = SourceChange::from_text_edit(self.file.original_file(sema.db), edit);
124 SourceFileEdit { file_id: self.file.original_file(sema.db), edit }.into();
125 125
126 Some(Fix::new("Remove this semicolon", source_change, semicolon)) 126 Some(Fix::new("Remove this semicolon", source_change, semicolon))
127 } 127 }
@@ -202,15 +202,11 @@ fn missing_record_expr_field_fix(
202 new_field = format!(",{}", new_field); 202 new_field = format!(",{}", new_field);
203 } 203 }
204 204
205 let source_change = SourceFileEdit { 205 let source_change = SourceChange::from_text_edit(
206 file_id: def_file_id, 206 def_file_id,
207 edit: TextEdit::insert(last_field_syntax.text_range().end(), new_field), 207 TextEdit::insert(last_field_syntax.text_range().end(), new_field),
208 }; 208 );
209 return Some(Fix::new( 209 return Some(Fix::new("Create field", source_change, record_expr_field.syntax().text_range()));
210 "Create field",
211 source_change.into(),
212 record_expr_field.syntax().text_range(),
213 ));
214 210
215 fn record_field_list(field_def_list: ast::FieldList) -> Option<ast::RecordFieldList> { 211 fn record_field_list(field_def_list: ast::FieldList) -> Option<ast::RecordFieldList> {
216 match field_def_list { 212 match field_def_list {