@@ -2,8 +2,8 @@ package scala.cli.commands
22
33import caseapp ._
44
5- import scala .build .Inputs
6- import scala .build .internal . Runner
5+ import scala .build .internal .{ CustomCodeWrapper , Runner }
6+ import scala .build .{ CrossSources , Inputs , Sources }
77import scala .cli .internal .FetchExternalBinary
88
99object Fmt extends ScalaCommand [FmtOptions ] {
@@ -12,16 +12,16 @@ object Fmt extends ScalaCommand[FmtOptions] {
1212 def run (options : FmtOptions , args : RemainingArgs ): Unit = {
1313
1414 // TODO If no input is given, just pass '.' to scalafmt?
15- val (sourceFiles, workspace) =
15+ val (sourceFiles, workspace, inputsOpt ) =
1616 if (args.remaining.isEmpty)
17- (Seq (os.pwd), os.pwd)
17+ (Seq (os.pwd), os.pwd, None )
1818 else {
1919 val i = options.shared.inputsOrExit(args)
2020 val s = i.sourceFiles().collect {
2121 case sc : Inputs .Script => sc.path
2222 case sc : Inputs .ScalaFile => sc.path
2323 }
24- (s, i.workspace)
24+ (s, i.workspace, Some (i) )
2525 }
2626
2727 val logger = options.shared.logger
@@ -31,6 +31,36 @@ object Fmt extends ScalaCommand[FmtOptions] {
3131 logger.debug(" No source files, not formatting anything" )
3232 else {
3333
34+ def scalaVerOpt = inputsOpt.map { inputs =>
35+ val crossSources =
36+ CrossSources .forInputs(
37+ inputs,
38+ Sources .defaultPreprocessors(
39+ options.buildOptions.scriptOptions.codeWrapper.getOrElse(CustomCodeWrapper )
40+ )
41+ ).orExit(logger)
42+ val sharedOptions = crossSources.sharedOptions(options.buildOptions)
43+ sharedOptions
44+ .scalaParams
45+ .orExit(logger)
46+ .scalaVersion
47+ }
48+
49+ def dialectOpt = options.dialect.map(_.trim).filter(_.nonEmpty).orElse {
50+ scalaVerOpt.flatMap {
51+ case v if v.startsWith(" 2.12." ) => Some (" Scala212" )
52+ case v if v.startsWith(" 2.13." ) => Some (" Scala213" )
53+ case v if v.startsWith(" 3." ) => Some (" Scala3" )
54+ case _ => None
55+ }
56+ }
57+
58+ val dialectArgs =
59+ if (options.scalafmtArg.isEmpty && ! os.exists(workspace / " .scalafmt.conf" ))
60+ dialectOpt.toSeq.flatMap(dialect => Seq (" --config-str" , s " runner.dialect= $dialect" ))
61+ else
62+ Nil
63+
3464 val fmtLauncher = options.scalafmtLauncher.filter(_.nonEmpty) match {
3565 case Some (launcher) =>
3666 os.Path (launcher, os.pwd)
@@ -41,7 +71,10 @@ object Fmt extends ScalaCommand[FmtOptions] {
4171
4272 logger.debug(s " Using scalafmt launcher $fmtLauncher" )
4373
44- val command = Seq (fmtLauncher.toString) ++ sourceFiles.map(_.toString)
74+ val command = Seq (fmtLauncher.toString) ++
75+ sourceFiles.map(_.toString) ++
76+ dialectArgs ++
77+ options.scalafmtArg
4578 Runner .run(
4679 " scalafmt" ,
4780 command,
0 commit comments