Skip to content

Commit

Permalink
FormatWriter: prepare for configurable line ending
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jul 8, 2024
1 parent 199e729 commit cfed1f8
Showing 1 changed file with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ class FormatWriter(formatOps: FormatOps) {
else locations.extraBlankTokens.get(last.meta.idx)
.fold(0)(x => if (x > 0) 1 else 0)
}
sb.append(getNewlines(numBlanks)).append(getIndentation(indent))
.append("end ").append(label)
sb.append(locations.getNewlines(numBlanks))
.append(getIndentation(indent)).append("end ").append(label)
}
}

// missing braces
if (location.missingBracesIndent.nonEmpty) {
location.missingBracesIndent.toSeq.sorted(Ordering.Int.reverse)
.foreach(i => sb.append('\n').append(getIndentation(i)).append("}"))
.foreach(i => locations.startNewLine(getIndentation(i)).append("}"))
if (location.missingBracesOpenOrTuck) {
skipWs = true
sb.append(" ")
Expand Down Expand Up @@ -139,7 +139,7 @@ class FormatWriter(formatOps: FormatOps) {
) replaceRedundantBraces(result)
}

new FormatLocations(result)
new FormatLocations(result, "\n")
}

private def replaceRedundantBraces(locations: Array[FormatLocation]): Unit = {
Expand Down Expand Up @@ -429,10 +429,29 @@ class FormatWriter(formatOps: FormatOps) {
}
}

class FormatLocations(val locations: Array[FormatLocation]) {
class FormatLocations(val locations: Array[FormatLocation], val eol: String) {

val tokenAligns: Map[Int, Int] = alignmentTokens

// cache newlines to some level
private val extraNewlines: IndexedSeq[String] = {
val size = 4
val buf = new mutable.ArrayBuffer[String](size)
buf += eol
// use the previous indentation to add another newline
(1 until size).foreach(_ => buf += eol + buf.last)
buf.toIndexedSeq
}

// see if blank level is cached first
private[internal] def getNewlines(extra: Int): String =
if (extra < extraNewlines.length) extraNewlines(extra)
else eol * (1 + extra)

private[internal] def startNewLine(indent: String)(implicit
sb: StringBuilder,
) = sb.append(eol).append(indent)

def foreach(f: Entry => Unit): Unit = Iterator.range(0, locations.length)
.foreach { i =>
val entry = new Entry(i)
Expand Down Expand Up @@ -724,10 +743,10 @@ class FormatWriter(formatOps: FormatOps) {
val appendLineBreak: () => Unit =
if (useSlc) {
val spaces: String = getIndentation(indent)
() => sb.append('\n').append(spaces).append("//")
() => startNewLine(spaces).append("//")
} else {
val spaces: String = getIndentation(indent + 1)
() => sb.append('\n').append(spaces).append('*')
() => startNewLine(spaces).append('*')
}
val contents = text.substring(2).trim
val wordIter = splitAsIterator(slcDelim)(contents)
Expand Down Expand Up @@ -791,8 +810,7 @@ class FormatWriter(formatOps: FormatOps) {
sb.append(leadingAsteriskSpace.matcher(trimmed).replaceAll(spaces))
}

private def appendLineBreak(): Unit = sb.append('\n').append(spaces)
.append('*')
private def appendLineBreak(): Unit = startNewLine(spaces).append('*')

private val wf = new WordFormatter(appendLineBreak)

Expand Down Expand Up @@ -1112,7 +1130,7 @@ class FormatWriter(formatOps: FormatOps) {
}

@inline
private def appendBreak() = sb.append('\n').append(spaces).append('*')
private def appendBreak() = startNewLine(spaces).append('*')
}

}
Expand Down Expand Up @@ -1901,21 +1919,6 @@ object FormatWriter {
private def getIndentation(len: Int): String =
if (len < indentations.length) indentations(len) else " " * len

// cache newlines to some level
private val extraNewlines: IndexedSeq[String] = {
val size = 4
val buf = new mutable.ArrayBuffer[String](size)
buf += "\n"
// use the previous indentation to add another newline
(1 until size).foreach(_ => buf += "\n" + buf.last)
buf.toIndexedSeq
}

// see if blank level is cached first
private def getNewlines(extra: Int): String =
if (extra < extraNewlines.length) extraNewlines(extra)
else "\n" * (1 + extra)

private val trailingSpace = Pattern.compile("\\h++$", Pattern.MULTILINE)
private def removeTrailingWhiteSpace(str: String): String = trailingSpace
.matcher(str).replaceAll("")
Expand Down

0 comments on commit cfed1f8

Please sign in to comment.