@@ -15,6 +15,9 @@ import scala.util.Success
15
15
import scala .util .Try
16
16
17
17
import org .scalafmt .interfaces .Scalafmt
18
+ import org .scalafmt .sysops .AbsoluteFile
19
+ import org .scalafmt .sysops .FileOps
20
+ import org .scalafmt .sysops .GitOps
18
21
19
22
import complete .DefaultParsers ._
20
23
@@ -64,6 +67,9 @@ object ScalafmtPlugin extends AutoPlugin {
64
67
settingKey[Boolean ](
65
68
" Enables logging of detailed errors with stacktraces, disabled by default"
66
69
)
70
+ val scalafmtFilter = settingKey[String ](
71
+ " File filtering mode when running scalafmt."
72
+ )
67
73
}
68
74
69
75
import autoImport ._
@@ -102,10 +108,17 @@ object ScalafmtPlugin extends AutoPlugin {
102
108
.create(this .getClass.getClassLoader)
103
109
.withRespectProjectFilters(true )
104
110
111
+ private object FilterMode {
112
+ val diffDirty = " diff-dirty"
113
+ val diffRefPrefix = " diff-ref="
114
+ }
115
+
105
116
private class FormatSession (
106
117
config : Path ,
107
118
taskStreams : TaskStreams ,
108
119
resolvers : Seq [Resolver ],
120
+ currentProject : ResolvedProject ,
121
+ filterMode : String ,
109
122
detailedErrorEnabled : Boolean
110
123
) {
111
124
private val log = taskStreams.log
@@ -134,11 +147,27 @@ object ScalafmtPlugin extends AutoPlugin {
134
147
scalafmtSession
135
148
}
136
149
137
- private def filterFiles (sources : Seq [File ]): Seq [File ] =
150
+ private def filterFiles (sources : Seq [File ]): Seq [File ] = {
151
+ val filter = getFileFilter()
138
152
sources.distinct.filter { file =>
139
153
val path = file.toPath.toAbsolutePath
140
- scalafmtSession.matchesProjectFilters(path)
154
+ scalafmtSession.matchesProjectFilters(path) && filter(path)
141
155
}
156
+ }
157
+
158
+ private def getFileFilter (): Path => Boolean = {
159
+ def gitOps = GitOps .FactoryImpl (AbsoluteFile (currentProject.base.toPath))
160
+ val files =
161
+ if (filterMode == FilterMode .diffDirty)
162
+ gitOps.status()
163
+ else if (filterMode.startsWith(FilterMode .diffRefPrefix))
164
+ gitOps.diff(filterMode.substring(FilterMode .diffRefPrefix.length))
165
+ else if (scalafmtSession.isGitOnly)
166
+ gitOps.lsTree()
167
+ else null
168
+ if (files eq null ) _ => true
169
+ else FileOps .getFileMatcher(files.map(_.path))
170
+ }
142
171
143
172
private def withFormattedSources [T ](sources : Seq [File ])(
144
173
onFormat : (File , Input , Output ) => T
@@ -374,6 +403,8 @@ object ScalafmtPlugin extends AutoPlugin {
374
403
config,
375
404
streams.value,
376
405
fullResolvers.value,
406
+ thisProject.value,
407
+ scalafmtFilter.value,
377
408
scalafmtDetailedError.value
378
409
)
379
410
func(files, session)
@@ -412,6 +443,8 @@ object ScalafmtPlugin extends AutoPlugin {
412
443
scalaConfig.value,
413
444
streams.value,
414
445
fullResolvers.value,
446
+ thisProject.value,
447
+ " " ,
415
448
scalafmtDetailedError.value
416
449
).formatSources(absFiles)
417
450
}
@@ -437,6 +470,7 @@ object ScalafmtPlugin extends AutoPlugin {
437
470
438
471
override def globalSettings : Seq [Def .Setting [_]] =
439
472
Seq (
473
+ scalafmtFilter := " " ,
440
474
scalafmtOnCompile := false ,
441
475
scalafmtDetailedError := false
442
476
)
0 commit comments