GithubHelp home page GithubHelp logo

sort-imports's Introduction

SortImports

GitHub Workflow Status GitHub commits since latest release GitHub last commit GitHub contributors

GitHub tag (latest SemVer) GitHub Release Date

Scala Steward badge License

Description

SortImports is a simplistic Scalafix rule.

It will organize imports into prefix-blocks and order imports inside those blocks alphabetically.

For example, these imports

import scala.util._
import scala.collection._
import java.util.Map
import org.xml._
import com.sun._

will be organized as follows

import java.util.Map

import scala.collection._
import scala.util._

import org.xml._

import com.sun._

if the blocks from the below Configuration example are used.

Important sort-imports does not (currently) take into account shadowing. It is a faily dumb sorter of imports. If your code is using shadowing, it may end up no longer compiling! If you run into this issue, consider using liancheng/scalafix-organize-imports, which implements a semantic rule.

Usage

Latest version: GitHub tag (latest SemVer)

ThisBuild / scalafixDependencies += "com.nequissimus" %% "sort-imports" % "<VERSION>"

Configuration

rule = SortImports
SortImports.blocks = [
  "re:javax?\\.", // a re: prefix denotes a regex, this will group java. and javax. packages together
  "scala.",
  "*",
  "com.sun."
]

sort-imports's People

Contributors

bjaglin avatar endertunc avatar github-brice-jaglin avatar iam4722202468 avatar jozic avatar mergify[bot] avatar nequissimus avatar olafurpg avatar pjrt avatar scala-steward avatar slandelle avatar tpetillot avatar tuures avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sort-imports's Issues

Imports with comments

comments on import lines are handled

/*
rule = SortImports
 SortImports.blocks = [
 "java",
 "scala",
 "*",
 "com.sun"
 ]
 */
import scala.util._ // foobar
import scala.collection._
import java.util.Map
import com.oracle.net._
import com.sun.awt._
import java.math.BigInteger

becomes:

import java.math.BigInteger
 // foobarimport java.util.Map

import scala.collection._
import scala.util._

import com.oracle.net._

import com.sun.awt._

could not quickly see any straightforward fix for this, some bigger refactoring would probably be required

Formatted the `;` to next line

import java.util.concurrent.Flow.Subscriber;
import java.util.concurrent.Flow.Publisher;

import akka.NotUsed;
import akka.stream.scaladsl.Source;
import akka.stream.scaladsl.JavaFlowSupport;

is formatted to

import java.util.concurrent.Flow.Publisher
;import java.util.concurrent.Flow.Subscriber

;import akka.NotUsed
;import akka.stream.scaladsl.JavaFlowSupport
;import akka.stream.scaladsl.Source;

Support regex in group definition

It seems I can't combine several packages in a group, like (java + javax).
Scalastyle allows for that using regex. One can say "javax?" and have those in same group.
I think that's easy to implement, but it will be not backward compatible with current config file.
If that's okay, i can send a PR at some point, if not, feel free to close this issue.
Thanks!

comments is ignored eg:`//#imports`

Then results in something like :

//#importsimport org.scalatest.wordspec.AnyWordSpec

expect:

//#imports
import org.scalatest.wordspec.AnyWordSpec

Trouble with enumerated imports

Affected version: 0.2.0

Example:

object Foo {
  import A._, B._, C._
}

Expected behavior: code remains unchanged

Actual behavior:

object Foo {
  import A._, 
B._, 
C._
}

not respect scalafix:off and scalafix:on

//scalafix:off
//#imports
import scala.collection.immutable
import scala.concurrent.duration._
import scala.concurrent.{ Await, Future }

import org.scalatest.wordspec.AnyWordSpecLike

import akka.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit
import akka.actor.typed.ActorRef
//#imports
//scalafix:on
import akka.actor.typed.scaladsl.Behaviors

gives me

//scalafix:off
//#imports
import scala.collection.immutable
import scala.concurrent.duration._
import scala.concurrent.{ Await, Future }

import org.scalatest.wordspec.AnyWordSpecLike

import akka.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit
import akka.actor.typed.ActorRef
//#imports//scalafix:onimport akka.actor.typed.scaladsl.Behaviors

Multiple option for the same prefix

Hey there, great work ๐Ÿ’ฏ

Do you think something like below would be trivial to add?

SortImports.blocks = [
  "java.",
  "scala.",
  "*",                               // all other third party imports
  "com.company.",                    // company imports that doesn't match specific definitions like below
  "com.company.org2.",             // imports from org2's projects
  "com.company.org1.",             // imports from org1's other projects
  "com.company.org1.projectName"   // repo's own `buildInfoPackage` value.
]

Support symbol first import sort order

