Skip to content

Commit 46027e1

Browse files
bors[bot]matklad
andauthored
Merge #3035
3035: Doctest autoimport r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 895cdb5 + 755077e commit 46027e1

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

crates/ra_assists/src/assists/auto_import.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ use ra_ide_db::imports_locator::ImportsLocatorIde;
1818
// fn main() {
1919
// let map = HashMap<|>::new();
2020
// }
21+
// # pub mod std { pub mod collections { pub struct HashMap { } } }
2122
// ```
2223
// ->
2324
// ```
2425
// use std::collections::HashMap;
2526
//
2627
// fn main() {
27-
// let map = HashMap<|>::new();
28+
// let map = HashMap::new();
2829
// }
30+
// # pub mod std { pub mod collections { pub struct HashMap { } } }
2931
// ```
3032
pub(crate) fn auto_import(ctx: AssistCtx) -> Option<Assist> {
3133
let path_to_import: ast::Path = ctx.find_node_at_offset()?;

crates/ra_assists/src/doc_tests.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,12 @@
55
66
mod generated;
77

8-
use ra_db::{fixture::WithFixture, FileRange};
8+
use ra_db::FileRange;
99
use test_utils::{assert_eq_text, extract_range_or_offset};
1010

11-
use ra_ide_db::RootDatabase;
12-
1311
fn check(assist_id: &str, before: &str, after: &str) {
14-
// FIXME we cannot get the imports search functionality here yet, but still need to generate a test and a doc for an assist
15-
if assist_id == "auto_import" {
16-
return;
17-
}
1812
let (selection, before) = extract_range_or_offset(before);
19-
let (db, file_id) = RootDatabase::with_single_file(&before);
13+
let (db, file_id) = crate::helpers::with_single_file(&before);
2014
let frange = FileRange { file_id, range: selection.into() };
2115

2216
let assist = crate::assists(&db, frange)

crates/ra_assists/src/doc_tests/generated.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,15 @@ fn doctest_auto_import() {
222222
fn main() {
223223
let map = HashMap<|>::new();
224224
}
225+
pub mod std { pub mod collections { pub struct HashMap { } } }
225226
"#####,
226227
r#####"
227228
use std::collections::HashMap;
228229
229230
fn main() {
230-
let map = HashMap<|>::new();
231+
let map = HashMap::new();
231232
}
233+
pub mod std { pub mod collections { pub struct HashMap { } } }
232234
"#####,
233235
)
234236
}

docs/user/assists.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ fn main() {
223223
use std::collections::HashMap;
224224

225225
fn main() {
226-
let map = HashMap::new();
226+
let map = HashMap::new();
227227
}
228228
```
229229

xtask/src/codegen/gen_assists_docs.rs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@ struct Assist {
2020
after: String,
2121
}
2222

23+
fn hide_hash_comments(text: &str) -> String {
24+
text.split('\n') // want final newline
25+
.filter(|&it| !(it.starts_with("# ") || it == "#"))
26+
.map(|it| format!("{}\n", it))
27+
.collect()
28+
}
29+
30+
fn reveal_hash_comments(text: &str) -> String {
31+
text.split('\n') // want final newline
32+
.map(|it| {
33+
if it.starts_with("# ") {
34+
&it[2..]
35+
} else if it == "#" {
36+
""
37+
} else {
38+
it
39+
}
40+
})
41+
.map(|it| format!("{}\n", it))
42+
.collect()
43+
}
44+
2345
fn collect_assists() -> Result<Vec<Assist>> {
2446
let mut res = Vec::new();
2547
for entry in fs::read_dir(project_root().join(codegen::ASSISTS_DIR))? {
@@ -91,13 +113,14 @@ fn doctest_{}() {{
91113
check(
92114
"{}",
93115
r#####"
94-
{}
95-
"#####, r#####"
96-
{}
97-
"#####)
116+
{}"#####, r#####"
117+
{}"#####)
98118
}}
99119
"######,
100-
assist.id, assist.id, assist.before, assist.after
120+
assist.id,
121+
assist.id,
122+
reveal_hash_comments(&assist.before),
123+
reveal_hash_comments(&assist.after)
101124
);
102125

103126
buf.push_str(&test)
@@ -123,12 +146,13 @@ fn generate_docs(assists: &[Assist], mode: Mode) -> Result<()> {
123146
```rust
124147
// BEFORE
125148
{}
126-
127149
// AFTER
128-
{}
129-
```
150+
{}```
130151
",
131-
assist.id, assist.doc, before, after
152+
assist.id,
153+
assist.doc,
154+
hide_hash_comments(&before),
155+
hide_hash_comments(&after)
132156
);
133157
buf.push_str(&docs);
134158
}

0 commit comments

Comments
 (0)