Unnamed repository; edit this file 'description' to name the repository.
| -rw-r--r-- | crates/ide-completion/src/lib.rs | 17 | ||||
| -rw-r--r-- | crates/syntax/src/ast/syntax_factory/constructors.rs | 6 |
2 files changed, 15 insertions, 8 deletions
diff --git a/crates/ide-completion/src/lib.rs b/crates/ide-completion/src/lib.rs index 66ecb790a0..4ca3257c5c 100644 --- a/crates/ide-completion/src/lib.rs +++ b/crates/ide-completion/src/lib.rs @@ -23,7 +23,7 @@ use ide_db::{ syntax_helpers::tree_diff::diff, text_edit::TextEdit, }; -use syntax::ast::make; +use syntax::{AstNode, syntax_editor::SyntaxEditor}; use crate::{ completions::Completions, @@ -296,23 +296,26 @@ pub fn resolve_completion_edits( let current_module = sema.scope(position_for_import)?.module(); let current_crate = current_module.krate(db); let current_edition = current_crate.edition(db); - let new_ast = scope.clone_for_update(); let mut import_insert = TextEdit::builder(); + let (editor, _) = SyntaxEditor::new(original_file.syntax().clone()); + let make = editor.make(); imports.into_iter().for_each(|import| { - let full_path = make::path_from_text_with_edition(&import.path, current_edition); + let full_path = make.path_from_text_with_edition(&import.path, current_edition); if import.as_underscore { - insert_use::insert_use_as_alias( - &new_ast, + insert_use::insert_use_as_alias_with_editor( + &scope, full_path, &config.insert_use, current_edition, + &editor, ); } else { - insert_use::insert_use(&new_ast, full_path, &config.insert_use); + insert_use::insert_use_with_editor(&scope, full_path, &config.insert_use, &editor); } }); - diff(scope.as_syntax_node(), new_ast.as_syntax_node()).into_text_edit(&mut import_insert); + let edit = editor.finish(); + diff(edit.old_root(), edit.new_root()).into_text_edit(&mut import_insert); Some(vec![import_insert.finish()]) } diff --git a/crates/syntax/src/ast/syntax_factory/constructors.rs b/crates/syntax/src/ast/syntax_factory/constructors.rs index 5a01580c56..bebf595f00 100644 --- a/crates/syntax/src/ast/syntax_factory/constructors.rs +++ b/crates/syntax/src/ast/syntax_factory/constructors.rs @@ -2,7 +2,7 @@ use either::Either; use crate::{ - AstNode, NodeOrToken, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, + AstNode, Edition, NodeOrToken, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, ast::{ self, HasArgList, HasAttrs, HasGenericArgs, HasGenericParams, HasLoopBody, HasName, HasTypeBounds, HasVisibility, Lifetime, Param, RangeItem, make, @@ -131,6 +131,10 @@ impl SyntaxFactory { make::path_from_text(text).clone_for_update() } + pub fn path_from_text_with_edition(&self, text: &str, edition: Edition) -> ast::Path { + make::path_from_text_with_edition(text, edition).clone_for_update() + } + pub fn path_concat(&self, first: ast::Path, second: ast::Path) -> ast::Path { make::path_concat(first, second).clone_for_update() } |