2020-02-27 10:07:26 +00:00
|
|
|
//! Generates `assists.md` documentation.
|
|
|
|
|
2021-07-03 19:11:03 +00:00
|
|
|
use std::{fmt, fs, path::Path};
|
2019-10-25 11:16:46 +00:00
|
|
|
|
2021-07-03 19:11:03 +00:00
|
|
|
use test_utils::project_root;
|
2021-03-08 18:39:09 +00:00
|
|
|
|
2021-07-03 19:11:03 +00:00
|
|
|
#[test]
|
|
|
|
fn sourcegen_assists_docs() {
|
|
|
|
let assists = Assist::collect();
|
|
|
|
|
|
|
|
{
|
|
|
|
// Generate doctests.
|
|
|
|
|
|
|
|
let mut buf = "
|
|
|
|
use super::check_doc_test;
|
|
|
|
"
|
|
|
|
.to_string();
|
|
|
|
for assist in assists.iter() {
|
2021-07-31 06:52:02 +00:00
|
|
|
for (idx, section) in assist.sections.iter().enumerate() {
|
2021-07-31 07:18:38 +00:00
|
|
|
let test_id =
|
2022-12-23 18:42:58 +00:00
|
|
|
if idx == 0 { assist.id.clone() } else { format!("{}_{idx}", &assist.id) };
|
2021-07-31 06:52:02 +00:00
|
|
|
let test = format!(
|
2021-07-31 07:18:38 +00:00
|
|
|
r######"
|
2021-07-03 19:11:03 +00:00
|
|
|
#[test]
|
|
|
|
fn doctest_{}() {{
|
|
|
|
check_doc_test(
|
|
|
|
"{}",
|
|
|
|
r#####"
|
|
|
|
{}"#####, r#####"
|
|
|
|
{}"#####)
|
|
|
|
}}
|
|
|
|
"######,
|
2021-07-31 07:18:38 +00:00
|
|
|
&test_id,
|
2022-05-20 12:39:22 +00:00
|
|
|
&assist.id,
|
2021-07-31 06:52:02 +00:00
|
|
|
reveal_hash_comments(§ion.before),
|
|
|
|
reveal_hash_comments(§ion.after)
|
|
|
|
);
|
2021-07-31 07:18:38 +00:00
|
|
|
|
|
|
|
buf.push_str(&test)
|
2021-07-31 06:52:02 +00:00
|
|
|
}
|
2021-07-03 19:11:03 +00:00
|
|
|
}
|
|
|
|
let buf = sourcegen::add_preamble("sourcegen_assists_docs", sourcegen::reformat(buf));
|
|
|
|
sourcegen::ensure_file_contents(
|
2022-04-30 21:00:41 +00:00
|
|
|
&project_root().join("crates/ide-assists/src/tests/generated.rs"),
|
2021-07-03 19:11:03 +00:00
|
|
|
&buf,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Generate assists manual. Note that we do _not_ commit manual to the
|
|
|
|
// git repo. Instead, `cargo xtask release` runs this test before making
|
|
|
|
// a release.
|
2020-05-31 13:02:12 +00:00
|
|
|
|
2021-07-03 19:11:03 +00:00
|
|
|
let contents = sourcegen::add_preamble(
|
|
|
|
"sourcegen_assists_docs",
|
|
|
|
assists.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"),
|
|
|
|
);
|
|
|
|
let dst = project_root().join("docs/user/generated_assists.adoc");
|
|
|
|
fs::write(dst, contents).unwrap();
|
|
|
|
}
|
2019-10-25 11:16:46 +00:00
|
|
|
}
|
2022-07-23 00:06:11 +00:00
|
|
|
|
2021-07-31 07:18:38 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct Section {
|
2021-07-31 06:52:02 +00:00
|
|
|
doc: String,
|
|
|
|
before: String,
|
|
|
|
after: String,
|
|
|
|
}
|
2019-10-25 11:16:46 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Assist {
|
|
|
|
id: String,
|
2021-07-03 19:11:03 +00:00
|
|
|
location: sourcegen::Location,
|
2021-07-31 07:18:38 +00:00
|
|
|
sections: Vec<Section>,
|
2019-10-25 11:16:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-30 21:57:16 +00:00
|
|
|
impl Assist {
|
2021-07-03 19:11:03 +00:00
|
|
|
fn collect() -> Vec<Assist> {
|
2022-04-30 21:00:41 +00:00
|
|
|
let handlers_dir = project_root().join("crates/ide-assists/src/handlers");
|
2021-07-03 19:11:03 +00:00
|
|
|
|
2020-05-30 21:57:16 +00:00
|
|
|
let mut res = Vec::new();
|
2021-07-03 19:11:03 +00:00
|
|
|
for path in sourcegen::list_rust_files(&handlers_dir) {
|
|
|
|
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));
|
2021-07-03 19:11:03 +00:00
|
|
|
return res;
|
2020-05-30 21:57:16 +00:00
|
|
|
|
2021-07-03 19:11:03 +00:00
|
|
|
fn collect_file(acc: &mut Vec<Assist>, path: &Path) {
|
|
|
|
let text = fs::read_to_string(path).unwrap();
|
|
|
|
let comment_blocks = sourcegen::CommentBlock::extract("Assist", &text);
|
2020-05-30 21:57:16 +00:00
|
|
|
|
|
|
|
for block in comment_blocks {
|
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 == '_'),
|
2022-12-30 07:59:11 +00:00
|
|
|
"invalid assist id: {id:?}"
|
2020-05-30 21:57:16 +00:00
|
|
|
);
|
2021-07-31 06:52:02 +00:00
|
|
|
let mut lines = block.contents.iter().peekable();
|
2021-07-03 19:11:03 +00:00
|
|
|
let location = sourcegen::Location { file: path.to_path_buf(), line: block.line };
|
2021-07-31 06:52:02 +00:00
|
|
|
let mut assist = Assist { id, location, sections: Vec::new() };
|
|
|
|
|
|
|
|
while lines.peek().is_some() {
|
|
|
|
let doc = take_until(lines.by_ref(), "```").trim().to_string();
|
|
|
|
assert!(
|
2022-07-23 00:06:11 +00:00
|
|
|
(doc.chars().next().unwrap().is_ascii_uppercase() && doc.ends_with('.'))
|
|
|
|
|| assist.sections.len() > 0,
|
2021-07-31 06:52:02 +00:00
|
|
|
"\n\n{}: assist docs should be proper sentences, with capitalization and a full stop at the end.\n\n{}\n\n",
|
2022-07-23 00:06:11 +00:00
|
|
|
&assist.id,
|
|
|
|
doc,
|
2021-07-31 06:52:02 +00:00
|
|
|
);
|
2021-07-31 07:18:38 +00:00
|
|
|
|
2021-07-31 06:52:02 +00:00
|
|
|
let before = take_until(lines.by_ref(), "```");
|
2021-07-31 07:18:38 +00:00
|
|
|
|
2022-05-20 12:39:22 +00:00
|
|
|
assert_eq!(lines.next().unwrap().as_str(), "->");
|
2021-07-31 06:52:02 +00:00
|
|
|
assert_eq!(lines.next().unwrap().as_str(), "```");
|
|
|
|
let after = take_until(lines.by_ref(), "```");
|
|
|
|
|
2022-05-20 12:39:22 +00:00
|
|
|
assist.sections.push(Section { doc, before, after });
|
2021-07-31 06:52:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
acc.push(assist)
|
2020-05-30 21:57:16 +00:00
|
|
|
}
|
2021-07-03 19:11:03 +00:00
|
|
|
}
|
2019-10-25 11:16:46 +00:00
|
|
|
|
2021-07-03 19:11:03 +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;
|
2019-10-25 11:16:46 +00:00
|
|
|
}
|
2021-07-03 19:11:03 +00:00
|
|
|
buf.push(line.clone());
|
2019-10-25 11:16:46 +00:00
|
|
|
}
|
2021-07-03 19:11:03 +00:00
|
|
|
buf.join("\n")
|
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 {
|
2021-07-31 06:52:02 +00:00
|
|
|
let _ = writeln!(
|
2020-05-31 13:02:12 +00:00
|
|
|
f,
|
|
|
|
"[discrete]\n=== `{}`
|
2021-07-31 06:52:02 +00:00
|
|
|
**Source:** {}",
|
2021-07-31 07:18:38 +00:00
|
|
|
self.id, self.location,
|
|
|
|
);
|
2020-05-31 13:02:12 +00:00
|
|
|
|
2021-07-31 06:52:02 +00:00
|
|
|
for section in &self.sections {
|
|
|
|
let before = section.before.replace("$0", "┃"); // Unicode pseudo-graphics bar
|
|
|
|
let after = section.after.replace("$0", "┃");
|
2021-07-31 07:18:38 +00:00
|
|
|
let _ = writeln!(
|
2021-07-31 06:52:02 +00:00
|
|
|
f,
|
2021-07-31 07:18:38 +00:00
|
|
|
"
|
2020-05-31 13:02:12 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
.Before
|
|
|
|
```rust
|
|
|
|
{}```
|
|
|
|
|
|
|
|
.After
|
|
|
|
```rust
|
|
|
|
{}```",
|
2021-07-31 06:52:02 +00:00
|
|
|
section.doc,
|
|
|
|
hide_hash_comments(&before),
|
|
|
|
hide_hash_comments(&after)
|
2021-07-31 07:18:38 +00:00
|
|
|
);
|
2021-07-31 06:52:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2020-05-31 13:02:12 +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 == "#"))
|
2022-12-23 18:42:58 +00:00
|
|
|
.map(|it| format!("{it}\n"))
|
2020-05-30 21:57:16 +00:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reveal_hash_comments(text: &str) -> String {
|
|
|
|
text.split('\n') // want final newline
|
|
|
|
.map(|it| {
|
2021-03-21 11:38:21 +00:00
|
|
|
if let Some(stripped) = it.strip_prefix("# ") {
|
|
|
|
stripped
|
2020-05-30 21:57:16 +00:00
|
|
|
} else if it == "#" {
|
|
|
|
""
|
|
|
|
} else {
|
|
|
|
it
|
|
|
|
}
|
|
|
|
})
|
2022-12-23 18:42:58 +00:00
|
|
|
.map(|it| format!("{it}\n"))
|
2020-05-30 21:57:16 +00:00
|
|
|
.collect()
|
|
|
|
}
|