From da5528824a836a4f36f44f90adc9fadcc98ca75b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 27 Oct 2019 12:22:53 +0300 Subject: document almost all assists --- crates/ra_assists/src/assists/raw_string.rs | 62 +++++++- crates/ra_assists/src/assists/remove_dbg.rs | 20 ++- .../src/assists/replace_if_let_with_match.rs | 28 +++- crates/ra_assists/src/assists/split_import.rs | 13 +- crates/ra_assists/src/doc_tests/generated.rs | 126 +++++++++++++++ docs/user/assists.md | 119 ++++++++++++++ docs/user/features.md | 174 --------------------- 7 files changed, 359 insertions(+), 183 deletions(-) diff --git a/crates/ra_assists/src/assists/raw_string.rs b/crates/ra_assists/src/assists/raw_string.rs index ea756d1ca..2df48a838 100644 --- a/crates/ra_assists/src/assists/raw_string.rs +++ b/crates/ra_assists/src/assists/raw_string.rs @@ -1,5 +1,3 @@ -//! FIXME: write short doc here - use hir::db::HirDatabase; use ra_syntax::{ SyntaxKind::{RAW_STRING, STRING}, @@ -9,6 +7,21 @@ use rustc_lexer; use crate::{Assist, AssistCtx, AssistId}; +// Assist: make_raw_string +// +// Adds `r#` to a plain string literal. +// +// ``` +// fn main() { +// "Hello,<|> World!"; +// } +// ``` +// -> +// ``` +// fn main() { +// r#"Hello, World!"#; +// } +// ``` pub(crate) fn make_raw_string(mut ctx: AssistCtx) -> Option { let token = ctx.find_token_at_offset(STRING)?; let text = token.text().as_str(); @@ -40,6 +53,21 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx) -> Option "World!""#; +// } +// ``` +// -> +// ``` +// fn main() { +// "Hello, \"World!\""; +// } +// ``` pub(crate) fn make_usual_string(mut ctx: AssistCtx) -> Option { let token = ctx.find_token_at_offset(RAW_STRING)?; let text = token.text().as_str(); @@ -56,6 +84,21 @@ pub(crate) fn make_usual_string(mut ctx: AssistCtx) -> Option< ctx.build() } +// Assist: add_hash +// +// Adds a hash to a raw string literal. +// +// ``` +// fn main() { +// r#"Hello,<|> World!"#; +// } +// ``` +// -> +// ``` +// fn main() { +// r##"Hello, World!"##; +// } +// ``` pub(crate) fn add_hash(mut ctx: AssistCtx) -> Option { let token = ctx.find_token_at_offset(RAW_STRING)?; ctx.add_action(AssistId("add_hash"), "add hash to raw string", |edit| { @@ -66,6 +109,21 @@ pub(crate) fn add_hash(mut ctx: AssistCtx) -> Option { ctx.build() } +// Assist: remove_hash +// +// Removes a hash from a raw string literal. +// +// ``` +// fn main() { +// r#"Hello,<|> World!"#; +// } +// ``` +// -> +// ``` +// fn main() { +// r"Hello, World!"; +// } +// ``` pub(crate) fn remove_hash(mut ctx: AssistCtx) -> Option { let token = ctx.find_token_at_offset(RAW_STRING)?; let text = token.text().as_str(); diff --git a/crates/ra_assists/src/assists/remove_dbg.rs b/crates/ra_assists/src/assists/remove_dbg.rs index ac2c43e1a..44b8de814 100644 --- a/crates/ra_assists/src/assists/remove_dbg.rs +++ b/crates/ra_assists/src/assists/remove_dbg.rs @@ -1,12 +1,26 @@ -//! FIXME: write short doc here - -use crate::{Assist, AssistCtx, AssistId}; use hir::db::HirDatabase; use ra_syntax::{ ast::{self, AstNode}, TextUnit, T, }; +use crate::{Assist, AssistCtx, AssistId}; + +// Assist: remove_dbg +// +// Removes `dbg!()` macro call. +// +// ``` +// fn main() { +// <|>dbg!(92); +// } +// ``` +// -> +// ``` +// fn main() { +// 92; +// } +// ``` pub(crate) fn remove_dbg(mut ctx: AssistCtx) -> Option { let macro_call = ctx.find_node_at_offset::()?; diff --git a/crates/ra_assists/src/assists/replace_if_let_with_match.rs b/crates/ra_assists/src/assists/replace_if_let_with_match.rs index da276e47b..58ef2ff20 100644 --- a/crates/ra_assists/src/assists/replace_if_let_with_match.rs +++ b/crates/ra_assists/src/assists/replace_if_let_with_match.rs @@ -1,5 +1,3 @@ -//! FIXME: write short doc here - use format_buf::format; use hir::db::HirDatabase; use ra_fmt::extract_trivial_expression; @@ -7,6 +5,32 @@ use ra_syntax::{ast, AstNode}; use crate::{Assist, AssistCtx, AssistId}; +// Assist: replace_if_let_with_match +// +// Replaces `if let` with an else branch with a `match` expression. +// +// ``` +// enum Action { Move { distance: u32 }, Stop } +// +// fn handle(action: Action) { +// <|>if let Action::Move { distance } = action { +// foo(distance) +// } else { +// bar() +// } +// } +// ``` +// -> +// ``` +// enum Action { Move { distance: u32 }, Stop } +// +// fn handle(action: Action) { +// match action { +// Action::Move { distance } => foo(distance), +// _ => bar(), +// } +// } +// ``` pub(crate) fn replace_if_let_with_match(mut ctx: AssistCtx) -> Option { let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; let cond = if_expr.condition()?; diff --git a/crates/ra_assists/src/assists/split_import.rs b/crates/ra_assists/src/assists/split_import.rs index 09bde1b72..8d8a28987 100644 --- a/crates/ra_assists/src/assists/split_import.rs +++ b/crates/ra_assists/src/assists/split_import.rs @@ -1,5 +1,3 @@ -//! FIXME: write short doc here - use std::iter::successors; use hir::db::HirDatabase; @@ -7,6 +5,17 @@ use ra_syntax::{ast, AstNode, TextUnit, T}; use crate::{Assist, AssistCtx, AssistId}; +// Assist: split_import +// +// Wraps the tail of import into braces. +// +// ``` +// use std::<|>collections::HashMap; +// ``` +// -> +// ``` +// use std::{collections::HashMap}; +// ``` pub(crate) fn split_import(mut ctx: AssistCtx) -> Option { let colon_colon = ctx.find_token_at_offset(T![::])?; let path = ast::Path::cast(colon_colon.parent())?; diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs index 09677af68..b8d335911 100644 --- a/crates/ra_assists/src/doc_tests/generated.rs +++ b/crates/ra_assists/src/doc_tests/generated.rs @@ -39,6 +39,23 @@ fn main() { ) } +#[test] +fn doctest_add_hash() { + check( + "add_hash", + r#####" +fn main() { + r#"Hello,<|> World!"#; +} +"#####, + r#####" +fn main() { + r##"Hello, World!"##; +} +"#####, + ) +} + #[test] fn doctest_add_impl() { check( @@ -274,6 +291,40 @@ fn main() { ) } +#[test] +fn doctest_make_raw_string() { + check( + "make_raw_string", + r#####" +fn main() { + "Hello,<|> World!"; +} +"#####, + r#####" +fn main() { + r#"Hello, World!"#; +} +"#####, + ) +} + +#[test] +fn doctest_make_usual_string() { + check( + "make_usual_string", + r#####" +fn main() { + r#"Hello,<|> "World!""#; +} +"#####, + r#####" +fn main() { + "Hello, \"World!\""; +} +"#####, + ) +} + #[test] fn doctest_merge_match_arms() { check( @@ -370,3 +421,78 @@ fn handle(action: Action) { "#####, ) } + +#[test] +fn doctest_remove_dbg() { + check( + "remove_dbg", + r#####" +fn main() { + <|>dbg!(92); +} +"#####, + r#####" +fn main() { + 92; +} +"#####, + ) +} + +#[test] +fn doctest_remove_hash() { + check( + "remove_hash", + r#####" +fn main() { + r#"Hello,<|> World!"#; +} +"#####, + r#####" +fn main() { + r"Hello, World!"; +} +"#####, + ) +} + +#[test] +fn doctest_replace_if_let_with_match() { + check( + "replace_if_let_with_match", + r#####" +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + <|>if let Action::Move { distance } = action { + foo(distance) + } else { + bar() + } +} +"#####, + r#####" +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + Action::Move { distance } => foo(distance), + _ => bar(), + } +} +"#####, + ) +} + +#[test] +fn doctest_split_import() { + check( + "split_import", + r#####" +use std::<|>collections::HashMap; +"#####, + r#####" +use std::{collections::HashMap}; +"#####, + ) +} diff --git a/docs/user/assists.md b/docs/user/assists.md index 34a95696c..e4d08a7dc 100644 --- a/docs/user/assists.md +++ b/docs/user/assists.md @@ -38,6 +38,22 @@ fn main() { } ``` +## `add_hash` + +Adds a hash to a raw string literal. + +```rust +// BEFORE +fn main() { + r#"Hello,┃ World!"#; +} + +// AFTER +fn main() { + r##"Hello, World!"##; +} +``` + ## `add_impl` Adds a new inherent impl for a type. @@ -266,6 +282,38 @@ fn main() { } ``` +## `make_raw_string` + +Adds `r#` to a plain string literal. + +```rust +// BEFORE +fn main() { + "Hello,┃ World!"; +} + +// AFTER +fn main() { + r#"Hello, World!"#; +} +``` + +## `make_usual_string` + +Turns a raw string into a plain string. + +```rust +// BEFORE +fn main() { + r#"Hello,┃ "World!""#; +} + +// AFTER +fn main() { + "Hello, \"World!\""; +} +``` + ## `merge_match_arms` Merges identical match arms. @@ -358,3 +406,74 @@ fn handle(action: Action) { } } ``` + +## `remove_dbg` + +Removes `dbg!()` macro call. + +```rust +// BEFORE +fn main() { + ┃dbg!(92); +} + +// AFTER +fn main() { + 92; +} +``` + +## `remove_hash` + +Removes a hash from a raw string literal. + +```rust +// BEFORE +fn main() { + r#"Hello,┃ World!"#; +} + +// AFTER +fn main() { + r"Hello, World!"; +} +``` + +## `replace_if_let_with_match` + +Replaces `if let` with an else branch with a `match` expression. + +```rust +// BEFORE +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + ┃if let Action::Move { distance } = action { + foo(distance) + } else { + bar() + } +} + +// AFTER +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + Action::Move { distance } => foo(distance), + _ => bar(), + } +} +``` + +## `split_import` + +Wraps the tail of import into braces. + +```rust +// BEFORE +use std::┃collections::HashMap; + +// AFTER +use std::{collections::HashMap}; +``` diff --git a/docs/user/features.md b/docs/user/features.md index 2e213e34c..7ae2ca7b6 100644 --- a/docs/user/features.md +++ b/docs/user/features.md @@ -118,180 +118,6 @@ impl Debug<|> for Foo { } ``` -- Fill struct fields - -```rust -// before: -struct S<'a, D> { - a: u32, - b: String, - c: (i32, i32), - d: D, - r: &'a str, -} - -fn main() { - let s = S<|> {} -} - -// after: -struct S<'a, D> { - a: u32, - b: String, - c: (i32, i32), - d: D, - r: &'a str, -} - -fn main() { - let s = <|>S { - a: (), - b: (), - c: (), - d: (), - r: (), - } -} -``` - -- Remove `dbg!` - -```rust -// before: -fn foo(n: usize) { - if let Some(_) = dbg!(n.<|>checked_sub(4)) { - // ... - } -} - -// after: -fn foo(n: usize) { - if let Some(_) = n.<|>checked_sub(4) { - // ... - } -} -``` - -- Replace if-let with match: - -```rust -// before: -impl VariantData { - pub fn is_struct(&self) -> bool { - if <|>let VariantData::Struct(..) = *self { - true - } else { - false - } - } -} - -// after: -impl VariantData { - pub fn is_struct(&self) -> bool { - <|>match *self { - VariantData::Struct(..) => true, - _ => false, - } - } -} -``` - -- Split import - -```rust -// before: -use crate:<|>:db::{RootDatabase, FileSymbol}; -// after: -use crate::{<|>db::{RootDatabase, FileSymbol}}; -``` - -- Move if condition to match arm guard -```rust -// before: -fn f() { - let mut t = 'a'; - let chars = "abcd"; - match t { - '\r' => if chars.clone().next().is_some() { - t = 'e';<|> - false - }, - _ => true - } -} - -// after: -fn f() { - let mut t = 'a'; - let chars = "abcd"; - match t { - '\r' <|>if chars.clone().next().is_some() => { - t = 'e'; - false - }, - _ => true - } -} -``` - -- Make raw string unescaped - -```rust -// before: -fn f() { - let s = <|>"ab\ncd"; -} - -// after: -fn f() { - let s = <|>r#"ab -cd"#; -} -``` - -- Make usual string - -```rust -// before: -fn f() { - let s = <|>r#"abcd"#; -} - -// after: -fn f() { - let s = <|>"abcd"; -} -``` - -- Add hash - -```rust -// before: -fn f() { - let s = <|>r"abcd"; -} - -// after: -fn f() { - let s = <|>r#"abcd"#; -} -``` - -- Remove hash - -```rust -// before: -fn f() { - let s = <|>r#"abcd"#; -} - -// after: -fn f() { - let s = <|>r"abcd"; -} -``` - ### Magic Completions In addition to usual reference completion, rust-analyzer provides some ✨magic✨ -- cgit v1.2.3