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 @@ fn finish(mut self) -> Vec<(Assist, Option<SourceChange>)> {
pub(crate) struct AssistBuilder {
edit: TextEditBuilder,
cursor_position: Option<TextSize>,
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 @@ pub(crate) fn rewrite(&mut self, rewriter: SyntaxRewriter) {
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 @@ pub(crate) fn text_edit_builder(&mut self) -> &mut TextEditBuilder {
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;
}

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_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 @@ fn main() {
r#"
fn main() {
bar();
<|>foo();
foo();
//comment
bar();
@ -188,7 +184,7 @@ fn main() {
//comment
bar();
}<|>
}
println!("bar");
}
"#,
@ -222,7 +218,7 @@ fn main() {
//comment
//bar();
}<|>
}
println!("bar");
}
"#,
@ -258,7 +254,7 @@ fn main() {
//bar();
} else if false {
println!("bar");
}<|>
}
println!("foo");
}
"#,
@ -298,7 +294,7 @@ fn main() {
println!("bar");
} else if true {
println!("foo");
}<|>
}
println!("else");
}
"#,
@ -336,7 +332,7 @@ fn main() {
//bar();
} else if false {
println!("bar");
}<|>
}
println!("foo");
}
"#,
@ -383,7 +379,7 @@ fn main() {
"#,
r#"
fn main() {
<|>if true {
if true {
foo();
//comment
@ -417,7 +413,7 @@ fn main() {
r#"
fn main() {
for i in 0..5 {
<|>foo();
foo();
//comment
bar();
@ -447,7 +443,7 @@ fn main() {
"#,
r#"
fn main() {
<|>if true {
if true {
foo();
//comment
@ -480,7 +476,7 @@ fn main() {
"#,
r#"
fn main() {
<|>if true {
if true {
foo();
//comment