@@ -176,15 +176,17 @@ object ScalafmtPlugin extends AutoPlugin {
176176 @ inline private def asRelative (file : File ): String =
177177 baseDir.relativize(file.getCanonicalFile.toPath).toString
178178
179- private def filterFiles (sources : Seq [File ]): Seq [File ] = {
180- val filter = getFileFilter()
179+ private def filterFiles (sources : Seq [File ], dirs : Seq [ File ] ): Seq [File ] = {
180+ val filter = getFileFilter(dirs )
181181 sources.map(_.getCanonicalFile).distinct.filter { file =>
182182 val path = file.toPath
183183 scalafmtSession.matchesProjectFilters(path) && filter(path)
184184 }
185185 }
186186
187- private def getFileFilter (): Path => Boolean = {
187+ private def getFileFilter (dirs : Seq [File ]): Path => Boolean = {
188+ // dirs don't have to be within baseDir but within the same git tree
189+ def absDirs = dirs.map(x => AbsoluteFile (x.getCanonicalFile.toPath))
188190 def gitOps = GitOps .FactoryImpl (AbsoluteFile (baseDir))
189191 def getFromFiles (getFiles : => Seq [AbsoluteFile ], gitCmd : => String ) = {
190192 def gitMessage = s " [git $gitCmd] ( $baseDir) "
@@ -199,12 +201,12 @@ object ScalafmtPlugin extends AutoPlugin {
199201 }
200202
201203 if (filterMode == FilterMode .diffDirty)
202- getFromFiles(gitOps.status(), " status" )
204+ getFromFiles(gitOps.status(absDirs : _* ), " status" )
203205 else if (filterMode.startsWith(FilterMode .diffRefPrefix)) {
204206 val branch = filterMode.substring(FilterMode .diffRefPrefix.length)
205- getFromFiles(gitOps.diff(branch), s " diff $branch" )
207+ getFromFiles(gitOps.diff(branch, absDirs : _* ), s " diff $branch" )
206208 } else if (filterMode != FilterMode .none && scalafmtSession.isGitOnly)
207- getFromFiles(gitOps.lsTree(), " ls-files" )
209+ getFromFiles(gitOps.lsTree(absDirs : _* ), " ls-files" )
208210 else {
209211 log.debug(" considering all files (no git)" )
210212 _ => true
@@ -244,8 +246,8 @@ object ScalafmtPlugin extends AutoPlugin {
244246 res
245247 }
246248
247- def formatTrackedSources (sources : Seq [File ]): Unit = {
248- val filteredSources = filterFiles(sources)
249+ def formatTrackedSources (sources : Seq [File ], dirs : Seq [ File ] ): Unit = {
250+ val filteredSources = filterFiles(sources, dirs )
249251 trackSourcesAndConfig(cacheStoreFactory, filteredSources) {
250252 (outDiff, configChanged, prev) =>
251253 val filesToFormat : Seq [File ] =
@@ -261,8 +263,8 @@ object ScalafmtPlugin extends AutoPlugin {
261263 }
262264 }
263265
264- def formatSources (sources : Seq [File ]): Unit =
265- formatFilteredSources(filterFiles(sources))
266+ def formatSources (sources : Seq [File ], dirs : Seq [ File ] ): Unit =
267+ formatFilteredSources(filterFiles(sources, dirs ))
266268
267269 private def formatFilteredSources (sources : Seq [File ]): Unit = {
268270 if (sources.nonEmpty)
@@ -274,8 +276,8 @@ object ScalafmtPlugin extends AutoPlugin {
274276 if (cnt > 0 ) log.info(s " Reformatted $cnt Scala sources " )
275277 }
276278
277- def checkTrackedSources (sources : Seq [File ]): Unit = {
278- val filteredSources = filterFiles(sources)
279+ def checkTrackedSources (sources : Seq [File ], dirs : Seq [ File ] ): Unit = {
280+ val filteredSources = filterFiles(sources, dirs )
279281 val result = trackSourcesAndConfig(cacheStoreFactory, filteredSources) {
280282 (outDiff, configChanged, prev) =>
281283 val filesToCheck : Seq [File ] =
@@ -300,8 +302,8 @@ object ScalafmtPlugin extends AutoPlugin {
300302 throwOnFailure(result)
301303 }
302304
303- def checkSources (sources : Seq [File ]): Unit =
304- throwOnFailure(checkFilteredSources(filterFiles(sources)))
305+ def checkSources (sources : Seq [File ], dirs : Seq [ File ] ): Unit =
306+ throwOnFailure(checkFilteredSources(filterFiles(sources, dirs )))
305307
306308 private def checkFilteredSources (sources : Seq [File ]): ScalafmtAnalysis = {
307309 if (sources.nonEmpty) {
@@ -393,57 +395,69 @@ object ScalafmtPlugin extends AutoPlugin {
393395 }
394396 }
395397
396- private def scalafmtTask (sources : Seq [File ], session : FormatSession ) =
398+ private def scalafmtTask (
399+ sources : Seq [File ],
400+ dirs : Seq [File ],
401+ session : FormatSession
402+ ) =
397403 Def .task {
398- session.formatTrackedSources(sources)
404+ session.formatTrackedSources(sources, dirs )
399405 } tag (ScalafmtTagPack : _* )
400406
401- private def scalafmtCheckTask (sources : Seq [File ], session : FormatSession ) =
407+ private def scalafmtCheckTask (
408+ sources : Seq [File ],
409+ dirs : Seq [File ],
410+ session : FormatSession
411+ ) =
402412 Def .task {
403- session.checkTrackedSources(sources)
413+ session.checkTrackedSources(sources, dirs )
404414 } tag (ScalafmtTagPack : _* )
405415
406416 private def getScalafmtSourcesTask (
407- f : (Seq [File ], FormatSession ) => InitTask
417+ f : (Seq [File ], Seq [ File ], FormatSession ) => InitTask
408418 ) = Def .taskDyn[Unit ] {
409419 val sources = (unmanagedSources in scalafmt).? .value.getOrElse(Seq .empty)
410- getScalafmtTask(f)(sources, scalaConfig.value)
420+ val dirs = (unmanagedSourceDirectories in scalafmt).? .value.getOrElse(Nil )
421+ getScalafmtTask(f)(sources, dirs, scalaConfig.value)
411422 }
412423
413424 private def scalafmtSbtTask (
414425 sources : Seq [File ],
426+ dirs : Seq [File ],
415427 session : FormatSession
416428 ) = Def .task {
417- session.formatSources(sources)
429+ session.formatSources(sources, dirs )
418430 } tag (ScalafmtTagPack : _* )
419431
420432 private def scalafmtSbtCheckTask (
421433 sources : Seq [File ],
434+ dirs : Seq [File ],
422435 session : FormatSession
423436 ) = Def .task {
424- session.checkSources(sources)
437+ session.checkSources(sources, dirs )
425438 } tag (ScalafmtTagPack : _* )
426439
427440 private def getScalafmtSbtTasks (
428- func : (Seq [File ], FormatSession ) => InitTask
441+ func : (Seq [File ], Seq [ File ], FormatSession ) => InitTask
429442 ) = Def .taskDyn {
430443 joinScalafmtTasks(func)(
431- (sbtSources.value, sbtConfig.value),
432- (metabuildSources.value, scalaConfig.value)
444+ (sbtSources.value, Nil , sbtConfig.value),
445+ (metabuildSources.value, Nil , scalaConfig.value)
433446 )
434447 }
435448
436449 private def joinScalafmtTasks (
437- func : (Seq [File ], FormatSession ) => InitTask
438- )(tuples : (Seq [File ], Path )* ) = {
439- val tasks = tuples
440- .map { case (files, config) => getScalafmtTask(func)(files, config) }
450+ func : (Seq [File ], Seq [File ], FormatSession ) => InitTask
451+ )(tuples : (Seq [File ], Seq [File ], Path )* ) = {
452+ val tasks = tuples.map { case (files, dirs, config) =>
453+ getScalafmtTask(func)(files, dirs, config)
454+ }
441455 Def .sequential(tasks.tail.toList, tasks.head)
442456 }
443457
444458 private def getScalafmtTask (
445- func : (Seq [File ], FormatSession ) => InitTask
446- )(files : Seq [File ], config : Path ) = Def .taskDyn[Unit ] {
459+ func : (Seq [File ], Seq [ File ], FormatSession ) => InitTask
460+ )(files : Seq [File ], dirs : Seq [ File ], config : Path ) = Def .taskDyn[Unit ] {
447461 if (files.isEmpty) Def .task(Unit )
448462 else {
449463 val session = new FormatSession (
@@ -460,7 +474,7 @@ object ScalafmtPlugin extends AutoPlugin {
460474 scalafmtDetailedError.value
461475 )
462476 )
463- func(files, session)
477+ func(files, dirs, session)
464478 }
465479 }
466480
@@ -505,7 +519,7 @@ object ScalafmtPlugin extends AutoPlugin {
505519 scalafmtFailOnErrors.value,
506520 scalafmtDetailedError.value
507521 )
508- ).formatSources(absFiles)
522+ ).formatSources(absFiles, Nil )
509523 }
510524 )
511525
0 commit comments