Skip to content

Commit 4199024

Browse files
committed
Add ide-assist: flip_range_expr
Flips operands of a range expression. Example --- ```rust fn main() { let _ = 90..$02; } ``` -> ```rust fn main() { let _ = 2..90; } ``` --- ```rust fn main() { let _ = 90..$0; } ``` -> ```rust fn main() { let _ = ..90; } ```
1 parent 300aee8 commit 4199024

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
@@ -283,6 +283,7 @@ mod handlers {
283283
extract_type_alias::extract_type_alias,
284284
fix_visibility::fix_visibility,
285285
flip_binexpr::flip_binexpr,
286+
flip_binexpr::flip_range_expr,
286287
flip_comma::flip_comma,
287288
flip_or_pattern::flip_or_pattern,
288289
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
@@ -1298,6 +1298,40 @@ fn foo() {
12981298
)
12991299
}
13001300

1301+
#[test]
1302+
fn doctest_flip_range_expr() {
1303+
check_doc_test(
1304+
"flip_range_expr",
1305+
r#####"
1306+
fn main() {
1307+
let _ = 90..$02;
1308+
}
1309+
"#####,
1310+
r#####"
1311+
fn main() {
1312+
let _ = 2..90;
1313+
}
1314+
"#####,
1315+
)
1316+
}
1317+
1318+
#[test]
1319+
fn doctest_flip_range_expr_1() {
1320+
check_doc_test(
1321+
"flip_range_expr",
1322+
r#####"
1323+
fn main() {
1324+
let _ = 90..$0;
1325+
}
1326+
"#####,
1327+
r#####"
1328+
fn main() {
1329+
let _ = ..90;
1330+
}
1331+
"#####,
1332+
)
1333+
}
1334+
13011335
#[test]
13021336
fn doctest_flip_trait_bound() {
13031337
check_doc_test(

0 commit comments

Comments
 (0)