2021-07-03 14:11:03 -05:00
|
|
|
//! rust-analyzer relies heavily on source code generation.
|
|
|
|
//!
|
|
|
|
//! Things like feature documentation or assist tests are implemented by
|
|
|
|
//! processing rust-analyzer's own source code and generating the appropriate
|
|
|
|
//! output. See `sourcegen_` tests in various crates.
|
|
|
|
//!
|
|
|
|
//! This crate contains utilities to make this kind of source-gen easy.
|
|
|
|
|
2022-07-20 07:59:42 -05:00
|
|
|
#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
|
|
|
|
|
2021-07-03 14:11:03 -05:00
|
|
|
use std::{
|
|
|
|
fmt, fs, mem,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
|
|
|
|
2022-03-13 16:20:51 -05:00
|
|
|
use xshell::{cmd, Shell};
|
2021-07-03 14:11:03 -05:00
|
|
|
|
|
|
|
pub fn list_rust_files(dir: &Path) -> Vec<PathBuf> {
|
|
|
|
let mut res = list_files(dir);
|
|
|
|
res.retain(|it| {
|
|
|
|
it.file_name().unwrap_or_default().to_str().unwrap_or_default().ends_with(".rs")
|
|
|
|
});
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn list_files(dir: &Path) -> Vec<PathBuf> {
|
|
|
|
let mut res = Vec::new();
|
|
|
|
let mut work = vec![dir.to_path_buf()];
|
|
|
|
while let Some(dir) = work.pop() {
|
|
|
|
for entry in dir.read_dir().unwrap() {
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
let file_type = entry.file_type().unwrap();
|
|
|
|
let path = entry.path();
|
|
|
|
let is_hidden =
|
|
|
|
path.file_name().unwrap_or_default().to_str().unwrap_or_default().starts_with('.');
|
|
|
|
if !is_hidden {
|
|
|
|
if file_type.is_dir() {
|
2021-10-03 07:39:43 -05:00
|
|
|
work.push(path);
|
2021-07-03 14:11:03 -05:00
|
|
|
} else if file_type.is_file() {
|
2021-10-03 07:39:43 -05:00
|
|
|
res.push(path);
|
2021-07-03 14:11:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2021-09-13 04:34:05 -05:00
|
|
|
#[derive(Clone)]
|
2021-07-03 14:11:03 -05:00
|
|
|
pub struct CommentBlock {
|
|
|
|
pub id: String,
|
|
|
|
pub line: usize,
|
|
|
|
pub contents: Vec<String>,
|
2021-09-13 05:43:13 -05:00
|
|
|
is_doc: bool,
|
2021-07-03 14:11:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CommentBlock {
|
|
|
|
pub fn extract(tag: &str, text: &str) -> Vec<CommentBlock> {
|
|
|
|
assert!(tag.starts_with(char::is_uppercase));
|
|
|
|
|
2022-12-23 12:42:58 -06:00
|
|
|
let tag = format!("{tag}:");
|
2023-03-31 16:41:40 -05:00
|
|
|
let mut blocks = CommentBlock::extract_untagged(text);
|
|
|
|
blocks.retain_mut(|block| {
|
|
|
|
let first = block.contents.remove(0);
|
|
|
|
let Some(id) = first.strip_prefix(&tag) else { return false; };
|
2021-09-13 05:43:13 -05:00
|
|
|
|
2023-03-31 16:41:40 -05:00
|
|
|
if block.is_doc {
|
|
|
|
panic!("Use plain (non-doc) comments with tags like {tag}:\n {first}");
|
|
|
|
}
|
|
|
|
|
|
|
|
block.id = id.trim().to_string();
|
|
|
|
true
|
|
|
|
});
|
|
|
|
blocks
|
2021-07-03 14:11:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn extract_untagged(text: &str) -> Vec<CommentBlock> {
|
2021-09-13 04:34:05 -05:00
|
|
|
let mut res = Vec::new();
|
2021-07-03 14:11:03 -05:00
|
|
|
|
2021-09-13 04:34:05 -05:00
|
|
|
let lines = text.lines().map(str::trim_start);
|
2021-07-03 14:11:03 -05:00
|
|
|
|
2021-09-13 05:43:13 -05:00
|
|
|
let dummy_block =
|
|
|
|
CommentBlock { id: String::new(), line: 0, contents: Vec::new(), is_doc: false };
|
2021-09-13 04:34:05 -05:00
|
|
|
let mut block = dummy_block.clone();
|
|
|
|
for (line_num, line) in lines.enumerate() {
|
2021-09-13 05:35:31 -05:00
|
|
|
match line.strip_prefix("//") {
|
|
|
|
Some(mut contents) => {
|
2021-09-13 05:43:13 -05:00
|
|
|
if let Some('/' | '!') = contents.chars().next() {
|
|
|
|
contents = &contents[1..];
|
|
|
|
block.is_doc = true;
|
|
|
|
}
|
2021-09-13 05:35:31 -05:00
|
|
|
if let Some(' ') = contents.chars().next() {
|
|
|
|
contents = &contents[1..];
|
|
|
|
}
|
|
|
|
block.contents.push(contents.to_string());
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
if !block.contents.is_empty() {
|
|
|
|
let block = mem::replace(&mut block, dummy_block.clone());
|
|
|
|
res.push(block);
|
|
|
|
}
|
|
|
|
block.line = line_num + 2;
|
2021-09-13 04:34:05 -05:00
|
|
|
}
|
2021-07-03 14:11:03 -05:00
|
|
|
}
|
|
|
|
}
|
2021-09-13 04:34:05 -05:00
|
|
|
if !block.contents.is_empty() {
|
2021-10-03 07:39:43 -05:00
|
|
|
res.push(block);
|
2021-09-13 04:34:05 -05:00
|
|
|
}
|
|
|
|
res
|
2021-07-03 14:11:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Location {
|
|
|
|
pub file: PathBuf,
|
|
|
|
pub line: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Location {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2022-12-30 02:05:03 -06:00
|
|
|
let path = self.file.strip_prefix(project_root()).unwrap().display().to_string();
|
2021-07-03 14:11:03 -05:00
|
|
|
let path = path.replace('\\', "/");
|
|
|
|
let name = self.file.file_name().unwrap();
|
|
|
|
write!(
|
|
|
|
f,
|
2022-07-08 08:44:49 -05:00
|
|
|
"https://github.com/rust-lang/rust-analyzer/blob/master/{}#L{}[{}]",
|
2021-07-03 14:11:03 -05:00
|
|
|
path,
|
|
|
|
self.line,
|
|
|
|
name.to_str().unwrap()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-13 16:20:51 -05:00
|
|
|
fn ensure_rustfmt(sh: &Shell) {
|
2022-08-06 09:12:13 -05:00
|
|
|
let version = cmd!(sh, "rustup run stable rustfmt --version").read().unwrap_or_default();
|
2021-07-03 14:11:03 -05:00
|
|
|
if !version.contains("stable") {
|
|
|
|
panic!(
|
|
|
|
"Failed to run rustfmt from toolchain 'stable'. \
|
|
|
|
Please run `rustup component add rustfmt --toolchain stable` to install it.",
|
2021-10-03 07:39:43 -05:00
|
|
|
);
|
2021-07-03 14:11:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reformat(text: String) -> String {
|
2022-03-13 16:20:51 -05:00
|
|
|
let sh = Shell::new().unwrap();
|
|
|
|
ensure_rustfmt(&sh);
|
2021-07-03 14:11:03 -05:00
|
|
|
let rustfmt_toml = project_root().join("rustfmt.toml");
|
2022-08-06 09:12:13 -05:00
|
|
|
let mut stdout = cmd!(
|
|
|
|
sh,
|
|
|
|
"rustup run stable rustfmt --config-path {rustfmt_toml} --config fn_single_line=true"
|
|
|
|
)
|
|
|
|
.stdin(text)
|
|
|
|
.read()
|
|
|
|
.unwrap();
|
2021-07-03 14:11:03 -05:00
|
|
|
if !stdout.ends_with('\n') {
|
|
|
|
stdout.push('\n');
|
|
|
|
}
|
|
|
|
stdout
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_preamble(generator: &'static str, mut text: String) -> String {
|
2022-12-23 12:42:58 -06:00
|
|
|
let preamble = format!("//! Generated by `{generator}`, do not edit by hand.\n\n");
|
2021-07-03 14:11:03 -05:00
|
|
|
text.insert_str(0, &preamble);
|
|
|
|
text
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that the `file` has the specified `contents`. If that is not the
|
|
|
|
/// case, updates the file and then fails the test.
|
|
|
|
pub fn ensure_file_contents(file: &Path, contents: &str) {
|
|
|
|
if let Ok(old_contents) = fs::read_to_string(file) {
|
|
|
|
if normalize_newlines(&old_contents) == normalize_newlines(contents) {
|
|
|
|
// File is already up to date.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 02:05:03 -06:00
|
|
|
let display_path = file.strip_prefix(project_root()).unwrap_or(file);
|
2021-07-03 14:11:03 -05:00
|
|
|
eprintln!(
|
|
|
|
"\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n",
|
|
|
|
display_path.display()
|
|
|
|
);
|
|
|
|
if std::env::var("CI").is_ok() {
|
|
|
|
eprintln!(" NOTE: run `cargo test` locally and commit the updated files\n");
|
|
|
|
}
|
|
|
|
if let Some(parent) = file.parent() {
|
|
|
|
let _ = fs::create_dir_all(parent);
|
|
|
|
}
|
|
|
|
fs::write(file, contents).unwrap();
|
2021-10-03 07:39:43 -05:00
|
|
|
panic!("some file was not up to date and has been updated, simply re-run the tests");
|
2021-07-03 14:11:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn normalize_newlines(s: &str) -> String {
|
|
|
|
s.replace("\r\n", "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn project_root() -> PathBuf {
|
|
|
|
let dir = env!("CARGO_MANIFEST_DIR");
|
2021-07-13 07:02:29 -05:00
|
|
|
let res = PathBuf::from(dir).parent().unwrap().parent().unwrap().to_owned();
|
2022-07-05 08:18:00 -05:00
|
|
|
assert!(res.join("triagebot.toml").exists());
|
2021-07-13 07:02:29 -05:00
|
|
|
res
|
2021-07-03 14:11:03 -05:00
|
|
|
}
|