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`.
This commit is contained in:
Ville Penttinen 2019-03-25 09:03:10 +02:00
parent 22e1c7a112
commit b92fcbc956
4 changed files with 50 additions and 27 deletions

View File

@ -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(),

View File

@ -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 })
}
pub fn with_cursor(mut self, cursor_position: FilePosition) -> Self {
/// 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])
}
/// 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
}

View File

@ -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)]

View File

@ -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)
}