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

Sort imports with groups can reorder program execution #2718

Closed
Katrix opened this issue Sep 1, 2021 · 2 comments · Fixed by #2719
Closed

Sort imports with groups can reorder program execution #2718

Katrix opened this issue Sep 1, 2021 · 2 comments · Fixed by #2719

Comments

@Katrix
Copy link

Katrix commented Sep 1, 2021

This template is a guideline, not a strict requirement.

  • Version: 3.0.1
  • Integration: IntelliJ
  • Configuration:
version = "3.0.1"
rewrite.rules = [Imports]
rewrite.imports.groups = [["""scala\..*"""]]

Steps

Given code like this:

object Main {
  import Foo._
  val bar: Bar = getBar
  import bar.ec

  def getBar: Foo.Bar = ???
}
object Foo {
  trait Bar {
    implicit def ec: ExecutionContext
  }
}

When I run scalafmt like this:
IntelliJ keybind

Problem

Scalafmt formats code like this:

object Main {
  import Foo._
  import bar.ec
  val bar: Bar = getBar

  def getBar: Foo.Bar = ???
}
object Foo {
  trait Bar {
    implicit def ec: ExecutionContext
  }
}

Expectation

I would like the formatted output to look like this:

object Main {
  import Foo._
  val bar: Bar = getBar
  import bar.ec

  def getBar: Foo.Bar = ???
}
object Foo {
  trait Bar {
    implicit def ec: ExecutionContext
  }
}

Workaround

Keeping the first import outside the class it works fine.

Notes

See also..

@kitbellew
Copy link
Collaborator

@Katrix there's a comment in the documentation specifically calling out the limitation that this rule cannot support relative imports because there's no semantic information available. what you want is possible only with scalafix.

@Katrix
Copy link
Author

Katrix commented Sep 1, 2021

I would expect it to just cancel the current sort of it reaches a val inbetween imports though. That doesn't sound like that big of a deal. I'm not really asking for it to sort local imports like that. In this case (not top level would be good enough) I would prefer it just not doing anything instead of trying and failing.

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 a pull request may close this issue.

2 participants