about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVille Penttinen <villem.penttinen@gmail.com>2019-03-25 09:03:10 +0200
committerVille Penttinen <villem.penttinen@gmail.com>2019-03-25 09:03:10 +0200
commitb92fcbc9567674cd240cc533aa021f63019ec38d (patch)
tree4eb3b02be2a092284c3a6cee93db07df16ee12f7
parent22e1c7a112832a18509d400841b3d162228372bf (diff)
downloadrust-b92fcbc9567674cd240cc533aa021f63019ec38d.tar.gz
rust-b92fcbc9567674cd240cc533aa021f63019ec38d.zip
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`.
-rw-r--r--crates/ra_ide_api/src/diagnostics.rs2
-rw-r--r--crates/ra_ide_api/src/lib.rs49
-rw-r--r--crates/ra_ide_api/src/references.rs14
-rw-r--r--crates/ra_ide_api/src/typing.rs12
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 b0f23773e80..6559de29d9a 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 4e4001750db..928f3d604f3 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<L: Into<String>>(label: L, edits: Vec<SourceFileEdit>) -> Self {
+    /// Creates a new SourceChange with the given label
+    /// from the edits.
+    pub(crate) fn from_edits<L: Into<String>>(
+        label: L,
+        source_file_edits: Vec<SourceFileEdit>,
+        file_system_edits: Vec<FileSystemEdit>,
+    ) -> 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<L: Into<String>>(label: L, edits: Vec<SourceFileEdit>) -> Self {
         SourceChange {
             label: label.into(),
             source_file_edits: edits,
@@ -107,7 +124,9 @@ impl SourceChange {
         }
     }
 
-    pub fn system_edits<L: Into<String>>(label: L, edits: Vec<FileSystemEdit>) -> Self {
+    /// Creates a new SourceChange with the given label,
+    /// containing only the given `FileSystemEdits`.
+    pub(crate) fn file_system_edits<L: Into<String>>(label: L, edits: Vec<FileSystemEdit>) -> Self {
         SourceChange {
             label: label.into(),
             source_file_edits: vec![],
@@ -116,20 +135,36 @@ impl SourceChange {
         }
     }
 
-    pub fn source_edit<L: Into<String>>(label: L, edit: SourceFileEdit) -> Self {
+    /// Creates a new SourceChange with the given label,
+    /// containing only a single `SourceFileEdit`.
+    pub(crate) fn source_edit<L: Into<String>>(label: L, edit: SourceFileEdit) -> Self {
         SourceChange::source_edits(label, vec![edit])
     }
 
-    pub fn system_edit<L: Into<String>>(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<L: Into<String>>(
+        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<L: Into<String>>(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<FilePosition>) -> Self {
+    /// Sets the cursor position to the given `FilePosition`
+    pub(crate) fn with_cursor_opt(mut self, cursor_position: Option<FilePosition>) -> 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 b7784e57721..78f77e761a3 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 42408446818..aa9971450f7 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)
 }