From b92fcbc9567674cd240cc533aa021f63019ec38d Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Mon, 25 Mar 2019 09:03:10 +0200 Subject: Further improvements to the SourceChange convenience methods Rename system_edit to file_system_edit, add more documentation, add source_file_edit_from to create a SourceChange from `FileId` and `TextEdit`. --- crates/ra_ide_api/src/diagnostics.rs | 2 +- crates/ra_ide_api/src/lib.rs | 49 ++++++++++++++++++++++++++++++------ crates/ra_ide_api/src/references.rs | 14 ++--------- crates/ra_ide_api/src/typing.rs | 12 ++++----- 4 files changed, 50 insertions(+), 27 deletions(-) diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index b0f23773e..6559de29d 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -140,7 +140,7 @@ fn check_module( Problem::UnresolvedModule { candidate } => { let create_file = FileSystemEdit::CreateFile { source_root, path: candidate.clone() }; - let fix = SourceChange::system_edit("create module", create_file); + let fix = SourceChange::file_system_edit("create module", create_file); Diagnostic { range: name_node.range(), message: "unresolved module".to_string(), diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 4e4001750..928f3d604 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -98,7 +98,24 @@ pub struct SourceChange { } impl SourceChange { - pub fn source_edits>(label: L, edits: Vec) -> Self { + /// Creates a new SourceChange with the given label + /// from the edits. + pub(crate) fn from_edits>( + label: L, + source_file_edits: Vec, + file_system_edits: Vec, + ) -> Self { + SourceChange { + label: label.into(), + source_file_edits, + file_system_edits, + cursor_position: None, + } + } + + /// Creates a new SourceChange with the given label, + /// containing only the given `SourceFileEdits`. + pub(crate) fn source_edits>(label: L, edits: Vec) -> Self { SourceChange { label: label.into(), source_file_edits: edits, @@ -107,7 +124,9 @@ impl SourceChange { } } - pub fn system_edits>(label: L, edits: Vec) -> Self { + /// Creates a new SourceChange with the given label, + /// containing only the given `FileSystemEdits`. + pub(crate) fn file_system_edits>(label: L, edits: Vec) -> Self { SourceChange { label: label.into(), source_file_edits: vec![], @@ -116,20 +135,36 @@ impl SourceChange { } } - pub fn source_edit>(label: L, edit: SourceFileEdit) -> Self { + /// Creates a new SourceChange with the given label, + /// containing only a single `SourceFileEdit`. + pub(crate) fn source_edit>(label: L, edit: SourceFileEdit) -> Self { SourceChange::source_edits(label, vec![edit]) } - pub fn system_edit>(label: L, edit: FileSystemEdit) -> Self { - SourceChange::system_edits(label, vec![edit]) + /// Creates a new SourceChange with the given label + /// from the given `FileId` and `TextEdit` + pub(crate) fn source_file_edit_from>( + label: L, + file_id: FileId, + edit: TextEdit, + ) -> Self { + SourceChange::source_edit(label, SourceFileEdit { file_id, edit }) + } + + /// Creates a new SourceChange with the given label + /// from the given `FileId` and `TextEdit` + pub(crate) fn file_system_edit>(label: L, edit: FileSystemEdit) -> Self { + SourceChange::file_system_edits(label, vec![edit]) } - pub fn with_cursor(mut self, cursor_position: FilePosition) -> Self { + /// Sets the cursor position to the given `FilePosition` + pub(crate) fn with_cursor(mut self, cursor_position: FilePosition) -> Self { self.cursor_position = Some(cursor_position); self } - pub fn with_cursor_opt(mut self, cursor_position: Option) -> Self { + /// Sets the cursor position to the given `FilePosition` + pub(crate) fn with_cursor_opt(mut self, cursor_position: Option) -> Self { self.cursor_position = cursor_position; self } diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs index b7784e577..78f77e761 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs @@ -187,12 +187,7 @@ fn rename_mod( }; source_file_edits.push(edit); - Some(SourceChange { - label: "rename".to_string(), - source_file_edits, - file_system_edits, - cursor_position: None, - }) + Some(SourceChange::from_edits("rename", source_file_edits, file_system_edits)) } fn rename_reference( @@ -211,12 +206,7 @@ fn rename_reference( return None; } - Some(SourceChange { - label: "rename".to_string(), - source_file_edits: edit, - file_system_edits: Vec::new(), - cursor_position: None, - }) + Some(SourceChange::source_edits("rename", edit)) } #[cfg(test)] diff --git a/crates/ra_ide_api/src/typing.rs b/crates/ra_ide_api/src/typing.rs index 424084468..aa9971450 100644 --- a/crates/ra_ide_api/src/typing.rs +++ b/crates/ra_ide_api/src/typing.rs @@ -112,16 +112,14 @@ pub(crate) fn on_dot_typed(db: &RootDatabase, position: FilePosition) -> Option< TextRange::from_to(position.offset - current_indent_len, position.offset), target_indent.into(), ); - let res = SourceChange { - label: "reindent dot".to_string(), - source_file_edits: vec![SourceFileEdit { edit: edit.finish(), file_id: position.file_id }], - file_system_edits: vec![], - cursor_position: Some(FilePosition { + + let res = SourceChange::source_file_edit_from("reindent dot", position.file_id, edit.finish()) + .with_cursor(FilePosition { offset: position.offset + target_indent_len - current_indent_len + TextUnit::of_char('.'), file_id: position.file_id, - }), - }; + }); + Some(res) } -- cgit v1.2.3