Remove set_cursor

This commit is contained in:
Aleksey Kladov 2020-05-21 00:03:42 +02:00
parent 4ac0abd296
commit 70930d3bb2
2 changed files with 13 additions and 31 deletions

View File

@ -171,19 +171,13 @@ impl Assists {
pub(crate) struct AssistBuilder { pub(crate) struct AssistBuilder {
edit: TextEditBuilder, edit: TextEditBuilder,
cursor_position: Option<TextSize>,
file: FileId, file: FileId,
is_snippet: bool, is_snippet: bool,
} }
impl AssistBuilder { impl AssistBuilder {
pub(crate) fn new(file: FileId) -> AssistBuilder { pub(crate) fn new(file: FileId) -> AssistBuilder {
AssistBuilder { AssistBuilder { edit: TextEditBuilder::default(), file, is_snippet: false }
edit: TextEditBuilder::default(),
cursor_position: None,
file,
is_snippet: false,
}
} }
/// Remove specified `range` of text. /// Remove specified `range` of text.
@ -241,10 +235,6 @@ impl AssistBuilder {
algo::diff(&node, &new).into_text_edit(&mut self.edit) 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 // FIXME: better API
pub(crate) fn set_file(&mut self, assist_file: FileId) { pub(crate) fn set_file(&mut self, assist_file: FileId) {
self.file = assist_file; self.file = assist_file;
@ -258,12 +248,8 @@ impl AssistBuilder {
fn finish(self, change_label: String) -> SourceChange { fn finish(self, change_label: String) -> SourceChange {
let edit = self.edit.finish(); let edit = self.edit.finish();
if edit.is_empty() && self.cursor_position.is_none() { let mut res = SingleFileChange { label: change_label, edit, cursor_position: None }
panic!("Only call `add_assist` if the assist can be applied") .into_source_change(self.file);
}
let mut res =
SingleFileChange { label: change_label, edit, cursor_position: self.cursor_position }
.into_source_change(self.file);
if self.is_snippet { if self.is_snippet {
res.is_snippet = true; res.is_snippet = true;
} }

View File

@ -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_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()); 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_rest);
edit.delete(range_to_del_else_if); edit.delete(range_to_del_else_if);
edit.replace(target, update_expr_string(then_branch.to_string(), &[' ', '{'])); 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| { 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()); 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.delete(range_to_del);
edit.replace(target, update_expr_string(else_block.to_string(), &[' ', '{'])); 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(); let target = expr_to_unwrap.syntax().text_range();
acc.add(assist_id, assist_label, target, |edit| { acc.add(assist_id, assist_label, target, |edit| {
edit.set_cursor(expr.syntax().text_range().start());
edit.replace( edit.replace(
expr.syntax().text_range(), expr.syntax().text_range(),
update_expr_string(expr_to_unwrap.to_string(), &[' ', '{', '\n']), update_expr_string(expr_to_unwrap.to_string(), &[' ', '{', '\n']),
@ -154,7 +150,7 @@ mod tests {
r#" r#"
fn main() { fn main() {
bar(); bar();
<|>foo(); foo();
//comment //comment
bar(); bar();
@ -188,7 +184,7 @@ mod tests {
//comment //comment
bar(); bar();
}<|> }
println!("bar"); println!("bar");
} }
"#, "#,
@ -222,7 +218,7 @@ mod tests {
//comment //comment
//bar(); //bar();
}<|> }
println!("bar"); println!("bar");
} }
"#, "#,
@ -258,7 +254,7 @@ mod tests {
//bar(); //bar();
} else if false { } else if false {
println!("bar"); println!("bar");
}<|> }
println!("foo"); println!("foo");
} }
"#, "#,
@ -298,7 +294,7 @@ mod tests {
println!("bar"); println!("bar");
} else if true { } else if true {
println!("foo"); println!("foo");
}<|> }
println!("else"); println!("else");
} }
"#, "#,
@ -336,7 +332,7 @@ mod tests {
//bar(); //bar();
} else if false { } else if false {
println!("bar"); println!("bar");
}<|> }
println!("foo"); println!("foo");
} }
"#, "#,
@ -383,7 +379,7 @@ mod tests {
"#, "#,
r#" r#"
fn main() { fn main() {
<|>if true { if true {
foo(); foo();
//comment //comment
@ -417,7 +413,7 @@ mod tests {
r#" r#"
fn main() { fn main() {
for i in 0..5 { for i in 0..5 {
<|>foo(); foo();
//comment //comment
bar(); bar();
@ -447,7 +443,7 @@ mod tests {
"#, "#,
r#" r#"
fn main() { fn main() {
<|>if true { if true {
foo(); foo();
//comment //comment
@ -480,7 +476,7 @@ mod tests {
"#, "#,
r#" r#"
fn main() { fn main() {
<|>if true { if true {
foo(); foo();
//comment //comment