aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/assists
diff options
context:
space:
mode:
authorWesley Norris <[email protected]>2019-10-26 21:24:48 +0100
committerAleksey Kladov <[email protected]>2019-10-27 13:52:33 +0000
commit3a64a85a5279ab0fcd45b2712cb544929e86f2a0 (patch)
treeed8ac5ed57a0c978cde173c11ab9394548f697c0 /crates/ra_assists/src/assists
parente46c73dc4ecc6fa83b53aae75236193486008877 (diff)
Fixes #2054.
This adds the `flip_trait_bound` assist which allows for the swapping of two trait bounds in a trait list that are next to each other.
Diffstat (limited to 'crates/ra_assists/src/assists')
-rw-r--r--crates/ra_assists/src/assists/flip_trait_bound.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/crates/ra_assists/src/assists/flip_trait_bound.rs b/crates/ra_assists/src/assists/flip_trait_bound.rs
new file mode 100644
index 000000000..a2c954ec5
--- /dev/null
+++ b/crates/ra_assists/src/assists/flip_trait_bound.rs
@@ -0,0 +1,32 @@
1//! Assist for swapping traits inside of a trait bound list
2//!
3//! E.g. `A + B` => `B + A` when the cursor is placed by the `+` inside of a
4//! trait bound list
5
6use hir::db::HirDatabase;
7use ra_syntax::{algo::non_trivia_sibling, ast::TypeBoundList, Direction, T};
8
9use crate::{Assist, AssistCtx, AssistId};
10
11/// Flip trait bound assist.
12pub(crate) fn flip_trait_bound(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
13 // Make sure we're in a `TypeBoundList`
14 ctx.node_at_offset::<TypeBoundList>()?;
15
16 // We want to replicate the behavior of `flip_binexpr` by only suggesting
17 // the assist when the cursor is on a `+`
18 let plus = ctx.token_at_offset().find(|tkn| tkn.kind() == T![+])?;
19
20 let (before, after) = (
21 non_trivia_sibling(plus.clone().into(), Direction::Prev)?,
22 non_trivia_sibling(plus.clone().into(), Direction::Next)?,
23 );
24
25 ctx.add_action(AssistId("flip_trait_bound"), "flip trait bound", |edit| {
26 edit.target(plus.text_range());
27 edit.replace(before.text_range(), after.to_string());
28 edit.replace(after.text_range(), before.to_string());
29 });
30
31 ctx.build()
32}