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

Pass default dialect to scalafmt #223

Merged
merged 2 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 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.internal.{CustomCodeWrapper, Runner}
import scala.build.{CrossSources, Inputs, Sources}
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,36 @@ 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.flatMap {
case v if v.startsWith("2.12.") => Some("Scala212")
case v if v.startsWith("2.13.") => Some("Scala213")
case v if v.startsWith("3.") => Some("Scala3")
case _ => None
}
}

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 +71,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
6 changes: 6 additions & 0 deletions website/docs/reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ Check that sources are well formatted

#### `--scalafmt-launcher`

#### `--scalafmt-arg`

Aliases: `-F`

#### `--dialect`

## Help options

Available in commands:
Expand Down