-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Migrate merge imports to editor #22351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Shourya742
wants to merge
7
commits into
rust-lang:master
Choose a base branch
from
Shourya742:2026-05-10-migrate-merge-imports-to-editor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a201a94
remove all the ted based and editor based API's from edit_in_place
Shourya742 77f1b12
move all the editor variant of API's from edit-in-place to edit
Shourya742 88b2698
update test to use editor variant of try_merge_imports
Shourya742 b73872b
update insert_use to use editor variant of merge_import methods
Shourya742 06957e4
update normalize_import to use editor variant of merge_import methods
Shourya742 5180a80
update merge_import to use editor variant of merge_import methods
Shourya742 935f981
migrate merge_import to syntax_editor
Shourya742 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,10 +1,12 @@ | ||||||
| use either::Either; | ||||||
| use ide_db::imports::{ | ||||||
| insert_use::{ImportGranularity, InsertUseConfig}, | ||||||
| merge_imports::{MergeBehavior, try_merge_imports, try_merge_trees}, | ||||||
| }; | ||||||
| use syntax::{ | ||||||
| AstNode, SyntaxElement, SyntaxNode, algo::neighbor, ast, match_ast, syntax_editor::Removable, | ||||||
| AstNode, SyntaxElement, | ||||||
| algo::neighbor, | ||||||
| ast, match_ast, | ||||||
| syntax_editor::{Removable, SyntaxEditor}, | ||||||
| }; | ||||||
|
|
||||||
| use crate::{ | ||||||
|
|
@@ -13,8 +15,6 @@ use crate::{ | |||||
| utils::next_prev, | ||||||
| }; | ||||||
|
|
||||||
| use Edit::*; | ||||||
|
|
||||||
| // Assist: merge_imports | ||||||
| // | ||||||
| // Merges neighbor imports with a common prefix. | ||||||
|
|
@@ -28,16 +28,19 @@ use Edit::*; | |||||
| // use std::{fmt::Formatter, io}; | ||||||
| // ``` | ||||||
| pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext<'_, '_>) -> Option<()> { | ||||||
| let (target, edits) = if ctx.has_empty_selection() { | ||||||
| let (target, editor) = if ctx.has_empty_selection() { | ||||||
| // Merge a neighbor | ||||||
| cov_mark::hit!(merge_with_use_item_neighbors); | ||||||
| let tree = ctx.find_node_at_offset::<ast::UseTree>()?.top_use_tree(); | ||||||
| let target = tree.syntax().text_range(); | ||||||
|
|
||||||
| let use_item = tree.syntax().parent().and_then(ast::Use::cast)?; | ||||||
| let mut neighbor = next_prev().find_map(|dir| neighbor(&use_item, dir)).into_iter(); | ||||||
| let edits = use_item.try_merge_from(&mut neighbor, &ctx.config.insert_use); | ||||||
| (target, edits?) | ||||||
| let neighbor = next_prev().find_map(|dir| neighbor(&use_item, dir))?; | ||||||
| let parent_node = use_item.syntax().parent()?; | ||||||
| let (editor, _) = | ||||||
| SyntaxEditor::new(parent_node.ancestors().last().unwrap_or(parent_node.clone())); | ||||||
| merge_uses(use_item, vec![neighbor], &ctx.config.insert_use, &editor)?; | ||||||
| (target, editor) | ||||||
| } else { | ||||||
| // Merge selected | ||||||
| let selection_range = ctx.selection_trimmed(); | ||||||
|
|
@@ -50,104 +53,81 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext<'_, '_>) -> O | |||||
| }); | ||||||
|
|
||||||
| let first_selected = selected_nodes.next()?; | ||||||
| let edits = match_ast! { | ||||||
| let (editor, _) = | ||||||
| SyntaxEditor::new(parent_node.ancestors().last().unwrap_or(parent_node.clone())); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| match_ast! { | ||||||
| match first_selected { | ||||||
| ast::Use(use_item) => { | ||||||
| cov_mark::hit!(merge_with_selected_use_item_neighbors); | ||||||
| use_item.try_merge_from(&mut selected_nodes.filter_map(ast::Use::cast), &ctx.config.insert_use) | ||||||
| merge_uses( | ||||||
| use_item, | ||||||
| selected_nodes.filter_map(ast::Use::cast).collect(), | ||||||
| &ctx.config.insert_use, | ||||||
| &editor, | ||||||
| )?; | ||||||
| }, | ||||||
| ast::UseTree(use_tree) => { | ||||||
| cov_mark::hit!(merge_with_selected_use_tree_neighbors); | ||||||
| use_tree.try_merge_from(&mut selected_nodes.filter_map(ast::UseTree::cast), &ctx.config.insert_use) | ||||||
| merge_use_trees( | ||||||
| use_tree, | ||||||
| selected_nodes.filter_map(ast::UseTree::cast).collect(), | ||||||
| &editor, | ||||||
| )?; | ||||||
| }, | ||||||
| _ => return None, | ||||||
| } | ||||||
| }; | ||||||
| (selection_range, edits?) | ||||||
| }; | ||||||
|
|
||||||
| let parent_node = match ctx.covering_element() { | ||||||
| SyntaxElement::Node(n) => n, | ||||||
| SyntaxElement::Token(t) => t.parent()?, | ||||||
| } | ||||||
| (selection_range, editor) | ||||||
| }; | ||||||
|
|
||||||
| acc.add(AssistId::refactor_rewrite("merge_imports"), "Merge imports", target, |builder| { | ||||||
| let editor = builder.make_editor(&parent_node); | ||||||
|
|
||||||
| for edit in edits { | ||||||
| match edit { | ||||||
| Remove(it) => { | ||||||
| let node = it.as_ref(); | ||||||
| if let Some(left) = node.left() { | ||||||
| left.remove(&editor); | ||||||
| } else if let Some(right) = node.right() { | ||||||
| right.remove(&editor); | ||||||
| } | ||||||
| } | ||||||
| Replace(old, new) => { | ||||||
| editor.replace(old, &new); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| builder.add_file_edits(ctx.vfs_file_id(), editor); | ||||||
| }) | ||||||
| } | ||||||
|
|
||||||
| trait Merge: AstNode + Clone { | ||||||
| fn try_merge_from( | ||||||
| self, | ||||||
| items: &mut dyn Iterator<Item = Self>, | ||||||
| cfg: &InsertUseConfig, | ||||||
| ) -> Option<Vec<Edit>> { | ||||||
| let mut edits = Vec::new(); | ||||||
| let mut merged = self.clone(); | ||||||
| for item in items { | ||||||
| merged = merged.try_merge(&item, cfg)?; | ||||||
| edits.push(Edit::Remove(item.into_either())); | ||||||
| } | ||||||
| if !edits.is_empty() { | ||||||
| edits.push(Edit::replace(self, merged)); | ||||||
| Some(edits) | ||||||
| } else { | ||||||
| None | ||||||
| } | ||||||
| fn merge_uses( | ||||||
| first: ast::Use, | ||||||
| rest: Vec<ast::Use>, | ||||||
| cfg: &InsertUseConfig, | ||||||
| editor: &SyntaxEditor, | ||||||
| ) -> Option<()> { | ||||||
| if rest.is_empty() { | ||||||
| return None; | ||||||
| } | ||||||
| fn try_merge(&self, other: &Self, cfg: &InsertUseConfig) -> Option<Self>; | ||||||
| fn into_either(self) -> Either<ast::Use, ast::UseTree>; | ||||||
| } | ||||||
|
|
||||||
| impl Merge for ast::Use { | ||||||
| fn try_merge(&self, other: &Self, cfg: &InsertUseConfig) -> Option<Self> { | ||||||
| let mb = match cfg.granularity { | ||||||
| ImportGranularity::One => MergeBehavior::One, | ||||||
| _ => MergeBehavior::Crate, | ||||||
| }; | ||||||
| try_merge_imports(self, other, mb) | ||||||
| let mb = match cfg.granularity { | ||||||
| ImportGranularity::One => MergeBehavior::One, | ||||||
| _ => MergeBehavior::Crate, | ||||||
| }; | ||||||
| let mut merged = first.clone(); | ||||||
| for item in &rest { | ||||||
| merged = try_merge_imports(editor, &merged, item, mb)?; | ||||||
| } | ||||||
| fn into_either(self) -> Either<ast::Use, ast::UseTree> { | ||||||
| Either::Left(self) | ||||||
| for item in rest { | ||||||
| item.remove(editor); | ||||||
| } | ||||||
| editor.replace(first.syntax(), merged.syntax()); | ||||||
| Some(()) | ||||||
| } | ||||||
|
|
||||||
| impl Merge for ast::UseTree { | ||||||
| fn try_merge(&self, other: &Self, _: &InsertUseConfig) -> Option<Self> { | ||||||
| try_merge_trees(self, other, MergeBehavior::Crate) | ||||||
| } | ||||||
| fn into_either(self) -> Either<ast::Use, ast::UseTree> { | ||||||
| Either::Right(self) | ||||||
| fn merge_use_trees( | ||||||
| first: ast::UseTree, | ||||||
| rest: Vec<ast::UseTree>, | ||||||
| editor: &SyntaxEditor, | ||||||
| ) -> Option<()> { | ||||||
| if rest.is_empty() { | ||||||
| return None; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| #[derive(Debug)] | ||||||
| enum Edit { | ||||||
| Remove(Either<ast::Use, ast::UseTree>), | ||||||
| Replace(SyntaxNode, SyntaxNode), | ||||||
| } | ||||||
|
|
||||||
| impl Edit { | ||||||
| fn replace(old: impl AstNode, new: impl AstNode) -> Self { | ||||||
| Edit::Replace(old.syntax().clone(), new.syntax().clone()) | ||||||
| let mut merged = first.clone(); | ||||||
| for item in &rest { | ||||||
| merged = try_merge_trees(editor, &merged, item, MergeBehavior::Crate)?; | ||||||
| } | ||||||
| for item in rest { | ||||||
| item.remove(editor); | ||||||
| } | ||||||
| editor.replace(first.syntax(), merged.syntax()); | ||||||
| Some(()) | ||||||
| } | ||||||
|
|
||||||
| #[cfg(test)] | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.