liancheng / scalafix-organize-imports

A CI-friendly Scalafix semantic rule for organizing imports
MIT License
193 stars 22 forks source link

Issue with scala3 `*` imports and IntelliJ #197

Open guizmaii opened 2 years ago

guizmaii commented 2 years ago

Hi,

IntelliJ organises the imports this way:

import zio.test.*
import zio.test.Assertion.*

while OrganizeImports organises them this way:

import zio.test.Assertion.*
import zio.test.*

Here's my scalafix config concerning OrganizeImports:

OrganizeImports {
  # Allign with IntelliJ IDEA so that they don't fight each other
  groupedImports = Merge
}

I feel that IntelliJ is right on this one. What do you thing?

liancheng commented 2 years ago

This behavior is controlled by the importsOrder option. Setting it to SymbolsFirst should produce the desired result.

BTW, Scala 3 support is still experimental and only available in master. Are you using some master snapshot version?

guizmaii commented 2 years ago

It solves the problem but creates another one:

IntelliJ organises these imports like this:

import io.circe.syntax.*
import io.circe.{Encoder, Json}

while with importsOrder set to SymbolsFirst, OrganizeImports organize them this way:

import io.circe.{Encoder, Json}
import io.circe.syntax.*

Are you using some master snapshot version?

No, I'm using the latest published one.

liancheng commented 2 years ago

Huh, interesting. Could you please share your IntelliJ configuration relevant to import organization? Specifically:

  1. Editor / Code Style / Scala / Formatter: Are you using "IntelliJ" or "Scalafmt"
  2. If you are using the "IntelliJ" importer, in the "Imports" tab, which option are you using for "Sort imports (for optimize imports)"? Is it "lexicographically" or "scalastyle consistent"?

I suspect you are using the Scalastyle flavor? IIRC, Scalastyle uses a complicated sorting rule that I never quite understand...

guizmaii commented 2 years ago

Editor / Code Style / Scala / Formatter: Are you using "IntelliJ" or "Scalafmt"

Screenshot 2021-08-05 at 10 12 47
liancheng commented 2 years ago

@guizmaii, could you please also share your .scalafmt.conf?

guizmaii commented 2 years ago

Here it is:

version = "2.7.5"
maxColumn = 140
align.preset = most
align.multiline = false
continuationIndent.defnSite = 2
assumeStandardLibraryStripMargin = true
docstrings = JavaDoc
lineEndings = preserve
includeCurlyBraceInSelectChains = false
danglingParentheses.preset = true
optIn.annotationNewlines = true
newlines.alwaysBeforeMultilineDef = false
trailingCommas = preserve

rewrite.rules = [RedundantBraces]

rewrite.redundantBraces.generalExpressions = false
rewriteTokens = {
  "⇒": "=>"
  "→": "->"
  "←": "<-"
}
liancheng commented 2 years ago

@guizmaii, what I can confirm is that OrganizeImports does not handle the Scala 3 * wildcard properly when sorting imports. What OrganizeImports does when SymbolsFirst is used is to replace underscores and braces with \u0001 and \u0002 followed by a normal lexicographical sorting. The * wildcard is not handled yet.

But I haven't figured out how exactly the result you hit was produced. Need some more time for this.

mihaisoloi commented 2 years ago

To add to this, removeUnused doesn't work for imports that are specified using the * wildcard.