Skip to content

Commit

Permalink
Scalafmt: remove special case of space-only code
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jul 8, 2024
1 parent cbcd0d2 commit 2b580df
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
54 changes: 26 additions & 28 deletions scalafmt-core/shared/src/main/scala/org/scalafmt/Scalafmt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -120,35 +120,33 @@ object Scalafmt {
style: ScalafmtConfig,
file: String,
range: Set[Range],
): Try[String] =
if (code.matches("\\s*")) Success("")
else {
val runner = style.runner
val codeToInput: String => Input = toInput(_, file)
val parsed = runner.parse(Rewrite(codeToInput(code), style, codeToInput))
parsed.fold(
_.details match {
case ed: ParseException =>
val dialect = runner.dialectName
val msg = s"[dialect $dialect] ${ed.shortMessage}"
Failure(new ParseException(ed.pos, msg))
case ed => Failure(ed)
},
tree => {
implicit val formatOps = new FormatOps(tree, style, file)
runner.event(CreateFormatOps(formatOps))
implicit val formatWriter = new FormatWriter(formatOps)
Try(BestFirstSearch(range)).flatMap { res =>
val formattedString = formatWriter.mkString(res.state)
if (res.reachedEOF) Success(formattedString)
else {
val pos = formatOps.tokens(res.state.depth).left.pos
Failure(PreciseIncomplete(pos, formattedString))
}
): Try[String] = {
val runner = style.runner
val codeToInput: String => Input = toInput(_, file)
val parsed = runner.parse(Rewrite(codeToInput(code), style, codeToInput))
parsed.fold(
_.details match {
case ed: ParseException =>
val dialect = runner.dialectName
val msg = s"[dialect $dialect] ${ed.shortMessage}"
Failure(new ParseException(ed.pos, msg))
case ed => Failure(ed)
},
tree => {
implicit val formatOps = new FormatOps(tree, style, file)
runner.event(CreateFormatOps(formatOps))
implicit val formatWriter = new FormatWriter(formatOps)
Try(BestFirstSearch(range)).flatMap { res =>
val formattedString = formatWriter.mkString(res.state)
if (res.reachedEOF) Success(formattedString)
else {
val pos = formatOps.tokens(res.state.depth).left.pos
Failure(PreciseIncomplete(pos, formattedString))
}
},
)
}
}
},
)
}

// XXX: don't modify signature, scalafmt-dynamic expects it via reflection
def format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,13 @@ class Router(formatOps: FormatOps) {

ft match {
// between sources (EOF -> @ -> BOF)
case FormatToken(_: T.EOF, _, _) => Seq(Split(Newline, 0))
case FormatToken(_: T.EOF, _, _) =>
Seq(Split(NoSplit.orNL(prev(ft).left.is[T.BOF]), 0))
case FormatToken(_, _: T.BOF, _) =>
Seq(Split(NoSplit.orNL(next(ft).right.is[T.EOF]), 0))
// End files with trailing newline
case FormatToken(_, _: T.EOF, _) => Seq(Split(Newline, 0))
case FormatToken(_: T.BOF, _, _) => Seq(Split(NoSplit, 0))
case FormatToken(_, _: T.EOF, _) => Seq(
Split(Newline, 0), // End files with trailing newline
)
case FormatToken(_: T.Shebang, _, _) => Seq(Split(Newline2x(ft), 0))
case FormatToken(start: T.Interpolation.Start, _, m) =>
val end = matching(start)
val okNewlines = style.newlines.inInterpolation
Expand Down

0 comments on commit 2b580df

Please sign in to comment.