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 +++++++++++++++++++++ 5 files changed, 240 insertions(+), 9 deletions(-) (limited to 'crates/ra_assists/src') 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}; +"#####, + ) +} -- cgit v1.2.3