aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/assists/early_return.rs43
-rw-r--r--crates/ra_assists/src/doc_tests.rs23
-rw-r--r--crates/ra_assists/src/doc_tests/generated.rs27
-rw-r--r--crates/ra_assists/src/lib.rs4
-rw-r--r--docs/user/assists.md24
-rw-r--r--docs/user/features.md4
-rw-r--r--xtask/src/codegen.rs36
-rw-r--r--xtask/src/codegen/gen_assists_docs.rs123
-rw-r--r--xtask/src/codegen/gen_syntax.rs25
-rw-r--r--xtask/src/main.rs1
-rw-r--r--xtask/tests/tidy-tests/cli.rs7
-rw-r--r--xtask/tests/tidy-tests/docs.rs4
12 files changed, 269 insertions, 52 deletions
diff --git a/crates/ra_assists/src/assists/early_return.rs b/crates/ra_assists/src/assists/early_return.rs
index f7d7e12e7..b3d025340 100644
--- a/crates/ra_assists/src/assists/early_return.rs
+++ b/crates/ra_assists/src/assists/early_return.rs
@@ -1,26 +1,3 @@
1//! Assist: `convert_to_guarded_return`
2//!
3//! Replace a large conditional with a guarded return.
4//!
5//! ```text
6//! fn <|>main() {
7//! if cond {
8//! foo();
9//! bar();
10//! }
11//! }
12//! ```
13//! ->
14//! ```text
15//! fn main() {
16//! if !cond {
17//! return;
18//! }
19//! foo();
20//! bar();
21//! }
22//! ```
23
24use std::ops::RangeInclusive; 1use std::ops::RangeInclusive;
25 2
26use hir::db::HirDatabase; 3use hir::db::HirDatabase;
@@ -36,6 +13,26 @@ use crate::{
36 AssistId, 13 AssistId,
37}; 14};
38 15
16// Assist: convert_to_guarded_return
17// Replace a large conditional with a guarded return.
18// ```
19// fn main() {
20// <|>if cond {
21// foo();
22// bar();
23// }
24// }
25// ```
26// ->
27// ```
28// fn main() {
29// if !cond {
30// return;
31// }
32// foo();
33// bar();
34// }
35// ```
39pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 36pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
40 let if_expr: ast::IfExpr = ctx.node_at_offset()?; 37 let if_expr: ast::IfExpr = ctx.node_at_offset()?;
41 let expr = if_expr.condition()?.expr()?; 38 let expr = if_expr.condition()?.expr()?;
diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs
new file mode 100644
index 000000000..88e901517
--- /dev/null
+++ b/crates/ra_assists/src/doc_tests.rs
@@ -0,0 +1,23 @@
1//! Each assist definition has a special comment, which specifies docs and
2//! example.
3//!
4//! We collect all the example and write the as tests in this module.
5
6mod generated;
7
8use hir::mock::MockDatabase;
9use ra_db::FileRange;
10use ra_syntax::TextRange;
11use test_utils::{assert_eq_text, extract_offset};
12
13fn check(assist_id: &str, before: &str, after: &str) {
14 let (before_cursor_pos, before) = extract_offset(before);
15 let (db, _source_root, file_id) = MockDatabase::with_single_file(&before);
16 let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
17
18 let (_assist_id, action) =
19 crate::assists(&db, frange).into_iter().find(|(id, _)| id.id.0 == assist_id).unwrap();
20
21 let actual = action.edit.apply(&before);
22 assert_eq_text!(after, &actual);
23}
diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs
new file mode 100644
index 000000000..e5f6910f1
--- /dev/null
+++ b/crates/ra_assists/src/doc_tests/generated.rs
@@ -0,0 +1,27 @@
1//! Generated file, do not edit by hand, see `crate/ra_tools/src/codegen`
2
3use super::check;
4
5#[test]
6fn doctest_convert_to_guarded_return() {
7 check(
8 "convert_to_guarded_return",
9 r#####"
10fn main() {
11 <|>if cond {
12 foo();
13 bar();
14 }
15}
16"#####,
17 r#####"
18fn main() {
19 if !cond {
20 return;
21 }
22 foo();
23 bar();
24}
25"#####,
26 )
27}
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index ab77b46a9..de576324f 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -7,6 +7,8 @@
7 7
8mod assist_ctx; 8mod assist_ctx;
9mod marks; 9mod marks;
10#[cfg(test)]
11mod doc_tests;
10 12
11use hir::db::HirDatabase; 13use hir::db::HirDatabase;
12use itertools::Itertools; 14use itertools::Itertools;
@@ -36,7 +38,7 @@ pub struct AssistAction {
36 pub target: Option<TextRange>, 38 pub target: Option<TextRange>,
37} 39}
38 40
39/// Return all the assists eapplicable at the given position. 41/// Return all the assists applicable at the given position.
40/// 42///
41/// Assists are returned in the "unresolved" state, that is only labels are 43/// Assists are returned in the "unresolved" state, that is only labels are
42/// returned, without actual edits. 44/// returned, without actual edits.
diff --git a/docs/user/assists.md b/docs/user/assists.md
new file mode 100644
index 000000000..cb4b0b9fb
--- /dev/null
+++ b/docs/user/assists.md
@@ -0,0 +1,24 @@
1# Assists
2
3## `convert_to_guarded_return`
4
5Replace a large conditional with a guarded return.
6
7```rust
8// BEFORE
9fn main() {
10 <|>if cond {
11 foo();
12 bar();
13 }
14}
15
16// AFTER
17fn main() {
18 if !cond {
19 return;
20 }
21 foo();
22 bar();
23}
24```
diff --git a/docs/user/features.md b/docs/user/features.md
index 8b7a8d7fc..a94b65ad4 100644
--- a/docs/user/features.md
+++ b/docs/user/features.md
@@ -97,11 +97,13 @@ Start `cargo watch` for live error highlighting. Will prompt to install if it's
97 97
98Stop `cargo watch` 98Stop `cargo watch`
99 99
100### Code Actions (Assists) 100### Assists (Code Actions)
101 101
102These are triggered in a particular context via light bulb. We use custom code on 102These are triggered in a particular context via light bulb. We use custom code on
103the VS Code side to be able to position cursor. `<|>` signifies cursor 103the VS Code side to be able to position cursor. `<|>` signifies cursor
104 104
105See [assists.md](./assists.md)
106
105- Add `#[derive]` 107- Add `#[derive]`
106 108
107```rust 109```rust
diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs
index bf3a90119..44729cd57 100644
--- a/xtask/src/codegen.rs
+++ b/xtask/src/codegen.rs
@@ -7,12 +7,22 @@
7 7
8mod gen_syntax; 8mod gen_syntax;
9mod gen_parser_tests; 9mod gen_parser_tests;
10mod gen_assists_docs;
10 11
11use std::{fs, mem, path::Path}; 12use std::{
13 fs,
14 io::Write,
15 mem,
16 path::Path,
17 process::{Command, Stdio},
18};
12 19
13use crate::Result; 20use crate::{project_root, Result};
14 21
15pub use self::{gen_parser_tests::generate_parser_tests, gen_syntax::generate_syntax}; 22pub use self::{
23 gen_assists_docs::generate_assists_docs, gen_parser_tests::generate_parser_tests,
24 gen_syntax::generate_syntax,
25};
16 26
17pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron"; 27pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron";
18const GRAMMAR_DIR: &str = "crates/ra_parser/src/grammar"; 28const GRAMMAR_DIR: &str = "crates/ra_parser/src/grammar";
@@ -22,6 +32,10 @@ const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/err
22pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs"; 32pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs";
23pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs"; 33pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs";
24 34
35const ASSISTS_DIR: &str = "crates/ra_assists/src/assists";
36const ASSISTS_TESTS: &str = "crates/ra_assists/src/doc_tests/generated.rs";
37const ASSISTS_DOCS: &str = "docs/user/assists.md";
38
25#[derive(Debug, PartialEq, Eq, Clone, Copy)] 39#[derive(Debug, PartialEq, Eq, Clone, Copy)]
26pub enum Mode { 40pub enum Mode {
27 Overwrite, 41 Overwrite,
@@ -30,7 +44,7 @@ pub enum Mode {
30 44
31/// A helper to update file on disk if it has changed. 45/// A helper to update file on disk if it has changed.
32/// With verify = false, 46/// With verify = false,
33pub fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> { 47fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> {
34 match fs::read_to_string(path) { 48 match fs::read_to_string(path) {
35 Ok(ref old_contents) if old_contents == contents => { 49 Ok(ref old_contents) if old_contents == contents => {
36 return Ok(()); 50 return Ok(());
@@ -45,6 +59,20 @@ pub fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> {
45 Ok(()) 59 Ok(())
46} 60}
47 61
62fn reformat(text: impl std::fmt::Display) -> Result<String> {
63 let mut rustfmt = Command::new("rustfmt")
64 .arg("--config-path")
65 .arg(project_root().join("rustfmt.toml"))
66 .stdin(Stdio::piped())
67 .stdout(Stdio::piped())
68 .spawn()?;
69 write!(rustfmt.stdin.take().unwrap(), "{}", text)?;
70 let output = rustfmt.wait_with_output()?;
71 let stdout = String::from_utf8(output.stdout)?;
72 let preamble = "Generated file, do not edit by hand, see `crate/ra_tools/src/codegen`";
73 Ok(format!("//! {}\n\n{}", preamble, stdout))
74}
75
48fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> { 76fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> {
49 let mut res = Vec::new(); 77 let mut res = Vec::new();
50 78
diff --git a/xtask/src/codegen/gen_assists_docs.rs b/xtask/src/codegen/gen_assists_docs.rs
new file mode 100644
index 000000000..654ae09d6
--- /dev/null
+++ b/xtask/src/codegen/gen_assists_docs.rs
@@ -0,0 +1,123 @@
1use std::{fs, path::Path};
2
3use crate::{
4 codegen::{self, extract_comment_blocks, Mode},
5 project_root, Result,
6};
7
8pub fn generate_assists_docs(mode: Mode) -> Result<()> {
9 let assists = collect_assists()?;
10 generate_tests(&assists, mode)?;
11 generate_docs(&assists, mode)?;
12 Ok(())
13}
14
15#[derive(Debug)]
16struct Assist {
17 id: String,
18 doc: String,
19 before: String,
20 after: String,
21}
22
23fn collect_assists() -> Result<Vec<Assist>> {
24 let mut res = Vec::new();
25 for entry in fs::read_dir(project_root().join(codegen::ASSISTS_DIR))? {
26 let entry = entry?;
27 let path = entry.path();
28 if path.is_file() {
29 collect_file(&mut res, path.as_path())?;
30 }
31 }
32 res.sort_by(|lhs, rhs| lhs.id.cmp(&rhs.id));
33 return Ok(res);
34
35 fn collect_file(acc: &mut Vec<Assist>, path: &Path) -> Result<()> {
36 let text = fs::read_to_string(path)?;
37 let comment_blocks = extract_comment_blocks(&text);
38
39 for block in comment_blocks {
40 // FIXME: doesn't support blank lines yet, need to tweak
41 // `extract_comment_blocks` for that.
42 let mut lines = block.iter();
43 let first_line = lines.next().unwrap();
44 if !first_line.starts_with("Assist: ") {
45 continue;
46 }
47 let id = first_line["Assist: ".len()..].to_string();
48 assert!(id.chars().all(|it| it.is_ascii_lowercase() || it == '_'));
49
50 let doc = take_until(lines.by_ref(), "```");
51 let before = take_until(lines.by_ref(), "```");
52
53 assert_eq!(lines.next().unwrap().as_str(), "->");
54 assert_eq!(lines.next().unwrap().as_str(), "```");
55 let after = take_until(lines.by_ref(), "```");
56 acc.push(Assist { id, doc, before, after })
57 }
58
59 fn take_until<'a>(lines: impl Iterator<Item = &'a String>, marker: &str) -> String {
60 let mut buf = Vec::new();
61 for line in lines {
62 if line == marker {
63 break;
64 }
65 buf.push(line.clone());
66 }
67 buf.join("\n")
68 }
69 Ok(())
70 }
71}
72
73fn generate_tests(assists: &[Assist], mode: Mode) -> Result<()> {
74 let mut buf = String::from("use super::check;\n");
75
76 for assist in assists.iter() {
77 let test = format!(
78 r######"
79#[test]
80fn doctest_{}() {{
81 check(
82 "{}",
83r#####"
84{}
85"#####, r#####"
86{}
87"#####)
88}}
89"######,
90 assist.id, assist.id, assist.before, assist.after
91 );
92
93 buf.push_str(&test)
94 }
95 let buf = codegen::reformat(buf)?;
96 codegen::update(&project_root().join(codegen::ASSISTS_TESTS), &buf, mode)
97}
98
99fn generate_docs(assists: &[Assist], mode: Mode) -> Result<()> {
100 let mut buf = String::from("# Assists\n");
101
102 for assist in assists {
103 let docs = format!(
104 "
105## `{}`
106
107{}
108
109```rust
110// BEFORE
111{}
112
113// AFTER
114{}
115```
116",
117 assist.id, assist.doc, assist.before, assist.after
118 );
119 buf.push_str(&docs);
120 }
121
122 codegen::update(&project_root().join(codegen::ASSISTS_DOCS), &buf, mode)
123}
diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs
index 6a81c0e4d..88f2ac0e3 100644
--- a/xtask/src/codegen/gen_syntax.rs
+++ b/xtask/src/codegen/gen_syntax.rs
@@ -3,12 +3,7 @@
3//! Specifically, it generates the `SyntaxKind` enum and a number of newtype 3//! Specifically, it generates the `SyntaxKind` enum and a number of newtype
4//! wrappers around `SyntaxNode` which implement `ra_syntax::AstNode`. 4//! wrappers around `SyntaxNode` which implement `ra_syntax::AstNode`.
5 5
6use std::{ 6use std::{collections::BTreeMap, fs};
7 collections::BTreeMap,
8 fs,
9 io::Write,
10 process::{Command, Stdio},
11};
12 7
13use proc_macro2::{Punct, Spacing}; 8use proc_macro2::{Punct, Spacing};
14use quote::{format_ident, quote}; 9use quote::{format_ident, quote};
@@ -163,7 +158,7 @@ fn generate_ast(grammar: &Grammar) -> Result<String> {
163 #(#nodes)* 158 #(#nodes)*
164 }; 159 };
165 160
166 let pretty = reformat(ast)?; 161 let pretty = codegen::reformat(ast)?;
167 Ok(pretty) 162 Ok(pretty)
168} 163}
169 164
@@ -276,21 +271,7 @@ fn generate_syntax_kinds(grammar: &Grammar) -> Result<String> {
276 } 271 }
277 }; 272 };
278 273
279 reformat(ast) 274 codegen::reformat(ast)
280}
281
282fn reformat(text: impl std::fmt::Display) -> Result<String> {
283 let mut rustfmt = Command::new("rustfmt")
284 .arg("--config-path")
285 .arg(project_root().join("rustfmt.toml"))
286 .stdin(Stdio::piped())
287 .stdout(Stdio::piped())
288 .spawn()?;
289 write!(rustfmt.stdin.take().unwrap(), "{}", text)?;
290 let output = rustfmt.wait_with_output()?;
291 let stdout = String::from_utf8(output.stdout)?;
292 let preamble = "Generated file, do not edit by hand, see `crate/ra_tools/src/codegen`";
293 Ok(format!("//! {}\n\n{}", preamble, stdout))
294} 275}
295 276
296#[derive(Deserialize, Debug)] 277#[derive(Deserialize, Debug)]
diff --git a/xtask/src/main.rs b/xtask/src/main.rs
index db901ced2..06aa3c8ec 100644
--- a/xtask/src/main.rs
+++ b/xtask/src/main.rs
@@ -64,6 +64,7 @@ fn main() -> Result<()> {
64 } 64 }
65 codegen::generate_syntax(Mode::Overwrite)?; 65 codegen::generate_syntax(Mode::Overwrite)?;
66 codegen::generate_parser_tests(Mode::Overwrite)?; 66 codegen::generate_parser_tests(Mode::Overwrite)?;
67 codegen::generate_assists_docs(Mode::Overwrite)?;
67 } 68 }
68 "format" => { 69 "format" => {
69 if matches.contains(["-h", "--help"]) { 70 if matches.contains(["-h", "--help"]) {
diff --git a/xtask/tests/tidy-tests/cli.rs b/xtask/tests/tidy-tests/cli.rs
index 543c7d7c4..573ffadbf 100644
--- a/xtask/tests/tidy-tests/cli.rs
+++ b/xtask/tests/tidy-tests/cli.rs
@@ -19,6 +19,13 @@ fn generated_tests_are_fresh() {
19} 19}
20 20
21#[test] 21#[test]
22fn generated_assists_are_fresh() {
23 if let Err(error) = codegen::generate_assists_docs(Mode::Verify) {
24 panic!("{}. Please update assists by running `cargo xtask codegen`", error);
25 }
26}
27
28#[test]
22fn check_code_formatting() { 29fn check_code_formatting() {
23 if let Err(error) = run_rustfmt(Mode::Verify) { 30 if let Err(error) = run_rustfmt(Mode::Verify) {
24 panic!("{}. Please format the code by running `cargo format`", error); 31 panic!("{}. Please format the code by running `cargo format`", error);
diff --git a/xtask/tests/tidy-tests/docs.rs b/xtask/tests/tidy-tests/docs.rs
index fe5852bc6..b766aeff1 100644
--- a/xtask/tests/tidy-tests/docs.rs
+++ b/xtask/tests/tidy-tests/docs.rs
@@ -8,7 +8,9 @@ use walkdir::{DirEntry, WalkDir};
8use xtask::project_root; 8use xtask::project_root;
9 9
10fn is_exclude_dir(p: &Path) -> bool { 10fn is_exclude_dir(p: &Path) -> bool {
11 let exclude_dirs = ["tests", "test_data"]; 11 // Test hopefully don't really need comments, and for assists we already
12 // have special comments which are source of doc tests and user docs.
13 let exclude_dirs = ["tests", "test_data", "assists"];
12 let mut cur_path = p; 14 let mut cur_path = p;
13 while let Some(path) = cur_path.parent() { 15 while let Some(path) = cur_path.parent() {
14 if exclude_dirs.iter().any(|dir| path.ends_with(dir)) { 16 if exclude_dirs.iter().any(|dir| path.ends_with(dir)) {