mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 23:24:29 +00:00
check style for assist docs
This commit is contained in:
parent
2f7d1f10c1
commit
a5cbd8d5e8
10 changed files with 165 additions and 15 deletions
|
@ -8,7 +8,7 @@ use crate::{Assist, AssistCtx, AssistId};
|
|||
|
||||
// Assist: add_explicit_type
|
||||
//
|
||||
// Specify type for a let binding
|
||||
// Specify type for a let binding.
|
||||
//
|
||||
// ```
|
||||
// fn main() {
|
||||
|
|
|
@ -10,7 +10,7 @@ use crate::{Assist, AssistCtx, AssistId};
|
|||
|
||||
// Assist: add_impl
|
||||
//
|
||||
// Adds a new inherent impl for a type
|
||||
// Adds a new inherent impl for a type.
|
||||
//
|
||||
// ```
|
||||
// struct Ctx<T: Clone> {
|
||||
|
|
|
@ -14,7 +14,7 @@ enum AddMissingImplMembersMode {
|
|||
|
||||
// Assist: add_impl_missing_members
|
||||
//
|
||||
// Adds scaffold for required impl members
|
||||
// Adds scaffold for required impl members.
|
||||
//
|
||||
// ```
|
||||
// trait T {
|
||||
|
@ -50,7 +50,9 @@ pub(crate) fn add_missing_impl_members(ctx: AssistCtx<impl HirDatabase>) -> Opti
|
|||
}
|
||||
|
||||
// Assist: add_impl_default_members
|
||||
// Adds scaffold for overriding default impl members
|
||||
//
|
||||
// Adds scaffold for overriding default impl members.
|
||||
//
|
||||
// ```
|
||||
// trait T {
|
||||
// Type X;
|
||||
|
|
|
@ -14,7 +14,9 @@ use crate::{
|
|||
};
|
||||
|
||||
// Assist: convert_to_guarded_return
|
||||
//
|
||||
// Replace a large conditional with a guarded return.
|
||||
//
|
||||
// ```
|
||||
// fn main() {
|
||||
// <|>if cond {
|
||||
|
|
|
@ -1,11 +1,23 @@
|
|||
//! FIXME: write short doc here
|
||||
|
||||
use hir::db::HirDatabase;
|
||||
use ra_syntax::ast::{AstNode, BinExpr, BinOp};
|
||||
|
||||
use crate::{Assist, AssistCtx, AssistId};
|
||||
|
||||
/// Flip binary expression assist.
|
||||
// Assist: flip_binexpr
|
||||
//
|
||||
// Flips operands of a binary expression.
|
||||
//
|
||||
// ```
|
||||
// fn main() {
|
||||
// let _ = 90 +<|> 2;
|
||||
// }
|
||||
// ```
|
||||
// ->
|
||||
// ```
|
||||
// fn main() {
|
||||
// let _ = 2 + 90;
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn flip_binexpr(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let expr = ctx.node_at_offset::<BinExpr>()?;
|
||||
let lhs = expr.lhs()?.syntax().clone();
|
||||
|
|
|
@ -1,10 +1,23 @@
|
|||
//! FIXME: write short doc here
|
||||
|
||||
use hir::db::HirDatabase;
|
||||
use ra_syntax::{algo::non_trivia_sibling, Direction, T};
|
||||
|
||||
use crate::{Assist, AssistCtx, AssistId};
|
||||
|
||||
// Assist: flip_comma
|
||||
//
|
||||
// Flips two comma-separated items.
|
||||
//
|
||||
// ```
|
||||
// fn main() {
|
||||
// ((1, 2),<|> (3, 4));
|
||||
// }
|
||||
// ```
|
||||
// ->
|
||||
// ```
|
||||
// fn main() {
|
||||
// ((3, 4), (1, 2));
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let comma = ctx.token_at_offset().find(|leaf| leaf.kind() == T![,])?;
|
||||
let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?;
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
//! FIXME: write short doc here
|
||||
|
||||
use hir::db::HirDatabase;
|
||||
use ra_syntax::{
|
||||
ast::{self, AstNode, AstToken},
|
||||
|
@ -9,6 +7,22 @@ use ra_syntax::{
|
|||
use crate::assist_ctx::AssistBuilder;
|
||||
use crate::{Assist, AssistCtx, AssistId};
|
||||
|
||||
// Assist: inline_local_variable
|
||||
//
|
||||
// Inlines local variable.
|
||||
//
|
||||
// ```
|
||||
// fn main() {
|
||||
// let x<|> = 1 + 2;
|
||||
// x * 4;
|
||||
// }
|
||||
// ```
|
||||
// ->
|
||||
// ```
|
||||
// fn main() {
|
||||
// (1 + 2) * 4;
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let let_stmt = ctx.node_at_offset::<ast::LetStmt>()?;
|
||||
let bind_pat = match let_stmt.pat()? {
|
||||
|
|
|
@ -203,3 +203,55 @@ fn handle(action: Action) {
|
|||
"#####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_flip_binexpr() {
|
||||
check(
|
||||
"flip_binexpr",
|
||||
r#####"
|
||||
fn main() {
|
||||
let _ = 90 +<|> 2;
|
||||
}
|
||||
"#####,
|
||||
r#####"
|
||||
fn main() {
|
||||
let _ = 2 + 90;
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_flip_comma() {
|
||||
check(
|
||||
"flip_comma",
|
||||
r#####"
|
||||
fn main() {
|
||||
((1, 2),<|> (3, 4));
|
||||
}
|
||||
"#####,
|
||||
r#####"
|
||||
fn main() {
|
||||
((3, 4), (1, 2));
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_inline_local_variable() {
|
||||
check(
|
||||
"inline_local_variable",
|
||||
r#####"
|
||||
fn main() {
|
||||
let x<|> = 1 + 2;
|
||||
x * 4;
|
||||
}
|
||||
"#####,
|
||||
r#####"
|
||||
fn main() {
|
||||
(1 + 2) * 4;
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ struct Point {
|
|||
|
||||
## `add_explicit_type`
|
||||
|
||||
Specify type for a let binding
|
||||
Specify type for a let binding.
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
|
@ -37,7 +37,7 @@ fn main() {
|
|||
|
||||
## `add_impl`
|
||||
|
||||
Adds a new inherent impl for a type
|
||||
Adds a new inherent impl for a type.
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
|
@ -57,7 +57,7 @@ impl<T: Clone> Ctx<T> {
|
|||
|
||||
## `add_impl_default_members`
|
||||
|
||||
Adds scaffold for overriding default impl members
|
||||
Adds scaffold for overriding default impl members.
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
|
@ -90,7 +90,7 @@ impl T for () {
|
|||
|
||||
## `add_impl_missing_members`
|
||||
|
||||
Adds scaffold for required impl members
|
||||
Adds scaffold for required impl members.
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
|
@ -196,3 +196,52 @@ fn handle(action: Action) {
|
|||
}
|
||||
}
|
||||
```
|
||||
|
||||
## `flip_binexpr`
|
||||
|
||||
Flips operands of a binary expression.
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
fn main() {
|
||||
let _ = 90 +<|> 2;
|
||||
}
|
||||
|
||||
// AFTER
|
||||
fn main() {
|
||||
let _ = 2 + 90;
|
||||
}
|
||||
```
|
||||
|
||||
## `flip_comma`
|
||||
|
||||
Flips two comma-separated items.
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
fn main() {
|
||||
((1, 2),<|> (3, 4));
|
||||
}
|
||||
|
||||
// AFTER
|
||||
fn main() {
|
||||
((3, 4), (1, 2));
|
||||
}
|
||||
```
|
||||
|
||||
## `inline_local_variable`
|
||||
|
||||
Inlines local variable.
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
fn main() {
|
||||
let x<|> = 1 + 2;
|
||||
x * 4;
|
||||
}
|
||||
|
||||
// AFTER
|
||||
fn main() {
|
||||
(1 + 2) * 4;
|
||||
}
|
||||
```
|
||||
|
|
|
@ -52,6 +52,12 @@ fn collect_assists() -> Result<Vec<Assist>> {
|
|||
);
|
||||
|
||||
let doc = take_until(lines.by_ref(), "```").trim().to_string();
|
||||
assert!(
|
||||
doc.chars().next().unwrap().is_ascii_uppercase() && doc.ends_with("."),
|
||||
"\n\n{}: assist docs should be proper sentences, with capitalization and a full stop at the end.\n\n{}\n\n",
|
||||
id, doc,
|
||||
);
|
||||
|
||||
let before = take_until(lines.by_ref(), "```");
|
||||
|
||||
assert_eq!(lines.next().unwrap().as_str(), "->");
|
||||
|
|
Loading…
Reference in a new issue