mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 05:38:46 +00:00
minor
This commit is contained in:
parent
40bdd2e161
commit
e1829d8959
1 changed files with 73 additions and 71 deletions
|
@ -8,7 +8,7 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn generate_assists_docs(mode: Mode) -> Result<()> {
|
pub fn generate_assists_docs(mode: Mode) -> Result<()> {
|
||||||
let assists = collect_assists()?;
|
let assists = Assist::collect()?;
|
||||||
generate_tests(&assists, mode)?;
|
generate_tests(&assists, mode)?;
|
||||||
generate_docs(&assists, mode)?;
|
generate_docs(&assists, mode)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -22,81 +22,61 @@ struct Assist {
|
||||||
after: String,
|
after: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hide_hash_comments(text: &str) -> String {
|
impl Assist {
|
||||||
text.split('\n') // want final newline
|
fn collect() -> Result<Vec<Assist>> {
|
||||||
.filter(|&it| !(it.starts_with("# ") || it == "#"))
|
let mut res = Vec::new();
|
||||||
.map(|it| format!("{}\n", it))
|
for path in rust_files(&project_root().join(codegen::ASSISTS_DIR)) {
|
||||||
.collect()
|
collect_file(&mut res, path.as_path())?;
|
||||||
}
|
|
||||||
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn collect_assists() -> Result<Vec<Assist>> {
|
|
||||||
let mut res = Vec::new();
|
|
||||||
for path in rust_files(&project_root().join(codegen::ASSISTS_DIR)) {
|
|
||||||
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_with_empty_lines(&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 == '_'),
|
|
||||||
"invalid assist id: {:?}",
|
|
||||||
id
|
|
||||||
);
|
|
||||||
|
|
||||||
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(), "```");
|
|
||||||
acc.push(Assist { id, doc, before, after })
|
|
||||||
}
|
}
|
||||||
|
res.sort_by(|lhs, rhs| lhs.id.cmp(&rhs.id));
|
||||||
|
return Ok(res);
|
||||||
|
|
||||||
fn take_until<'a>(lines: impl Iterator<Item = &'a String>, marker: &str) -> String {
|
fn collect_file(acc: &mut Vec<Assist>, path: &Path) -> Result<()> {
|
||||||
let mut buf = Vec::new();
|
let text = fs::read_to_string(path)?;
|
||||||
for line in lines {
|
let comment_blocks = extract_comment_blocks_with_empty_lines(&text);
|
||||||
if line == marker {
|
|
||||||
break;
|
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;
|
||||||
}
|
}
|
||||||
buf.push(line.clone());
|
let id = first_line["Assist: ".len()..].to_string();
|
||||||
|
assert!(
|
||||||
|
id.chars().all(|it| it.is_ascii_lowercase() || it == '_'),
|
||||||
|
"invalid assist id: {:?}",
|
||||||
|
id
|
||||||
|
);
|
||||||
|
|
||||||
|
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(), "```");
|
||||||
|
acc.push(Assist { id, doc, before, after })
|
||||||
}
|
}
|
||||||
buf.join("\n")
|
|
||||||
|
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(())
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,3 +137,25 @@ fn generate_docs(assists: &[Assist], mode: Mode) -> Result<()> {
|
||||||
|
|
||||||
codegen::update(&project_root().join(codegen::ASSISTS_DOCS), &buf, mode)
|
codegen::update(&project_root().join(codegen::ASSISTS_DOCS), &buf, mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue