check style for assist docs

This commit is contained in:
Aleksey Kladov 2019-10-26 19:08:13 +03:00
parent 2f7d1f10c1
commit a5cbd8d5e8
10 changed files with 165 additions and 15 deletions

View file

@ -8,7 +8,7 @@ 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() {

View file

@ -10,7 +10,7 @@ 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> {

View file

@ -14,7 +14,7 @@ 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 {
@ -50,7 +50,9 @@ pub(crate) fn add_missing_impl_members(ctx: AssistCtx<impl HirDatabase>) -> Opti
} }
// Assist: add_impl_default_members // Assist: add_impl_default_members
// Adds scaffold for overriding default impl members //
// Adds scaffold for overriding default impl members.
//
// ``` // ```
// trait T { // trait T {
// Type X; // Type X;

View file

@ -14,7 +14,9 @@ use crate::{
}; };
// Assist: convert_to_guarded_return // Assist: convert_to_guarded_return
//
// Replace a large conditional with a guarded return. // Replace a large conditional with a guarded return.
//
// ``` // ```
// fn main() { // fn main() {
// <|>if cond { // <|>if cond {

View file

@ -1,11 +1,23 @@
//! FIXME: write short doc here
use hir::db::HirDatabase; use hir::db::HirDatabase;
use ra_syntax::ast::{AstNode, BinExpr, BinOp}; use ra_syntax::ast::{AstNode, BinExpr, BinOp};
use crate::{Assist, AssistCtx, AssistId}; 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> { pub(crate) fn flip_binexpr(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let expr = ctx.node_at_offset::<BinExpr>()?; let expr = ctx.node_at_offset::<BinExpr>()?;
let lhs = expr.lhs()?.syntax().clone(); let lhs = expr.lhs()?.syntax().clone();

View file

@ -1,10 +1,23 @@
//! FIXME: write short doc here
use hir::db::HirDatabase; use hir::db::HirDatabase;
use ra_syntax::{algo::non_trivia_sibling, Direction, T}; use ra_syntax::{algo::non_trivia_sibling, Direction, T};
use crate::{Assist, AssistCtx, AssistId}; 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> { pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let comma = ctx.token_at_offset().find(|leaf| leaf.kind() == T![,])?; let comma = ctx.token_at_offset().find(|leaf| leaf.kind() == T![,])?;
let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?; let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?;

View file

@ -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, AstNode, AstToken}, ast::{self, AstNode, AstToken},
@ -9,6 +7,22 @@ use ra_syntax::{
use crate::assist_ctx::AssistBuilder; use crate::assist_ctx::AssistBuilder;
use crate::{Assist, AssistCtx, AssistId}; 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> { pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let let_stmt = ctx.node_at_offset::<ast::LetStmt>()?; let let_stmt = ctx.node_at_offset::<ast::LetStmt>()?;
let bind_pat = match let_stmt.pat()? { let bind_pat = match let_stmt.pat()? {

View file

@ -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;
}
"#####,
)
}

View file

@ -21,7 +21,7 @@ struct Point {
## `add_explicit_type` ## `add_explicit_type`
Specify type for a let binding Specify type for a let binding.
```rust ```rust
// BEFORE // BEFORE
@ -37,7 +37,7 @@ fn main() {
## `add_impl` ## `add_impl`
Adds a new inherent impl for a type Adds a new inherent impl for a type.
```rust ```rust
// BEFORE // BEFORE
@ -57,7 +57,7 @@ impl<T: Clone> Ctx<T> {
## `add_impl_default_members` ## `add_impl_default_members`
Adds scaffold for overriding default impl members Adds scaffold for overriding default impl members.
```rust ```rust
// BEFORE // BEFORE
@ -90,7 +90,7 @@ impl T for () {
## `add_impl_missing_members` ## `add_impl_missing_members`
Adds scaffold for required impl members Adds scaffold for required impl members.
```rust ```rust
// BEFORE // 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;
}
```

View file

@ -52,6 +52,12 @@ fn collect_assists() -> Result<Vec<Assist>> {
); );
let doc = take_until(lines.by_ref(), "```").trim().to_string(); 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(), "```"); let before = take_until(lines.by_ref(), "```");
assert_eq!(lines.next().unwrap().as_str(), "->"); assert_eq!(lines.next().unwrap().as_str(), "->");