Skip to content

Fix #2957 single quote highlighting and refactor #2995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 18, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import parsing.Tokens._
import scala.annotation.switch
import scala.collection.mutable.StringBuilder
import core.Contexts.Context
import util.Chars.{ LF, FF, CR, SU }
import util.Chars
import Highlighting.{Highlight, HighlightBuffer}

/** This object provides functions for syntax highlighting in the REPL */
Expand Down Expand Up @@ -74,7 +74,7 @@ object SyntaxHighlighting {
newBuf += n
prev = n
if (remaining.nonEmpty) takeChar() // drop 1 for appendLiteral
appendLiteral('"', next == "\"\"\"")
appendString('"', next == "\"\"\"")
} else {
if (n.isUpper && keywordStart) {
appendWhile(n, !typeEnders.contains(_), typeDef)
Expand Down Expand Up @@ -115,9 +115,9 @@ object SyntaxHighlighting {
case '@' =>
appendWhile('@', !typeEnders.contains(_), annotation)
case '\"' =>
appendLiteral('\"', multiline = remaining.take(2).mkString == "\"\"")
appendString('\"', multiline = remaining.take(2).mkString == "\"\"")
case '\'' =>
appendLiteral('\'')
appendSingleQuote('\'')
case '`' =>
appendTo('`', _ == '`', none)
case _ => {
Expand Down Expand Up @@ -169,16 +169,15 @@ object SyntaxHighlighting {
if (curr == '*') open += 1
}

(curr: @switch) match {
case LF | FF | CR | SU => newBuf append CommentColor
case _ => ()
if (Chars.isLineBreakChar(curr)) {
newBuf append CommentColor
}
}
prev = curr
newBuf append NoColor
}

def appendLiteral(delim: Char, multiline: Boolean = false) = {
def appendString(delim: Char, multiline: Boolean = false) = {
var curr: Char = 0
var continue = true
var closing = 0
Expand Down Expand Up @@ -247,15 +246,30 @@ object SyntaxHighlighting {
closing = 0
}

(curr: @switch) match {
case LF | FF | CR | SU => newBuf append LiteralColor
case _ => ()
if (Chars.isLineBreakChar(curr)) {
newBuf append LiteralColor
}
}
newBuf append NoColor
prev = curr
}

def appendSingleQuote(delim: Char) = remaining.take(3) match {
case chr #:: '\'' #:: _ => // single character
newBuf append LiteralColor
newBuf appendAll s"'$chr'"
newBuf append NoColor
takeChars(2)
prev = '\''
case '\\' #:: chr #:: '\'' #:: _ => // escaped character
newBuf append LiteralColor
newBuf appendAll s"'\\$chr'"
newBuf append NoColor
takeChars(3)
prev = '\''
case _ => appendWhile(delim, !typeEnders.contains(_), literal)
}

def append(c: Char, shouldHL: String => Boolean, highlight: String => String) = {
var curr: Char = 0
val sb = new StringBuilder(s"$c")
Expand Down