|
| 1 | +use std::{ |
| 2 | + fmt, fs, mem, |
| 3 | + path::{Path, PathBuf}, |
| 4 | +}; |
| 5 | + |
| 6 | +use xshell::{cmd, Shell}; |
| 7 | + |
| 8 | +use crate::{flags, project_root}; |
| 9 | + |
| 10 | +pub(crate) mod assists_doc_tests; |
| 11 | + |
| 12 | +impl flags::Codegen { |
| 13 | + pub(crate) fn run(self, _sh: &Shell) -> anyhow::Result<()> { |
| 14 | + match self.codegen_type.unwrap_or_default() { |
| 15 | + flags::CodegenType::All => { |
| 16 | + assists_doc_tests::generate(self.check); |
| 17 | + } |
| 18 | + flags::CodegenType::AssistsDocTests => assists_doc_tests::generate(self.check), |
| 19 | + } |
| 20 | + Ok(()) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +fn list_rust_files(dir: &Path) -> Vec<PathBuf> { |
| 25 | + let mut res = list_files(dir); |
| 26 | + res.retain(|it| { |
| 27 | + it.file_name().unwrap_or_default().to_str().unwrap_or_default().ends_with(".rs") |
| 28 | + }); |
| 29 | + res |
| 30 | +} |
| 31 | + |
| 32 | +fn list_files(dir: &Path) -> Vec<PathBuf> { |
| 33 | + let mut res = Vec::new(); |
| 34 | + let mut work = vec![dir.to_path_buf()]; |
| 35 | + while let Some(dir) = work.pop() { |
| 36 | + for entry in dir.read_dir().unwrap() { |
| 37 | + let entry = entry.unwrap(); |
| 38 | + let file_type = entry.file_type().unwrap(); |
| 39 | + let path = entry.path(); |
| 40 | + let is_hidden = |
| 41 | + path.file_name().unwrap_or_default().to_str().unwrap_or_default().starts_with('.'); |
| 42 | + if !is_hidden { |
| 43 | + if file_type.is_dir() { |
| 44 | + work.push(path); |
| 45 | + } else if file_type.is_file() { |
| 46 | + res.push(path); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + res |
| 52 | +} |
| 53 | + |
| 54 | +#[derive(Clone)] |
| 55 | +pub(crate) struct CommentBlock { |
| 56 | + pub(crate) id: String, |
| 57 | + pub(crate) line: usize, |
| 58 | + pub(crate) contents: Vec<String>, |
| 59 | + is_doc: bool, |
| 60 | +} |
| 61 | + |
| 62 | +impl CommentBlock { |
| 63 | + fn extract(tag: &str, text: &str) -> Vec<CommentBlock> { |
| 64 | + assert!(tag.starts_with(char::is_uppercase)); |
| 65 | + |
| 66 | + let tag = format!("{tag}:"); |
| 67 | + let mut blocks = CommentBlock::extract_untagged(text); |
| 68 | + blocks.retain_mut(|block| { |
| 69 | + let first = block.contents.remove(0); |
| 70 | + let Some(id) = first.strip_prefix(&tag) else { |
| 71 | + return false; |
| 72 | + }; |
| 73 | + |
| 74 | + if block.is_doc { |
| 75 | + panic!("Use plain (non-doc) comments with tags like {tag}:\n {first}"); |
| 76 | + } |
| 77 | + |
| 78 | + block.id = id.trim().to_owned(); |
| 79 | + true |
| 80 | + }); |
| 81 | + blocks |
| 82 | + } |
| 83 | + |
| 84 | + fn extract_untagged(text: &str) -> Vec<CommentBlock> { |
| 85 | + let mut res = Vec::new(); |
| 86 | + |
| 87 | + let lines = text.lines().map(str::trim_start); |
| 88 | + |
| 89 | + let dummy_block = |
| 90 | + CommentBlock { id: String::new(), line: 0, contents: Vec::new(), is_doc: false }; |
| 91 | + let mut block = dummy_block.clone(); |
| 92 | + for (line_num, line) in lines.enumerate() { |
| 93 | + match line.strip_prefix("//") { |
| 94 | + Some(mut contents) => { |
| 95 | + if let Some('/' | '!') = contents.chars().next() { |
| 96 | + contents = &contents[1..]; |
| 97 | + block.is_doc = true; |
| 98 | + } |
| 99 | + if let Some(' ') = contents.chars().next() { |
| 100 | + contents = &contents[1..]; |
| 101 | + } |
| 102 | + block.contents.push(contents.to_owned()); |
| 103 | + } |
| 104 | + None => { |
| 105 | + if !block.contents.is_empty() { |
| 106 | + let block = mem::replace(&mut block, dummy_block.clone()); |
| 107 | + res.push(block); |
| 108 | + } |
| 109 | + block.line = line_num + 2; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + if !block.contents.is_empty() { |
| 114 | + res.push(block); |
| 115 | + } |
| 116 | + res |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +#[derive(Debug)] |
| 121 | +pub(crate) struct Location { |
| 122 | + pub(crate) file: PathBuf, |
| 123 | + pub(crate) line: usize, |
| 124 | +} |
| 125 | + |
| 126 | +impl fmt::Display for Location { |
| 127 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 128 | + let path = self.file.strip_prefix(project_root()).unwrap().display().to_string(); |
| 129 | + let path = path.replace('\\', "/"); |
| 130 | + let name = self.file.file_name().unwrap(); |
| 131 | + write!( |
| 132 | + f, |
| 133 | + "https://github.com/rust-lang/rust-analyzer/blob/master/{}#L{}[{}]", |
| 134 | + path, |
| 135 | + self.line, |
| 136 | + name.to_str().unwrap() |
| 137 | + ) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +fn ensure_rustfmt(sh: &Shell) { |
| 142 | + let version = cmd!(sh, "rustup run stable rustfmt --version").read().unwrap_or_default(); |
| 143 | + if !version.contains("stable") { |
| 144 | + panic!( |
| 145 | + "Failed to run rustfmt from toolchain 'stable'. \ |
| 146 | + Please run `rustup component add rustfmt --toolchain stable` to install it.", |
| 147 | + ); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +fn reformat(text: String) -> String { |
| 152 | + let sh = Shell::new().unwrap(); |
| 153 | + ensure_rustfmt(&sh); |
| 154 | + let rustfmt_toml = project_root().join("rustfmt.toml"); |
| 155 | + let mut stdout = cmd!( |
| 156 | + sh, |
| 157 | + "rustup run stable rustfmt --config-path {rustfmt_toml} --config fn_single_line=true" |
| 158 | + ) |
| 159 | + .stdin(text) |
| 160 | + .read() |
| 161 | + .unwrap(); |
| 162 | + if !stdout.ends_with('\n') { |
| 163 | + stdout.push('\n'); |
| 164 | + } |
| 165 | + stdout |
| 166 | +} |
| 167 | + |
| 168 | +fn add_preamble(generator: &'static str, mut text: String) -> String { |
| 169 | + let preamble = format!("//! Generated by `{generator}`, do not edit by hand.\n\n"); |
| 170 | + text.insert_str(0, &preamble); |
| 171 | + text |
| 172 | +} |
| 173 | + |
| 174 | +/// Checks that the `file` has the specified `contents`. If that is not the |
| 175 | +/// case, updates the file and then fails the test. |
| 176 | +#[allow(clippy::print_stderr)] |
| 177 | +fn ensure_file_contents(file: &Path, contents: &str, check: bool) { |
| 178 | + if let Ok(old_contents) = fs::read_to_string(file) { |
| 179 | + if normalize_newlines(&old_contents) == normalize_newlines(contents) { |
| 180 | + // File is already up to date. |
| 181 | + return; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + let display_path = file.strip_prefix(project_root()).unwrap_or(file); |
| 186 | + if check { |
| 187 | + panic!( |
| 188 | + "{} was not up-to-date{}", |
| 189 | + file.display(), |
| 190 | + if std::env::var("CI").is_ok() { |
| 191 | + "\n NOTE: run `cargo codegen` locally and commit the updated files\n" |
| 192 | + } else { |
| 193 | + "" |
| 194 | + } |
| 195 | + ); |
| 196 | + } else { |
| 197 | + eprintln!( |
| 198 | + "\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n", |
| 199 | + display_path.display() |
| 200 | + ); |
| 201 | + |
| 202 | + if let Some(parent) = file.parent() { |
| 203 | + let _ = fs::create_dir_all(parent); |
| 204 | + } |
| 205 | + fs::write(file, contents).unwrap(); |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +fn normalize_newlines(s: &str) -> String { |
| 210 | + s.replace("\r\n", "\n") |
| 211 | +} |
0 commit comments