2020-02-27 10:07:26 +00:00
|
|
|
//! Generates `assists.md` documentation.
|
|
|
|
|
2020-05-31 13:02:12 +00:00
|
|
|
use std::{fmt, fs, path::Path};
|
2019-10-25 11:16:46 +00:00
|
|
|
|
|
|
|
use crate::{
|
2020-08-17 13:49:46 +00:00
|
|
|
codegen::{self, extract_comment_blocks_with_empty_lines, reformat, Location, Mode, PREAMBLE},
|
2020-03-21 15:04:28 +00:00
|
|
|
project_root, rust_files, Result,
|
2019-10-25 11:16:46 +00:00
|
|
|
};
|
|
|
|
|
2020-06-03 16:22:05 +00:00
|
|
|
pub fn generate_assists_tests(mode: Mode) -> Result<()> {
|
2020-05-30 21:57:16 +00:00
|
|
|
let assists = Assist::collect()?;
|
2020-06-03 16:22:05 +00:00
|
|
|
generate_tests(&assists, mode)
|
|
|
|
}
|
2020-05-31 13:02:12 +00:00
|
|
|
|
2020-06-03 16:22:05 +00:00
|
|
|
pub fn generate_assists_docs(mode: Mode) -> Result<()> {
|
|
|
|
let assists = Assist::collect()?;
|
2020-05-31 13:02:12 +00:00
|
|
|
let contents = assists.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
|
2020-08-17 13:49:46 +00:00
|
|
|
let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim());
|
2020-05-31 13:02:12 +00:00
|
|
|
let dst = project_root().join("docs/user/generated_assists.adoc");
|
2020-06-03 16:22:05 +00:00
|
|
|
codegen::update(&dst, &contents, mode)
|
2019-10-25 11:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Assist {
|
|
|
|
id: String,
|
2020-05-31 13:02:12 +00:00
|
|
|
location: Location,
|
2019-10-25 11:16:46 +00:00
|
|
|
doc: String,
|
|
|
|
before: String,
|
|
|
|
after: String,
|
|
|
|
}
|
|
|
|
|
2020-05-30 21:57:16 +00:00
|
|
|
impl Assist {
|
|
|
|
fn collect() -> Result<Vec<Assist>> {
|
|
|
|
let mut res = Vec::new();
|
2020-08-18 17:31:06 +00:00
|
|
|
for path in rust_files(&project_root().join("crates/assists/src/handlers")) {
|
2020-05-30 21:57:16 +00:00
|
|
|
collect_file(&mut res, path.as_path())?;
|
2019-10-25 11:16:46 +00:00
|
|
|
}
|
2020-05-30 21:57:16 +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)?;
|
2020-05-30 22:33:37 +00:00
|
|
|
let comment_blocks = extract_comment_blocks_with_empty_lines("Assist", &text);
|
2020-05-30 21:57:16 +00:00
|
|
|
|
|
|
|
for block in comment_blocks {
|
|
|
|
// FIXME: doesn't support blank lines yet, need to tweak
|
|
|
|
// `extract_comment_blocks` for that.
|
2020-05-30 22:33:37 +00:00
|
|
|
let id = block.id;
|
2020-05-30 21:57:16 +00:00
|
|
|
assert!(
|
|
|
|
id.chars().all(|it| it.is_ascii_lowercase() || it == '_'),
|
|
|
|
"invalid assist id: {:?}",
|
|
|
|
id
|
|
|
|
);
|
2020-05-30 22:33:37 +00:00
|
|
|
let mut lines = block.contents.iter();
|
2020-05-30 21:57:16 +00:00
|
|
|
|
|
|
|
let doc = take_until(lines.by_ref(), "```").trim().to_string();
|
|
|
|
assert!(
|
|
|
|
doc.chars().next().unwrap().is_ascii_uppercase() && doc.ends_with('.'),
|
|
|
|
"\n\n{}: assist docs should be proper sentences, with capitalization and a full stop at the end.\n\n{}\n\n",
|
|
|
|
id, doc,
|
|
|
|
);
|
|
|
|
|
|
|
|
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(), "```");
|
2020-05-31 13:36:20 +00:00
|
|
|
let location = Location::new(path.to_path_buf(), block.line);
|
2020-05-31 13:02:12 +00:00
|
|
|
acc.push(Assist { id, location, doc, before, after })
|
2020-05-30 21:57:16 +00:00
|
|
|
}
|
2019-10-25 11:16:46 +00:00
|
|
|
|
2020-05-30 21:57:16 +00:00
|
|
|
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());
|
2019-10-25 11:16:46 +00:00
|
|
|
}
|
2020-05-30 21:57:16 +00:00
|
|
|
buf.join("\n")
|
2019-10-25 11:16:46 +00:00
|
|
|
}
|
2020-05-30 21:57:16 +00:00
|
|
|
Ok(())
|
2019-10-25 11:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 13:02:12 +00:00
|
|
|
impl fmt::Display for Assist {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let before = self.before.replace("<|>", "┃"); // Unicode pseudo-graphics bar
|
|
|
|
let after = self.after.replace("<|>", "┃");
|
|
|
|
writeln!(
|
|
|
|
f,
|
|
|
|
"[discrete]\n=== `{}`
|
2020-05-31 13:36:20 +00:00
|
|
|
**Source:** {}
|
2020-05-31 13:02:12 +00:00
|
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
.Before
|
|
|
|
```rust
|
|
|
|
{}```
|
|
|
|
|
|
|
|
.After
|
|
|
|
```rust
|
|
|
|
{}```",
|
|
|
|
self.id,
|
2020-05-31 13:36:20 +00:00
|
|
|
self.location,
|
2020-05-31 13:02:12 +00:00
|
|
|
self.doc,
|
|
|
|
hide_hash_comments(&before),
|
|
|
|
hide_hash_comments(&after)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-25 11:16:46 +00:00
|
|
|
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)
|
|
|
|
}
|
2020-08-17 13:49:46 +00:00
|
|
|
let buf = reformat(buf)?;
|
2020-08-18 17:31:06 +00:00
|
|
|
codegen::update(&project_root().join("crates/assists/src/tests/generated.rs"), &buf, mode)
|
2019-10-25 11:16:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-30 21:57:16 +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()
|
|
|
|
}
|