Skip to content

Commit

Permalink
Pass default dialect to scalafmt
Browse files Browse the repository at this point in the history
So that 'scala fmt .' works fine out-of-the-box for Scala 3 sources.
  • Loading branch information
alexarchambault committed Oct 15, 2021
1 parent 60eae70 commit 2fff73d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
44 changes: 38 additions & 6 deletions modules/cli/src/main/scala/scala/cli/commands/Fmt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package scala.cli.commands

import caseapp._

import scala.build.Inputs
import scala.build.internal.Runner
import scala.build.{CrossSources, Inputs, Sources}
import scala.build.internal.{CustomCodeWrapper, Runner}
import scala.cli.internal.FetchExternalBinary

object Fmt extends ScalaCommand[FmtOptions] {
Expand All @@ -12,16 +12,16 @@ object Fmt extends ScalaCommand[FmtOptions] {
def run(options: FmtOptions, args: RemainingArgs): Unit = {

// TODO If no input is given, just pass '.' to scalafmt?
val (sourceFiles, workspace) =
val (sourceFiles, workspace, inputsOpt) =
if (args.remaining.isEmpty)
(Seq(os.pwd), os.pwd)
(Seq(os.pwd), os.pwd, None)
else {
val i = options.shared.inputsOrExit(args)
val s = i.sourceFiles().collect {
case sc: Inputs.Script => sc.path
case sc: Inputs.ScalaFile => sc.path
}
(s, i.workspace)
(s, i.workspace, Some(i))
}

val logger = options.shared.logger
Expand All @@ -31,6 +31,35 @@ object Fmt extends ScalaCommand[FmtOptions] {
logger.debug("No source files, not formatting anything")
else {

def scalaVerOpt = inputsOpt.map { inputs =>
val crossSources =
CrossSources.forInputs(
inputs,
Sources.defaultPreprocessors(
options.buildOptions.scriptOptions.codeWrapper.getOrElse(CustomCodeWrapper)
)
).orExit(logger)
val sharedOptions = crossSources.sharedOptions(options.buildOptions)
sharedOptions
.scalaParams
.orExit(logger)
.scalaVersion
}

def dialectOpt = options.dialect.map(_.trim).filter(_.nonEmpty).orElse {
scalaVerOpt.map {
case v if v.startsWith("2.12.") => "Scala212"
case v if v.startsWith("2.13.") => "Scala213"
case v if v.startsWith("3.") => "Scala3"
}
}

val dialectArgs =
if (options.scalafmtArg.isEmpty && !os.exists(workspace / ".scalafmt.conf"))
dialectOpt.toSeq.flatMap(dialect => Seq("--config-str", s"runner.dialect=$dialect"))
else
Nil

val fmtLauncher = options.scalafmtLauncher.filter(_.nonEmpty) match {
case Some(launcher) =>
os.Path(launcher, os.pwd)
Expand All @@ -41,7 +70,10 @@ object Fmt extends ScalaCommand[FmtOptions] {

logger.debug(s"Using scalafmt launcher $fmtLauncher")

val command = Seq(fmtLauncher.toString) ++ sourceFiles.map(_.toString)
val command = Seq(fmtLauncher.toString) ++
sourceFiles.map(_.toString) ++
dialectArgs ++
options.scalafmtArg
Runner.run(
"scalafmt",
command,
Expand Down
21 changes: 20 additions & 1 deletion modules/cli/src/main/scala/scala/cli/commands/FmtOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scala.cli.commands
import caseapp._

import scala.build.internal.Constants
import scala.build.options.BuildOptions
import scala.cli.internal.FetchExternalBinary
import scala.util.Properties

Expand All @@ -11,19 +12,34 @@ import scala.util.Properties
final case class FmtOptions(
@Recurse
shared: SharedOptions = SharedOptions(),

@Group("Format")
@HelpMessage("Check that sources are well formatted")
check: Boolean = false,

@Group("Format")
@Hidden
osArchSuffix: Option[String] = None,
@Group("Format")
@Hidden
scalafmtTag: Option[String] = None,
@Group("Format")
@Hidden
scalafmtGithubOrgName: Option[String] = None,
@Group("Format")
@Hidden
scalafmtExtension: Option[String] = None,
@Group("Format")
@Hidden
scalafmtLauncher: Option[String] = None,

@Group("Format")
@Name("F")
@Hidden
scalafmtLauncher: Option[String] = None
scalafmtArg: List[String] = Nil,

@Group("Format")
dialect: Option[String] = None
) {
// format: on

Expand All @@ -38,6 +54,9 @@ final case class FmtOptions(
(url, !tag0.startsWith("v"))
}

def buildOptions: BuildOptions =
shared.buildOptions(enableJmh = false, jmhVersion = None, ignoreErrors = false)

}

object FmtOptions {
Expand Down

0 comments on commit 2fff73d

Please sign in to comment.