Skip to content

Commit

Permalink
RegexCompat: fix docstring patterns for \r
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Feb 1, 2025
1 parent 6f9b00a commit 2583145
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ private[scalafmt] object RegexCompat {
val leadingAsteriskSpace = Pattern.compile("\\h*\\r*\\n(\\h*+)(?:[*][^*])?")

val docstringLine = Pattern
.compile("^(?:\\h*+\\*)?(\\h*+)(.*?)\\h*+$", Pattern.MULTILINE)
.compile("^(\\h*+)([*]\\h*+)?.*$", Pattern.MULTILINE)

private val emptyLines = "\\h*+(\n\\h*+\\*?\\h*+)*"
private val emptyLines = "\\h*(\\r*\\n\\h*+\\*?\\h*+)*"

val emptyDocstring = Pattern.compile(s"^/\\*\\*$emptyLines\\*/$$")
val emptyDocstring = Pattern.compile(s"^/\\*\\*$emptyLines\\*/\\h*+\\r*+$$")

val onelineDocstring = {
val oneline = "[^*\n\\h](?:[^\n]*[^\n\\h])?"
Pattern.compile(s"^/\\*\\*$emptyLines($oneline)$emptyLines\\*/$$")
val oneline = "[^*\\s\\h](?:.*?[^\\s\\h])?"
Pattern.compile(s"^/\\*\\*$emptyLines($oneline)$emptyLines\\*/\\h*+\\r*+$$")
}

val docstringLeadingSpace = Pattern.compile("^\\h++")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ object RegexCompat {

val leadingAsteriskSpace = pat("\\h*\\r*\\n(\\h*)(?:[*][^*])?")

val docstringLine = pat("^(?:\\h*\\*)?(\\h*)(.*?)\\h*$", Pattern.MULTILINE)
val docstringLine = pat("^(\\h*)([*]\\h*)?.*$", Pattern.MULTILINE)

val emptyLines = fixHorizontalSpaceInRegex("\\h*(\n\\h*\\*?\\h*)*")
private val emptyLines = "\\h*(\\r*\\n\\h*\\*?\\h*)*"

val emptyDocstring = pat(s"^/\\*\\*$emptyLines\\*/$$")
val emptyDocstring = pat(s"^/\\*\\*$emptyLines\\*/\\h*\\r*$$")

val onelineDocstring = {
val oneline = fixHorizontalSpaceInRegex("[^*\n\\h](?:[^\n]*[^\n\\h])?")
pat(s"^/\\*\\*$emptyLines($oneline)$emptyLines\\*/$$")
val oneline = "[^*\\s\\h](?:.*?[^\\s\\h])?"
pat(s"^/\\*\\*$emptyLines($oneline)$emptyLines\\*/\\h*\\r*$$")
}

val docstringLeadingSpace = pat("^\\h+")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1098,26 +1098,33 @@ class FormatWriter(formatOps: FormatOps) {

private def formatNoWrap(): Unit = {
// remove "/*" (keep one asterisk) and "*/"
val trimmed = CharBuffer.wrap(text, 2, text.length - 2)
val matcher = docstringLine.matcher(trimmed)
val off = 2 // matcher.region is broken in scala-native
val matcher = docstringLine
.matcher(CharBuffer.wrap(text, off, text.length - 2))
sb.append("/**")
val sbLen = sb.length
@tailrec
def iter(prevWasBlank: Boolean): Unit = if (matcher.find()) {
val contentBeg = matcher.start(2)
val contentEnd = matcher.end(2)
if (contentBeg == contentEnd) iter(true)
val (marginBeg, marginEnd) = {
val beg2 = matcher.start(2)
// +1 is for leading asterisk
if (beg2 >= 0) (beg2 + 1, matcher.end(2))
else (matcher.start(), matcher.end(1))
}
val contentBeg = marginEnd + off
val contentLen = State
.getLineLength(text, contentBeg, off + matcher.end())
if (contentLen == 0) iter(true)
else {
if (sb.length != sbLen) {
if (prevWasBlank) appendBreak()
appendBreak().append(margin)
} else if (style.docstrings.skipFirstLineIf(prevWasBlank))
appendBreak().append(margin)
else sb.append(' ')
val extraMargin = matcher.end(1) - matcher.start(1) -
margin.length
val extraMargin = marginEnd - marginBeg - margin.length
if (extraMargin > 0) sb.append(getIndentation(extraMargin))
sb.add(trimmed, contentBeg, contentEnd)
sb.add(text, contentBeg, contentBeg + contentLen)
iter(false)
}
}
Expand Down

0 comments on commit 2583145

Please sign in to comment.