From 85c64ec7bed4fba183f8ed22c96241c7baec3972 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 27 Oct 2019 16:56:53 +0300 Subject: use new api for flip_trait_bound assist --- crates/ra_assists/src/assists/flip_trait_bound.rs | 33 +++++++++++++++-------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'crates/ra_assists/src/assists') diff --git a/crates/ra_assists/src/assists/flip_trait_bound.rs b/crates/ra_assists/src/assists/flip_trait_bound.rs index 203092d03..1625b241f 100644 --- a/crates/ra_assists/src/assists/flip_trait_bound.rs +++ b/crates/ra_assists/src/assists/flip_trait_bound.rs @@ -1,21 +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 ra_syntax::{ + algo::non_trivia_sibling, + ast::{self, AstNode}, + Direction, T, +}; use crate::{Assist, AssistCtx, AssistId}; -/// Flip trait bound assist. +// Assist: flip_trait_bound +// +// Flips two trait bounds. +// +// ``` +// fn foo Copy>() { } +// ``` +// -> +// ``` +// fn foo() { } +// ``` pub(crate) fn flip_trait_bound(mut ctx: AssistCtx) -> Option { - // Make sure we're in a `TypeBoundList` - ctx.node_at_offset::()?; - // 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 plus = ctx.find_token_at_offset(T![+])?; + + // Make sure we're in a `TypeBoundList` + if ast::TypeBoundList::cast(plus.parent()).is_none() { + return None; + } let (before, after) = ( non_trivia_sibling(plus.clone().into(), Direction::Prev)?, -- cgit v1.2.3