Skip to content

Commit f83785a

Browse files
bors[bot]matklad
andauthored
Merge #4541
4541: Remove set_cursor r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 4677cea + 70930d3 commit f83785a

File tree

8 files changed

+61
-68
lines changed

8 files changed

+61
-68
lines changed

crates/ra_assists/src/assist_context.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,13 @@ impl Assists {
171171

172172
pub(crate) struct AssistBuilder {
173173
edit: TextEditBuilder,
174-
cursor_position: Option<TextSize>,
175174
file: FileId,
176175
is_snippet: bool,
177176
}
178177

179178
impl AssistBuilder {
180179
pub(crate) fn new(file: FileId) -> AssistBuilder {
181-
AssistBuilder {
182-
edit: TextEditBuilder::default(),
183-
cursor_position: None,
184-
file,
185-
is_snippet: false,
186-
}
180+
AssistBuilder { edit: TextEditBuilder::default(), file, is_snippet: false }
187181
}
188182

189183
/// Remove specified `range` of text.
@@ -241,10 +235,6 @@ impl AssistBuilder {
241235
algo::diff(&node, &new).into_text_edit(&mut self.edit)
242236
}
243237

244-
/// Specify desired position of the cursor after the assist is applied.
245-
pub(crate) fn set_cursor(&mut self, offset: TextSize) {
246-
self.cursor_position = Some(offset)
247-
}
248238
// FIXME: better API
249239
pub(crate) fn set_file(&mut self, assist_file: FileId) {
250240
self.file = assist_file;
@@ -258,12 +248,8 @@ impl AssistBuilder {
258248

259249
fn finish(self, change_label: String) -> SourceChange {
260250
let edit = self.edit.finish();
261-
if edit.is_empty() && self.cursor_position.is_none() {
262-
panic!("Only call `add_assist` if the assist can be applied")
263-
}
264-
let mut res =
265-
SingleFileChange { label: change_label, edit, cursor_position: self.cursor_position }
266-
.into_source_change(self.file);
251+
let mut res = SingleFileChange { label: change_label, edit, cursor_position: None }
252+
.into_source_change(self.file);
267253
if self.is_snippet {
268254
res.is_snippet = true;
269255
}

crates/ra_assists/src/handlers/replace_if_let_with_match.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext)
6868
.indent(IndentLevel::from_node(if_expr.syntax()))
6969
};
7070

71-
edit.set_cursor(if_expr.syntax().text_range().start());
7271
edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr);
7372
})
7473
}
@@ -83,7 +82,7 @@ mod tests {
8382
fn test_replace_if_let_with_match_unwraps_simple_expressions() {
8483
check_assist(
8584
replace_if_let_with_match,
86-
"
85+
r#"
8786
impl VariantData {
8887
pub fn is_struct(&self) -> bool {
8988
if <|>let VariantData::Struct(..) = *self {
@@ -92,24 +91,24 @@ impl VariantData {
9291
false
9392
}
9493
}
95-
} ",
96-
"
94+
} "#,
95+
r#"
9796
impl VariantData {
9897
pub fn is_struct(&self) -> bool {
99-
<|>match *self {
98+
match *self {
10099
VariantData::Struct(..) => true,
101100
_ => false,
102101
}
103102
}
104-
} ",
103+
} "#,
105104
)
106105
}
107106

108107
#[test]
109108
fn test_replace_if_let_with_match_doesnt_unwrap_multiline_expressions() {
110109
check_assist(
111110
replace_if_let_with_match,
112-
"
111+
r#"
113112
fn foo() {
114113
if <|>let VariantData::Struct(..) = a {
115114
bar(
@@ -118,26 +117,26 @@ fn foo() {
118117
} else {
119118
false
120119
}
121-
} ",
122-
"
120+
} "#,
121+
r#"
123122
fn foo() {
124-
<|>match a {
123+
match a {
125124
VariantData::Struct(..) => {
126125
bar(
127126
123
128127
)
129128
}
130129
_ => false,
131130
}
132-
} ",
131+
} "#,
133132
)
134133
}
135134

136135
#[test]
137136
fn replace_if_let_with_match_target() {
138137
check_assist_target(
139138
replace_if_let_with_match,
140-
"
139+
r#"
141140
impl VariantData {
142141
pub fn is_struct(&self) -> bool {
143142
if <|>let VariantData::Struct(..) = *self {
@@ -146,7 +145,7 @@ impl VariantData {
146145
false
147146
}
148147
}
149-
} ",
148+
} "#,
150149
"if let VariantData::Struct(..) = *self {
151150
true
152151
} else {
@@ -176,7 +175,7 @@ enum Option<T> { Some(T), None }
176175
use Option::*;
177176
178177
fn foo(x: Option<i32>) {
179-
<|>match x {
178+
match x {
180179
Some(x) => println!("{}", x),
181180
None => println!("none"),
182181
}
@@ -206,7 +205,7 @@ enum Result<T, E> { Ok(T), Err(E) }
206205
use Result::*;
207206
208207
fn foo(x: Result<i32, ()>) {
209-
<|>match x {
208+
match x {
210209
Ok(x) => println!("{}", x),
211210
Err(_) => println!("none"),
212211
}

crates/ra_assists/src/handlers/replace_let_with_if_let.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,9 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) ->
5858
let stmt = make::expr_stmt(if_);
5959

6060
let placeholder = stmt.syntax().descendants().find_map(ast::PlaceholderPat::cast).unwrap();
61-
let target_offset =
62-
let_stmt.syntax().text_range().start() + placeholder.syntax().text_range().start();
6361
let stmt = stmt.replace_descendant(placeholder.into(), original_pat);
6462

6563
edit.replace_ast(ast::Stmt::from(let_stmt), ast::Stmt::from(stmt));
66-
edit.set_cursor(target_offset);
6764
})
6865
}
6966

@@ -88,7 +85,7 @@ fn main() {
8885
enum E<T> { X(T), Y(T) }
8986
9087
fn main() {
91-
if let <|>x = E::X(92) {
88+
if let x = E::X(92) {
9289
}
9390
}
9491
",

crates/ra_assists/src/handlers/replace_unwrap_with_match.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use ra_syntax::{
99
AstNode,
1010
};
1111

12-
use crate::{utils::TryEnum, AssistContext, AssistId, Assists};
12+
use crate::{
13+
utils::{render_snippet, Cursor, TryEnum},
14+
AssistContext, AssistId, Assists,
15+
};
1316

1417
// Assist: replace_unwrap_with_match
1518
//
@@ -29,7 +32,7 @@ use crate::{utils::TryEnum, AssistContext, AssistId, Assists};
2932
// let x: Result<i32, i32> = Result::Ok(92);
3033
// let y = match x {
3134
// Ok(a) => a,
32-
// _ => unreachable!(),
35+
// $0_ => unreachable!(),
3336
// };
3437
// }
3538
// ```
@@ -43,7 +46,7 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext)
4346
let ty = ctx.sema.type_of_expr(&caller)?;
4447
let happy_variant = TryEnum::from_ty(&ctx.sema, &ty)?.happy_case();
4548
let target = method_call.syntax().text_range();
46-
acc.add(AssistId("replace_unwrap_with_match"), "Replace unwrap with match", target, |edit| {
49+
acc.add(AssistId("replace_unwrap_with_match"), "Replace unwrap with match", target, |builder| {
4750
let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant)));
4851
let it = make::bind_pat(make::name("a")).into();
4952
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)
5861
let match_expr = make::expr_match(caller.clone(), match_arm_list)
5962
.indent(IndentLevel::from_node(method_call.syntax()));
6063

61-
edit.set_cursor(caller.syntax().text_range().start());
62-
edit.replace_ast::<ast::Expr>(method_call.into(), match_expr);
64+
let range = method_call.syntax().text_range();
65+
match ctx.config.snippet_cap {
66+
Some(cap) => {
67+
let err_arm = match_expr
68+
.syntax()
69+
.descendants()
70+
.filter_map(ast::MatchArm::cast)
71+
.last()
72+
.unwrap();
73+
let snippet =
74+
render_snippet(cap, match_expr.syntax(), Cursor::Before(err_arm.syntax()));
75+
builder.replace_snippet(cap, range, snippet)
76+
}
77+
None => builder.replace(range, match_expr.to_string()),
78+
}
6379
})
6480
}
6581

6682
#[cfg(test)]
6783
mod tests {
68-
use super::*;
6984
use crate::tests::{check_assist, check_assist_target};
7085

86+
use super::*;
87+
7188
#[test]
7289
fn test_replace_result_unwrap_with_match() {
7390
check_assist(
@@ -85,9 +102,9 @@ enum Result<T, E> { Ok(T), Err(E) }
85102
fn i<T>(a: T) -> T { a }
86103
fn main() {
87104
let x: Result<i32, i32> = Result::Ok(92);
88-
let y = <|>match i(x) {
105+
let y = match i(x) {
89106
Ok(a) => a,
90-
_ => unreachable!(),
107+
$0_ => unreachable!(),
91108
};
92109
}
93110
",
@@ -111,9 +128,9 @@ enum Option<T> { Some(T), None }
111128
fn i<T>(a: T) -> T { a }
112129
fn main() {
113130
let x = Option::Some(92);
114-
let y = <|>match i(x) {
131+
let y = match i(x) {
115132
Some(a) => a,
116-
_ => unreachable!(),
133+
$0_ => unreachable!(),
117134
};
118135
}
119136
",
@@ -137,9 +154,9 @@ enum Result<T, E> { Ok(T), Err(E) }
137154
fn i<T>(a: T) -> T { a }
138155
fn main() {
139156
let x: Result<i32, i32> = Result::Ok(92);
140-
let y = <|>match i(x) {
157+
let y = match i(x) {
141158
Ok(a) => a,
142-
_ => unreachable!(),
159+
$0_ => unreachable!(),
143160
}.count_zeroes();
144161
}
145162
",

crates/ra_assists/src/handlers/split_import.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@ pub(crate) fn split_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
2626
if new_tree == use_tree {
2727
return None;
2828
}
29-
let cursor = ctx.offset();
3029

3130
let target = colon_colon.text_range();
3231
acc.add(AssistId("split_import"), "Split import", target, |edit| {
3332
edit.replace_ast(use_tree, new_tree);
34-
edit.set_cursor(cursor);
3533
})
3634
}
3735

@@ -46,7 +44,7 @@ mod tests {
4644
check_assist(
4745
split_import,
4846
"use crate::<|>db::RootDatabase;",
49-
"use crate::<|>{db::RootDatabase};",
47+
"use crate::{db::RootDatabase};",
5048
)
5149
}
5250

@@ -55,7 +53,7 @@ mod tests {
5553
check_assist(
5654
split_import,
5755
"use crate:<|>:db::{RootDatabase, FileSymbol}",
58-
"use crate:<|>:{db::{RootDatabase, FileSymbol}}",
56+
"use crate::{db::{RootDatabase, FileSymbol}}",
5957
)
6058
}
6159

0 commit comments

Comments
 (0)