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
Rewrite import insertion #5935
Rewrite import insertion #5935
Conversation
Does this fix #5913? |
It seems to fix it, instead of erroring it turns the import into |
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.
Yes, this looks exactly how I would imagine it! Regarding find_insert_use_container
and insert_use_statement
, I think ideally we should just remove this functions from this module. Nothing here should mention AssistContext
or TextEditBuilder
. But it is OK to leave them in temporary, if that eases migration.
Similar point about code duplication -- it is fine if it make the rewrite easier, removing duplication can be done in a separate PR.
Basically, I think we should merge this as soon as the tests are green!
Excellent work @Veykril !
buf.push(make::tokens::single_newline().into()); | ||
} else if add_blank == AddBlankLine::AfterTwice { | ||
buf.push(make::tokens::single_newline().into()); | ||
buf.push(make::tokens::single_newline().into()); |
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.
make::tokens::blank_line()
maybe?
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'm honestly not sure how I would implement that function, I'm not too used to how rowan
and the like works yet. So I'm unsure how I would fetch two newlines as a single SyntaxToken
.
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 something like this should work:
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs
index c2c938ad1..aac4a066f 100644
--- a/crates/syntax/src/ast/make.rs
+++ b/crates/syntax/src/ast/make.rs
@@ -339,7 +339,7 @@ pub mod tokens {
use crate::{ast, AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken};
pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> =
- Lazy::new(|| SourceFile::parse("const C: <()>::Item = (1 != 1, 2 == 2, !true)\n;"));
+ Lazy::new(|| SourceFile::parse("const C: <()>::Item = (1 != 1, 2 == 2, !true)\n;\n\n"));
pub fn single_space() -> SyntaxToken {
SOURCE_FILE
@@ -379,6 +379,16 @@ pub mod tokens {
.unwrap()
}
+ pub fn blank_newline() -> SyntaxToken {
+ SOURCE_FILE
+ .tree()
+ .syntax()
+ .descendants_with_tokens()
+ .filter_map(|it| it.into_token())
+ .find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n\n")
+ .unwrap()
+ }
+
pub struct WsBuilder(SourceFile);
impl WsBuilder {
It's OK to not support weird cases initially, if we don't do something completely crazy. |
For config, I don't think we should handle it in this PR necessary, but it should work like this:
struct InsertUseConfig { ... }
|
Almost all of the current test failures are due to the tests using I've also started a Zulip topic, since its probably better to talk there about these things |
58309fb
to
d5b066c
Compare
9081620
to
feb1e27
Compare
bors r+ 🎉 |
5955: Remove merge import code duplication r=jonas-schievink a=Veykril This removes the code duplication caused by #5935, this also allows the assist to merge imports that have equal visibility and prevents merges of unequal visibility. This PR also fixes an iteration mistake in the mentioned PR: Turns out I made a mistake when writing the `segment_iter` function, I was assuming that the `children` of a path will just be the segments, which is obviously not the case. This also brings insertion order of shorter paths in line with how `rustfmt` orders them. Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
5985: Make MergeBehaviour configurable r=jonas-schievink a=Veykril This should make the newly implemented `MergeBehaviour` for import insertion configurable as roughly outlined in #5935 (comment). For the config name and the like I just picked what came to mind so that might be up for bikeshedding. Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This is my attempt at refactoring the import insertion #3947. I hope what I created here is somewhat in line with what was requested, it wouldn't surprise me .
common_prefix
is a copy frommerge_imports.rs
so those should be unified somewhere,try_merge_trees
is also copied from there but slighly modified to take theMergeBehaviour
enum into account.MergeBehaviour
should in the end become a configuration option, and the order ifImportGroup
probably as well?I'm not too familiar with the assist stuff and the like which is why I dont know what i have to do with
insert_use_statement
andfind_insert_use_container
for now.I will most likely add more test cases in the end as well as I currently only tried to hit every path in
find_insert_position
.Some of the merge tests also fail atm due to them not sorting what they insert. There is also this test case I'm not sure if we want to support it. I would assume we want to? https://github.com/rust-analyzer/rust-analyzer/pull/5935/files#diff-6923916dd8bdd2f1ab4b984adacd265fR540-R547
The entire module was rewritten so looking at the the file itself is probably better than looking at the diff.
Regarding the sub issues of #3947:
ImportGroup
variant order(Would be nice if this was configurable I imagine).MergeBehaviour
enum and it'sLast
variant.pub use
items #3946: This should also be fixedast
is a crate or the module that is already imported.