-
Notifications
You must be signed in to change notification settings - Fork 276
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
New scalafmt sbt plugin #1085
New scalafmt sbt plugin #1085
Changes from 1 commit
d9d0920
e1812be
1ef7273
e33449b
3ef624e
5bff421
7b52844
bed8e83
c0e049b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
style = defaultWithAlign | ||
style = default |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../p3/src |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,23 +15,32 @@ object ScalafmtPlugin extends AutoPlugin { | |
override def trigger: PluginTrigger = allRequirements | ||
|
||
object autoImport { | ||
val scalafmt = taskKey[Unit]("Format Scala sources") | ||
val scalafmt = taskKey[Unit]("Format Scala sources.") | ||
val scalafmtCheck = | ||
taskKey[Boolean]("Check that Scala sources is formatted properly") | ||
taskKey[Boolean]( | ||
"Fails if a Scala source is mis-formatted. Does not write to files.") | ||
val scalafmtOnCompile = | ||
settingKey[Boolean]("Format source when compiling") | ||
val scalafmtConfig = taskKey[File]("Scalafmt config file") | ||
val scalafmtSbt = taskKey[Unit]("Format SBT sources") | ||
settingKey[Boolean]( | ||
"Format Scala source files on compile, off by default.") | ||
val scalafmtConfig = taskKey[Option[File]]( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean that you must There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It searches for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, cool. OK this is fine then 👍 |
||
"Optional location of .scalafmt.conf file. If None the default config is used.") | ||
val scalafmtSbt = taskKey[Unit]( | ||
"Format *.sbt and project/*.scala files for this sbt build.") | ||
val scalafmtSbtCheck = | ||
taskKey[Boolean]("Check that SBT sources is formatted properly") | ||
val scalafmtOnly = inputKey[Unit]("Format only given file") | ||
taskKey[Boolean]( | ||
"Fails if a *.sbt or project/*.scala source is mis-formatted. Does not write to files.") | ||
val scalafmtOnly = inputKey[Unit]("Format only given file.") | ||
} | ||
import autoImport._ | ||
|
||
private val scalaConfig = scalafmtConfig.map(Config.fromHoconFile(_) match { | ||
case Configured.Ok(conf) => conf | ||
case Configured.NotOk(e) => throw new MessageOnlyException(e.msg) | ||
}) | ||
private val scalafmtDoFormatOnCompile = | ||
taskKey[Unit]("Format Scala source files if scalafmtOnCompile is on.") | ||
|
||
private val scalaConfig = | ||
scalafmtConfig.map(_.map(Config.fromHoconFile(_) match { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we at least cache this by the timestamp of the file? I feel it's unnecessary to reparse the config for every project/config on every compile. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, sure we can leave it for a separate PR :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@olafurpg maybe simply reuse that object? It does just what we need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Go ahead, we might want to move it to the |
||
case Configured.Ok(conf) => conf | ||
case Configured.NotOk(e) => throw new MessageOnlyException(e.msg) | ||
}).getOrElse(ScalafmtConfig.default)) | ||
private val sbtConfig = scalaConfig.map(_.forSbt) | ||
|
||
private type Input = String | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not help readability IMO, either create a value class or use String. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dwijnand Why did you put a dislike here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just because I disagree (even if the type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My view is that you might as well make it a value class then, a type alias like this gives a false sense of type safety There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well, it's 2 vs 2, so I leave it :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough, I don't feel strongly about this. I'm guilty of doing this myself sometimes 😏 |
||
|
@@ -137,13 +146,16 @@ object ScalafmtPlugin extends AutoPlugin { | |
checkSources(sbtSources.value, sbtConfig.value, streams.value.log) | ||
checkSources(projectSources.value, scalaConfig.value, streams.value.log) | ||
}, | ||
compileInputs in compile := Def.taskDyn { | ||
val task = | ||
if (scalafmtOnCompile.value) scalafmt in resolvedScoped.value.scope | ||
else Def.task(()) | ||
val previousInputs = (compileInputs in compile).value | ||
task.map(_ => previousInputs) | ||
scalafmtDoFormatOnCompile := Def.taskDyn { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As recommended by @sjrd we should make this a settingDyn |
||
if (scalafmtOnCompile.value) { | ||
scalafmt in resolvedScoped.value.scope | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will run scalafmt on all sources on every There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah it's only supposed to be formatting files that are being compiled, not all of them There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's leave caching for next PRs. I would like to implement it entirely, not only in some specific places. I will write a warning in the task description for now :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @olafurpg that's a wrong solution. I don't know why scalariform uses it. |
||
} else { | ||
Def.task(()) | ||
} | ||
}.value, | ||
compileInputs in compile := (compileInputs in compile) | ||
.dependsOn(scalafmtDoFormatOnCompile) | ||
.value, | ||
scalafmtOnly := { | ||
val files = spaceDelimited("<files>").parsed | ||
val absFiles = files.flatMap(fileS => { | ||
|
@@ -166,7 +178,14 @@ object ScalafmtPlugin extends AutoPlugin { | |
Seq(Compile, Test).flatMap(inConfig(_)(scalafmtConfigSettings)) | ||
|
||
override def buildSettings: Seq[Def.Setting[_]] = Seq( | ||
scalafmtConfig := (baseDirectory in ThisBuild).value / ".scalafmt.conf" | ||
scalafmtConfig := { | ||
val path = (baseDirectory in ThisBuild).value / ".scalafmt.conf" | ||
if (path.exists()) { | ||
Some(path) | ||
} else { | ||
None | ||
} | ||
} | ||
) | ||
|
||
override def globalSettings: Seq[Def.Setting[_]] = Seq( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we maybe add a negative check here?
to validate check fails on misformatted files.