Skip to content

Commit

Permalink
scalafmt: align strip margins
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Mar 31, 2024
1 parent 8c3ec5e commit d76cb7c
Show file tree
Hide file tree
Showing 26 changed files with 872 additions and 867 deletions.
1 change: 1 addition & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ project {
}
align {
preset = none
stripMargin = true
}
newlines {
avoidForSimpleOverflow = [punct, slc, tooLong]
Expand Down
12 changes: 6 additions & 6 deletions readme/src/main/scala/org/scalafmt/readme/Readme.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ object Readme {
val expressions = s"{$code}".parse[Stat].get.asInstanceOf[Term.Block].stats
val evaluated = eval[Any](code)
val output = evaluated match {
case s: String => s"""
|"$s"""".stripMargin
case s: String => s"""|
|"$s"""".stripMargin
case x => x.toString
}
val result =
s"""${expressions.map(x => s"scala> ${x.toString().trim}").mkString("\n")}
|res0: ${evaluated.getClass.getName} = $output
|""".stripMargin
val result = s"""|${expressions.map(x => s"scala> ${x.toString().trim}")
.mkString("\n")}
|res0: ${evaluated.getClass.getName} = $output
|""".stripMargin
hl.scala(result)
}

Expand Down
42 changes: 21 additions & 21 deletions scalafmt-cli/src/main/scala/org/scalafmt/cli/Cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ object Cli {
private def findRunner(options: CliOptions): MaybeRunner = options.hoconOpt
.fold[MaybeRunner] {
Left(
s"""error: missing Scalafmt configuration file.
|Consider creating '${options.getProposedConfigFile}'
|with the following (other parameters may also be required):
|${getProposedConfigVersion(options)}
|""".stripMargin
s"""|error: missing Scalafmt configuration file.
|Consider creating '${options.getProposedConfigFile}'
|with the following (other parameters may also be required):
|${getProposedConfigVersion(options)}
|""".stripMargin
)
} {
// Run format using
Expand All @@ -119,10 +119,10 @@ object Cli {
case _ => "--config-str option"
}
Left(
s"""error: missing Scalafmt version.
|Consider adding the following to $where:
|${getProposedConfigVersion(options)}
|""".stripMargin
s"""|error: missing Scalafmt version.
|Consider adding the following to $where:
|${getProposedConfigVersion(options)}
|""".stripMargin
)
} {
case Left(error) => Left(s"error: invalid configuration: ${error}")
Expand All @@ -131,18 +131,18 @@ object Cli {
Right(ScalafmtCoreRunner)
case Right(v) if isNativeImage =>
Left(
s"""error: invalid Scalafmt version.
|
|This Scalafmt installation has version '$stableVersion' and the version configured in '${options
.configPath}' is '$v'.
|To fix this problem, add the following line to .scalafmt.conf:
|```
|version = $stableVersion
|```
|
|NOTE: this error happens only when running a native Scalafmt binary.
|Scalafmt automatically installs and invokes the correct version of Scalafmt when running on the JVM.
|""".stripMargin
s"""|error: invalid Scalafmt version.
|
|This Scalafmt installation has version '$stableVersion' and the version configured in '${options
.configPath}' is '$v'.
|To fix this problem, add the following line to .scalafmt.conf:
|```
|version = $stableVersion
|```
|
|NOTE: this error happens only when running a native Scalafmt binary.
|Scalafmt automatically installs and invokes the correct version of Scalafmt when running on the JVM.
|""".stripMargin
)
case Right(v) =>
options.common.debug.println(s"Using dynamic runner [$v]")
Expand Down
61 changes: 31 additions & 30 deletions scalafmt-cli/src/main/scala/org/scalafmt/cli/CliArgParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ object CliArgParser {

val usageExamples: String =
"""|scalafmt # Format all files in the current project, configuration is determined in this order:
| # 1. .scalafmt.conf file in current directory
| # 2. .scalafmt.conf inside root directory of current git repo
| # 3. no configuration, default style
|scalafmt --test # throw exception on mis-formatted files, won't write to files.
|scalafmt --mode diff # Format all files that were edited in git diff against master branch.
|scalafmt --mode changed # Format files listed in `git status` (latest changes against previous commit.
|scalafmt --diff-branch 2.x # same as --diff, except against branch 2.x
|scalafmt --stdin # read from stdin (doesn't imply --stdout)
|scalafmt --stdin --assume-filename foo.sbt < foo.sbt # required when using --stdin to format .sbt files.
|scalafmt Code1.scala A.scala # write formatted contents to file.
|scalafmt --stdout Code.scala # print formatted contents to stdout.
|scalafmt --exclude target # format all files in directory excluding target
|scalafmt --config .scalafmt.conf # read custom style from file.
|scalafmt --config-str "style=IntelliJ" # define custom style as a flag, must be quoted."""
| # 1. .scalafmt.conf file in current directory
| # 2. .scalafmt.conf inside root directory of current git repo
| # 3. no configuration, default style
|scalafmt --test # throw exception on mis-formatted files, won't write to files.
|scalafmt --mode diff # Format all files that were edited in git diff against master branch.
|scalafmt --mode changed # Format files listed in `git status` (latest changes against previous commit.
|scalafmt --diff-branch 2.x # same as --diff, except against branch 2.x
|scalafmt --stdin # read from stdin (doesn't imply --stdout)
|scalafmt --stdin --assume-filename foo.sbt < foo.sbt # required when using --stdin to format .sbt files.
|scalafmt Code1.scala A.scala # write formatted contents to file.
|scalafmt --stdout Code.scala # print formatted contents to stdout.
|scalafmt --exclude target # format all files in directory excluding target
|scalafmt --config .scalafmt.conf # read custom style from file.
|scalafmt --config-str "style=IntelliJ" # define custom style as a flag, must be quoted."""
.stripMargin

val scoptParser: OptionParser[CliOptions] =
Expand Down Expand Up @@ -53,9 +53,9 @@ object CliArgParser {
arg[Path]("<file>...").optional().unbounded().action((file, c) =>
c.addFile(file)
).text(
"""file, or directory (in which all *.scala files are to be formatted);
|if starts with '@', refers to path listing files to be formatted
|(with "@-" referring to standard input as a special case)"""
"""|file, or directory (in which all *.scala files are to be formatted);
|if starts with '@', refers to path listing files to be formatted
|(with "@-" referring to standard input as a special case)"""
.stripMargin
)

Expand Down Expand Up @@ -99,20 +99,20 @@ object CliArgParser {
writeMode(c, WriteMode.Test).copy(error = true, check = true)
).text("test for mis-formatted code only, exits with status 1 on first failure.")
opt[FileFetchMode]("mode").action((m, c) => c.copy(mode = Option(m))).text(
s"""Sets the files to be formatted fetching mode.
|Options:
|${FileFetchMode.help}
|""".stripMargin
s"""|Sets the files to be formatted fetching mode.
|Options:
|${FileFetchMode.help}
|""".stripMargin
)
opt[Unit]("diff")
.action((_, c) => c.copy(mode = Option(DiffFiles("master")))).text(
s"""Format files listed in `git diff` against master.
|Deprecated: use --mode diff instead""".stripMargin
s"""|Format files listed in `git diff` against master.
|Deprecated: use --mode diff instead""".stripMargin
)
opt[String]("diff-branch")
.action((branch, c) => c.copy(mode = Option(DiffFiles(branch)))).text(
s"""Format files listed in `git diff` against given git ref.
|Deprecated: use --mode diff-ref=<ref> instead""".stripMargin
s"""|Format files listed in `git diff` against given git ref.
|Deprecated: use --mode diff-ref=<ref> instead""".stripMargin
)
opt[Unit]("build-info").action((_, _) => { println(buildInfo); sys.exit })
.text("prints build information")
Expand All @@ -132,9 +132,9 @@ object CliArgParser {

note(
s"""|Examples:
|$usageExamples
|Please file bugs to https://github.com/scalameta/scalafmt/issues
""".stripMargin
|$usageExamples
|Please file bugs to https://github.com/scalameta/scalafmt/issues
|""".stripMargin
)

checkConfig { c =>
Expand All @@ -143,8 +143,9 @@ object CliArgParser {
else success
}
}
def buildInfo = s"""build commit: ${Versions.commit}
|build time: ${new Date(Versions.timestamp.toLong)}""".stripMargin
def buildInfo =
s"""|build commit: ${Versions.commit}
|build time: ${new Date(Versions.timestamp.toLong)}""".stripMargin

private def writeMode(c: CliOptions, writeMode: WriteMode): CliOptions = c
.writeModeOpt.fold { c.copy(writeModeOpt = Some(writeMode)) } { x =>
Expand Down
20 changes: 10 additions & 10 deletions scalafmt-core/shared/src/main/scala/org/scalafmt/Error.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ object Error {

case class FormatterChangedAST(diff: String, output: String)
extends Error(
s"""Formatter changed AST
|=====================
|$diff
|=====================
|${output.linesIterator.toVector.take(10).mkString("\n")}
|=====================
|Formatter changed AST
""".stripMargin
s"""|Formatter changed AST
|=====================
|$diff
|=====================
|${output.linesIterator.toVector.take(10).mkString("\n")}
|=====================
|Formatter changed AST
|""".stripMargin
)

case class FormatterOutputDoesNotParse(msg: String, line: Int)
extends Error("Formatter output does not parse:\n" + msg)

case class UnexpectedTree[Expected <: Tree: ClassTag](obtained: Tree)
extends Error(
s"""Expected: ${classTag[Expected].runtimeClass.getName}
|Obtained: ${log(obtained)}""".stripMargin
s"""|Expected: ${classTag[Expected].runtimeClass.getName}
|Obtained: ${log(obtained)}""".stripMargin
)

case class CantFormatFile(msg: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ object NamedDialect {

def getUnknownError = {
val knownStr = known.map(_.name).mkString(",")
s"""Default dialect is deprecated; use explicit: [$knownStr]
|Also see https://scalameta.org/scalafmt/docs/configuration.html#scala-dialects"
|""".stripMargin
s"""|Default dialect is deprecated; use explicit: [$knownStr]
|Also see https://scalameta.org/scalafmt/docs/configuration.html#scala-dialects"
|""".stripMargin
}

implicit val codec: ConfCodecEx[NamedDialect] = ReaderUtil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ object ProjectFiles {
case e: java.util.regex.PatternSyntaxException =>
throw new ScalafmtConfigException(
s"""|Illegal regex in configuration: $regex
|reason: ${e.getMessage()}""".stripMargin
|reason: ${e.getMessage()}""".stripMargin
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,17 @@ private class BestFirstSearch private (
val tok = tokens(deepestYet.depth)
val splitsAfterPolicy = deepestYet.policy
.execute(Decision(tok, nextSplits))
val msg = s"""UNABLE TO FORMAT,
|tok=$tok
|toks.length=${tokens.length}
|deepestYet.length=${deepestYet.depth}
|policies=${deepestYet.policy.policies}
|nextSplits=$nextSplits
|splitsAfterPolicy=$splitsAfterPolicy""".stripMargin
val msg = s"""|UNABLE TO FORMAT,
|tok=$tok
|toks.length=${tokens.length}
|deepestYet.length=${deepestYet.depth}
|policies=${deepestYet.policy.policies}
|nextSplits=$nextSplits
|splitsAfterPolicy=$splitsAfterPolicy""".stripMargin
if (runner.debug) {
logger.debug(
s"""Failed to format
|$msg""".stripMargin
s"""|Failed to format
|$msg""".stripMargin
)
}
complete(deepestYet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1341,11 +1341,11 @@ class FormatOps(
case _ if orNil => Nil
case t =>
logger.debug(
s"""getApplyArgs: unknown tree
|Tree: ${log(t)}
|Parent: ${log(t.parent)}
|GrandParent: ${log(t.parent.flatMap(_.parent))}
|""".stripMargin
s"""|getApplyArgs: unknown tree
|Tree: ${log(t)}
|Parent: ${log(t.parent)}
|GrandParent: ${log(t.parent.flatMap(_.parent))}
|""".stripMargin
)
throw UnexpectedTree[Member.SyntaxValuesClause](t)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ object Patch {
case (add: Add, _: Remove) => add.copy(keepTok = false)
case (rem: Remove, _: Remove) => rem
case _ => sys.error(
s"""Can't merge token patches:
|1. $a
|2. $b""".stripMargin
s"""|Can't merge token patches:
|1. $a
|2. $b""".stripMargin
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ object LoggerOps {
}
def log(split: Split): String = s"$split"

def log(formatToken: FormatToken): String = s"""${log(formatToken.left)}
|${log(formatToken.between: _*)}
|${log(formatToken.right)}""".stripMargin
def log(formatToken: FormatToken): String =
s"""|${log(formatToken.left)}
|${log(formatToken.between: _*)}
|${log(formatToken.right)}""".stripMargin

def log2(formatToken: FormatToken): String = formatToken.toString

Expand All @@ -52,11 +53,11 @@ object LoggerOps {
def log(t: Tree, tokensOnly: Boolean): String = {
val tokens = s"TOKENS: ${t.tokens.map(x => reveal(x.syntax)).mkString(",")}"
if (tokensOnly) tokens
else s"""TYPE: ${t.getClass.getName.stripPrefix("scala.meta.")}
|SOURCE: $t
|STRUCTURE: ${t.show[Structure]}
|$tokens
|""".stripMargin
else s"""|TYPE: ${t.getClass.getName.stripPrefix("scala.meta.")}
|SOURCE: $t
|STRUCTURE: ${t.show[Structure]}
|$tokens
|""".stripMargin
}

def log(t: Option[Tree]): String = log(t, false)
Expand Down
Loading

0 comments on commit d76cb7c

Please sign in to comment.