From 09d48b30c71d0d60e7e355828e7bf52069c08e0c Mon Sep 17 00:00:00 2001 From: Max Ovsiankin Date: Fri, 18 Aug 2017 09:16:14 -0700 Subject: [PATCH 1/3] Fix #2957 single quote highlighting and refactor --- .../dotc/printing/SyntaxHighlighting.scala | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala index 87ce31ad9b9a..e1cd6cd2ac71 100644 --- a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala +++ b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala @@ -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 */ @@ -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) @@ -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 _ => { @@ -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 @@ -247,15 +246,32 @@ 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 match { + case '\'' #:: chr #:: '\'' #:: _ => // single character + newBuf append LiteralColor + newBuf appendAll s"'$chr'" + newBuf append NoColor + takeChars(3) + println(remaining.take(4)) + prev = '\'' + case '\'' #:: '\\' #:: chr #:: '\'' #:: _ => // escaped character + newBuf append LiteralColor + newBuf appendAll s"'\\$chr'" + newBuf append NoColor + takeChars(3) + println(remaining.take(4)) + 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") From ba64a6375f3c000c86f10c5f59b52595c3094721 Mon Sep 17 00:00:00 2001 From: Max Ovsiankin Date: Fri, 18 Aug 2017 09:26:51 -0700 Subject: [PATCH 2/3] Take out debug printlns in SyntaxHighlighter --- .../src/dotty/tools/dotc/printing/SyntaxHighlighting.scala | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala index e1cd6cd2ac71..32950e0afdd4 100644 --- a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala +++ b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala @@ -260,14 +260,12 @@ object SyntaxHighlighting { newBuf appendAll s"'$chr'" newBuf append NoColor takeChars(3) - println(remaining.take(4)) prev = '\'' case '\'' #:: '\\' #:: chr #:: '\'' #:: _ => // escaped character newBuf append LiteralColor newBuf appendAll s"'\\$chr'" newBuf append NoColor - takeChars(3) - println(remaining.take(4)) + takeChars(4) prev = '\'' case _ => appendWhile(delim, !typeEnders.contains(_), literal) } From 5cf617374761ae09acb5c5a0fa1652d67ff615b9 Mon Sep 17 00:00:00 2001 From: Max Ovsiankin Date: Fri, 18 Aug 2017 09:36:20 -0700 Subject: [PATCH 3/3] Fix SyntaxHighlighter character handling --- .../dotty/tools/dotc/printing/SyntaxHighlighting.scala | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala index 32950e0afdd4..eb88cb64f766 100644 --- a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala +++ b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala @@ -254,18 +254,18 @@ object SyntaxHighlighting { prev = curr } - def appendSingleQuote(delim: Char) = remaining match { - case '\'' #:: chr #:: '\'' #:: _ => // single character + def appendSingleQuote(delim: Char) = remaining.take(3) match { + case chr #:: '\'' #:: _ => // single character newBuf append LiteralColor newBuf appendAll s"'$chr'" newBuf append NoColor - takeChars(3) + takeChars(2) prev = '\'' - case '\'' #:: '\\' #:: chr #:: '\'' #:: _ => // escaped character + case '\\' #:: chr #:: '\'' #:: _ => // escaped character newBuf append LiteralColor newBuf appendAll s"'\\$chr'" newBuf append NoColor - takeChars(4) + takeChars(3) prev = '\'' case _ => appendWhile(delim, !typeEnders.contains(_), literal) }