Thanks a lot for this plugin, this is something we were really missing!

Then, it looks like sort is purely alphabetical and doesn't account for packages.

// currently
import foo.bar._
import foo._

// expecting
import foo._
import foo.bar._

or

// currently
import foo.bar.Baz
import foo.{ Xxx, Yyy}

// expecting
import foo.{ Xxx, Yyy}
import foo.bar.Baz

Imports get added a semi-colon if code contains a semi-colon somewhere

This is a regression introduced in 2.5.3.

ex:

import java.lang.Integer

class Foo {
  def bar(): Unit = { println("hello"); println("world")} // <= note the semicolon here 
}

gets formatted into:

import java.lang.Integer; // <== extra semicolon here

class Foo {
  def bar(): Unit = { println("hello"); println("world")}
}

Not identifying comments after semicolon

Follow-up to #69

def trailingComment(comments: AssociatedComments): Map[Import, Token.Comment] =
value
.map(currentImport => currentImport -> comments.trailing(currentImport).headOption)
.collect {
case (imp, comment) if comment.nonEmpty => (imp, comment.get)
}
.toMap
finds trailing comments only if they directly trail the import. If a semicolon is "in the way", the comment is not correctly identified.

This test currently fails:
Input:

/*
 rule = SortImports
 SortImports.blocks = [
 "java",
 "scala"
 ]
 */
import java.math.BigInteger;
import java.util.Map; // Just a map
import java.util.HashSet;

object JavaStyleImports {
  // Add code that needs fixing here.
}

Expected:

import java.math.BigInteger;
import java.util.HashSet;
import java.util.Map; // Just a map

object JavaStyleImports {
  // Add code that needs fixing here.
}

Actual:

import java.math.BigInteger;
import java.util.HashSet;
 // Just a mapimport java.util.Map;

object JavaStyleImports {
  // Add code that needs fixing here.
}

Code between imports is deleted

Here's an example of the bug. This

import java.time._

object DC {
  def config(dcId: Int, settings: List[String]): Map[Int, Long] = {
    import scala.util

    ???
  }
}

becomes

import java.time._
    import scala.util

    ???
  }
}

This intended behavior should be nothing changing at all.

Otherwise this is a very cool and useful plugin!

Do not move out local scope imports

Hi! Thank you for this great plugin!
This is a first issue ๐Ÿ˜ƒ

Expected:

object DC {
  def config(dcId: Int, settings: Settings): Config = {
    import settings.dc
    // ...

Current result:

import settings.dc
object DC {
  def config(dcId: Int, settings: Settings): Config = {
    // ...

Import jumps on to comment line when there is a comment

Given the following code block, if you run scalafix it will cause the second import to jump up a line resulting in it being commented out.

import java.time.Period
// import this here
import java.time.Instant

object hello extends App {

  val x: Instant = Instant.now()
  val y: Period = Period.ZERO

}

It will become this:

import java.time.Period
// import this hereimport java.time.Instant

object hello extends App {

  val x: Instant = Instant.now()
  val y: Period = Period.ZERO

}

import shadowing not been taken into account

  import StageActorRefSpec._
  import ControlProtocol._

gives

  import ControlProtocol._
  import StageActorRefSpec._

so I have change it into

  import StageActorRefSpec.ControlProtocol._
  import StageActorRefSpec._

"scala" block includes scalafix packages

Hey there. Great plugin!

Was playing around with it today and noticed that, when applied to one of my SBT plugins (which sets some scalafix settings), the scalafix packages were considered as a part of the scala block.

import io.gatling.sbt.GatlingKeys._
import io.gatling.sbt.GatlingPlugin
import org.scalafmt.sbt.ScalafmtPlugin
import sbt.Keys._
import sbt._
import scalafix.sbt.ScalafixPlugin
import scalafix.sbt.ScalafixPlugin.autoImport._

Becomes

import scalafix.sbt.ScalafixPlugin
import scalafix.sbt.ScalafixPlugin.autoImport._

import io.gatling.sbt.GatlingKeys._
import io.gatling.sbt.GatlingPlugin
import org.scalafmt.sbt.ScalafmtPlugin
import sbt.Keys._
import sbt._

With a configuration like so:

SortImports.blocks = [
  "java",
  "scala",
  "*"
]

`import` keyword missing in output

in:

import argonaut._, Argonaut._
import scala.util.Try

object Bug {
  def apply() = Try(Json.obj("foo" := "bar"))
}

out:

scala.util.Try

import import argonaut._, Argonaut._

object Bug {
  def apply() = Try(Json.obj("foo" := "bar"))
}

conf:

SortImports.blocks = [
  "scala.",
  "*",
]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.