Skip to content

Commit 0cb3e14

Browse files
committed
Auto merge of #16751 - Veykril:codegen, r=Veykril
internal: Move diagnostics docs generation and lint definition generation into xtask/codegen
2 parents 4e8cbf3 + 4bf9cf3 commit 0cb3e14

File tree

17 files changed

+437
-198
lines changed

17 files changed

+437
-198
lines changed

Cargo.lock

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ide-assists/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,5 @@ expect-test = "1.4.0"
3333
test-utils.workspace = true
3434
test-fixture.workspace = true
3535

36-
[features]
37-
in-rust-tree = []
38-
3936
[lints]
4037
workspace = true

crates/ide-db/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,10 @@ line-index.workspace = true
4444

4545
[dev-dependencies]
4646
expect-test = "1.4.0"
47-
oorandom = "11.1.3"
48-
xshell.workspace = true
4947

5048
# local deps
5149
test-utils.workspace = true
5250
test-fixture.workspace = true
53-
sourcegen.workspace = true
5451

5552
[lints]
5653
workspace = true

crates/ide-db/src/generated/lints.rs

Lines changed: 331 additions & 89 deletions
Large diffs are not rendered by default.

crates/ide-db/src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,3 @@ impl SnippetCap {
412412
}
413413
}
414414
}
415-
416-
#[cfg(test)]
417-
mod tests {
418-
mod line_index;
419-
mod sourcegen_lints;
420-
}

crates/ide-db/src/tests/line_index.rs

Lines changed: 0 additions & 49 deletions
This file was deleted.

crates/ide-diagnostics/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ expect-test = "1.4.0"
3333
# local deps
3434
test-utils.workspace = true
3535
test-fixture.workspace = true
36-
sourcegen.workspace = true
37-
38-
[features]
39-
in-rust-tree = []
4036

4137
[lints]
4238
workspace = true

crates/ide-diagnostics/src/tests.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#![allow(clippy::print_stderr)]
2-
#[cfg(not(feature = "in-rust-tree"))]
3-
mod sourcegen;
42

