Skip to content

SortModifiers does not follow Scala style guide #3788

@dagmendez

Description

@dagmendez

The Scala Style Guide suggest the following:

Modifiers
Method modifiers should be given in the following order (when each is applicable):

  1. Annotations, each on their own line
  2. Override modifier (override)
  3. Access modifier (protected, private)
  4. Implicit modifier (implicit)
  5. Final modifier (final)
  6. def
@Transaction
@throws(classOf[IOException])
override protected final def foo(): Unit = {
 ...
}

Is the current sorting of the modifiers correct and the Scala Style Guide is outdated or is it the other way around?

Configuration (required)

version = 3.8.0 - main branch
...
object SortSettings {
  final case class ModKey(
      name: String,
      matches: Mod => Boolean
  )

  val defaultOrder: List[ModKey] = List(
    ModKey("implicit", _.is[Mod.Implicit]),
    //
    ModKey("final", _.is[Mod.Final]),
    ModKey("sealed", _.is[Mod.Sealed]),
    ModKey("abstract", _.is[Mod.Abstract]),
    //
    ModKey("override", _.is[Mod.Override]),
    //
    ModKey("private", _.is[Mod.Private]),
    ModKey("protected", _.is[Mod.Protected]),
    //
    ModKey("lazy", _.is[Mod.Lazy]),
    ModKey("open", _.is[Mod.Open]),
    ModKey("transparent", _.is[Mod.Transparent]),
    ModKey("inline", _.is[Mod.Inline]),
    ModKey("infix", _.is[Mod.Infix]),
    ModKey("opaque", _.is[Mod.Opaque])
  )

Command-line parameters (required)

When I run scalafmt via CLI like this: scalafmtAll

Steps

Given code like this:

override protected final def foo(): Unit = {
  ...
}

Problem

Scalafmt formats code like this:

final override protected def foo(): Unit = {
  ...
}

Expectation

I would like the formatted output to look like this:

override protected final def foo(): Unit = {

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions