rust-analyzer/xtask/src/codegen.rs

110 lines
3.2 KiB
Rust
Raw Normal View History

2019-10-23 15:13:40 +00:00
//! We use code generation heavily in rust-analyzer.
//!
//! Rather then doing it via proc-macros, we use old-school way of just dumping
//! the source code.
//!
//! This module's submodules define specific bits that we generate.
mod gen_syntax;
mod gen_parser_tests;
2019-10-25 11:16:46 +00:00
mod gen_assists_docs;
2020-05-30 23:54:54 +00:00
mod gen_feature_docs;
2019-10-23 15:13:40 +00:00
2020-04-18 18:46:24 +00:00
use std::{mem, path::Path};
2019-10-23 15:13:40 +00:00
2020-04-18 18:46:24 +00:00
use crate::{not_bash::fs2, Result};
2019-10-23 15:13:40 +00:00
2019-10-25 11:16:46 +00:00
pub use self::{
2020-05-30 23:54:54 +00:00
gen_assists_docs::generate_assists_docs, gen_feature_docs::generate_feature_docs,
gen_parser_tests::generate_parser_tests, gen_syntax::generate_syntax,
2019-10-25 11:16:46 +00:00
};
2019-10-23 15:13:40 +00:00
const GRAMMAR_DIR: &str = "crates/ra_parser/src/grammar";
const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/ok";
const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/err";
2020-04-09 13:49:17 +00:00
const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs";
const AST_NODES: &str = "crates/ra_syntax/src/ast/generated/nodes.rs";
2020-04-09 14:25:06 +00:00
const AST_TOKENS: &str = "crates/ra_syntax/src/ast/generated/tokens.rs";
2019-10-23 15:13:40 +00:00
2020-02-07 14:53:31 +00:00
const ASSISTS_DIR: &str = "crates/ra_assists/src/handlers";
2020-05-06 08:21:35 +00:00
const ASSISTS_TESTS: &str = "crates/ra_assists/src/tests/generated.rs";
2019-10-25 11:16:46 +00:00
const ASSISTS_DOCS: &str = "docs/user/assists.md";
2019-10-23 15:13:40 +00:00
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Mode {
Overwrite,
Verify,
}
/// A helper to update file on disk if it has changed.
/// With verify = false,
2019-10-25 11:16:46 +00:00
fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> {
2020-04-18 18:46:24 +00:00
match fs2::read_to_string(path) {
Ok(ref old_contents) if normalize(old_contents) == normalize(contents) => {
2019-10-23 15:13:40 +00:00
return Ok(());
}
_ => (),
}
if mode == Mode::Verify {
anyhow::bail!("`{}` is not up-to-date", path.display());
2019-10-23 15:13:40 +00:00
}
eprintln!("updating {}", path.display());
2020-04-18 18:46:24 +00:00
fs2::write(path, contents)?;
return Ok(());
fn normalize(s: &str) -> String {
s.replace("\r\n", "\n")
}
2019-10-23 15:13:40 +00:00
}
2019-10-24 16:29:38 +00:00
fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> {
2019-10-25 20:38:15 +00:00
do_extract_comment_blocks(text, false)
}
2020-05-30 22:33:37 +00:00
fn extract_comment_blocks_with_empty_lines(tag: &str, text: &str) -> Vec<CommentBlock> {
assert!(tag.starts_with(char::is_uppercase));
let tag = format!("{}:", tag);
let mut res = Vec::new();
for mut block in do_extract_comment_blocks(text, true) {
let first = block.remove(0);
if first.starts_with(&tag) {
let id = first[tag.len()..].trim().to_string();
let block = CommentBlock { id, contents: block };
res.push(block);
}
}
res
}
struct CommentBlock {
id: String,
contents: Vec<String>,
2019-10-25 20:38:15 +00:00
}
2020-04-18 18:46:24 +00:00
fn do_extract_comment_blocks(text: &str, allow_blocks_with_empty_lines: bool) -> Vec<Vec<String>> {
2019-10-24 16:29:38 +00:00
let mut res = Vec::new();
let prefix = "// ";
let lines = text.lines().map(str::trim_start);
let mut block = vec![];
for line in lines {
2020-04-18 18:46:24 +00:00
if line == "//" && allow_blocks_with_empty_lines {
2019-10-25 20:38:15 +00:00
block.push(String::new());
continue;
}
2019-10-24 16:29:38 +00:00
let is_comment = line.starts_with(prefix);
if is_comment {
block.push(line[prefix.len()..].to_string());
} else if !block.is_empty() {
res.push(mem::replace(&mut block, Vec::new()));
2019-10-24 16:29:38 +00:00
}
}
if !block.is_empty() {
res.push(mem::replace(&mut block, Vec::new()))
}
res
}