rust-analyzer/xtask/src/codegen/gen_assists_docs.rs

160 lines
4.4 KiB
Rust
Raw Normal View History

2020-02-27 10:07:26 +00:00
//! Generates `assists.md` documentation.
2019-10-25 11:16:46 +00:00
use std::{fs, path::Path};
use crate::{
2019-10-25 20:38:15 +00:00
codegen::{self, extract_comment_blocks_with_empty_lines, Mode},
2020-03-21 15:04:28 +00:00
project_root, rust_files, Result,
2019-10-25 11:16:46 +00:00
};
pub fn generate_assists_docs(mode: Mode) -> Result<()> {
let assists = collect_assists()?;
generate_tests(&assists, mode)?;
generate_docs(&assists, mode)?;
Ok(())
}
#[derive(Debug)]
struct Assist {
id: String,
doc: String,
before: String,
after: String,
}
2020-02-06 17:10:25 +00:00
fn hide_hash_comments(text: &str) -> String {
text.split('\n') // want final newline
.filter(|&it| !(it.starts_with("# ") || it == "#"))
.map(|it| format!("{}\n", it))
.collect()
}
fn reveal_hash_comments(text: &str) -> String {
text.split('\n') // want final newline
.map(|it| {
if it.starts_with("# ") {
&it[2..]
} else if it == "#" {
""
} else {
it
}
})
.map(|it| format!("{}\n", it))
.collect()
}
2019-10-25 11:16:46 +00:00
fn collect_assists() -> Result<Vec<Assist>> {
let mut res = Vec::new();
2020-03-21 15:04:28 +00:00
for path in rust_files(&project_root().join(codegen::ASSISTS_DIR)) {
collect_file(&mut res, path.as_path())?;
2019-10-25 11:16:46 +00:00
}
res.sort_by(|lhs, rhs| lhs.id.cmp(&rhs.id));
return Ok(res);
fn collect_file(acc: &mut Vec<Assist>, path: &Path) -> Result<()> {
let text = fs::read_to_string(path)?;
2019-10-25 20:38:15 +00:00
let comment_blocks = extract_comment_blocks_with_empty_lines(&text);
2019-10-25 11:16:46 +00:00
for block in comment_blocks {
// FIXME: doesn't support blank lines yet, need to tweak
// `extract_comment_blocks` for that.
let mut lines = block.iter();
let first_line = lines.next().unwrap();
if !first_line.starts_with("Assist: ") {
continue;
}
let id = first_line["Assist: ".len()..].to_string();
2019-10-25 20:38:15 +00:00
assert!(
id.chars().all(|it| it.is_ascii_lowercase() || it == '_'),
"invalid assist id: {:?}",
id
);
2019-10-25 11:16:46 +00:00
2019-10-26 14:27:47 +00:00
let doc = take_until(lines.by_ref(), "```").trim().to_string();
2019-10-26 16:08:13 +00:00
assert!(
2019-10-30 17:36:37 +00:00
doc.chars().next().unwrap().is_ascii_uppercase() && doc.ends_with('.'),
2019-10-26 16:08:13 +00:00
"\n\n{}: assist docs should be proper sentences, with capitalization and a full stop at the end.\n\n{}\n\n",
id, doc,
);
2019-10-25 11:16:46 +00:00
let before = take_until(lines.by_ref(), "```");
assert_eq!(lines.next().unwrap().as_str(), "->");
assert_eq!(lines.next().unwrap().as_str(), "```");
let after = take_until(lines.by_ref(), "```");
acc.push(Assist { id, doc, before, after })
}
fn take_until<'a>(lines: impl Iterator<Item = &'a String>, marker: &str) -> String {
let mut buf = Vec::new();
for line in lines {
if line == marker {
break;
}
buf.push(line.clone());
}
buf.join("\n")
}
Ok(())
}
}
fn generate_tests(assists: &[Assist], mode: Mode) -> Result<()> {
2020-05-06 08:21:35 +00:00
let mut buf = String::from("use super::check_doc_test;\n");
2019-10-25 11:16:46 +00:00
for assist in assists.iter() {
let test = format!(
r######"
#[test]
fn doctest_{}() {{
2020-05-06 08:21:35 +00:00
check_doc_test(
2019-10-25 11:16:46 +00:00
"{}",
r#####"
2020-02-06 17:10:25 +00:00
{}"#####, r#####"
{}"#####)
2019-10-25 11:16:46 +00:00
}}
"######,
2020-02-06 17:10:25 +00:00
assist.id,
assist.id,
reveal_hash_comments(&assist.before),
reveal_hash_comments(&assist.after)
2019-10-25 11:16:46 +00:00
);
buf.push_str(&test)
}
let buf = crate::reformat(buf)?;
2019-10-25 11:16:46 +00:00
codegen::update(&project_root().join(codegen::ASSISTS_TESTS), &buf, mode)
}
fn generate_docs(assists: &[Assist], mode: Mode) -> Result<()> {
2019-10-26 18:17:39 +00:00
let mut buf = String::from(
"# Assists\n\nCursor position or selection is signified by `┃` character.\n\n",
);
2019-10-25 11:16:46 +00:00
for assist in assists {
2019-10-26 18:17:39 +00:00
let before = assist.before.replace("<|>", ""); // Unicode pseudo-graphics bar
let after = assist.after.replace("<|>", "");
2019-10-25 11:16:46 +00:00
let docs = format!(
"
## `{}`
{}
```rust
// BEFORE
{}
// AFTER
2020-02-06 17:10:25 +00:00
{}```
2019-10-25 11:16:46 +00:00
",
2020-02-06 17:10:25 +00:00
assist.id,
assist.doc,
hide_hash_comments(&before),
hide_hash_comments(&after)
2019-10-25 11:16:46 +00:00
);
buf.push_str(&docs);
}
codegen::update(&project_root().join(codegen::ASSISTS_DOCS), &buf, mode)
}