-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
base: master
Are you sure you want to change the base?
Migrate merge_imports
Assist to Use SyntaxFactory
#18484
Conversation
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(WHITESPACE@156..158 "\n\n"), Some(Token(WHITESPACE@0..2 "\n\n"))), Replace(Node(USE@158..172), None), Replace(Token(WHITESPACE@172..173 "\n"), None), Replace(Node(USE@173..197), Some(Node(USE@0..32)))]
---- 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(WHITESPACE@156..158 "\n\n"), Some(Token(WHITESPACE@0..2 "\n\n"))), Replace(Node(USE@158..172), None), Replace(Token(WHITESPACE@172..173 "\n"), None), Replace(Node(USE@173..202), Some(Node(USE@0..37)))]
---- 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(USE@0..24), Some(Node(USE@0..30))), Replace(Token(WHITESPACE@24..25 "\n"), Some(Token(WHITESPACE@0..1 "\n"))), Replace(Node(USE@25..37), None), Replace(Token(WHITESPACE@37..38 "\n"), None)] |
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`. |
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.
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 { |
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.
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
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.
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?
☔ The latest upstream changes (presumably #18483) made this pull request unmergeable. Please resolve the merge conflicts. |
ca0b516
to
01eaf9a
Compare
☔ The latest upstream changes (presumably #18657) made this pull request unmergeable. Please resolve the merge conflicts. |
Signed-off-by: Tarek <tareknaser360@gmail.com>
Signed-off-by: Tarek <tareknaser360@gmail.com>
Signed-off-by: Tarek <tareknaser360@gmail.com>
Signed-off-by: Tarek <tareknaser360@gmail.com>
Signed-off-by: Tarek <tareknaser360@gmail.com>
4fb1b1e
to
b9275f1
Compare
I've rebased this on top of master. Full errorfailures:
---- handlers::merge_imports::tests::merge_selection_use_trees stdout ----
Left:
use std::fmt::{Debug, Display};
Right:
use std::{fmt::{Debug, Display}};
Diff:
use std::{fmt::{Debug, Display}};
thread 'handlers::merge_imports::tests::merge_selection_use_trees' panicked at crates/ide-assists/src/handlers/merge_imports.rs:741:9:
text differs
stack backtrace:
0: rust_begin_unwind
at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/panicking.rs:662:5
1: core::panicking::panic_fmt
at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/panicking.rs:74:14
2: ide_assists::tests::check_with_config
at ./src/tests.rs:308:13
3: ide_assists::tests::check
at ./src/tests.rs:231:5
4: ide_assists::tests::check_assist
at ./src/tests.rs:82:5
5: ide_assists::handlers::merge_imports::tests::merge_selection_use_trees
at ./src/handlers/merge_imports.rs:741:9
6: ide_assists::handlers::merge_imports::tests::merge_selection_use_trees::{{closure}}
at ./src/handlers/merge_imports.rs:712:35
7: core::ops::function::FnOnce::call_once
at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/ops/function.rs:250:5
8: core::ops::function::FnOnce::call_once
at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
---- handlers::merge_imports::tests::merge_selection_uses stdout ----
thread 'handlers::merge_imports::tests::merge_selection_uses' panicked at crates/syntax/src/syntax_editor/edit_algo.rs:79:8:
some replace change ranges intersect: [Replace(Node(USE@21..43), Some(Node(USE@0..38))), Replace(Token(WHITESPACE@43..44 "\n"), Some(Token(WHITESPACE@0..1 "\n"))), Replace(Node(USE@44..64), None), Replace(Token(WHITESPACE@64..65 "\n"), None), Replace(Token(WHITESPACE@64..65 "\n"), Some(Token(WHITESPACE@0..1 "\n"))), Replace(Node(USE@65..85), None), Replace(Token(WHITESPACE@85..86 "\n"), None)]
stack backtrace:
0: rust_begin_unwind
at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/panicking.rs:662:5
1: core::panicking::panic_fmt
at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/panicking.rs:74:14
2: syntax::syntax_editor::edit_algo::apply_edits
at /Users/tareknasser/Documents/workspace/mlh/rust-analyzer/crates/syntax/src/syntax_editor/edit_algo.rs:79:8
3: syntax::syntax_editor::SyntaxEditor::finish
at /Users/tareknasser/Documents/workspace/mlh/rust-analyzer/crates/syntax/src/syntax_editor.rs:102:9
4: ide_db::source_change::SourceChangeBuilder::commit
at /Users/tareknasser/Documents/workspace/mlh/rust-analyzer/crates/ide-db/src/source_change.rs:288:31
5: ide_db::source_change::SourceChangeBuilder::finish
at /Users/tareknasser/Documents/workspace/mlh/rust-analyzer/crates/ide-db/src/source_change.rs:457:9
6: ide_assists::assist_context::Assists::add_impl
at ./src/assist_context.rs:201:18
7: ide_assists::assist_context::Assists::add
at ./src/assist_context.rs:169:9
8: ide_assists::handlers::merge_imports::merge_imports
at ./src/handlers/merge_imports.rs:79:5
9: ide_assists::tests::check_with_config
at ./src/tests.rs:255:5
10: ide_assists::tests::check
at ./src/tests.rs:231:5
11: ide_assists::tests::check_assist
at ./src/tests.rs:82:5
12: ide_assists::handlers::merge_imports::tests::merge_selection_uses
at ./src/handlers/merge_imports.rs:677:9
13: ide_assists::handlers::merge_imports::tests::merge_selection_uses::{{closure}}
at ./src/handlers/merge_imports.rs:675:30
14: core::ops::function::FnOnce::call_once
at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/ops/function.rs:250:5
15: core::ops::function::FnOnce::call_once
at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
failures:
handlers::merge_imports::tests::merge_selection_use_trees
handlers::merge_imports::tests::merge_selection_uses |
This PR is part of #15710 and #18285.
Changes Made
SyntaxEditor
#18285 for migration.edits_mut
vector fromcrates/ide-assists/src/handlers/merge_imports.rs
SyntaxFactory
methodwhitespace
whitespace
as it mirrorsmake::tokens::whitespace
buttokens_whitespace
might be a more fitting name.edit_in_place::Removable
trait withedit_in_place::EditorRemovable
(Relevant conversation here)SyntaxEditor
instance in theremove
method to use instead ofted
.ast::Use
andast::UseTree
SyntaxEditor::delete
instead ofted::remove
andted::remove_all_iter
. SinceSyntaxEditor::remove
doesn’t exist, I assumed delete would perform the same function—though this might be causing issues.