@@ -2,8 +2,8 @@ package scala.cli.commands
2
2
3
3
import caseapp ._
4
4
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 }
7
7
import scala .cli .internal .FetchExternalBinary
8
8
9
9
object Fmt extends ScalaCommand [FmtOptions ] {
@@ -12,16 +12,16 @@ object Fmt extends ScalaCommand[FmtOptions] {
12
12
def run (options : FmtOptions , args : RemainingArgs ): Unit = {
13
13
14
14
// TODO If no input is given, just pass '.' to scalafmt?
15
- val (sourceFiles, workspace) =
15
+ val (sourceFiles, workspace, inputsOpt ) =
16
16
if (args.remaining.isEmpty)
17
- (Seq (os.pwd), os.pwd)
17
+ (Seq (os.pwd), os.pwd, None )
18
18
else {
19
19
val i = options.shared.inputsOrExit(args)
20
20
val s = i.sourceFiles().collect {
21
21
case sc : Inputs .Script => sc.path
22
22
case sc : Inputs .ScalaFile => sc.path
23
23
}
24
- (s, i.workspace)
24
+ (s, i.workspace, Some (i) )
25
25
}
26
26
27
27
val logger = options.shared.logger
@@ -31,6 +31,36 @@ object Fmt extends ScalaCommand[FmtOptions] {
31
31
logger.debug(" No source files, not formatting anything" )
32
32
else {
33
33
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
+
34
64
val fmtLauncher = options.scalafmtLauncher.filter(_.nonEmpty) match {
35
65
case Some (launcher) =>
36
66
os.Path (launcher, os.pwd)
@@ -41,7 +71,10 @@ object Fmt extends ScalaCommand[FmtOptions] {
41
71
42
72
logger.debug(s " Using scalafmt launcher $fmtLauncher" )
43
73
44
- val command = Seq (fmtLauncher.toString) ++ sourceFiles.map(_.toString)
74
+ val command = Seq (fmtLauncher.toString) ++
75
+ sourceFiles.map(_.toString) ++
76
+ dialectArgs ++
77
+ options.scalafmtArg
45
78
Runner .run(
46
79
" scalafmt" ,
47
80
command,
0 commit comments