Skip to content

Commit 5e13e4e

Browse files
committed
More snippets
1 parent fd77170 commit 5e13e4e

File tree

6 files changed

+45
-97
lines changed

6 files changed

+45
-97
lines changed

crates/ra_assists/src/handlers/early_return.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext)
9797
}
9898

9999
then_block.syntax().last_child_or_token().filter(|t| t.kind() == R_CURLY)?;
100-
let cursor_position = ctx.offset();
101100

102101
let target = if_expr.syntax().text_range();
103102
acc.add(AssistId("convert_to_guarded_return"), "Convert to guarded return", target, |edit| {
@@ -148,7 +147,6 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext)
148147
}
149148
};
150149
edit.replace_ast(parent_block, ast::BlockExpr::cast(new_block).unwrap());
151-
edit.set_cursor(cursor_position);
152150

153151
fn replace(
154152
new_expr: &SyntaxNode,
@@ -207,7 +205,7 @@ mod tests {
207205
r#"
208206
fn main() {
209207
bar();
210-
if<|> !true {
208+
if !true {
211209
return;
212210
}
213211
foo();
@@ -237,7 +235,7 @@ mod tests {
237235
r#"
238236
fn main(n: Option<String>) {
239237
bar();
240-
le<|>t n = match n {
238+
let n = match n {
241239
Some(it) => it,
242240
_ => return,
243241
};
@@ -263,7 +261,7 @@ mod tests {
263261
"#,
264262
r#"
265263
fn main() {
266-
le<|>t x = match Err(92) {
264+
let x = match Err(92) {
267265
Ok(it) => it,
268266
_ => return,
269267
};
@@ -291,7 +289,7 @@ mod tests {
291289
r#"
292290
fn main(n: Option<String>) {
293291
bar();
294-
le<|>t n = match n {
292+
let n = match n {
295293
Ok(it) => it,
296294
_ => return,
297295
};
@@ -321,7 +319,7 @@ mod tests {
321319
r#"
322320
fn main() {
323321
while true {
324-
if<|> !true {
322+
if !true {
325323
continue;
326324
}
327325
foo();
@@ -349,7 +347,7 @@ mod tests {
349347
r#"
350348
fn main() {
351349
while true {
352-
le<|>t n = match n {
350+
let n = match n {
353351
Some(it) => it,
354352
_ => continue,
355353
};
@@ -378,7 +376,7 @@ mod tests {
378376
r#"
379377
fn main() {
380378
loop {
381-
if<|> !true {
379+
if !true {
382380
continue;
383381
}
384382
foo();
@@ -406,7 +404,7 @@ mod tests {
406404
r#"
407405
fn main() {
408406
loop {
409-
le<|>t n = match n {
407+
let n = match n {
410408
Some(it) => it,
411409
_ => continue,
412410
};

crates/ra_assists/src/handlers/merge_imports.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext) -> Option<()
5858
let target = tree.syntax().text_range();
5959
acc.add(AssistId("merge_imports"), "Merge imports", target, |builder| {
6060
builder.rewrite(rewriter);
61-
// FIXME: we only need because our diff is imprecise
62-
builder.set_cursor(offset);
6361
})
6462
}
6563

@@ -142,7 +140,7 @@ use std::fmt<|>::Debug;
142140
use std::fmt::Display;
143141
",
144142
r"
145-
use std::fmt<|>::{Debug, Display};
143+
use std::fmt::{Debug, Display};
146144
",
147145
)
148146
}
@@ -156,7 +154,7 @@ use std::fmt::Debug;
156154
use std::fmt<|>::Display;
157155
",
158156
r"
159-
use std::fmt:<|>:{Display, Debug};
157+
use std::fmt::{Display, Debug};
160158
",
161159
);
162160
}
@@ -169,7 +167,7 @@ use std::fmt:<|>:{Display, Debug};
169167
use std::{fmt<|>::Debug, fmt::Display};
170168
",
171169
r"
172-
use std::{fmt<|>::{Debug, Display}};
170+
use std::{fmt::{Debug, Display}};
173171
",
174172
);
175173
check_assist(
@@ -178,7 +176,7 @@ use std::{fmt<|>::{Debug, Display}};
178176
use std::{fmt::Debug, fmt<|>::Display};
179177
",
180178
r"
181-
use std::{fmt::<|>{Display, Debug}};
179+
use std::{fmt::{Display, Debug}};
182180
",
183181
);
184182
}
@@ -192,7 +190,7 @@ use std<|>::cell::*;
192190
use std::str;
193191
",
194192
r"
195-
use std<|>::{cell::*, str};
193+
use std::{cell::*, str};
196194
",
197195
)
198196
}
@@ -206,7 +204,7 @@ use std<|>::cell::*;
206204
use std::str::*;
207205
",
208206
r"
209-
use std<|>::{cell::*, str::*};
207+
use std::{cell::*, str::*};
210208
",
211209
)
212210
}
@@ -222,7 +220,7 @@ use foo::baz;
222220
/// Doc comment
223221
",
224222
r"
225-
use foo<|>::{bar, baz};
223+
use foo::{bar, baz};
226224
227225
/// Doc comment
228226
",
@@ -241,7 +239,7 @@ use {
241239
",
242240
r"
243241
use {
244-
foo<|>::{bar, baz},
242+
foo::{bar, baz},
245243
};
246244
",
247245
);
@@ -255,7 +253,7 @@ use {
255253
",
256254
r"
257255
use {
258-
foo::{bar<|>, baz},
256+
foo::{bar, baz},
259257
};
260258
",
261259
);
@@ -272,7 +270,7 @@ use foo::<|>{
272270
};
273271
",
274272
r"
275-
use foo::{<|>
273+
use foo::{
276274
FooBar,
277275
bar::baz};
278276
",

crates/ra_assists/src/handlers/merge_match_arms.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::iter::successors;
33
use ra_syntax::{
44
algo::neighbor,
55
ast::{self, AstNode},
6-
Direction, TextSize,
6+
Direction,
77
};
88

99
use crate::{AssistContext, AssistId, Assists, TextRange};
@@ -41,17 +41,6 @@ pub(crate) fn merge_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option
4141
let current_expr = current_arm.expr()?;
4242
let current_text_range = current_arm.syntax().text_range();
4343

44-
enum CursorPos {
45-
InExpr(TextSize),
46-
InPat(TextSize),
47-
}
48-
let cursor_pos = ctx.offset();
49-
let cursor_pos = if current_expr.syntax().text_range().contains(cursor_pos) {
50-
CursorPos::InExpr(current_text_range.end() - cursor_pos)
51-
} else {
52-
CursorPos::InPat(cursor_pos)
53-
};
54-
5544
// We check if the following match arms match this one. We could, but don't,
5645
// compare to the previous match arm as well.
5746
let arms_to_merge = successors(Some(current_arm), |it| neighbor(it, Direction::Next))
@@ -87,10 +76,6 @@ pub(crate) fn merge_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option
8776
let start = arms_to_merge.first().unwrap().syntax().text_range().start();
8877
let end = arms_to_merge.last().unwrap().syntax().text_range().end();
8978

90-
edit.set_cursor(match cursor_pos {
91-
CursorPos::InExpr(back_offset) => start + TextSize::of(&arm) - back_offset,
92-
CursorPos::InPat(offset) => offset,
93-
});
9479
edit.replace(TextRange::new(start, end), arm);
9580
})
9681
}
@@ -132,7 +117,7 @@ mod tests {
132117
fn main() {
133118
let x = X::A;
134119
let y = match x {
135-
X::A | X::B => { 1i32<|> }
120+
X::A | X::B => { 1i32 }
136121
X::C => { 2i32 }
137122
}
138123
}
@@ -164,7 +149,7 @@ mod tests {
164149
fn main() {
165150
let x = X::A;
166151
let y = match x {
167-
X::A | X::B | X::C | X::D => {<|> 1i32 },
152+
X::A | X::B | X::C | X::D => { 1i32 },
168153
X::E => { 2i32 },
169154
}
170155
}
@@ -197,7 +182,7 @@ mod tests {
197182
let x = X::A;
198183
let y = match x {
199184
X::A => { 1i32 },
200-
_ => { 2i<|>32 }
185+
_ => { 2i32 }
201186
}
202187
}
203188
"#,
@@ -226,7 +211,7 @@ mod tests {
226211
227212
fn main() {
228213
match X::A {
229-
X::A<|> | X::B | X::C => 92,
214+
X::A | X::B | X::C => 92,
230215
X::D => 62,
231216
_ => panic!(),
232217
}

crates/ra_assists/src/handlers/move_guard.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use ra_syntax::{
2-
ast,
3-
ast::{AstNode, AstToken, IfExpr, MatchArm},
4-
TextSize,
2+
ast::{AstNode, IfExpr, MatchArm},
3+
SyntaxKind::WHITESPACE,
54
};
65

76
use crate::{AssistContext, AssistId, Assists};
@@ -42,24 +41,15 @@ pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext) ->
4241

4342
let target = guard.syntax().text_range();
4443
acc.add(AssistId("move_guard_to_arm_body"), "Move guard to arm body", target, |edit| {
45-
let offseting_amount = match space_before_guard.and_then(|it| it.into_token()) {
46-
Some(tok) => {
47-
if ast::Whitespace::cast(tok.clone()).is_some() {
48-
let ele = tok.text_range();
49-
edit.delete(ele);
50-
ele.len()
51-
} else {
52-
TextSize::from(0)
53-
}
44+
match space_before_guard {
45+
Some(element) if element.kind() == WHITESPACE => {
46+
edit.delete(element.text_range());
5447
}
55-
_ => TextSize::from(0),
48+
_ => (),
5649
};
5750

5851
edit.delete(guard.syntax().text_range());
5952
edit.replace_node_and_indent(arm_expr.syntax(), buf);
60-
edit.set_cursor(
61-
arm_expr.syntax().text_range().start() + TextSize::from(3) - offseting_amount,
62-
);
6353
})
6454
}
6555

@@ -124,7 +114,6 @@ pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContex
124114
}
125115

126116
edit.insert(match_pat.syntax().text_range().end(), buf);
127-
edit.set_cursor(match_pat.syntax().text_range().end() + TextSize::from(1));
128117
},
129118
)
130119
}
@@ -172,7 +161,7 @@ mod tests {
172161
let t = 'a';
173162
let chars = "abcd";
174163
match t {
175-
'\r' => if chars.clone().next() == Some('\n') { <|>false },
164+
'\r' => if chars.clone().next() == Some('\n') { false },
176165
_ => true
177166
}
178167
}
@@ -195,7 +184,7 @@ mod tests {
195184
r#"
196185
fn f() {
197186
match x {
198-
y @ 4 | y @ 5 => if y > 5 { <|>true },
187+
y @ 4 | y @ 5 => if y > 5 { true },
199188
_ => false
200189
}
201190
}
@@ -222,7 +211,7 @@ mod tests {
222211
let t = 'a';
223212
let chars = "abcd";
224213
match t {
225-
'\r' <|>if chars.clone().next() == Some('\n') => false,
214+
'\r' if chars.clone().next() == Some('\n') => false,
226215
_ => true
227216
}
228217
}
@@ -266,7 +255,7 @@ mod tests {
266255
let t = 'a';
267256
let chars = "abcd";
268257
match t {
269-
'\r' <|>if chars.clone().next().is_some() => { },
258+
'\r' if chars.clone().next().is_some() => { },
270259
_ => true
271260
}
272261
}
@@ -296,7 +285,7 @@ mod tests {
296285
let mut t = 'a';
297286
let chars = "abcd";
298287
match t {
299-
'\r' <|>if chars.clone().next().is_some() => {
288+
'\r' if chars.clone().next().is_some() => {
300289
t = 'e';
301290
false
302291
},

0 commit comments

Comments
 (0)