From 22e1c7a112832a18509d400841b3d162228372bf Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Sun, 24 Mar 2019 22:53:41 +0200 Subject: Add convenience functions to SourceChange for creating single edits --- crates/ra_ide_api/src/assists.rs | 11 ++------ crates/ra_ide_api/src/diagnostics.rs | 27 ++++++------------ crates/ra_ide_api/src/lib.rs | 55 ++++++++++++++++++++++++++++-------- crates/ra_ide_api/src/typing.rs | 14 +++++---- 4 files changed, 63 insertions(+), 44 deletions(-) (limited to 'crates/ra_ide_api/src') diff --git a/crates/ra_ide_api/src/assists.rs b/crates/ra_ide_api/src/assists.rs index 3c0475a51..9a8c23a15 100644 --- a/crates/ra_ide_api/src/assists.rs +++ b/crates/ra_ide_api/src/assists.rs @@ -17,14 +17,9 @@ pub(crate) fn assists(db: &RootDatabase, frange: FileRange) -> Vec { let file_id = frange.file_id; let file_edit = SourceFileEdit { file_id, edit: action.edit }; let id = label.id; - let change = SourceChange { - label: label.label, - source_file_edits: vec![file_edit], - file_system_edits: vec![], - cursor_position: action - .cursor_position - .map(|offset| FilePosition { offset, file_id }), - }; + let change = SourceChange::source_edit(label.label, file_edit).with_cursor_opt( + action.cursor_position.map(|offset| FilePosition { offset, file_id }), + ); Assist { id, change } }) .collect() diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index b9dc424c6..b0f23773e 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -71,12 +71,10 @@ fn check_unnecessary_braces_in_use_statement( range, message: format!("Unnecessary braces in use statement"), severity: Severity::WeakWarning, - fix: Some(SourceChange { - label: "Remove unnecessary braces".to_string(), - source_file_edits: vec![SourceFileEdit { file_id, edit }], - file_system_edits: Vec::new(), - cursor_position: None, - }), + fix: Some(SourceChange::source_edit( + "Remove unnecessary braces", + SourceFileEdit { file_id, edit }, + )), }); } @@ -119,12 +117,10 @@ fn check_struct_shorthand_initialization( range: named_field.syntax().range(), message: format!("Shorthand struct initialization"), severity: Severity::WeakWarning, - fix: Some(SourceChange { - label: "use struct shorthand initialization".to_string(), - source_file_edits: vec![SourceFileEdit { file_id, edit }], - file_system_edits: Vec::new(), - cursor_position: None, - }), + fix: Some(SourceChange::source_edit( + "use struct shorthand initialization", + SourceFileEdit { file_id, edit }, + )), }); } } @@ -144,12 +140,7 @@ fn check_module( Problem::UnresolvedModule { candidate } => { let create_file = FileSystemEdit::CreateFile { source_root, path: candidate.clone() }; - let fix = SourceChange { - label: "create module".to_string(), - source_file_edits: Vec::new(), - file_system_edits: vec![create_file], - cursor_position: None, - }; + let fix = SourceChange::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 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 { pub cursor_position: Option, } +impl SourceChange { + pub fn source_edits>(label: L, edits: Vec) -> Self { + SourceChange { + label: label.into(), + source_file_edits: edits, + file_system_edits: vec![], + cursor_position: None, + } + } + + pub fn system_edits>(label: L, edits: Vec) -> Self { + SourceChange { + label: label.into(), + source_file_edits: vec![], + file_system_edits: edits, + cursor_position: None, + } + } + + pub 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]) + } + + pub 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 { + self.cursor_position = cursor_position; + self + } +} + #[derive(Debug)] pub struct SourceFileEdit { pub file_id: FileId, @@ -285,12 +323,7 @@ impl Analysis { file_id: frange.file_id, edit: join_lines::join_lines(&file, frange.range), }; - SourceChange { - label: "join lines".to_string(), - source_file_edits: vec![file_edit], - file_system_edits: vec![], - cursor_position: None, - } + SourceChange::source_edit("join lines", file_edit) } /// Returns an edit which should be applied when opening a new line, fixing @@ -305,12 +338,10 @@ impl Analysis { pub fn on_eq_typed(&self, position: FilePosition) -> Option { let file = self.db.parse(position.file_id); let edit = typing::on_eq_typed(&file, position.offset)?; - Some(SourceChange { - label: "add semicolon".to_string(), - source_file_edits: vec![SourceFileEdit { edit, file_id: position.file_id }], - file_system_edits: vec![], - cursor_position: None, - }) + Some(SourceChange::source_edit( + "add semicolon", + SourceFileEdit { edit, file_id: position.file_id }, + )) } /// Returns an edit which should be applied when a dot ('.') is typed on a blank line, indenting the line appropriately. diff --git a/crates/ra_ide_api/src/typing.rs b/crates/ra_ide_api/src/typing.rs index 94b228466..424084468 100644 --- a/crates/ra_ide_api/src/typing.rs +++ b/crates/ra_ide_api/src/typing.rs @@ -31,12 +31,14 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option(file: &'a SourceFile, node: &SyntaxNode) -> Option<&'a str> { -- cgit v1.2.3 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(-) (limited to 'crates/ra_ide_api/src') 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 From 4d26bae46db1e4d7a72f9808e7d19ea11fd3bd7d Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Mon, 25 Mar 2019 09:13:58 +0200 Subject: Rename source_edit to source_file_edit to match file_system_edit --- crates/ra_ide_api/src/assists.rs | 2 +- crates/ra_ide_api/src/diagnostics.rs | 4 ++-- crates/ra_ide_api/src/lib.rs | 12 ++++++------ crates/ra_ide_api/src/references.rs | 2 +- crates/ra_ide_api/src/typing.rs | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) (limited to 'crates/ra_ide_api/src') diff --git a/crates/ra_ide_api/src/assists.rs b/crates/ra_ide_api/src/assists.rs index 9a8c23a15..355c0a42a 100644 --- a/crates/ra_ide_api/src/assists.rs +++ b/crates/ra_ide_api/src/assists.rs @@ -17,7 +17,7 @@ pub(crate) fn assists(db: &RootDatabase, frange: FileRange) -> Vec { let file_id = frange.file_id; let file_edit = SourceFileEdit { file_id, edit: action.edit }; let id = label.id; - let change = SourceChange::source_edit(label.label, file_edit).with_cursor_opt( + let change = SourceChange::source_file_edit(label.label, file_edit).with_cursor_opt( action.cursor_position.map(|offset| FilePosition { offset, file_id }), ); Assist { id, change } diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index 6559de29d..156f28ca3 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -71,7 +71,7 @@ fn check_unnecessary_braces_in_use_statement( range, message: format!("Unnecessary braces in use statement"), severity: Severity::WeakWarning, - fix: Some(SourceChange::source_edit( + fix: Some(SourceChange::source_file_edit( "Remove unnecessary braces", SourceFileEdit { file_id, edit }, )), @@ -117,7 +117,7 @@ fn check_struct_shorthand_initialization( range: named_field.syntax().range(), message: format!("Shorthand struct initialization"), severity: Severity::WeakWarning, - fix: Some(SourceChange::source_edit( + fix: Some(SourceChange::source_file_edit( "use struct shorthand initialization", SourceFileEdit { file_id, edit }, )), diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 928f3d604..8aa3eb088 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -115,7 +115,7 @@ impl SourceChange { /// Creates a new SourceChange with the given label, /// containing only the given `SourceFileEdits`. - pub(crate) fn source_edits>(label: L, edits: Vec) -> Self { + pub(crate) fn source_file_edits>(label: L, edits: Vec) -> Self { SourceChange { label: label.into(), source_file_edits: edits, @@ -137,8 +137,8 @@ impl SourceChange { /// 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(crate) fn source_file_edit>(label: L, edit: SourceFileEdit) -> Self { + SourceChange::source_file_edits(label, vec![edit]) } /// Creates a new SourceChange with the given label @@ -148,7 +148,7 @@ impl SourceChange { file_id: FileId, edit: TextEdit, ) -> Self { - SourceChange::source_edit(label, SourceFileEdit { file_id, edit }) + SourceChange::source_file_edit(label, SourceFileEdit { file_id, edit }) } /// Creates a new SourceChange with the given label @@ -358,7 +358,7 @@ impl Analysis { file_id: frange.file_id, edit: join_lines::join_lines(&file, frange.range), }; - SourceChange::source_edit("join lines", file_edit) + SourceChange::source_file_edit("join lines", file_edit) } /// Returns an edit which should be applied when opening a new line, fixing @@ -373,7 +373,7 @@ impl Analysis { pub fn on_eq_typed(&self, position: FilePosition) -> Option { let file = self.db.parse(position.file_id); let edit = typing::on_eq_typed(&file, position.offset)?; - Some(SourceChange::source_edit( + Some(SourceChange::source_file_edit( "add semicolon", SourceFileEdit { edit, file_id: position.file_id }, )) diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs index 78f77e761..22741445a 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs @@ -206,7 +206,7 @@ fn rename_reference( return None; } - Some(SourceChange::source_edits("rename", edit)) + Some(SourceChange::source_file_edits("rename", edit)) } #[cfg(test)] diff --git a/crates/ra_ide_api/src/typing.rs b/crates/ra_ide_api/src/typing.rs index aa9971450..501d44dbb 100644 --- a/crates/ra_ide_api/src/typing.rs +++ b/crates/ra_ide_api/src/typing.rs @@ -33,7 +33,7 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option