Skip to content

Commit

Permalink
FormatWriter: escape end marker labels if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Sep 17, 2021
1 parent d71411d commit 345cdc7
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ class FormatWriter(formatOps: FormatOps) {
sb.append(getNewlines(numBlanks))
.append(getIndentation(indent))
.append("end ")
.append(label)
val escape = !nonOpIdPattern.matcher(label).matches()
if (escape) sb.append('`')
sb.append(label)
if (escape) sb.append('`')
}
}

Expand Down Expand Up @@ -1637,6 +1640,8 @@ object FormatWriter {
private def splitAsIterator(regex: Pattern)(value: String): Iterator[String] =
regex.splitAsStream(value).iterator().asScala

private val nonOpIdPattern = Pattern.compile("\\w+")

// "slc" stands for single-line comment
private val slcDelim = Pattern.compile("\\h++")
// "mlc" stands for multi-line comment
Expand Down
4 changes: 1 addition & 3 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1975,8 +1975,6 @@ rewrite.scala3.insertEndMarkerMinLines = 1
object `Accept-Ranges`:
val r = ""
>>>
test does not parse
object `Accept-Ranges`:
val r = ""
end Accept-Ranges
^
end `Accept-Ranges`
Original file line number Diff line number Diff line change
Expand Up @@ -1924,8 +1924,6 @@ rewrite.scala3.insertEndMarkerMinLines = 1
object `Accept-Ranges`:
val r = ""
>>>
test does not parse
object `Accept-Ranges`:
val r = ""
end Accept-Ranges
^
end `Accept-Ranges`
Original file line number Diff line number Diff line change
Expand Up @@ -2016,8 +2016,6 @@ rewrite.scala3.insertEndMarkerMinLines = 1
object `Accept-Ranges`:
val r = ""
>>>
test does not parse
object `Accept-Ranges`:
val r = ""
end Accept-Ranges
^
end `Accept-Ranges`
Original file line number Diff line number Diff line change
Expand Up @@ -2134,8 +2134,6 @@ rewrite.scala3.insertEndMarkerMinLines = 1
object `Accept-Ranges`:
val r = ""
>>>
test does not parse
object `Accept-Ranges`:
val r = ""
end Accept-Ranges
^
end `Accept-Ranges`

0 comments on commit 345cdc7

Please sign in to comment.