aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/lib.rs
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/lib.rs
parent449eea11617e79c90b5d8de0959ef2bbe2a5d730 (diff)
Add convenience functions to SourceChange for creating single edits
Diffstat (limited to 'crates/ra_ide_api/src/lib.rs')
-rw-r--r--crates/ra_ide_api/src/lib.rs55
1 files changed, 43 insertions, 12 deletions
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.