Skip to content

Commit 10066b3

Browse files
authored
Merge pull request rust-lang#20705 from A4-Tacks/flip-range
Add ide-assist: flip_range_expr
2 parents 6c18a87 + 4199024 commit 10066b3

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/flip_binexpr.rs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use syntax::{
22
SyntaxKind, T,
3-
ast::{self, AstNode, BinExpr, syntax_factory::SyntaxFactory},
3+
ast::{self, AstNode, BinExpr, RangeItem, syntax_factory::SyntaxFactory},
4+
syntax_editor::Position,
45
};
56

67
use crate::{AssistContext, AssistId, Assists};
@@ -87,6 +88,74 @@ impl From<ast::BinaryOp> for FlipAction {
8788
}
8889
}
8990

91+
// Assist: flip_range_expr
92+
//
93+
// Flips operands of a range expression.
94+
//
95+
// ```
96+
// fn main() {
97+
// let _ = 90..$02;
98+
// }
99+
// ```
100+
// ->
101+
// ```
102+
// fn main() {
103+
// let _ = 2..90;
104+
// }
105+
// ```
106+
// ---
107+
// ```
108+
// fn main() {
109+
// let _ = 90..$0;
110+
// }
111+
// ```
112+
// ->
113+
// ```
114+
// fn main() {
115+
// let _ = ..90;
116+
// }
117+
// ```
118+
pub(crate) fn flip_range_expr(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
119+
let range_expr = ctx.find_node_at_offset::<ast::RangeExpr>()?;
120+
let op = range_expr.op_token()?;
121+
let start = range_expr.start();
122+
let end = range_expr.end();
123+
124+
if !op.text_range().contains_range(ctx.selection_trimmed()) {
125+
return None;
126+
}
127+
if start.is_none() && end.is_none() {
128+
return None;
129+
}
130+
131+
acc.add(
132+
AssistId::refactor_rewrite("flip_range_expr"),
133+
"Flip range expression",
134+
op.text_range(),
135+
|builder| {
136+
let mut edit = builder.make_editor(range_expr.syntax());
137+
138+
match (start, end) {
139+
(Some(start), Some(end)) => {
140+
edit.replace(start.syntax(), end.syntax());
141+
edit.replace(end.syntax(), start.syntax());
142+
}
143+
(Some(start), None) => {
144+
edit.delete(start.syntax());
145+
edit.insert(Position::after(&op), start.syntax().clone_for_update());
146+
}
147+
(None, Some(end)) => {
148+
edit.delete(end.syntax());
149+
edit.insert(Position::before(&op), end.syntax().clone_for_update());
150+
}
151+
(None, None) => (),
152+
}
153+
154+
builder.add_file_edits(ctx.vfs_file_id(), edit);
155+
},
156+
)
157+
}
158+
90159
#[cfg(test)]
91160
mod tests {
92161
use super::*;

src/tools/rust-analyzer/crates/ide-assists/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ mod handlers {
284284
extract_type_alias::extract_type_alias,
285285
fix_visibility::fix_visibility,
286286
flip_binexpr::flip_binexpr,
287+
flip_binexpr::flip_range_expr,
287288
flip_comma::flip_comma,
288289
flip_or_pattern::flip_or_pattern,
289290
flip_trait_bound::flip_trait_bound,

src/tools/rust-analyzer/crates/ide-assists/src/tests/generated.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,40 @@ fn foo() {
13511351
)
13521352
}
13531353

1354+
#[test]
1355+
fn doctest_flip_range_expr() {
1356+
check_doc_test(
1357+
"flip_range_expr",
1358+
r#####"
1359+
fn main() {
1360+
let _ = 90..$02;
1361+
}
1362+
"#####,
1363+
r#####"
1364+
fn main() {
1365+
let _ = 2..90;
1366+
}
1367+
"#####,
1368+
)
1369+
}
1370+
1371+
#[test]
1372+
fn doctest_flip_range_expr_1() {
1373+
check_doc_test(
1374+
"flip_range_expr",
1375+
r#####"
1376+
fn main() {
1377+
let _ = 90..$0;
1378+
}
1379+
"#####,
1380+
r#####"
1381+
fn main() {
1382+
let _ = ..90;
1383+
}
1384+
"#####,
1385+
)
1386+
}
1387+
13541388
#[test]
13551389
fn doctest_flip_trait_bound() {
13561390
check_doc_test(

0 commit comments

Comments
 (0)