Skip to content

Commit b290cd5

Browse files
committed
Add cov_marks to insert_use tests
1 parent 2c8f1b5 commit b290cd5

File tree

7 files changed

+77
-42
lines changed

7 files changed

+77
-42
lines changed

crates/ide_assists/src/handlers/auto_import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
9393

9494
let range = ctx.sema.original_range(&syntax_under_caret).range;
9595
let group_label = group_label(import_assets.import_candidate());
96-
let scope = ImportScope::find_insert_use_container(&syntax_under_caret, &ctx.sema)?;
96+
let scope = ImportScope::find_insert_use_container_with_macros(&syntax_under_caret, &ctx.sema)?;
9797
for import in proposed_imports {
9898
acc.add_group(
9999
&group_label,

crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use hir::{Module, ModuleDef, Name, Variant};
55
use ide_db::{
66
defs::Definition,
77
helpers::{
8-
insert_use::{insert_use, ImportScope},
8+
insert_use::{insert_use, ImportScope, InsertUseConfig},
99
mod_path_to_ast,
1010
},
1111
search::FileReference,
@@ -79,16 +79,8 @@ pub(crate) fn extract_struct_from_enum_variant(
7979
&variant_hir_name,
8080
references,
8181
);
82-
processed.into_iter().for_each(|(segment, node, import)| {
83-
if let Some((scope, path)) = import {
84-
insert_use(&scope, mod_path_to_ast(&path), ctx.config.insert_use);
85-
}
86-
ted::insert_raw(
87-
ted::Position::before(segment.syntax()),
88-
make::path_from_text(&format!("{}", segment)).clone_for_update().syntax(),
89-
);
90-
ted::insert_raw(ted::Position::before(segment.syntax()), make::token(T!['(']));
91-
ted::insert_raw(ted::Position::after(&node), make::token(T![')']));
82+
processed.into_iter().for_each(|(path, node, import)| {
83+
apply_references(ctx.config.insert_use, path, node, import)
9284
});
9385
}
9486
builder.edit_file(ctx.frange.file_id);
@@ -103,21 +95,12 @@ pub(crate) fn extract_struct_from_enum_variant(
10395
&variant_hir_name,
10496
references,
10597
);
106-
processed.into_iter().for_each(|(segment, node, import)| {
107-
if let Some((scope, path)) = import {
108-
insert_use(&scope, mod_path_to_ast(&path), ctx.config.insert_use);
109-
}
110-
ted::insert_raw(
111-
ted::Position::before(segment.syntax()),
112-
make::path_from_text(&format!("{}", segment)).clone_for_update().syntax(),
113-
);
114-
ted::insert_raw(ted::Position::before(segment.syntax()), make::token(T!['(']));
115-
ted::insert_raw(ted::Position::after(&node), make::token(T![')']));
98+
processed.into_iter().for_each(|(path, node, import)| {
99+
apply_references(ctx.config.insert_use, path, node, import)
116100
});
117101
}
118102

119-
let def = create_struct_def(variant_name.clone(), &field_list, enum_ast.visibility())
120-
.unwrap();
103+
let def = create_struct_def(variant_name.clone(), &field_list, enum_ast.visibility());
121104
let start_offset = &variant.parent_enum().syntax().clone();
122105
ted::insert_raw(ted::Position::before(start_offset), def.syntax());
123106
ted::insert_raw(ted::Position::before(start_offset), &make::tokens::blank_line());
@@ -167,7 +150,7 @@ fn create_struct_def(
167150
variant_name: ast::Name,
168151
field_list: &Either<ast::RecordFieldList, ast::TupleFieldList>,
169152
visibility: Option<ast::Visibility>,
170-
) -> Option<ast::Struct> {
153+
) -> ast::Struct {
171154
let pub_vis = Some(make::visibility_pub());
172155
let field_list = match field_list {
173156
Either::Left(field_list) => {
@@ -184,7 +167,7 @@ fn create_struct_def(
184167
.into(),
185168
};
186169

187-
Some(make::struct_(visibility, variant_name, None, field_list).clone_for_update())
170+
make::struct_(visibility, variant_name, None, field_list).clone_for_update()
188171
}
189172

190173
fn update_variant(variant: &ast::Variant) -> Option<()> {
@@ -199,6 +182,23 @@ fn update_variant(variant: &ast::Variant) -> Option<()> {
199182
Some(())
200183
}
201184

185+
fn apply_references(
186+
insert_use_cfg: InsertUseConfig,
187+
segment: ast::PathSegment,
188+
node: SyntaxNode,
189+
import: Option<(ImportScope, hir::ModPath)>,
190+
) {
191+
if let Some((scope, path)) = import {
192+
insert_use(&scope, mod_path_to_ast(&path), insert_use_cfg);
193+
}
194+
ted::insert_raw(
195+
ted::Position::before(segment.syntax()),
196+
make::path_from_text(&format!("{}", segment)).clone_for_update().syntax(),
197+
);
198+
ted::insert_raw(ted::Position::before(segment.syntax()), make::token(T!['(']));
199+
ted::insert_raw(ted::Position::after(&node), make::token(T![')']));
200+
}
201+
202202
fn process_references(
203203
ctx: &AssistContext,
204204
visited_modules: &mut FxHashSet<Module>,
@@ -207,6 +207,8 @@ fn process_references(
207207
variant_hir_name: &Name,
208208
refs: Vec<FileReference>,
209209
) -> Vec<(ast::PathSegment, SyntaxNode, Option<(ImportScope, hir::ModPath)>)> {
210+
// we have to recollect here eagerly as we are about to edit the tree we need to calculate the changes
211+
// and corresponding nodes up front
210212
refs.into_iter()
211213
.flat_map(|reference| {
212214
let (segment, scope_node, module) =
@@ -220,8 +222,7 @@ fn process_references(
220222
if let Some(mut mod_path) = mod_path {
221223
mod_path.pop_segment();
222224
mod_path.push_segment(variant_hir_name.clone());
223-
// uuuh this wont properly work, find_insert_use_container ascends macros so we might a get new syntax node???
224-
let scope = ImportScope::find_insert_use_container(&scope_node, &ctx.sema)?;
225+
let scope = ImportScope::find_insert_use_container(&scope_node)?;
225226
visited_modules.insert(module);
226227
return Some((segment, scope_node, Some((scope, mod_path))));
227228
}

crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub(crate) fn replace_qualified_name_with_use(
3131
}
3232

3333
let target = path.syntax().text_range();
34-
let scope = ImportScope::find_insert_use_container(path.syntax(), &ctx.sema)?;
34+
let scope = ImportScope::find_insert_use_container_with_macros(path.syntax(), &ctx.sema)?;
3535
let syntax = scope.as_syntax_node();
3636
acc.add(
3737
AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite),
@@ -42,15 +42,15 @@ pub(crate) fn replace_qualified_name_with_use(
4242
// affected (that is, all paths inside the node we added the `use` to).
4343
let syntax = builder.make_mut(syntax.clone());
4444
if let Some(ref import_scope) = ImportScope::from(syntax.clone()) {
45-
insert_use(import_scope, path.clone(), ctx.config.insert_use);
45+
shorten_paths(&syntax, &path.clone_for_update());
46+
insert_use(import_scope, path, ctx.config.insert_use);
4647
}
47-
shorten_paths(syntax.clone(), &path.clone_for_update());
4848
},
4949
)
5050
}
5151

5252
/// Adds replacements to `re` that shorten `path` in all descendants of `node`.
53-
fn shorten_paths(node: SyntaxNode, path: &ast::Path) {
53+
fn shorten_paths(node: &SyntaxNode, path: &ast::Path) {
5454
for child in node.children() {
5555
match_ast! {
5656
match child {
@@ -59,14 +59,10 @@ fn shorten_paths(node: SyntaxNode, path: &ast::Path) {
5959
ast::Use(_it) => continue,
6060
// Don't descend into submodules, they don't have the same `use` items in scope.
6161
ast::Module(_it) => continue,
62-
63-
ast::Path(p) => {
64-
match maybe_replace_path(p.clone(), path.clone()) {
65-
Some(()) => {},
66-
None => shorten_paths(p.syntax().clone(), path),
67-
}
62+
ast::Path(p) => if maybe_replace_path(p.clone(), path.clone()).is_none() {
63+
shorten_paths(p.syntax(), path);
6864
},
69-
_ => shorten_paths(child, path),
65+
_ => shorten_paths(&child, path),
7066
}
7167
}
7268
}

crates/ide_completion/src/completions/flyimport.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
132132

133133
let user_input_lowercased = potential_import_name.to_lowercase();
134134
let import_assets = import_assets(ctx, potential_import_name)?;
135-
let import_scope = ImportScope::find_insert_use_container(
135+
let import_scope = ImportScope::find_insert_use_container_with_macros(
136136
position_for_import(ctx, Some(import_assets.import_candidate()))?,
137137
&ctx.sema,
138138
)?;

crates/ide_completion/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ pub fn resolve_completion_edits(
179179
) -> Option<Vec<TextEdit>> {
180180
let ctx = CompletionContext::new(db, position, config)?;
181181
let position_for_import = position_for_import(&ctx, None)?;
182-
let scope = ImportScope::find_insert_use_container(position_for_import, &ctx.sema)?;
182+
let scope = ImportScope::find_insert_use_container_with_macros(position_for_import, &ctx.sema)?;
183183

184184
let current_module = ctx.sema.scope(position_for_import).module()?;
185185
let current_crate = current_module.krate();

crates/ide_db/src/helpers/insert_use.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,18 @@ impl ImportScope {
3838
}
3939

4040
/// Determines the containing syntax node in which to insert a `use` statement affecting `position`.
41-
pub fn find_insert_use_container(
41+
pub fn find_insert_use_container_with_macros(
4242
position: &SyntaxNode,
4343
sema: &Semantics<'_, RootDatabase>,
4444
) -> Option<Self> {
4545
sema.ancestors_with_macros(position.clone()).find_map(Self::from)
4646
}
4747

48+
/// Determines the containing syntax node in which to insert a `use` statement affecting `position`.
49+
pub fn find_insert_use_container(position: &SyntaxNode) -> Option<Self> {
50+
std::iter::successors(Some(position.clone()), SyntaxNode::parent).find_map(Self::from)
51+
}
52+
4853
pub fn as_syntax_node(&self) -> &SyntaxNode {
4954
match self {
5055
ImportScope::File(file) => file.syntax(),
@@ -446,8 +451,10 @@ fn insert_use_(
446451

447452
if !group_imports {
448453
if let Some((_, _, node)) = path_node_iter.last() {
454+
cov_mark::hit!(insert_no_grouping_last);
449455
ted::insert(ted::Position::after(node), use_item.syntax());
450456
} else {
457+
cov_mark::hit!(insert_no_grouping_last2);
451458
ted::insert(ted::Position::first_child_of(scope_syntax), make::tokens::blank_line());
452459
ted::insert(ted::Position::first_child_of(scope_syntax), use_item.syntax());
453460
}
@@ -471,10 +478,12 @@ fn insert_use_(
471478
});
472479

473480
if let Some((.., node)) = post_insert {
481+
cov_mark::hit!(insert_group);
474482
// insert our import before that element
475483
return ted::insert(ted::Position::before(node), use_item.syntax());
476484
}
477485
if let Some(node) = last {
486+
cov_mark::hit!(insert_group_last);
478487
// there is no element after our new import, so append it to the end of the group
479488
return ted::insert(ted::Position::after(node), use_item.syntax());
480489
}
@@ -487,6 +496,7 @@ fn insert_use_(
487496
.inspect(|(.., node)| last = Some(node.clone()))
488497
.find(|(p, ..)| ImportGroup::new(p) > group);
489498
if let Some((.., node)) = post_group {
499+
cov_mark::hit!(insert_group_new_group);
490500
ted::insert(ted::Position::before(&node), use_item.syntax());
491501
if let Some(node) = algo::non_trivia_sibling(node.into(), Direction::Prev) {
492502
ted::insert(ted::Position::after(node), make::tokens::single_newline());
@@ -495,6 +505,7 @@ fn insert_use_(
495505
}
496506
// there is no such group, so append after the last one
497507
if let Some(node) = last {
508+
cov_mark::hit!(insert_group_no_group);
498509
ted::insert(ted::Position::after(&node), use_item.syntax());
499510
ted::insert(ted::Position::after(node), make::tokens::single_newline());
500511
return;
@@ -508,22 +519,26 @@ fn insert_use_(
508519
})
509520
.last()
510521
{
522+
cov_mark::hit!(insert_group_empty_inner_attr);
511523
ted::insert(ted::Position::after(&last_inner_element), use_item.syntax());
512524
ted::insert(ted::Position::after(last_inner_element), make::tokens::single_newline());
513525
return;
514526
}
515527
match scope {
516528
ImportScope::File(_) => {
529+
cov_mark::hit!(insert_group_empty_file);
517530
ted::insert(ted::Position::first_child_of(scope_syntax), make::tokens::blank_line());
518531
ted::insert(ted::Position::first_child_of(scope_syntax), use_item.syntax())
519532
}
520533
// don't insert the imports before the item list's opening curly brace
521534
ImportScope::Module(item_list) => match item_list.l_curly_token() {
522535
Some(b) => {
536+
cov_mark::hit!(insert_group_empty_module);
523537
ted::insert(ted::Position::after(&b), make::tokens::single_newline());
524538
ted::insert(ted::Position::after(&b), use_item.syntax());
525539
}
526540
None => {
541+
// This should never happens, broken module syntax node
527542
ted::insert(
528543
ted::Position::first_child_of(scope_syntax),
529544
make::tokens::blank_line(),

crates/ide_db/src/helpers/insert_use/tests.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use test_utils::assert_eq_text;
55

66
#[test]
77
fn insert_not_group() {
8+
cov_mark::check!(insert_no_grouping_last);
89
check(
910
"use external_crate2::bar::A",
1011
r"
@@ -26,6 +27,21 @@ use external_crate2::bar::A;",
2627
);
2728
}
2829

30+
#[test]
31+
fn insert_not_group_empty() {
32+
cov_mark::check!(insert_no_grouping_last2);
33+
check(
34+
"use external_crate2::bar::A",
35+
r"",
36+
r"use external_crate2::bar::A;
37+
38+
",
39+
None,
40+
false,
41+
false,
42+
);
43+
}
44+
2945
#[test]
3046
fn insert_existing() {
3147
check_full("std::fs", "use std::fs;", "use std::fs;")
@@ -65,6 +81,7 @@ fn insert_start_indent() {
6581

6682
#[test]
6783
fn insert_middle() {
84+
cov_mark::check!(insert_group);
6885
check_none(
6986
"std::bar::EE",
7087
r"
@@ -101,6 +118,7 @@ fn insert_middle_indent() {
101118

102119
#[test]
103120
fn insert_end() {
121+
cov_mark::check!(insert_group_last);
104122
check_none(
105123
"std::bar::ZZ",
106124
r"
@@ -199,6 +217,7 @@ fn insert_first_matching_group() {
199217

200218
#[test]
201219
fn insert_missing_group_std() {
220+
cov_mark::check!(insert_group_new_group);
202221
check_none(
203222
"std::fmt",
204223
r"
@@ -214,6 +233,7 @@ fn insert_missing_group_std() {
214233

215234
#[test]
216235
fn insert_missing_group_self() {
236+
cov_mark::check!(insert_group_no_group);
217237
check_none(
218238
"self::fmt",
219239
r"
@@ -240,6 +260,7 @@ fn main() {}",
240260

241261
#[test]
242262
fn insert_empty_file() {
263+
cov_mark::check!(insert_group_empty_file);
243264
// empty files will get two trailing newlines
244265
// this is due to the test case insert_no_imports above
245266
check_full(
@@ -253,6 +274,7 @@ fn insert_empty_file() {
253274

254275
#[test]
255276
fn insert_empty_module() {
277+
cov_mark::check!(insert_group_empty_module);
256278
check(
257279
"foo::bar",
258280
"mod x {}",
@@ -267,6 +289,7 @@ fn insert_empty_module() {
267289

268290
#[test]
269291
fn insert_after_inner_attr() {
292+
cov_mark::check!(insert_group_empty_inner_attr);
270293
check_full(
271294
"foo::bar",
272295
r"#![allow(unused_imports)]",

0 commit comments

Comments
 (0)