-
Notifications
You must be signed in to change notification settings - Fork 888
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
Add use
declaration re-ordering
#1104
Conversation
Also todo for later is that I would expect imports to be squished together, e.g.,
becomes
Note the second import stays separate due to the comment. |
Could you file an issue for this and add a |
I would put them in imports.rs, I guess the more generic stuff (e.g., Paths) should be somewhere else, but we can deal with that if they become more widely used. |
Items, | ||
// Order `use` statements by their path and items within `use` statements by their name. | ||
LinesAndItems | ||
} |
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 would prefer to add another option, rather than have a single enum like this - this way lies combinatorial explosion!
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.
Absolutely - I had a look to see if there was a way that a list of enumerations could be supplied rather than just one (something like reorder_imports = [ "Lines", "Imports" ]
or reorder_imports = "Lines,Imports"
), but that was looking a bit involved, so I'll change to separate options.
This looks great, thanks for the PR! A few very minor comments to address. |
|
Thank you! |
* Add config options for combinations of lines and items * Reordering of import lines implemented. * Changed nested matches to tuple pattern matching * Added ordering of path list items to the ordering of use declarations * Move `format_imports` and `format_import` methods to `imports.rs` * Add comment to explain how `use` declarations are split off while walking through a module * Change `ImportReordering` config option to separate boolean options
This partially addresses #298, adding sorting of contiguous runs of
use
declarations depending on the value and type of path referenced. As an example (taken from the added integration test files), the following codegets transformed to
The
reorder_imports
config item has changed from a boolean to {None, Lines, Items, LinesAndItems}, to allow combinations of use declaration lines and use declaration items to be selected.I would have to say that this isn't a finished item - there are several areas that I'm not 100% happy with:
Any 'missing spans' before the run of
use
declarations remain at the start of the run, even after the declarations are reordered. I can see situations where a comment is expected to stay attached to the firstuse
in a run. I'm thinking situations like:which will currently become
Similarly, any end of line comment after a
use
will stay with the code after that use:which will currently become
Contents of import lists are currently ignored when comparing imports, meaning that
use a::b::{c, d};
is considered equal touse a::b::{e, f};
(but that is relatively easy to implement).Comparison functions for AST items such as paths etc are clustered at the top of
visitor.rs
. Is there either a) a preferable place to put them, or b) a preferable way to implement them?!Looking at the code again, some refactoring would be beneficial so that the
use
declarations in functions were processed the same as those in modules (that would mean changing the code around the call tovisit_item
invisit_stmt
)Anyway - it's a start...