aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src
diff options
context:
space:
mode:
authorVille Penttinen <[email protected]>2019-03-24 20:53:41 +0000
committerVille Penttinen <[email protected]>2019-03-24 20:53:41 +0000
commit22e1c7a112832a18509d400841b3d162228372bf (patch)
tree0d578585dc4d706a910e248d8525e5537b952fb5 /crates/ra_ide_api/src
parent449eea11617e79c90b5d8de0959ef2bbe2a5d730 (diff)
Add convenience functions to SourceChange for creating single edits
Diffstat (limited to 'crates/ra_ide_api/src')
-rw-r--r--crates/ra_ide_api/src/assists.rs11
-rw-r--r--crates/ra_ide_api/src/diagnostics.rs27
-rw-r--r--crates/ra_ide_api/src/lib.rs55
-rw-r--r--crates/ra_ide_api/src/typing.rs14
4 files changed, 63 insertions, 44 deletions
diff --git a/crates/ra_ide_api/src/assists.rs b/crates/ra_ide_api/src/assists.rs
index 3c0475a51..9a8c23a15 100644
--- a/crates/ra_ide_api/src/assists.rs
+++ b/crates/ra_ide_api/src/assists.rs
@@ -17,14 +17,9 @@ pub(crate) fn assists(db: &RootDatabase, frange: FileRange) -> Vec<Assist> {
17 let file_id = frange.file_id; 17 let file_id = frange.file_id;
18 let file_edit = SourceFileEdit { file_id, edit: action.edit }; 18 let file_edit = SourceFileEdit { file_id, edit: action.edit };
19 let id = label.id; 19 let id = label.id;
20 let change = SourceChange { 20 let change = SourceChange::source_edit(label.label, file_edit).with_cursor_opt(
21 label: label.label, 21 action.cursor_position.map(|offset| FilePosition { offset, file_id }),
22 source_file_edits: vec![file_edit], 22 );
23 file_system_edits: vec![],
24 cursor_position: action
25 .cursor_position
26 .map(|offset| FilePosition { offset, file_id }),
27 };
28 Assist { id, change } 23 Assist { id, change }
29 }) 24 })
30 .collect() 25 .collect()
diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs
index b9dc424c6..b0f23773e 100644
--- a/crates/ra_ide_api/src/diagnostics.rs
+++ b/crates/ra_ide_api/src/diagnostics.rs
@@ -71,12 +71,10 @@ fn check_unnecessary_braces_in_use_statement(
71 range, 71 range,
72 message: format!("Unnecessary braces in use statement"), 72 message: format!("Unnecessary braces in use statement"),
73 severity: Severity::WeakWarning, 73 severity: Severity::WeakWarning,
74 fix: Some(SourceChange { 74 fix: Some(SourceChange::source_edit(
75 label: "Remove unnecessary braces".to_string(), 75 "Remove unnecessary braces",
76 source_file_edits: vec![SourceFileEdit { file_id, edit }], 76 SourceFileEdit { file_id, edit },
77 file_system_edits: Vec::new(), 77 )),
78 cursor_position: None,
79 }),
80 }); 78 });
81 } 79 }
82 80
@@ -119,12 +117,10 @@ fn check_struct_shorthand_initialization(
119 range: named_field.syntax().range(), 117 range: named_field.syntax().range(),
120 message: format!("Shorthand struct initialization"), 118 message: format!("Shorthand struct initialization"),
121 severity: Severity::WeakWarning, 119 severity: Severity::WeakWarning,
122 fix: Some(SourceChange { 120 fix: Some(SourceChange::source_edit(
123 label: "use struct shorthand initialization".to_string(), 121 "use struct shorthand initialization",
124 source_file_edits: vec![SourceFileEdit { file_id, edit }], 122 SourceFileEdit { file_id, edit },
125 file_system_edits: Vec::new(), 123 )),
126 cursor_position: None,
127 }),
128 }); 124 });
129 } 125 }
130 } 126 }
@@ -144,12 +140,7 @@ fn check_module(
144 Problem::UnresolvedModule { candidate } => { 140 Problem::UnresolvedModule { candidate } => {
145 let create_file = 141 let create_file =
146 FileSystemEdit::CreateFile { source_root, path: candidate.clone() }; 142 FileSystemEdit::CreateFile { source_root, path: candidate.clone() };
147 let fix = SourceChange { 143 let fix = SourceChange::system_edit("create module", create_file);
148 label: "create module".to_string(),
149 source_file_edits: Vec::new(),
150 file_system_edits: vec![create_file],
151 cursor_position: None,
152 };
153 Diagnostic { 144 Diagnostic {
154 range: name_node.range(), 145 range: name_node.range(),
155 message: "unresolved module".to_string(), 146 message: "unresolved module".to_string(),
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs
index 99f18b6b8..4e4001750 100644
--- a/crates/ra_ide_api/src/lib.rs
+++ b/crates/ra_ide_api/src/lib.rs
@@ -97,6 +97,44 @@ pub struct SourceChange {
97 pub cursor_position: Option<FilePosition>, 97 pub cursor_position: Option<FilePosition>,
98} 98}
99 99
100impl SourceChange {
101 pub fn source_edits<L: Into<String>>(label: L, edits: Vec<SourceFileEdit>) -> Self {
102 SourceChange {
103 label: label.into(),
104 source_file_edits: edits,
105 file_system_edits: vec![],
106 cursor_position: None,
107 }
108 }
109
110 pub fn system_edits<L: Into<String>>(label: L, edits: Vec<FileSystemEdit>) -> Self {
111 SourceChange {
112 label: label.into(),
113 source_file_edits: vec![],
114 file_system_edits: edits,
115 cursor_position: None,
116 }
117 }
118
119 pub fn source_edit<L: Into<String>>(label: L, edit: SourceFileEdit) -> Self {
120 SourceChange::source_edits(label, vec![edit])
121 }
122
123 pub fn system_edit<L: Into<String>>(label: L, edit: FileSystemEdit) -> Self {
124 SourceChange::system_edits(label, vec![edit])
125 }
126
127 pub fn with_cursor(mut self, cursor_position: FilePosition) -> Self {
128 self.cursor_position = Some(cursor_position);
129 self
130 }
131
132 pub fn with_cursor_opt(mut self, cursor_position: Option<FilePosition>) -> Self {
133 self.cursor_position = cursor_position;
134 self
135 }
136}
137
100#[derive(Debug)] 138#[derive(Debug)]
101pub struct SourceFileEdit { 139pub struct SourceFileEdit {
102 pub file_id: FileId, 140 pub file_id: FileId,
@@ -285,12 +323,7 @@ impl Analysis {
285 file_id: frange.file_id, 323 file_id: frange.file_id,
286 edit: join_lines::join_lines(&file, frange.range), 324 edit: join_lines::join_lines(&file, frange.range),
287 }; 325 };
288 SourceChange { 326 SourceChange::source_edit("join lines", file_edit)
289 label: "join lines".to_string(),
290 source_file_edits: vec![file_edit],
291 file_system_edits: vec![],
292 cursor_position: None,
293 }
294 } 327 }
295 328
296 /// Returns an edit which should be applied when opening a new line, fixing 329 /// Returns an edit which should be applied when opening a new line, fixing
@@ -305,12 +338,10 @@ impl Analysis {
305 pub fn on_eq_typed(&self, position: FilePosition) -> Option<SourceChange> { 338 pub fn on_eq_typed(&self, position: FilePosition) -> Option<SourceChange> {
306 let file = self.db.parse(position.file_id); 339 let file = self.db.parse(position.file_id);
307 let edit = typing::on_eq_typed(&file, position.offset)?; 340 let edit = typing::on_eq_typed(&file, position.offset)?;
308 Some(SourceChange { 341 Some(SourceChange::source_edit(
309 label: "add semicolon".to_string(), 342 "add semicolon",
310 source_file_edits: vec![SourceFileEdit { edit, file_id: position.file_id }], 343 SourceFileEdit { edit, file_id: position.file_id },
311 file_system_edits: vec![], 344 ))
312 cursor_position: None,
313 })
314 } 345 }
315 346
316 /// Returns an edit which should be applied when a dot ('.') is typed on a blank line, indenting the line appropriately. 347 /// Returns an edit which should be applied when a dot ('.') is typed on a blank line, indenting the line appropriately.
diff --git a/crates/ra_ide_api/src/typing.rs b/crates/ra_ide_api/src/typing.rs
index 94b228466..424084468 100644
--- a/crates/ra_ide_api/src/typing.rs
+++ b/crates/ra_ide_api/src/typing.rs
@@ -31,12 +31,14 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<Sour
31 let cursor_position = position.offset + TextUnit::of_str(&inserted); 31 let cursor_position = position.offset + TextUnit::of_str(&inserted);
32 let mut edit = TextEditBuilder::default(); 32 let mut edit = TextEditBuilder::default();
33 edit.insert(position.offset, inserted); 33 edit.insert(position.offset, inserted);
34 Some(SourceChange { 34
35 label: "on enter".to_string(), 35 Some(
36 source_file_edits: vec![SourceFileEdit { edit: edit.finish(), file_id: position.file_id }], 36 SourceChange::source_edit(
37 file_system_edits: vec![], 37 "on enter",
38 cursor_position: Some(FilePosition { offset: cursor_position, file_id: position.file_id }), 38 SourceFileEdit { edit: edit.finish(), file_id: position.file_id },
39 }) 39 )
40 .with_cursor(FilePosition { offset: cursor_position, file_id: position.file_id }),
41 )
40} 42}
41 43
42fn node_indent<'a>(file: &'a SourceFile, node: &SyntaxNode) -> Option<&'a str> { 44fn node_indent<'a>(file: &'a SourceFile, node: &SyntaxNode) -> Option<&'a str> {