Skip to content
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

Migrate merge_imports Assist to Use SyntaxFactory #18484

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

tareknaser
Copy link
Contributor

This PR is part of #15710 and #18285.

Changes Made

  • Followed the steps in Migrate assists to SyntaxEditor #18285 for migration.
  • Removed the edits_mut vector from crates/ide-assists/src/handlers/merge_imports.rs
  • Added a new SyntaxFactory method whitespace
    • There are no mappings needed as far as I am concerned
    • Currently named whitespace as it mirrors make::tokens::whitespace but tokens_whitespace might be a more fitting name.
  • Replaced the edit_in_place::Removable trait with edit_in_place::EditorRemovable (Relevant conversation here)
    • This trait requires a SyntaxEditor instance in the remove method to use instead of ted.
    • Implemented for ast::Use and ast::UseTree
    • Most implementations remain the same, except that I used SyntaxEditor::delete instead of ted::remove and ted::remove_all_iter. Since SyntaxEditor::remove doesn’t exist, I assumed delete would perform the same function—though this might be causing issues.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 6, 2024
@tareknaser
Copy link
Contributor Author

Marking this as a draft for now since I’m running into an error.

---- handlers::merge_imports::tests::test_merge_with_synonymous_imports_1 stdout ----

thread 'handlers::merge_imports::tests::test_merge_with_synonymous_imports_1' panicked at crates/syntax/src/syntax_editor/edit_algo.rs:79:8:

some replace change ranges intersect: [Replace(Token([email protected] "\n\n"), Some(Token([email protected] "\n\n"))), Replace(Node([email protected]), None), Replace(Token([email protected] "\n"), None), Replace(Node([email protected]), Some(Node([email protected])))]

  

---- handlers::merge_imports::tests::test_merge_with_synonymous_imports_2 stdout ----

thread 'handlers::merge_imports::tests::test_merge_with_synonymous_imports_2' panicked at crates/syntax/src/syntax_editor/edit_algo.rs:79:8:

some replace change ranges intersect: [Replace(Token([email protected] "\n\n"), Some(Token([email protected] "\n\n"))), Replace(Node([email protected]), None), Replace(Token([email protected] "\n"), None), Replace(Node([email protected]), Some(Node([email protected])))]

  

---- tests::generated::doctest_merge_imports stdout ----

thread 'tests::generated::doctest_merge_imports' panicked at crates/syntax/src/syntax_editor/edit_algo.rs:79:8:

some replace change ranges intersect: [Replace(Node([email protected]), Some(Node([email protected]))), Replace(Token([email protected] "\n"), Some(Token([email protected] "\n"))), Replace(Node([email protected]), None), Replace(Token([email protected] "\n"), None)]

@ShoyuVanilla
Copy link
Member

I'll review this tomorrow

@@ -68,24 +68,31 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
(selection_range, edits?)
};

// FIXME: Is this the best to get a `SyntaxNode` object? We need one for `SourceChangeBuilder::make_editor`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we can get node for SyntaxEditor inside the above if-else blocks; like let (node, target, edits) = if ctx.has_empty_selection() { ..

.collect();
for edit in edits_mut {
let mut editor = builder.make_editor(&parent_node);
for edit in edits {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that to avoid the intersection assertion failure, we should use SyntaxEditor::replace_all for consecutive text ranges instead of individual insert or delete calls

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the edits are generated in a vector by calling either use_item.try_merge_from() or use_tree.try_merge_from(). How can we combine all of these edits into a single replace_all() call?

@bors
Copy link
Contributor

bors commented Dec 5, 2024

☔ The latest upstream changes (presumably #18483) made this pull request unmergeable. Please resolve the merge conflicts.

@tareknaser tareknaser force-pushed the syntax_factory_merge_imports branch from ca0b516 to 01eaf9a Compare December 7, 2024 20:51
@Veykril Veykril added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 9, 2024
@bors
Copy link
Contributor

bors commented Dec 11, 2024

☔ The latest upstream changes (presumably #18657) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants