mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
auto-generate assists docs and tests
This commit is contained in:
parent
518f99e16b
commit
0dd35ff2b2
12 changed files with 269 additions and 52 deletions
123
xtask/src/codegen/gen_assists_docs.rs
Normal file
123
xtask/src/codegen/gen_assists_docs.rs
Normal file
|
@ -0,0 +1,123 @@
|
|||
use std::{fs, path::Path};
|
||||
|
||||
use crate::{
|
||||
codegen::{self, extract_comment_blocks, Mode},
|
||||
project_root, Result,
|
||||
};
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
fn collect_assists() -> Result<Vec<Assist>> {
|
||||
let mut res = Vec::new();
|
||||
for entry in fs::read_dir(project_root().join(codegen::ASSISTS_DIR))? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
if path.is_file() {
|
||||
collect_file(&mut res, path.as_path())?;
|
||||
}
|
||||
}
|
||||
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)?;
|
||||
let comment_blocks = extract_comment_blocks(&text);
|
||||
|
||||
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();
|
||||
assert!(id.chars().all(|it| it.is_ascii_lowercase() || it == '_'));
|
||||
|
||||
let doc = take_until(lines.by_ref(), "```");
|
||||
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<()> {
|
||||
let mut buf = String::from("use super::check;\n");
|
||||
|
||||
for assist in assists.iter() {
|
||||
let test = format!(
|
||||
r######"
|
||||
#[test]
|
||||
fn doctest_{}() {{
|
||||
check(
|
||||
"{}",
|
||||
r#####"
|
||||
{}
|
||||
"#####, r#####"
|
||||
{}
|
||||
"#####)
|
||||
}}
|
||||
"######,
|
||||
assist.id, assist.id, assist.before, assist.after
|
||||
);
|
||||
|
||||
buf.push_str(&test)
|
||||
}
|
||||
let buf = codegen::reformat(buf)?;
|
||||
codegen::update(&project_root().join(codegen::ASSISTS_TESTS), &buf, mode)
|
||||
}
|
||||
|
||||
fn generate_docs(assists: &[Assist], mode: Mode) -> Result<()> {
|
||||
let mut buf = String::from("# Assists\n");
|
||||
|
||||
for assist in assists {
|
||||
let docs = format!(
|
||||
"
|
||||
## `{}`
|
||||
|
||||
{}
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
{}
|
||||
|
||||
// AFTER
|
||||
{}
|
||||
```
|
||||
",
|
||||
assist.id, assist.doc, assist.before, assist.after
|
||||
);
|
||||
buf.push_str(&docs);
|
||||
}
|
||||
|
||||
codegen::update(&project_root().join(codegen::ASSISTS_DOCS), &buf, mode)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue