Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #22125 from Shourya742/2026-04-21-migrate-replace-qualified-name-with-use
migrate replace qualified name with use to SyntaxEditor
| -rw-r--r-- | crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs | 23 | ||||
| -rw-r--r-- | crates/ide-db/src/helpers.rs | 27 | ||||
| -rw-r--r-- | crates/syntax/src/ast/syntax_factory/constructors.rs | 12 |
3 files changed, 50 insertions, 12 deletions
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 eebe93f005..92fd119184 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 @@ -1,11 +1,11 @@ use hir::{AsAssocItem, ModuleDef, PathResolution}; use ide_db::{ - helpers::mod_path_to_ast, - imports::insert_use::{ImportScope, insert_use}, + helpers::mod_path_to_ast_with_factory, + imports::insert_use::{ImportScope, insert_use_with_editor}, }; use syntax::{ AstNode, Edition, SyntaxNode, - ast::{self, HasGenericArgs, make}, + ast::{self, HasGenericArgs}, match_ast, syntax_editor::SyntaxEditor, }; @@ -75,7 +75,7 @@ pub(crate) fn replace_qualified_name_with_use( let scope_node = scope.as_syntax_node(); let editor = builder.make_editor(scope_node); shorten_paths(&editor, scope_node, &original_path); - builder.add_file_edits(ctx.vfs_file_id(), editor); + let make = editor.make(); let path = drop_generic_args(&original_path); let edition = ctx .sema @@ -83,13 +83,14 @@ pub(crate) fn replace_qualified_name_with_use( .map(|semantics_scope| semantics_scope.krate().edition(ctx.db())) .unwrap_or(Edition::CURRENT); // stick the found import in front of the to be replaced path - let path = - match path_to_qualifier.and_then(|it| mod_path_to_ast(&it, edition).qualifier()) { - Some(qualifier) => make::path_concat(qualifier, path), - None => path, - }; - let scope = builder.make_import_scope_mut(scope); - insert_use(&scope, path, &ctx.config.insert_use); + let path = match path_to_qualifier + .and_then(|it| mod_path_to_ast_with_factory(make, &it, edition).qualifier()) + { + Some(qualifier) => make.path_concat(qualifier, path), + None => path, + }; + insert_use_with_editor(&scope, path, &ctx.config.insert_use, &editor); + builder.add_file_edits(ctx.vfs_file_id(), editor); }, ) } diff --git a/crates/ide-db/src/helpers.rs b/crates/ide-db/src/helpers.rs index 08cf1eeed3..838aac2283 100644 --- a/crates/ide-db/src/helpers.rs +++ b/crates/ide-db/src/helpers.rs @@ -7,7 +7,7 @@ use hir::{Crate, ItemInNs, ModuleDef, Name, Semantics}; use span::{Edition, FileId}; use syntax::{ AstToken, SyntaxKind, SyntaxToken, ToSmolStr, TokenAtOffset, - ast::{self, make}, + ast::{self, make, syntax_factory::SyntaxFactory}, }; use crate::{ @@ -57,6 +57,31 @@ pub fn mod_path_to_ast(path: &hir::ModPath, edition: Edition) -> ast::Path { make::path_from_segments(segments, is_abs) } +pub fn mod_path_to_ast_with_factory( + make: &SyntaxFactory, + path: &hir::ModPath, + edition: Edition, +) -> ast::Path { + let _p = tracing::info_span!("mod_path_to_ast").entered(); + + let mut segments = Vec::new(); + let mut is_abs = false; + match path.kind { + hir::PathKind::Plain => {} + hir::PathKind::SELF => segments.push(make.path_segment_self()), + hir::PathKind::Super(n) => segments.extend((0..n).map(|_| make.path_segment_super())), + hir::PathKind::DollarCrate(_) | hir::PathKind::Crate => { + segments.push(make.path_segment_crate()) + } + hir::PathKind::Abs => is_abs = true, + } + + segments.extend(path.segments().iter().map(|segment| { + make.path_segment(make.name_ref(&segment.display_no_db(edition).to_smolstr())) + })); + make.path_from_segments(segments, is_abs) +} + /// Iterates all `ModuleDef`s and `Impl` blocks of the given file. pub fn visit_file_defs( sema: &Semantics<'_, RootDatabase>, diff --git a/crates/syntax/src/ast/syntax_factory/constructors.rs b/crates/syntax/src/ast/syntax_factory/constructors.rs index 0f3b3d301c..885d48b064 100644 --- a/crates/syntax/src/ast/syntax_factory/constructors.rs +++ b/crates/syntax/src/ast/syntax_factory/constructors.rs @@ -491,6 +491,18 @@ impl SyntaxFactory { ast } + pub fn path_segment_self(&self) -> ast::PathSegment { + make::path_segment_self().clone_for_update() + } + + pub fn path_segment_super(&self) -> ast::PathSegment { + make::path_segment_super().clone_for_update() + } + + pub fn path_segment_crate(&self) -> ast::PathSegment { + make::path_segment_crate().clone_for_update() + } + pub fn generic_ty_path_segment( &self, name_ref: ast::NameRef, |