diff options
Diffstat (limited to 'crates/ra_ide_api/src/lib.rs')
-rw-r--r-- | crates/ra_ide_api/src/lib.rs | 55 |
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 | ||
100 | impl 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)] |
101 | pub struct SourceFileEdit { | 139 | pub 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. |