rust/crates/rust-analyzer/tests/slow-tests/sourcegen.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

81 lines
2.4 KiB
Rust
Raw Normal View History

2020-05-30 18:54:54 -05:00
//! Generates `assists.md` documentation.
use std::{fmt, fs, io, path::PathBuf};
2020-05-30 18:54:54 -05:00
#[test]
fn sourcegen_feature_docs() {
let features = Feature::collect().unwrap();
2020-05-30 18:54:54 -05:00
let contents = features.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
let contents = format!(
"
// Generated file, do not edit by hand, see `sourcegen_feature_docs`.
{}
",
contents.trim()
);
let dst = sourcegen::project_root().join("docs/user/generated_features.adoc");
fs::write(dst, contents).unwrap();
2020-05-30 18:54:54 -05:00
}
#[derive(Debug)]
struct Feature {
id: String,
location: sourcegen::Location,
2020-05-30 18:54:54 -05:00
doc: String,
}
impl Feature {
fn collect() -> io::Result<Vec<Feature>> {
let crates_dir = sourcegen::project_root().join("crates");
2020-05-30 18:54:54 -05:00
let mut res = Vec::new();
for path in sourcegen::list_rust_files(&crates_dir) {
2020-05-30 18:54:54 -05:00
collect_file(&mut res, path)?;
}
res.sort_by(|lhs, rhs| lhs.id.cmp(&rhs.id));
return Ok(res);
fn collect_file(acc: &mut Vec<Feature>, path: PathBuf) -> io::Result<()> {
let text = std::fs::read_to_string(&path)?;
let comment_blocks = sourcegen::CommentBlock::extract("Feature", &text);
2020-05-30 18:54:54 -05:00
for block in comment_blocks {
let id = block.id;
2020-10-05 13:22:44 -05:00
if let Err(msg) = is_valid_feature_name(&id) {
panic!("invalid feature name: {id:?}:\n {msg}")
2020-10-05 13:22:44 -05:00
}
2020-05-30 18:54:54 -05:00
let doc = block.contents.join("\n");
let location = sourcegen::Location { file: path.clone(), line: block.line };
2020-05-31 08:36:20 -05:00
acc.push(Feature { id, location, doc })
2020-05-30 18:54:54 -05:00
}
Ok(())
}
}
}
2020-10-05 13:22:44 -05:00
fn is_valid_feature_name(feature: &str) -> Result<(), String> {
2020-05-31 02:45:41 -05:00
'word: for word in feature.split_whitespace() {
2021-10-22 01:23:29 -05:00
for short in ["to", "and"] {
2020-05-31 02:45:41 -05:00
if word == short {
continue 'word;
}
}
2021-10-22 01:23:29 -05:00
for short in ["To", "And"] {
2020-05-31 02:45:41 -05:00
if word == short {
return Err(format!("Don't capitalize {word:?}"));
2020-05-31 02:45:41 -05:00
}
}
if !word.starts_with(char::is_uppercase) {
return Err(format!("Capitalize {word:?}"));
2020-05-31 02:45:41 -05:00
}
}
2020-10-05 13:22:44 -05:00
Ok(())
2020-05-31 02:45:41 -05:00
}
2020-05-30 18:54:54 -05:00
impl fmt::Display for Feature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "=== {}\n**Source:** {}\n{}", self.id, self.location, self.doc)
2020-05-30 18:54:54 -05:00
}
}