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

Rewrite import insertion #5935

Merged
merged 10 commits into from Sep 4, 2020
Merged

Rewrite import insertion #5935

merged 10 commits into from Sep 4, 2020

Conversation

Veykril
Copy link
Member

@Veykril Veykril commented Sep 2, 2020

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 from merge_imports.rs so those should be unified somewhere, try_merge_trees is also copied from there but slighly modified to take the MergeBehaviour enum into account.
MergeBehaviour should in the end become a configuration option, and the order if ImportGroup 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 and find_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:

@kjeremy
Copy link
Contributor

kjeremy commented Sep 2, 2020

Does this fix #5913?

@Veykril
Copy link
Member Author

Veykril commented Sep 2, 2020

It seems to fix it, instead of erroring it turns the import into use token::TokenKind::{self::*, self};

Copy link
Member

@matklad matklad left a 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 !

crates/assists/src/utils/insert_use.rs Outdated Show resolved Hide resolved
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());
Copy link
Member

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?

Copy link
Member Author

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.

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 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 {

crates/assists/src/utils/insert_use.rs Outdated Show resolved Hide resolved
crates/assists/src/utils/insert_use.rs Outdated Show resolved Hide resolved
crates/assists/src/utils/insert_use.rs Show resolved Hide resolved
crates/assists/src/utils/insert_use.rs Outdated Show resolved Hide resolved
crates/assists/src/utils/insert_use.rs Outdated Show resolved Hide resolved
crates/assists/src/utils/insert_use.rs Outdated Show resolved Hide resolved
crates/assists/src/utils/insert_use.rs Show resolved Hide resolved
crates/assists/src/utils/insert_use.rs Outdated Show resolved Hide resolved
@matklad
Copy link
Member

matklad commented Sep 2, 2020

There is also this test case I'm not sure if we want to support it. I would assume we want to?

It's OK to not support weird cases initially, if we don't do something completely crazy.

@matklad
Copy link
Member

matklad commented Sep 2, 2020

For config, I don't think we should handle it in this PR necessary, but it should work like this:

  • define
struct InsertUseConfig { ... }

@Veykril
Copy link
Member Author

Veykril commented Sep 2, 2020

Almost all of the current test failures are due to the tests using check_assist which panic on an Option::unwrap in https://github.com/rust-analyzer/rust-analyzer/blob/7f79dbc84f5d69ec6f48fa8d67bdfa0525c5dd34/crates/assists/src/tests.rs#L84 which I assume comes from me not using the AssistContext/TextEditBuilder anymore.

I've also started a Zulip topic, since its probably better to talk there about these things

@Veykril Veykril force-pushed the use-rewrite branch 2 times, most recently from 58309fb to d5b066c Compare September 3, 2020 14:25
@Veykril Veykril force-pushed the use-rewrite branch 3 times, most recently from 9081620 to feb1e27 Compare September 3, 2020 17:07
@Veykril Veykril changed the title [WIP] Rewrite import insertion Rewrite import insertion Sep 3, 2020
@matklad
Copy link
Member

matklad commented Sep 4, 2020

bors r+

🎉

@bors
Copy link
Contributor

bors bot commented Sep 4, 2020

@bors bors bot merged commit 9dfa69a into rust-lang:master Sep 4, 2020
bors bot added a commit that referenced this pull request Sep 11, 2020
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>
bors bot added a commit that referenced this pull request Sep 14, 2020
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>
@Veykril Veykril deleted the use-rewrite branch December 2, 2021 00:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants