Skip to content
This repository has been archived by the owner on Sep 3, 2020. It is now read-only.

Do not throw exception when imports are collapsed to wildcard import #140

Merged
merged 1 commit into from Feb 29, 2016

Conversation

kiritsuku
Copy link
Member

It is kind of difficult to describe why this exception is thrown and the
fact that it took me an entire working day to fix it should speak for
itself. But I will try nevertheless to give a description:

The problem starts in the class OrganizeImports.AlwaysUseWildcards. If
the "collapse to wildcard import" option is enabled, this class replaces
all imports that are matched by a wildcard import with such a wildcard
import. Even though the wildcard import is generated it has positions
since the position of the first matching import are set to it. This
means that ReusingPrinter is used to print the import. However, in
order to print import selectors the refactoring logic needs to generate
its own trees, called ImportSelectorTree. This is necessary, since
import selectors are not trees by default and therefore it wouldn't be
easily possible to print them through the current tree printing
algorithm. Since the ImportSelectorTree is synthetic, it doesn't have
positions and is therefore forwarded to PrettyPrinter, therefore
PrettyPrinter contains the bug fix.

I didn't add the same logic to ReusingPrinter, since I don't see how
such an implementation could ever be called there (there can't be a
reusing part in a synthetic tree). Furthermore, the fix is more
powerful, than what the test can cover. In theory it can even print
rename imports but I was not able to come up with a test case that would
invoke this logic, therefore we have no guarantee that this part of the
implementation is correct or even necessary.

Fixes #1002654

It is kind of difficult to describe why this exception is thrown and the
fact that it took me an entire working day to fix it should speak for
itself. But I will try nevertheless to give a description:

The problem starts in the class `OrganizeImports.AlwaysUseWildcards`. If
the "collapse to wildcard import" option is enabled, this class replaces
all imports that are matched by a wildcard import with such a wildcard
import. Even though the wildcard import is generated it has positions
since the position of the first matching import are set to it. This
means that `ReusingPrinter` is used to print the import. However, in
order to print import selectors the refactoring logic needs to generate
its own trees, called `ImportSelectorTree`. This is necessary, since
import selectors are not trees by default and therefore it wouldn't be
easily possible to print them through the current tree printing
algorithm. Since the `ImportSelectorTree` is synthetic, it doesn't have
positions and is therefore forwarded to `PrettyPrinter`, therefore
`PrettyPrinter` contains the bug fix.

I didn't add the same logic to ReusingPrinter, since I don't see how
such an implementation could ever be called there (there can't be a
reusing part in a synthetic tree). Furthermore, the fix is more
powerful, than what the test can cover. In theory it can even print
rename imports but I was not able to come up with a test case that would
invoke this logic, therefore we have no guarantee that this part of the
implementation is correct or even necessary.

Fixes #1002654
@misto
Copy link
Member

misto commented Feb 29, 2016

This looks like a perfectly fine solution for the problem!

misto added a commit that referenced this pull request Feb 29, 2016
Do not throw exception when imports are collapsed to wildcard import
@misto misto merged commit a262726 into scala-ide:master Feb 29, 2016
@kiritsuku kiritsuku deleted the t1002654-not-implemented-error branch February 29, 2016 10:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants