mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-27 12:25:05 +00:00
Merge #2075
2075: document a couple of assists r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
65e3fc8e77
11 changed files with 122 additions and 54 deletions
|
@ -8,7 +8,9 @@ use ra_syntax::{
|
||||||
use crate::{Assist, AssistCtx, AssistId};
|
use crate::{Assist, AssistCtx, AssistId};
|
||||||
|
|
||||||
// Assist: add_derive
|
// Assist: add_derive
|
||||||
|
//
|
||||||
// Adds a new `#[derive()]` clause to a struct or enum.
|
// Adds a new `#[derive()]` clause to a struct or enum.
|
||||||
|
//
|
||||||
// ```
|
// ```
|
||||||
// struct Point {
|
// struct Point {
|
||||||
// x: u32,
|
// x: u32,
|
||||||
|
|
|
@ -7,7 +7,9 @@ use ra_syntax::{
|
||||||
use crate::{Assist, AssistCtx, AssistId};
|
use crate::{Assist, AssistCtx, AssistId};
|
||||||
|
|
||||||
// Assist: add_explicit_type
|
// Assist: add_explicit_type
|
||||||
|
//
|
||||||
// Specify type for a let binding
|
// Specify type for a let binding
|
||||||
|
//
|
||||||
// ```
|
// ```
|
||||||
// fn main() {
|
// fn main() {
|
||||||
// let x<|> = 92;
|
// let x<|> = 92;
|
||||||
|
|
|
@ -9,7 +9,9 @@ use ra_syntax::{
|
||||||
use crate::{Assist, AssistCtx, AssistId};
|
use crate::{Assist, AssistCtx, AssistId};
|
||||||
|
|
||||||
// Assist: add_impl
|
// Assist: add_impl
|
||||||
|
//
|
||||||
// Adds a new inherent impl for a type
|
// Adds a new inherent impl for a type
|
||||||
|
//
|
||||||
// ```
|
// ```
|
||||||
// struct Ctx<T: Clone> {
|
// struct Ctx<T: Clone> {
|
||||||
// data: T,<|>
|
// data: T,<|>
|
||||||
|
|
|
@ -13,7 +13,9 @@ enum AddMissingImplMembersMode {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assist: add_impl_missing_members
|
// Assist: add_impl_missing_members
|
||||||
|
//
|
||||||
// Adds scaffold for required impl members
|
// Adds scaffold for required impl members
|
||||||
|
//
|
||||||
// ```
|
// ```
|
||||||
// trait T {
|
// trait T {
|
||||||
// Type X;
|
// Type X;
|
||||||
|
|
|
@ -5,11 +5,13 @@ use ra_syntax::SyntaxNode;
|
||||||
use crate::{Assist, AssistCtx, AssistId};
|
use crate::{Assist, AssistCtx, AssistId};
|
||||||
|
|
||||||
// Assist: apply_demorgan
|
// Assist: apply_demorgan
|
||||||
|
//
|
||||||
// Apply [De Morgan's law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws).
|
// Apply [De Morgan's law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws).
|
||||||
// This transforms expressions of the form `!l || !r` into `!(l && r)`.
|
// This transforms expressions of the form `!l || !r` into `!(l && r)`.
|
||||||
// This also works with `&&`. This assist can only be applied with the cursor
|
// This also works with `&&`. This assist can only be applied with the cursor
|
||||||
// on either `||` or `&&`, with both operands being a negation of some kind.
|
// on either `||` or `&&`, with both operands being a negation of some kind.
|
||||||
// This means something of the form `!x` or `x != y`.
|
// This means something of the form `!x` or `x != y`.
|
||||||
|
//
|
||||||
// ```
|
// ```
|
||||||
// fn main() {
|
// fn main() {
|
||||||
// if x != 4 ||<|> !y {}
|
// if x != 4 ||<|> !y {}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
//! FIXME: write short doc here
|
|
||||||
|
|
||||||
use hir::db::HirDatabase;
|
use hir::db::HirDatabase;
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
ast::{self, NameOwner, VisibilityOwner},
|
ast::{self, NameOwner, VisibilityOwner},
|
||||||
|
@ -13,6 +11,17 @@ use ra_syntax::{
|
||||||
|
|
||||||
use crate::{Assist, AssistCtx, AssistId};
|
use crate::{Assist, AssistCtx, AssistId};
|
||||||
|
|
||||||
|
// Assist: change_visibility
|
||||||
|
//
|
||||||
|
// Adds or changes existing visibility specifier.
|
||||||
|
//
|
||||||
|
// ```
|
||||||
|
// fn<|> frobnicate() {}
|
||||||
|
// ```
|
||||||
|
// ->
|
||||||
|
// ```
|
||||||
|
// pub(crate) fn frobnicate() {}
|
||||||
|
// ```
|
||||||
pub(crate) fn change_visibility(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
pub(crate) fn change_visibility(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||||
if let Some(vis) = ctx.node_at_offset::<ast::Visibility>() {
|
if let Some(vis) = ctx.node_at_offset::<ast::Visibility>() {
|
||||||
return change_vis(ctx, vis);
|
return change_vis(ctx, vis);
|
||||||
|
|
|
@ -7,6 +7,30 @@ use ra_syntax::ast::{self, edit::IndentLevel, make, AstNode, NameOwner};
|
||||||
|
|
||||||
use crate::{Assist, AssistCtx, AssistId};
|
use crate::{Assist, AssistCtx, AssistId};
|
||||||
|
|
||||||
|
// Assist: fill_match_arms
|
||||||
|
//
|
||||||
|
// Adds missing clauses to a `match` expression.
|
||||||
|
//
|
||||||
|
// ```
|
||||||
|
// enum Action { Move { distance: u32 }, Stop }
|
||||||
|
//
|
||||||
|
// fn handle(action: Action) {
|
||||||
|
// match action {
|
||||||
|
// <|>
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// ```
|
||||||
|
// ->
|
||||||
|
// ```
|
||||||
|
// enum Action { Move { distance: u32 }, Stop }
|
||||||
|
//
|
||||||
|
// fn handle(action: Action) {
|
||||||
|
// match action {
|
||||||
|
// Action::Move{ distance } => (),
|
||||||
|
// Action::Stop => (),
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// ```
|
||||||
pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||||
let match_expr = ctx.node_at_offset::<ast::MatchExpr>()?;
|
let match_expr = ctx.node_at_offset::<ast::MatchExpr>()?;
|
||||||
let match_arm_list = match_expr.match_arm_list()?;
|
let match_arm_list = match_expr.match_arm_list()?;
|
||||||
|
|
|
@ -141,6 +141,19 @@ fn main() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn doctest_change_visibility() {
|
||||||
|
check(
|
||||||
|
"change_visibility",
|
||||||
|
r#####"
|
||||||
|
fn<|> frobnicate() {}
|
||||||
|
"#####,
|
||||||
|
r#####"
|
||||||
|
pub(crate) fn frobnicate() {}
|
||||||
|
"#####,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn doctest_convert_to_guarded_return() {
|
fn doctest_convert_to_guarded_return() {
|
||||||
check(
|
check(
|
||||||
|
@ -164,3 +177,29 @@ fn main() {
|
||||||
"#####,
|
"#####,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn doctest_fill_match_arms() {
|
||||||
|
check(
|
||||||
|
"fill_match_arms",
|
||||||
|
r#####"
|
||||||
|
enum Action { Move { distance: u32 }, Stop }
|
||||||
|
|
||||||
|
fn handle(action: Action) {
|
||||||
|
match action {
|
||||||
|
<|>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#####,
|
||||||
|
r#####"
|
||||||
|
enum Action { Move { distance: u32 }, Stop }
|
||||||
|
|
||||||
|
fn handle(action: Action) {
|
||||||
|
match action {
|
||||||
|
Action::Move{ distance } => (),
|
||||||
|
Action::Stop => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#####,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
|
@ -137,6 +137,18 @@ fn main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## `change_visibility`
|
||||||
|
|
||||||
|
Adds or changes existing visibility specifier.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// BEFORE
|
||||||
|
fn<|> frobnicate() {}
|
||||||
|
|
||||||
|
// AFTER
|
||||||
|
pub(crate) fn frobnicate() {}
|
||||||
|
```
|
||||||
|
|
||||||
## `convert_to_guarded_return`
|
## `convert_to_guarded_return`
|
||||||
|
|
||||||
Replace a large conditional with a guarded return.
|
Replace a large conditional with a guarded return.
|
||||||
|
@ -159,3 +171,28 @@ fn main() {
|
||||||
bar();
|
bar();
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## `fill_match_arms`
|
||||||
|
|
||||||
|
Adds missing clauses to a `match` expression.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// BEFORE
|
||||||
|
enum Action { Move { distance: u32 }, Stop }
|
||||||
|
|
||||||
|
fn handle(action: Action) {
|
||||||
|
match action {
|
||||||
|
<|>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AFTER
|
||||||
|
enum Action { Move { distance: u32 }, Stop }
|
||||||
|
|
||||||
|
fn handle(action: Action) {
|
||||||
|
match action {
|
||||||
|
Action::Move{ distance } => (),
|
||||||
|
Action::Stop => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
|
@ -118,57 +118,6 @@ impl Debug<|> for Foo {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
- Change Visibility
|
|
||||||
|
|
||||||
```rust
|
|
||||||
// before:
|
|
||||||
<|>fn foo() {}
|
|
||||||
|
|
||||||
// after:
|
|
||||||
<|>pub(crate) fn foo() {}
|
|
||||||
|
|
||||||
// after:
|
|
||||||
<|>pub fn foo() {}
|
|
||||||
```
|
|
||||||
|
|
||||||
- Fill match arms
|
|
||||||
|
|
||||||
```rust
|
|
||||||
// before:
|
|
||||||
enum A {
|
|
||||||
As,
|
|
||||||
Bs,
|
|
||||||
Cs(String),
|
|
||||||
Ds(String, String),
|
|
||||||
Es{x: usize, y: usize}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let a = A::As;
|
|
||||||
match a<|> {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// after:
|
|
||||||
enum A {
|
|
||||||
As,
|
|
||||||
Bs,
|
|
||||||
Cs(String),
|
|
||||||
Ds(String, String),
|
|
||||||
Es{x: usize, y: usize}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let a = A::As;
|
|
||||||
match <|>a {
|
|
||||||
A::As => (),
|
|
||||||
A::Bs => (),
|
|
||||||
A::Cs(_) => (),
|
|
||||||
A::Ds(_, _) => (),
|
|
||||||
A::Es{x, y} => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
- Fill struct fields
|
- Fill struct fields
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
|
|
@ -51,7 +51,7 @@ fn collect_assists() -> Result<Vec<Assist>> {
|
||||||
id
|
id
|
||||||
);
|
);
|
||||||
|
|
||||||
let doc = take_until(lines.by_ref(), "```");
|
let doc = take_until(lines.by_ref(), "```").trim().to_string();
|
||||||
let before = take_until(lines.by_ref(), "```");
|
let before = take_until(lines.by_ref(), "```");
|
||||||
|
|
||||||
assert_eq!(lines.next().unwrap().as_str(), "->");
|
assert_eq!(lines.next().unwrap().as_str(), "->");
|
||||||
|
|
Loading…
Reference in a new issue