aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/assists/flip_trait_bound.rs33
-rw-r--r--crates/ra_assists/src/doc_tests.rs12
-rw-r--r--crates/ra_assists/src/doc_tests/generated.rs13
-rw-r--r--docs/user/assists.md12
4 files changed, 58 insertions, 12 deletions
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 @@
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; 1use hir::db::HirDatabase;
7use ra_syntax::{algo::non_trivia_sibling, ast::TypeBoundList, Direction, T}; 2use ra_syntax::{
3 algo::non_trivia_sibling,
4 ast::{self, AstNode},
5 Direction, T,
6};
8 7
9use crate::{Assist, AssistCtx, AssistId}; 8use crate::{Assist, AssistCtx, AssistId};
10 9
11/// Flip trait bound assist. 10// Assist: flip_trait_bound
11//
12// Flips two trait bounds.
13//
14// ```
15// fn foo<T: Clone +<|> Copy>() { }
16// ```
17// ->
18// ```
19// fn foo<T: Copy + Clone>() { }
20// ```
12pub(crate) fn flip_trait_bound(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 21pub(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 22 // We want to replicate the behavior of `flip_binexpr` by only suggesting
17 // the assist when the cursor is on a `+` 23 // the assist when the cursor is on a `+`
18 let plus = ctx.token_at_offset().find(|tkn| tkn.kind() == T![+])?; 24 let plus = ctx.find_token_at_offset(T![+])?;
25
26 // Make sure we're in a `TypeBoundList`
27 if ast::TypeBoundList::cast(plus.parent()).is_none() {
28 return None;
29 }
19 30
20 let (before, after) = ( 31 let (before, after) = (
21 non_trivia_sibling(plus.clone().into(), Direction::Prev)?, 32 non_trivia_sibling(plus.clone().into(), Direction::Prev)?,
diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs
index 0ccf9d730..6e1e3de84 100644
--- a/crates/ra_assists/src/doc_tests.rs
+++ b/crates/ra_assists/src/doc_tests.rs
@@ -17,7 +17,17 @@ fn check(assist_id: &str, before: &str, after: &str) {
17 let (_assist_id, action) = crate::assists(&db, frange) 17 let (_assist_id, action) = crate::assists(&db, frange)
18 .into_iter() 18 .into_iter()
19 .find(|(id, _)| id.id.0 == assist_id) 19 .find(|(id, _)| id.id.0 == assist_id)
20 .unwrap_or_else(|| panic!("Assist {:?} is not applicable", assist_id)); 20 .unwrap_or_else(|| {
21 panic!(
22 "\n\nAssist is not applicable: {}\nAvailable assists: {}",
23 assist_id,
24 crate::assists(&db, frange)
25 .into_iter()
26 .map(|(id, _)| id.id.0)
27 .collect::<Vec<_>>()
28 .join(", ")
29 )
30 });
21 31
22 let actual = action.edit.apply(&before); 32 let actual = action.edit.apply(&before);
23 assert_eq_text!(after, &actual); 33 assert_eq_text!(after, &actual);
diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs
index b8d335911..ebe49aecf 100644
--- a/crates/ra_assists/src/doc_tests/generated.rs
+++ b/crates/ra_assists/src/doc_tests/generated.rs
@@ -256,6 +256,19 @@ fn main() {
256} 256}
257 257
258#[test] 258#[test]
259fn doctest_flip_trait_bound() {
260 check(
261 "flip_trait_bound",
262 r#####"
263fn foo<T: Clone +<|> Copy>() { }
264"#####,
265 r#####"
266fn foo<T: Copy + Clone>() { }
267"#####,
268 )
269}
270
271#[test]
259fn doctest_inline_local_variable() { 272fn doctest_inline_local_variable() {
260 check( 273 check(
261 "inline_local_variable", 274 "inline_local_variable",
diff --git a/docs/user/assists.md b/docs/user/assists.md
index e4d08a7dc..b1fe44d84 100644
--- a/docs/user/assists.md
+++ b/docs/user/assists.md
@@ -248,6 +248,18 @@ fn main() {
248} 248}
249``` 249```
250 250
251## `flip_trait_bound`
252
253Flips two trait bounds.
254
255```rust
256// BEFORE
257fn foo<T: Clone +┃ Copy>() { }
258
259// AFTER
260fn foo<T: Copy + Clone>() { }
261```
262
251## `inline_local_variable` 263## `inline_local_variable`
252 264
253Inlines local variable. 265Inlines local variable.