From d385438bcc8d302fbcb91114e19ac0cc30528822 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 25 Oct 2019 23:38:15 +0300 Subject: generate more assists docs --- crates/ra_assists/src/assists/add_derive.rs | 18 ++- crates/ra_assists/src/assists/add_explicit_type.rs | 16 ++- crates/ra_assists/src/assists/add_impl.rs | 19 ++- .../src/assists/add_missing_impl_members.rs | 58 ++++++++- crates/ra_assists/src/assists/apply_demorgan.rs | 26 ++-- crates/ra_assists/src/doc_tests.rs | 6 +- crates/ra_assists/src/doc_tests/generated.rs | 139 +++++++++++++++++++++ 7 files changed, 262 insertions(+), 20 deletions(-) (limited to 'crates') diff --git a/crates/ra_assists/src/assists/add_derive.rs b/crates/ra_assists/src/assists/add_derive.rs index 77ecc33c9..d3ba634c4 100644 --- a/crates/ra_assists/src/assists/add_derive.rs +++ b/crates/ra_assists/src/assists/add_derive.rs @@ -1,5 +1,3 @@ -//! FIXME: write short doc here - use hir::db::HirDatabase; use ra_syntax::{ ast::{self, AstNode, AttrsOwner}, @@ -9,6 +7,22 @@ use ra_syntax::{ use crate::{Assist, AssistCtx, AssistId}; +// Assist: add_derive +// Adds a new `#[derive()]` clause to a struct or enum. +// ``` +// struct Point { +// x: u32, +// y: u32,<|> +// } +// ``` +// -> +// ``` +// #[derive()] +// struct Point { +// x: u32, +// y: u32, +// } +// ``` pub(crate) fn add_derive(mut ctx: AssistCtx) -> Option { let nominal = ctx.node_at_offset::()?; let node_start = derive_insertion_offset(&nominal)?; diff --git a/crates/ra_assists/src/assists/add_explicit_type.rs b/crates/ra_assists/src/assists/add_explicit_type.rs index 8c83dc987..33b7bea7f 100644 --- a/crates/ra_assists/src/assists/add_explicit_type.rs +++ b/crates/ra_assists/src/assists/add_explicit_type.rs @@ -1,5 +1,3 @@ -//! FIXME: write short doc here - use hir::{db::HirDatabase, HirDisplay, Ty}; use ra_syntax::{ ast::{self, AstNode, LetStmt, NameOwner}, @@ -8,7 +6,19 @@ use ra_syntax::{ use crate::{Assist, AssistCtx, AssistId}; -/// Add explicit type assist. +// Assist: add_explicit_type +// Specify type for a let binding +// ``` +// fn main() { +// let x<|> = 92; +// } +// ``` +// -> +// ``` +// fn main() { +// let x: i32 = 92; +// } +// ``` pub(crate) fn add_explicit_type(mut ctx: AssistCtx) -> Option { let stmt = ctx.node_at_offset::()?; let expr = stmt.initializer()?; diff --git a/crates/ra_assists/src/assists/add_impl.rs b/crates/ra_assists/src/assists/add_impl.rs index 94801fbc9..40bc5c464 100644 --- a/crates/ra_assists/src/assists/add_impl.rs +++ b/crates/ra_assists/src/assists/add_impl.rs @@ -1,5 +1,3 @@ -//! FIXME: write short doc here - use format_buf::format; use hir::db::HirDatabase; use join_to_string::join; @@ -10,6 +8,23 @@ use ra_syntax::{ use crate::{Assist, AssistCtx, AssistId}; +// Assist: add_impl +// Adds a new inherent impl for a type +// ``` +// struct Ctx { +// data: T,<|> +// } +// ``` +// -> +// ``` +// struct Ctx { +// data: T, +// } +// +// impl Ctx { +// +// } +// ``` pub(crate) fn add_impl(mut ctx: AssistCtx) -> Option { let nominal = ctx.node_at_offset::()?; let name = nominal.name()?; diff --git a/crates/ra_assists/src/assists/add_missing_impl_members.rs b/crates/ra_assists/src/assists/add_missing_impl_members.rs index 565b96fb5..36fa6f9ea 100644 --- a/crates/ra_assists/src/assists/add_missing_impl_members.rs +++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs @@ -1,5 +1,3 @@ -//! FIXME: write short doc here - use hir::{db::HirDatabase, HasSource}; use ra_syntax::{ ast::{self, edit, make, AstNode, NameOwner}, @@ -14,6 +12,32 @@ enum AddMissingImplMembersMode { NoDefaultMethods, } +// Assist: add_impl_missing_members +// Adds scaffold for required impl members +// ``` +// trait T { +// Type X; +// fn foo(&self); +// fn bar(&self) {} +// } +// +// impl T for () {<|> +// +// } +// ``` +// -> +// ``` +// trait T { +// Type X; +// fn foo(&self); +// fn bar(&self) {} +// } +// +// impl T for () { +// fn foo(&self) { unimplemented!() } +// +// } +// ``` pub(crate) fn add_missing_impl_members(ctx: AssistCtx) -> Option { add_missing_impl_members_inner( ctx, @@ -23,6 +47,36 @@ pub(crate) fn add_missing_impl_members(ctx: AssistCtx) -> Opti ) } +// Assist: add_impl_default_members +// Adds scaffold for overriding default impl members +// ``` +// trait T { +// Type X; +// fn foo(&self); +// fn bar(&self) {} +// } +// +// impl T for () { +// Type X = (); +// fn foo(&self) {}<|> +// +// } +// ``` +// -> +// ``` +// trait T { +// Type X; +// fn foo(&self); +// fn bar(&self) {} +// } +// +// impl T for () { +// Type X = (); +// fn foo(&self) {} +// fn bar(&self) {} +// +// } +// ``` pub(crate) fn add_missing_default_members(ctx: AssistCtx) -> Option { add_missing_impl_members_inner( ctx, diff --git a/crates/ra_assists/src/assists/apply_demorgan.rs b/crates/ra_assists/src/assists/apply_demorgan.rs index 5f2b0dd18..a072f63e7 100644 --- a/crates/ra_assists/src/assists/apply_demorgan.rs +++ b/crates/ra_assists/src/assists/apply_demorgan.rs @@ -1,18 +1,26 @@ -//! This contains the functions associated with the demorgan assist. -//! This assist transforms boolean expressions of the form `!a || !b` into -//! `!(a && b)`. use hir::db::HirDatabase; use ra_syntax::ast::{self, AstNode}; use ra_syntax::SyntaxNode; use crate::{Assist, AssistCtx, AssistId}; -/// Assist for applying demorgan's law -/// -/// This transforms expressions of the form `!l || !r` into `!(l && r)`. -/// 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. -/// This means something of the form `!x` or `x != y`. +// Assist: apply_demorgan +// 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 also works with `&&`. This assist can only be applied with the cursor +// on either `||` or `&&`, with both operands being a negation of some kind. +// This means something of the form `!x` or `x != y`. +// ``` +// fn main() { +// if x != 4 ||<|> !y {} +// } +// ``` +// -> +// ``` +// fn main() { +// if !(x == 4 && y) {} +// } +// ``` pub(crate) fn apply_demorgan(mut ctx: AssistCtx) -> Option { let expr = ctx.node_at_offset::()?; let op = expr.op_kind()?; diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs index 88e901517..872bbdf17 100644 --- a/crates/ra_assists/src/doc_tests.rs +++ b/crates/ra_assists/src/doc_tests.rs @@ -15,8 +15,10 @@ fn check(assist_id: &str, before: &str, after: &str) { let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; - let (_assist_id, action) = - crate::assists(&db, frange).into_iter().find(|(id, _)| id.id.0 == assist_id).unwrap(); + let (_assist_id, action) = crate::assists(&db, frange) + .into_iter() + .find(|(id, _)| id.id.0 == assist_id) + .unwrap_or_else(|| panic!("Assist {:?} is not applicable", assist_id)); let actual = action.edit.apply(&before); assert_eq_text!(after, &actual); diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs index e5f6910f1..76d86b93d 100644 --- a/crates/ra_assists/src/doc_tests/generated.rs +++ b/crates/ra_assists/src/doc_tests/generated.rs @@ -2,6 +2,145 @@ use super::check; +#[test] +fn doctest_add_derive() { + check( + "add_derive", + r#####" +struct Point { + x: u32, + y: u32,<|> +} +"#####, + r#####" +#[derive()] +struct Point { + x: u32, + y: u32, +} +"#####, + ) +} + +#[test] +fn doctest_add_explicit_type() { + check( + "add_explicit_type", + r#####" +fn main() { + let x<|> = 92; +} +"#####, + r#####" +fn main() { + let x: i32 = 92; +} +"#####, + ) +} + +#[test] +fn doctest_add_impl() { + check( + "add_impl", + r#####" +struct Ctx { + data: T,<|> +} +"#####, + r#####" +struct Ctx { + data: T, +} + +impl Ctx { + +} +"#####, + ) +} + +#[test] +fn doctest_add_impl_default_members() { + check( + "add_impl_default_members", + r#####" +trait T { + Type X; + fn foo(&self); + fn bar(&self) {} +} + +impl T for () { + Type X = (); + fn foo(&self) {}<|> + +} +"#####, + r#####" +trait T { + Type X; + fn foo(&self); + fn bar(&self) {} +} + +impl T for () { + Type X = (); + fn foo(&self) {} + fn bar(&self) {} + +} +"#####, + ) +} + +#[test] +fn doctest_add_impl_missing_members() { + check( + "add_impl_missing_members", + r#####" +trait T { + Type X; + fn foo(&self); + fn bar(&self) {} +} + +impl T for () {<|> + +} +"#####, + r#####" +trait T { + Type X; + fn foo(&self); + fn bar(&self) {} +} + +impl T for () { + fn foo(&self) { unimplemented!() } + +} +"#####, + ) +} + +#[test] +fn doctest_apply_demorgan() { + check( + "apply_demorgan", + r#####" +fn main() { + if x != 4 ||<|> !y {} +} +"#####, + r#####" +fn main() { + if !(x == 4 && y) {} +} +"#####, + ) +} + #[test] fn doctest_convert_to_guarded_return() { check( -- cgit v1.2.3