Skip to content

Commit 9f5711f

Browse files
maxovfelixmulder
authored andcommitted
Fix #2957 single quote highlighting and refactor (#2995)
* Fix #2957 single quote highlighting and refactor * Take out debug printlns in SyntaxHighlighter * Fix SyntaxHighlighter character handling
1 parent fed2dc2 commit 9f5711f

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import parsing.Tokens._
66
import scala.annotation.switch
77
import scala.collection.mutable.StringBuilder
88
import core.Contexts.Context
9-
import util.Chars.{ LF, FF, CR, SU }
9+
import util.Chars
1010
import Highlighting.{Highlight, HighlightBuffer}
1111

1212
/** This object provides functions for syntax highlighting in the REPL */
@@ -74,7 +74,7 @@ object SyntaxHighlighting {
7474
newBuf += n
7575
prev = n
7676
if (remaining.nonEmpty) takeChar() // drop 1 for appendLiteral
77-
appendLiteral('"', next == "\"\"\"")
77+
appendString('"', next == "\"\"\"")
7878
} else {
7979
if (n.isUpper && keywordStart) {
8080
appendWhile(n, !typeEnders.contains(_), typeDef)
@@ -115,9 +115,9 @@ object SyntaxHighlighting {
115115
case '@' =>
116116
appendWhile('@', !typeEnders.contains(_), annotation)
117117
case '\"' =>
118-
appendLiteral('\"', multiline = remaining.take(2).mkString == "\"\"")
118+
appendString('\"', multiline = remaining.take(2).mkString == "\"\"")
119119
case '\'' =>
120-
appendLiteral('\'')
120+
appendSingleQuote('\'')
121121
case '`' =>
122122
appendTo('`', _ == '`', none)
123123
case _ => {
@@ -169,16 +169,15 @@ object SyntaxHighlighting {
169169
if (curr == '*') open += 1
170170
}
171171

172-
(curr: @switch) match {
173-
case LF | FF | CR | SU => newBuf append CommentColor
174-
case _ => ()
172+
if (Chars.isLineBreakChar(curr)) {
173+
newBuf append CommentColor
175174
}
176175
}
177176
prev = curr
178177
newBuf append NoColor
179178
}
180179

181-
def appendLiteral(delim: Char, multiline: Boolean = false) = {
180+
def appendString(delim: Char, multiline: Boolean = false) = {
182181
var curr: Char = 0
183182
var continue = true
184183
var closing = 0
@@ -247,15 +246,30 @@ object SyntaxHighlighting {
247246
closing = 0
248247
}
249248

250-
(curr: @switch) match {
251-
case LF | FF | CR | SU => newBuf append LiteralColor
252-
case _ => ()
249+
if (Chars.isLineBreakChar(curr)) {
250+
newBuf append LiteralColor
253251
}
254252
}
255253
newBuf append NoColor
256254
prev = curr
257255
}
258256

257+
def appendSingleQuote(delim: Char) = remaining.take(3) match {
258+
case chr #:: '\'' #:: _ => // single character
259+
newBuf append LiteralColor
260+
newBuf appendAll s"'$chr'"
261+
newBuf append NoColor
262+
takeChars(2)
263+
prev = '\''
264+
case '\\' #:: chr #:: '\'' #:: _ => // escaped character
265+
newBuf append LiteralColor
266+
newBuf appendAll s"'\\$chr'"
267+
newBuf append NoColor
268+
takeChars(3)
269+
prev = '\''
270+
case _ => appendWhile(delim, !typeEnders.contains(_), literal)
271+
}
272+
259273
def append(c: Char, shouldHL: String => Boolean, highlight: String => String) = {
260274
var curr: Char = 0
261275
val sb = new StringBuilder(s"$c")

0 commit comments

Comments
 (0)