4541: Remove set_cursor r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-05-20 22:04:42 +00:00 committed by GitHub
commit f83785a94a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 61 additions and 68 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

@ -68,7 +68,6 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext)
.indent(IndentLevel::from_node(if_expr.syntax())) .indent(IndentLevel::from_node(if_expr.syntax()))
}; };
edit.set_cursor(if_expr.syntax().text_range().start());
edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr); edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr);
}) })
} }
@ -83,7 +82,7 @@ mod tests {
fn test_replace_if_let_with_match_unwraps_simple_expressions() { fn test_replace_if_let_with_match_unwraps_simple_expressions() {
check_assist( check_assist(
replace_if_let_with_match, replace_if_let_with_match,
" r#"
impl VariantData { impl VariantData {
pub fn is_struct(&self) -> bool { pub fn is_struct(&self) -> bool {
if <|>let VariantData::Struct(..) = *self { if <|>let VariantData::Struct(..) = *self {
@ -92,16 +91,16 @@ impl VariantData {
false false
} }
} }
} ", } "#,
" r#"
impl VariantData { impl VariantData {
pub fn is_struct(&self) -> bool { pub fn is_struct(&self) -> bool {
<|>match *self { match *self {
VariantData::Struct(..) => true, VariantData::Struct(..) => true,
_ => false, _ => false,
} }
} }
} ", } "#,
) )
} }
@ -109,7 +108,7 @@ impl VariantData {
fn test_replace_if_let_with_match_doesnt_unwrap_multiline_expressions() { fn test_replace_if_let_with_match_doesnt_unwrap_multiline_expressions() {
check_assist( check_assist(
replace_if_let_with_match, replace_if_let_with_match,
" r#"
fn foo() { fn foo() {
if <|>let VariantData::Struct(..) = a { if <|>let VariantData::Struct(..) = a {
bar( bar(
@ -118,10 +117,10 @@ fn foo() {
} else { } else {
false false
} }
} ", } "#,
" r#"
fn foo() { fn foo() {
<|>match a { match a {
VariantData::Struct(..) => { VariantData::Struct(..) => {
bar( bar(
123 123
@ -129,7 +128,7 @@ fn foo() {
} }
_ => false, _ => false,
} }
} ", } "#,
) )
} }
@ -137,7 +136,7 @@ fn foo() {
fn replace_if_let_with_match_target() { fn replace_if_let_with_match_target() {
check_assist_target( check_assist_target(
replace_if_let_with_match, replace_if_let_with_match,
" r#"
impl VariantData { impl VariantData {
pub fn is_struct(&self) -> bool { pub fn is_struct(&self) -> bool {
if <|>let VariantData::Struct(..) = *self { if <|>let VariantData::Struct(..) = *self {
@ -146,7 +145,7 @@ impl VariantData {
false false
} }
} }
} ", } "#,
"if let VariantData::Struct(..) = *self { "if let VariantData::Struct(..) = *self {
true true
} else { } else {
@ -176,7 +175,7 @@ enum Option<T> { Some(T), None }
use Option::*; use Option::*;
fn foo(x: Option<i32>) { fn foo(x: Option<i32>) {
<|>match x { match x {
Some(x) => println!("{}", x), Some(x) => println!("{}", x),
None => println!("none"), None => println!("none"),
} }
@ -206,7 +205,7 @@ enum Result<T, E> { Ok(T), Err(E) }
use Result::*; use Result::*;
fn foo(x: Result<i32, ()>) { fn foo(x: Result<i32, ()>) {
<|>match x { match x {
Ok(x) => println!("{}", x), Ok(x) => println!("{}", x),
Err(_) => println!("none"), Err(_) => println!("none"),
} }

View File

@ -58,12 +58,9 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) ->
let stmt = make::expr_stmt(if_); let stmt = make::expr_stmt(if_);
let placeholder = stmt.syntax().descendants().find_map(ast::PlaceholderPat::cast).unwrap(); let placeholder = stmt.syntax().descendants().find_map(ast::PlaceholderPat::cast).unwrap();
let target_offset =
let_stmt.syntax().text_range().start() + placeholder.syntax().text_range().start();
let stmt = stmt.replace_descendant(placeholder.into(), original_pat); let stmt = stmt.replace_descendant(placeholder.into(), original_pat);
edit.replace_ast(ast::Stmt::from(let_stmt), ast::Stmt::from(stmt)); edit.replace_ast(ast::Stmt::from(let_stmt), ast::Stmt::from(stmt));
edit.set_cursor(target_offset);
}) })
} }
@ -88,7 +85,7 @@ fn main() {
enum E<T> { X(T), Y(T) } enum E<T> { X(T), Y(T) }
fn main() { fn main() {
if let <|>x = E::X(92) { if let x = E::X(92) {
} }
} }
", ",

View File

@ -9,7 +9,10 @@ use ra_syntax::{
AstNode, AstNode,
}; };
use crate::{utils::TryEnum, AssistContext, AssistId, Assists}; use crate::{
utils::{render_snippet, Cursor, TryEnum},
AssistContext, AssistId, Assists,
};
// Assist: replace_unwrap_with_match // Assist: replace_unwrap_with_match
// //
@ -29,7 +32,7 @@ use crate::{utils::TryEnum, AssistContext, AssistId, Assists};
// let x: Result<i32, i32> = Result::Ok(92); // let x: Result<i32, i32> = Result::Ok(92);
// let y = match x { // let y = match x {
// Ok(a) => a, // Ok(a) => a,
// _ => unreachable!(), // $0_ => unreachable!(),
// }; // };
// } // }
// ``` // ```
@ -43,7 +46,7 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext)
let ty = ctx.sema.type_of_expr(&caller)?; let ty = ctx.sema.type_of_expr(&caller)?;
let happy_variant = TryEnum::from_ty(&ctx.sema, &ty)?.happy_case(); let happy_variant = TryEnum::from_ty(&ctx.sema, &ty)?.happy_case();
let target = method_call.syntax().text_range(); let target = method_call.syntax().text_range();
acc.add(AssistId("replace_unwrap_with_match"), "Replace unwrap with match", target, |edit| { acc.add(AssistId("replace_unwrap_with_match"), "Replace unwrap with match", target, |builder| {
let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant))); let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant)));
let it = make::bind_pat(make::name("a")).into(); let it = make::bind_pat(make::name("a")).into();
let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into(); let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into();
@ -58,16 +61,30 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext)
let match_expr = make::expr_match(caller.clone(), match_arm_list) let match_expr = make::expr_match(caller.clone(), match_arm_list)
.indent(IndentLevel::from_node(method_call.syntax())); .indent(IndentLevel::from_node(method_call.syntax()));
edit.set_cursor(caller.syntax().text_range().start()); let range = method_call.syntax().text_range();
edit.replace_ast::<ast::Expr>(method_call.into(), match_expr); match ctx.config.snippet_cap {
Some(cap) => {
let err_arm = match_expr
.syntax()
.descendants()
.filter_map(ast::MatchArm::cast)
.last()
.unwrap();
let snippet =
render_snippet(cap, match_expr.syntax(), Cursor::Before(err_arm.syntax()));
builder.replace_snippet(cap, range, snippet)
}
None => builder.replace(range, match_expr.to_string()),
}
}) })
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*;
use crate::tests::{check_assist, check_assist_target}; use crate::tests::{check_assist, check_assist_target};
use super::*;
#[test] #[test]
fn test_replace_result_unwrap_with_match() { fn test_replace_result_unwrap_with_match() {
check_assist( check_assist(
@ -85,9 +102,9 @@ enum Result<T, E> { Ok(T), Err(E) }
fn i<T>(a: T) -> T { a } fn i<T>(a: T) -> T { a }
fn main() { fn main() {
let x: Result<i32, i32> = Result::Ok(92); let x: Result<i32, i32> = Result::Ok(92);
let y = <|>match i(x) { let y = match i(x) {
Ok(a) => a, Ok(a) => a,
_ => unreachable!(), $0_ => unreachable!(),
}; };
} }
", ",
@ -111,9 +128,9 @@ enum Option<T> { Some(T), None }
fn i<T>(a: T) -> T { a } fn i<T>(a: T) -> T { a }
fn main() { fn main() {
let x = Option::Some(92); let x = Option::Some(92);
let y = <|>match i(x) { let y = match i(x) {
Some(a) => a, Some(a) => a,
_ => unreachable!(), $0_ => unreachable!(),
}; };
} }
", ",
@ -137,9 +154,9 @@ enum Result<T, E> { Ok(T), Err(E) }
fn i<T>(a: T) -> T { a } fn i<T>(a: T) -> T { a }
fn main() { fn main() {
let x: Result<i32, i32> = Result::Ok(92); let x: Result<i32, i32> = Result::Ok(92);
let y = <|>match i(x) { let y = match i(x) {
Ok(a) => a, Ok(a) => a,
_ => unreachable!(), $0_ => unreachable!(),
}.count_zeroes(); }.count_zeroes();
} }
", ",

View File

@ -26,12 +26,10 @@ pub(crate) fn split_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
if new_tree == use_tree { if new_tree == use_tree {
return None; return None;
} }
let cursor = ctx.offset();
let target = colon_colon.text_range(); let target = colon_colon.text_range();
acc.add(AssistId("split_import"), "Split import", target, |edit| { acc.add(AssistId("split_import"), "Split import", target, |edit| {
edit.replace_ast(use_tree, new_tree); edit.replace_ast(use_tree, new_tree);
edit.set_cursor(cursor);
}) })
} }
@ -46,7 +44,7 @@ mod tests {
check_assist( check_assist(
split_import, split_import,
"use crate::<|>db::RootDatabase;", "use crate::<|>db::RootDatabase;",
"use crate::<|>{db::RootDatabase};", "use crate::{db::RootDatabase};",
) )
} }
@ -55,7 +53,7 @@ mod tests {
check_assist( check_assist(
split_import, split_import,
"use crate:<|>:db::{RootDatabase, FileSymbol}", "use crate:<|>:db::{RootDatabase, FileSymbol}",
"use crate:<|>:{db::{RootDatabase, FileSymbol}}", "use crate::{db::{RootDatabase, FileSymbol}}",
) )
} }

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

View File

@ -764,7 +764,7 @@ fn main() {
let x: Result<i32, i32> = Result::Ok(92); let x: Result<i32, i32> = Result::Ok(92);
let y = match x { let y = match x {
Ok(a) => a, Ok(a) => a,
_ => unreachable!(), $0_ => unreachable!(),
}; };
} }
"#####, "#####,

View File

@ -733,7 +733,7 @@ fn main() {
let x: Result<i32, i32> = Result::Ok(92); let x: Result<i32, i32> = Result::Ok(92);
let y = match x { let y = match x {
Ok(a) => a, Ok(a) => a,
_ => unreachable!(), $0_ => unreachable!(),
}; };
} }
``` ```