Merge #4541
4541: Remove set_cursor r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
f83785a94a
@ -171,19 +171,13 @@ impl Assists {
|
||||
|
||||
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 @@ 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;
|
||||
}
|
||||
|
@ -68,7 +68,6 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext)
|
||||
.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);
|
||||
})
|
||||
}
|
||||
@ -83,7 +82,7 @@ mod tests {
|
||||
fn test_replace_if_let_with_match_unwraps_simple_expressions() {
|
||||
check_assist(
|
||||
replace_if_let_with_match,
|
||||
"
|
||||
r#"
|
||||
impl VariantData {
|
||||
pub fn is_struct(&self) -> bool {
|
||||
if <|>let VariantData::Struct(..) = *self {
|
||||
@ -92,16 +91,16 @@ impl VariantData {
|
||||
false
|
||||
}
|
||||
}
|
||||
} ",
|
||||
"
|
||||
} "#,
|
||||
r#"
|
||||
impl VariantData {
|
||||
pub fn is_struct(&self) -> bool {
|
||||
<|>match *self {
|
||||
match *self {
|
||||
VariantData::Struct(..) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
} ",
|
||||
} "#,
|
||||
)
|
||||
}
|
||||
|
||||
@ -109,7 +108,7 @@ impl VariantData {
|
||||
fn test_replace_if_let_with_match_doesnt_unwrap_multiline_expressions() {
|
||||
check_assist(
|
||||
replace_if_let_with_match,
|
||||
"
|
||||
r#"
|
||||
fn foo() {
|
||||
if <|>let VariantData::Struct(..) = a {
|
||||
bar(
|
||||
@ -118,10 +117,10 @@ fn foo() {
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} ",
|
||||
"
|
||||
} "#,
|
||||
r#"
|
||||
fn foo() {
|
||||
<|>match a {
|
||||
match a {
|
||||
VariantData::Struct(..) => {
|
||||
bar(
|
||||
123
|
||||
@ -129,7 +128,7 @@ fn foo() {
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
} ",
|
||||
} "#,
|
||||
)
|
||||
}
|
||||
|
||||
@ -137,7 +136,7 @@ fn foo() {
|
||||
fn replace_if_let_with_match_target() {
|
||||
check_assist_target(
|
||||
replace_if_let_with_match,
|
||||
"
|
||||
r#"
|
||||
impl VariantData {
|
||||
pub fn is_struct(&self) -> bool {
|
||||
if <|>let VariantData::Struct(..) = *self {
|
||||
@ -146,7 +145,7 @@ impl VariantData {
|
||||
false
|
||||
}
|
||||
}
|
||||
} ",
|
||||
} "#,
|
||||
"if let VariantData::Struct(..) = *self {
|
||||
true
|
||||
} else {
|
||||
@ -176,7 +175,7 @@ enum Option<T> { Some(T), None }
|
||||
use Option::*;
|
||||
|
||||
fn foo(x: Option<i32>) {
|
||||
<|>match x {
|
||||
match x {
|
||||
Some(x) => println!("{}", x),
|
||||
None => println!("none"),
|
||||
}
|
||||
@ -206,7 +205,7 @@ enum Result<T, E> { Ok(T), Err(E) }
|
||||
use Result::*;
|
||||
|
||||
fn foo(x: Result<i32, ()>) {
|
||||
<|>match x {
|
||||
match x {
|
||||
Ok(x) => println!("{}", x),
|
||||
Err(_) => println!("none"),
|
||||
}
|
||||
|
@ -58,12 +58,9 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) ->
|
||||
let stmt = make::expr_stmt(if_);
|
||||
|
||||
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);
|
||||
|
||||
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) }
|
||||
|
||||
fn main() {
|
||||
if let <|>x = E::X(92) {
|
||||
if let x = E::X(92) {
|
||||
}
|
||||
}
|
||||
",
|
||||
|
@ -9,7 +9,10 @@ use ra_syntax::{
|
||||
AstNode,
|
||||
};
|
||||
|
||||
use crate::{utils::TryEnum, AssistContext, AssistId, Assists};
|
||||
use crate::{
|
||||
utils::{render_snippet, Cursor, TryEnum},
|
||||
AssistContext, AssistId, Assists,
|
||||
};
|
||||
|
||||
// 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 y = match x {
|
||||
// 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 happy_variant = TryEnum::from_ty(&ctx.sema, &ty)?.happy_case();
|
||||
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 it = make::bind_pat(make::name("a")).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)
|
||||
.indent(IndentLevel::from_node(method_call.syntax()));
|
||||
|
||||
edit.set_cursor(caller.syntax().text_range().start());
|
||||
edit.replace_ast::<ast::Expr>(method_call.into(), match_expr);
|
||||
let range = method_call.syntax().text_range();
|
||||
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)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::tests::{check_assist, check_assist_target};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_replace_result_unwrap_with_match() {
|
||||
check_assist(
|
||||
@ -85,9 +102,9 @@ enum Result<T, E> { Ok(T), Err(E) }
|
||||
fn i<T>(a: T) -> T { a }
|
||||
fn main() {
|
||||
let x: Result<i32, i32> = Result::Ok(92);
|
||||
let y = <|>match i(x) {
|
||||
let y = match i(x) {
|
||||
Ok(a) => a,
|
||||
_ => unreachable!(),
|
||||
$0_ => unreachable!(),
|
||||
};
|
||||
}
|
||||
",
|
||||
@ -111,9 +128,9 @@ enum Option<T> { Some(T), None }
|
||||
fn i<T>(a: T) -> T { a }
|
||||
fn main() {
|
||||
let x = Option::Some(92);
|
||||
let y = <|>match i(x) {
|
||||
let y = match i(x) {
|
||||
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 main() {
|
||||
let x: Result<i32, i32> = Result::Ok(92);
|
||||
let y = <|>match i(x) {
|
||||
let y = match i(x) {
|
||||
Ok(a) => a,
|
||||
_ => unreachable!(),
|
||||
$0_ => unreachable!(),
|
||||
}.count_zeroes();
|
||||
}
|
||||
",
|
||||
|
@ -26,12 +26,10 @@ pub(crate) fn split_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
|
||||
if new_tree == use_tree {
|
||||
return None;
|
||||
}
|
||||
let cursor = ctx.offset();
|
||||
|
||||
let target = colon_colon.text_range();
|
||||
acc.add(AssistId("split_import"), "Split import", target, |edit| {
|
||||
edit.replace_ast(use_tree, new_tree);
|
||||
edit.set_cursor(cursor);
|
||||
})
|
||||
}
|
||||
|
||||
@ -46,7 +44,7 @@ mod tests {
|
||||
check_assist(
|
||||
split_import,
|
||||
"use crate::<|>db::RootDatabase;",
|
||||
"use crate::<|>{db::RootDatabase};",
|
||||
"use crate::{db::RootDatabase};",
|
||||
)
|
||||
}
|
||||
|
||||
@ -55,7 +53,7 @@ mod tests {
|
||||
check_assist(
|
||||
split_import,
|
||||
"use crate:<|>:db::{RootDatabase, FileSymbol}",
|
||||
"use crate:<|>:{db::{RootDatabase, FileSymbol}}",
|
||||
"use crate::{db::{RootDatabase, FileSymbol}}",
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -764,7 +764,7 @@ fn main() {
|
||||
let x: Result<i32, i32> = Result::Ok(92);
|
||||
let y = match x {
|
||||
Ok(a) => a,
|
||||
_ => unreachable!(),
|
||||
$0_ => unreachable!(),
|
||||
};
|
||||
}
|
||||
"#####,
|
||||
|
@ -733,7 +733,7 @@ fn main() {
|
||||
let x: Result<i32, i32> = Result::Ok(92);
|
||||
let y = match x {
|
||||
Ok(a) => a,
|
||||
_ => unreachable!(),
|
||||
$0_ => unreachable!(),
|
||||
};
|
||||
}
|
||||
```
|
||||
|
Loading…
x
Reference in New Issue
Block a user