1 //! We use code generation heavily in rust-analyzer.
3 //! Rather then doing it via proc-macros, we use old-school way of just dumping
6 //! This module's submodules define specific bits that we generate.
12 mod gen_unstable_future_descriptor;
16 path::{Path, PathBuf},
21 not_bash::{fs2, pushenv, run},
26 gen_assists_docs::{generate_assists_docs, generate_assists_tests},
27 gen_feature_docs::generate_feature_docs,
28 gen_parser_tests::generate_parser_tests,
29 gen_syntax::generate_syntax,
30 gen_unstable_future_descriptor::generate_unstable_future_descriptor,
33 // Directory used by xtask
34 const STORAGE: &str = ".xtask";
36 const GRAMMAR_DIR: &str = "crates/parser/src/grammar";
37 const OK_INLINE_TESTS_DIR: &str = "crates/syntax/test_data/parser/inline/ok";
38 const ERR_INLINE_TESTS_DIR: &str = "crates/syntax/test_data/parser/inline/err";
40 const SYNTAX_KINDS: &str = "crates/parser/src/syntax_kind/generated.rs";
41 const AST_NODES: &str = "crates/syntax/src/ast/generated/nodes.rs";
42 const AST_TOKENS: &str = "crates/syntax/src/ast/generated/tokens.rs";
44 const ASSISTS_DIR: &str = "crates/assists/src/handlers";
45 const ASSISTS_TESTS: &str = "crates/assists/src/tests/generated.rs";
47 const REPOSITORY_URL: &str = "https://github.com/rust-lang/rust";
48 const UNSTABLE_FEATURE: &str = "crates/ide/src/completion/unstable_feature_descriptor.rs";
49 const REPO_PATH: &str = "src/doc/unstable-book/src";
51 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
57 /// A helper to update file on disk if it has changed.
58 /// With verify = false,
59 fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> {
60 match fs2::read_to_string(path) {
61 Ok(old_contents) if normalize(&old_contents) == normalize(contents) => {
66 if mode == Mode::Verify {
67 anyhow::bail!("`{}` is not up-to-date", path.display());
69 eprintln!("updating {}", path.display());
70 fs2::write(path, contents)?;
73 fn normalize(s: &str) -> String {
74 s.replace("\r\n", "\n")
78 const PREAMBLE: &str = "Generated file, do not edit by hand, see `xtask/src/codegen`";
80 fn reformat(text: impl std::fmt::Display) -> Result<String> {
81 let _e = pushenv("RUSTUP_TOOLCHAIN", "stable");
84 "rustfmt --config-path {} --config fn_single_line=true", project_root().join("rustfmt.toml").display();
85 <text.to_string().as_bytes()
87 Ok(format!("//! {}\n\n{}\n", PREAMBLE, stdout))
90 fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> {
91 do_extract_comment_blocks(text, false).into_iter().map(|(_line, block)| block).collect()
94 fn extract_comment_blocks_with_empty_lines(tag: &str, text: &str) -> Vec<CommentBlock> {
95 assert!(tag.starts_with(char::is_uppercase));
96 let tag = format!("{}:", tag);
97 let mut res = Vec::new();
98 for (line, mut block) in do_extract_comment_blocks(text, true) {
99 let first = block.remove(0);
100 if first.starts_with(&tag) {
101 let id = first[tag.len()..].trim().to_string();
102 let block = CommentBlock { id, line, contents: block };
109 struct CommentBlock {
112 contents: Vec<String>,
115 fn do_extract_comment_blocks(
117 allow_blocks_with_empty_lines: bool,
118 ) -> Vec<(usize, Vec<String>)> {
119 let mut res = Vec::new();
122 let lines = text.lines().map(str::trim_start);
124 let mut block = (0, vec![]);
125 for (line_num, line) in lines.enumerate() {
126 if line == "//" && allow_blocks_with_empty_lines {
127 block.1.push(String::new());
131 let is_comment = line.starts_with(prefix);
133 block.1.push(line[prefix.len()..].to_string());
135 if !block.1.is_empty() {
136 res.push(mem::take(&mut block));
138 block.0 = line_num + 2;
141 if !block.1.is_empty() {
154 fn new(file: PathBuf, line: usize) -> Self {
159 impl fmt::Display for Location {
160 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
161 let path = self.file.strip_prefix(&project_root()).unwrap().display().to_string();
162 let path = path.replace('\\', "/");
163 let name = self.file.file_name().unwrap();
166 "https://github.com/rust-analyzer/rust-analyzer/blob/master/{}#L{}[{}]",
169 name.to_str().unwrap()