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.
This commit is contained in:
Wesley Norris 2019-10-26 16:24:48 -04:00 committed by Aleksey Kladov
parent e46c73dc4e
commit 3a64a85a52
2 changed files with 34 additions and 0 deletions

View file

@ -0,0 +1,32 @@
//! Assist for swapping traits inside of a trait bound list
//!
//! E.g. `A + B` => `B + A` when the cursor is placed by the `+` inside of a
//! trait bound list
use hir::db::HirDatabase;
use ra_syntax::{algo::non_trivia_sibling, ast::TypeBoundList, Direction, T};
use crate::{Assist, AssistCtx, AssistId};
/// Flip trait bound assist.
pub(crate) fn flip_trait_bound(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
// Make sure we're in a `TypeBoundList`
ctx.node_at_offset::<TypeBoundList>()?;
// We want to replicate the behavior of `flip_binexpr` by only suggesting
// the assist when the cursor is on a `+`
let plus = ctx.token_at_offset().find(|tkn| tkn.kind() == T![+])?;
let (before, after) = (
non_trivia_sibling(plus.clone().into(), Direction::Prev)?,
non_trivia_sibling(plus.clone().into(), Direction::Next)?,
);
ctx.add_action(AssistId("flip_trait_bound"), "flip trait bound", |edit| {
edit.target(plus.text_range());
edit.replace(before.text_range(), after.to_string());
edit.replace(after.text_range(), before.to_string());
});
ctx.build()
}

View file

@ -97,6 +97,7 @@ mod assists {
mod apply_demorgan; mod apply_demorgan;
mod flip_comma; mod flip_comma;
mod flip_binexpr; mod flip_binexpr;
mod flip_trait_bound;
mod change_visibility; mod change_visibility;
mod fill_match_arms; mod fill_match_arms;
mod merge_match_arms; mod merge_match_arms;
@ -123,6 +124,7 @@ mod assists {
merge_match_arms::merge_match_arms, merge_match_arms::merge_match_arms,
flip_comma::flip_comma, flip_comma::flip_comma,
flip_binexpr::flip_binexpr, flip_binexpr::flip_binexpr,
flip_trait_bound::flip_trait_bound,
introduce_variable::introduce_variable, introduce_variable::introduce_variable,
replace_if_let_with_match::replace_if_let_with_match, replace_if_let_with_match::replace_if_let_with_match,
split_import::split_import, split_import::split_import,