53
use ide_db::{
64
assists::AssistResolveStrategy, base_db::SourceDatabaseExt, LineIndexDatabase, RootDatabase,

crates/ide/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,5 @@ expect-test = "1.4.0"
5151
test-utils.workspace = true
5252
test-fixture.workspace = true
5353

54-
[features]
55-
in-rust-tree = ["ide-assists/in-rust-tree", "ide-diagnostics/in-rust-tree"]
56-
5754
[lints]
5855
workspace = true

crates/rust-analyzer/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ force-always-assert = ["always-assert/force"]
8585
sysroot-abi = []
8686
in-rust-tree = [
8787
"sysroot-abi",
88-
"ide/in-rust-tree",
8988
"syntax/in-rust-tree",
9089
"parser/in-rust-tree",
9190
"hir/in-rust-tree",

lib/line-index/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ edition = "2021"
1010
text-size = "1.1.1"
1111
nohash-hasher = "0.2.0"
1212

13+
[dev-dependencies]
14+
oorandom = "11.1.3"
15+
1316
[lints]
14-
workspace = true
17+
workspace = true

lib/line-index/src/tests.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,56 @@ fn test_to_wide() {
142142
let wide_line_col = line_index.to_wide(WideEncoding::Utf16, line_col.unwrap());
143143
assert_eq!(wide_line_col, Some(WideLineCol { line: 5, col: 4 }));
144144
}
145+
146+
#[test]
147+
fn test_every_chars() {
148+
let text: String = {
149+
let mut chars: Vec<char> = ((0 as char)..char::MAX).collect(); // Neat!
150+
chars.extend("\n".repeat(chars.len() / 16).chars());
151+
let seed = std::hash::Hasher::finish(&std::hash::BuildHasher::build_hasher(
152+
#[allow(clippy::disallowed_types)]
153+
&std::collections::hash_map::RandomState::new(),
154+
));
155+
let mut rng = oorandom::Rand32::new(seed);
156+
let mut rand_index = |i| rng.rand_range(0..i as u32) as usize;
157+
let mut remaining = chars.len() - 1;
158+
while remaining > 0 {
159+
let index = rand_index(remaining);
160+
chars.swap(remaining, index);
161+
remaining -= 1;
162+
}
163+
chars.into_iter().collect()
164+
};
165+
assert!(text.contains('💩')); // Sanity check.
166+
167+
let line_index = LineIndex::new(&text);
168+
169+
let mut lin_col = LineCol { line: 0, col: 0 };
170+
let mut col_utf16 = 0;
171+
let mut col_utf32 = 0;
172+
for (offset, c) in text.char_indices() {
173+
let got_offset = line_index.offset(lin_col).unwrap();
174+
assert_eq!(usize::from(got_offset), offset);
175+
176+
let got_lin_col = line_index.line_col(got_offset);
177+
assert_eq!(got_lin_col, lin_col);
178+
179+
for (enc, col) in [(WideEncoding::Utf16, col_utf16), (WideEncoding::Utf32, col_utf32)] {
180+
let wide_lin_col = line_index.to_wide(enc, lin_col).unwrap();
181+
let got_lin_col = line_index.to_utf8(enc, wide_lin_col).unwrap();
182+
assert_eq!(got_lin_col, lin_col);
183+
assert_eq!(wide_lin_col.col, col)
184+
}
185+
186+
if c == '\n' {
187+
lin_col.line += 1;
188+
lin_col.col = 0;
189+
col_utf16 = 0;
190+
col_utf32 = 0;
191+
} else {
192+
lin_col.col += c.len_utf8() as u32;
193+
col_utf16 += c.len_utf16() as u32;
194+
col_utf32 += 1;
195+
}
196+
}
197+
}

xtask/src/codegen.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,21 @@ use xshell::{cmd, Shell};
88
use crate::{flags, project_root};
99

1010
pub(crate) mod assists_doc_tests;
11+
pub(crate) mod diagnostics_docs;
12+
mod lints;
1113

1214
impl flags::Codegen {
1315
pub(crate) fn run(self, _sh: &Shell) -> anyhow::Result<()> {
1416
match self.codegen_type.unwrap_or_default() {
1517
flags::CodegenType::All => {
18+
diagnostics_docs::generate(self.check);
1619
assists_doc_tests::generate(self.check);
20+
// lints::generate(self.check) Updating clones the rust repo, so don't run it unless
21+
// explicitly asked for
1722
}
1823
flags::CodegenType::AssistsDocTests => assists_doc_tests::generate(self.check),
24+
flags::CodegenType::DiagnosticsDocs => diagnostics_docs::generate(self.check),
25+
flags::CodegenType::LintDefinitions => lints::generate(self.check),
1926
}
2027
Ok(())
2128
}

crates/ide-diagnostics/src/tests/sourcegen.rs renamed to xtask/src/codegen/diagnostics_docs.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22
33
use std::{fmt, fs, io, path::PathBuf};
44

5-
use sourcegen::project_root;
5+
use crate::{
6+
codegen::{add_preamble, list_rust_files, CommentBlock, Location},
7+
project_root,
8+
};
69

7-
#[test]
8-
fn sourcegen_diagnostic_docs() {
10+
pub(crate) fn generate(check: bool) {
911
let diagnostics = Diagnostic::collect().unwrap();
10-
let contents =
11-
diagnostics.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
12-
let contents = sourcegen::add_preamble("sourcegen_diagnostic_docs", contents);
13-
let dst = project_root().join("docs/user/generated_diagnostic.adoc");
14-
fs::write(dst, contents).unwrap();
12+
if !check {
13+
let contents =
14+
diagnostics.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
15+
let contents = add_preamble("sourcegen_diagnostic_docs", contents);
16+
let dst = project_root().join("docs/user/generated_diagnostic.adoc");
17+
fs::write(dst, contents).unwrap();
18+
}
1519
}
1620

1721
#[derive(Debug)]
1822
struct Diagnostic {
1923
id: String,
20-
location: sourcegen::Location,
24+
location: Location,
2125
doc: String,
2226
}
2327

@@ -26,23 +30,23 @@ impl Diagnostic {
2630
let handlers_dir = project_root().join("crates/ide-diagnostics/src/handlers");
2731

2832
let mut res = Vec::new();
29-
for path in sourcegen::list_rust_files(&handlers_dir) {
33+
for path in list_rust_files(&handlers_dir) {
3034
collect_file(&mut res, path)?;
3135
}
3236
res.sort_by(|lhs, rhs| lhs.id.cmp(&rhs.id));
3337
return Ok(res);
3438

3539
fn collect_file(acc: &mut Vec<Diagnostic>, path: PathBuf) -> io::Result<()> {
3640
let text = fs::read_to_string(&path)?;
37-
let comment_blocks = sourcegen::CommentBlock::extract("Diagnostic", &text);
41+
let comment_blocks = CommentBlock::extract("Diagnostic", &text);
3842

3943
for block in comment_blocks {
4044
let id = block.id;
4145
if let Err(msg) = is_valid_diagnostic_name(&id) {
4246
panic!("invalid diagnostic name: {id:?}:\n {msg}")
4347
}
4448
let doc = block.contents.join("\n");
45-
let location = sourcegen::Location { file: path.clone(), line: block.line };
49+
let location = Location { file: path.clone(), line: block.line };
4650
acc.push(Diagnostic { id, location, doc })
4751
}
4852

0 commit comments

Comments
 (0)