From 70930d3bb2ba1d4a7a7d4a489da714096294acca Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 May 2020 00:03:42 +0200 Subject: Remove set_cursor --- crates/ra_assists/src/assist_context.rs | 20 +++----------------- crates/ra_assists/src/handlers/unwrap_block.rs | 24 ++++++++++-------------- 2 files changed, 13 insertions(+), 31 deletions(-) diff --git a/crates/ra_assists/src/assist_context.rs b/crates/ra_assists/src/assist_context.rs index 005c17776..9f6ca449b 100644 --- a/crates/ra_assists/src/assist_context.rs +++ b/crates/ra_assists/src/assist_context.rs @@ -171,19 +171,13 @@ impl Assists { pub(crate) struct AssistBuilder { edit: TextEditBuilder, - cursor_position: Option, file: FileId, is_snippet: bool, } impl AssistBuilder { pub(crate) fn new(file: FileId) -> AssistBuilder { - AssistBuilder { - edit: TextEditBuilder::default(), - cursor_position: None, - file, - is_snippet: false, - } + AssistBuilder { edit: TextEditBuilder::default(), file, is_snippet: false } } /// Remove specified `range` of text. @@ -241,10 +235,6 @@ impl AssistBuilder { algo::diff(&node, &new).into_text_edit(&mut self.edit) } - /// Specify desired position of the cursor after the assist is applied. - pub(crate) fn set_cursor(&mut self, offset: TextSize) { - self.cursor_position = Some(offset) - } // FIXME: better API pub(crate) fn set_file(&mut self, assist_file: FileId) { self.file = assist_file; @@ -258,12 +248,8 @@ impl AssistBuilder { fn finish(self, change_label: String) -> SourceChange { let edit = self.edit.finish(); - if edit.is_empty() && self.cursor_position.is_none() { - panic!("Only call `add_assist` if the assist can be applied") - } - let mut res = - SingleFileChange { label: change_label, edit, cursor_position: self.cursor_position } - .into_source_change(self.file); + let mut res = SingleFileChange { label: change_label, edit, cursor_position: None } + .into_source_change(self.file); if self.is_snippet { res.is_snippet = true; } diff --git a/crates/ra_assists/src/handlers/unwrap_block.rs b/crates/ra_assists/src/handlers/unwrap_block.rs index b76182d79..8440c7d0f 100644 --- a/crates/ra_assists/src/handlers/unwrap_block.rs +++ b/crates/ra_assists/src/handlers/unwrap_block.rs @@ -62,7 +62,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> let range_to_del_else_if = TextRange::new(ancestor_then_branch.syntax().text_range().end(), l_curly_token.text_range().start()); let range_to_del_rest = TextRange::new(then_branch.syntax().text_range().end(), if_expr.syntax().text_range().end()); - edit.set_cursor(ancestor_then_branch.syntax().text_range().end()); edit.delete(range_to_del_rest); edit.delete(range_to_del_else_if); edit.replace(target, update_expr_string(then_branch.to_string(), &[' ', '{'])); @@ -79,7 +78,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> return acc.add(assist_id, assist_label, target, |edit| { let range_to_del = TextRange::new(then_branch.syntax().text_range().end(), l_curly_token.text_range().start()); - edit.set_cursor(then_branch.syntax().text_range().end()); edit.delete(range_to_del); edit.replace(target, update_expr_string(else_block.to_string(), &[' ', '{'])); }); @@ -97,8 +95,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> let target = expr_to_unwrap.syntax().text_range(); acc.add(assist_id, assist_label, target, |edit| { - edit.set_cursor(expr.syntax().text_range().start()); - edit.replace( expr.syntax().text_range(), update_expr_string(expr_to_unwrap.to_string(), &[' ', '{', '\n']), @@ -154,7 +150,7 @@ mod tests { r#" fn main() { bar(); - <|>foo(); + foo(); //comment bar(); @@ -188,7 +184,7 @@ mod tests { //comment bar(); - }<|> + } println!("bar"); } "#, @@ -222,7 +218,7 @@ mod tests { //comment //bar(); - }<|> + } println!("bar"); } "#, @@ -258,7 +254,7 @@ mod tests { //bar(); } else if false { println!("bar"); - }<|> + } println!("foo"); } "#, @@ -298,7 +294,7 @@ mod tests { println!("bar"); } else if true { println!("foo"); - }<|> + } println!("else"); } "#, @@ -336,7 +332,7 @@ mod tests { //bar(); } else if false { println!("bar"); - }<|> + } println!("foo"); } "#, @@ -383,7 +379,7 @@ mod tests { "#, r#" fn main() { - <|>if true { + if true { foo(); //comment @@ -417,7 +413,7 @@ mod tests { r#" fn main() { for i in 0..5 { - <|>foo(); + foo(); //comment bar(); @@ -447,7 +443,7 @@ mod tests { "#, r#" fn main() { - <|>if true { + if true { foo(); //comment @@ -480,7 +476,7 @@ mod tests { "#, r#" fn main() { - <|>if true { + if true { foo(); //comment -- cgit v1.2.3