From b290cd578260f307e872a95f971e5a7c656a80ed Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 20 Apr 2021 19:28:18 +0200 Subject: Add cov_marks to insert_use tests --- crates/ide_assists/src/handlers/auto_import.rs | 2 +- .../handlers/extract_struct_from_enum_variant.rs | 55 +++++++++++----------- .../handlers/replace_qualified_name_with_use.rs | 18 +++---- 3 files changed, 36 insertions(+), 39 deletions(-) (limited to 'crates/ide_assists') diff --git a/crates/ide_assists/src/handlers/auto_import.rs b/crates/ide_assists/src/handlers/auto_import.rs index 6db2d2edd..a454a2af3 100644 --- a/crates/ide_assists/src/handlers/auto_import.rs +++ b/crates/ide_assists/src/handlers/auto_import.rs @@ -93,7 +93,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> let range = ctx.sema.original_range(&syntax_under_caret).range; let group_label = group_label(import_assets.import_candidate()); - let scope = ImportScope::find_insert_use_container(&syntax_under_caret, &ctx.sema)?; + let scope = ImportScope::find_insert_use_container_with_macros(&syntax_under_caret, &ctx.sema)?; for import in proposed_imports { acc.add_group( &group_label, diff --git a/crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs index 1f800f82b..66f274fa7 100644 --- a/crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs +++ b/crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs @@ -5,7 +5,7 @@ use hir::{Module, ModuleDef, Name, Variant}; use ide_db::{ defs::Definition, helpers::{ - insert_use::{insert_use, ImportScope}, + insert_use::{insert_use, ImportScope, InsertUseConfig}, mod_path_to_ast, }, search::FileReference, @@ -79,16 +79,8 @@ pub(crate) fn extract_struct_from_enum_variant( &variant_hir_name, references, ); - processed.into_iter().for_each(|(segment, node, import)| { - if let Some((scope, path)) = import { - insert_use(&scope, mod_path_to_ast(&path), ctx.config.insert_use); - } - ted::insert_raw( - ted::Position::before(segment.syntax()), - make::path_from_text(&format!("{}", segment)).clone_for_update().syntax(), - ); - ted::insert_raw(ted::Position::before(segment.syntax()), make::token(T!['('])); - ted::insert_raw(ted::Position::after(&node), make::token(T![')'])); + processed.into_iter().for_each(|(path, node, import)| { + apply_references(ctx.config.insert_use, path, node, import) }); } builder.edit_file(ctx.frange.file_id); @@ -103,21 +95,12 @@ pub(crate) fn extract_struct_from_enum_variant( &variant_hir_name, references, ); - processed.into_iter().for_each(|(segment, node, import)| { - if let Some((scope, path)) = import { - insert_use(&scope, mod_path_to_ast(&path), ctx.config.insert_use); - } - ted::insert_raw( - ted::Position::before(segment.syntax()), - make::path_from_text(&format!("{}", segment)).clone_for_update().syntax(), - ); - ted::insert_raw(ted::Position::before(segment.syntax()), make::token(T!['('])); - ted::insert_raw(ted::Position::after(&node), make::token(T![')'])); + processed.into_iter().for_each(|(path, node, import)| { + apply_references(ctx.config.insert_use, path, node, import) }); } - let def = create_struct_def(variant_name.clone(), &field_list, enum_ast.visibility()) - .unwrap(); + let def = create_struct_def(variant_name.clone(), &field_list, enum_ast.visibility()); let start_offset = &variant.parent_enum().syntax().clone(); ted::insert_raw(ted::Position::before(start_offset), def.syntax()); ted::insert_raw(ted::Position::before(start_offset), &make::tokens::blank_line()); @@ -167,7 +150,7 @@ fn create_struct_def( variant_name: ast::Name, field_list: &Either, visibility: Option, -) -> Option { +) -> ast::Struct { let pub_vis = Some(make::visibility_pub()); let field_list = match field_list { Either::Left(field_list) => { @@ -184,7 +167,7 @@ fn create_struct_def( .into(), }; - Some(make::struct_(visibility, variant_name, None, field_list).clone_for_update()) + make::struct_(visibility, variant_name, None, field_list).clone_for_update() } fn update_variant(variant: &ast::Variant) -> Option<()> { @@ -199,6 +182,23 @@ fn update_variant(variant: &ast::Variant) -> Option<()> { Some(()) } +fn apply_references( + insert_use_cfg: InsertUseConfig, + segment: ast::PathSegment, + node: SyntaxNode, + import: Option<(ImportScope, hir::ModPath)>, +) { + if let Some((scope, path)) = import { + insert_use(&scope, mod_path_to_ast(&path), insert_use_cfg); + } + ted::insert_raw( + ted::Position::before(segment.syntax()), + make::path_from_text(&format!("{}", segment)).clone_for_update().syntax(), + ); + ted::insert_raw(ted::Position::before(segment.syntax()), make::token(T!['('])); + ted::insert_raw(ted::Position::after(&node), make::token(T![')'])); +} + fn process_references( ctx: &AssistContext, visited_modules: &mut FxHashSet, @@ -207,6 +207,8 @@ fn process_references( variant_hir_name: &Name, refs: Vec, ) -> Vec<(ast::PathSegment, SyntaxNode, Option<(ImportScope, hir::ModPath)>)> { + // we have to recollect here eagerly as we are about to edit the tree we need to calculate the changes + // and corresponding nodes up front refs.into_iter() .flat_map(|reference| { let (segment, scope_node, module) = @@ -220,8 +222,7 @@ fn process_references( if let Some(mut mod_path) = mod_path { mod_path.pop_segment(); mod_path.push_segment(variant_hir_name.clone()); - // uuuh this wont properly work, find_insert_use_container ascends macros so we might a get new syntax node??? - let scope = ImportScope::find_insert_use_container(&scope_node, &ctx.sema)?; + let scope = ImportScope::find_insert_use_container(&scope_node)?; visited_modules.insert(module); return Some((segment, scope_node, Some((scope, mod_path)))); } diff --git a/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs b/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs index 2f2306fcc..99ba79860 100644 --- a/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs @@ -31,7 +31,7 @@ pub(crate) fn replace_qualified_name_with_use( } let target = path.syntax().text_range(); - let scope = ImportScope::find_insert_use_container(path.syntax(), &ctx.sema)?; + let scope = ImportScope::find_insert_use_container_with_macros(path.syntax(), &ctx.sema)?; let syntax = scope.as_syntax_node(); acc.add( AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite), @@ -42,15 +42,15 @@ pub(crate) fn replace_qualified_name_with_use( // affected (that is, all paths inside the node we added the `use` to). let syntax = builder.make_mut(syntax.clone()); if let Some(ref import_scope) = ImportScope::from(syntax.clone()) { - insert_use(import_scope, path.clone(), ctx.config.insert_use); + shorten_paths(&syntax, &path.clone_for_update()); + insert_use(import_scope, path, ctx.config.insert_use); } - shorten_paths(syntax.clone(), &path.clone_for_update()); }, ) } /// Adds replacements to `re` that shorten `path` in all descendants of `node`. -fn shorten_paths(node: SyntaxNode, path: &ast::Path) { +fn shorten_paths(node: &SyntaxNode, path: &ast::Path) { for child in node.children() { match_ast! { match child { @@ -59,14 +59,10 @@ fn shorten_paths(node: SyntaxNode, path: &ast::Path) { ast::Use(_it) => continue, // Don't descend into submodules, they don't have the same `use` items in scope. ast::Module(_it) => continue, - - ast::Path(p) => { - match maybe_replace_path(p.clone(), path.clone()) { - Some(()) => {}, - None => shorten_paths(p.syntax().clone(), path), - } + ast::Path(p) => if maybe_replace_path(p.clone(), path.clone()).is_none() { + shorten_paths(p.syntax(), path); }, - _ => shorten_paths(child, path), + _ => shorten_paths(&child, path), } } } -- cgit v1.2.3