aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala19
-rw-r--r--compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala44
-rw-r--r--compiler/src/dotty/tools/dotc/util/SourcePosition.scala8
-rw-r--r--doc-tool/src/dotty/tools/dottydoc/model/comment/CommentParser.scala3
4 files changed, 56 insertions, 18 deletions
diff --git a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala
index 86f34e64d..e20f846ac 100644
--- a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala
+++ b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala
@@ -6,6 +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 Highlighting.{Highlight, HighlightBuffer}
/** This object provides functions for syntax highlighting in the REPL */
@@ -26,7 +27,7 @@ object SyntaxHighlighting {
private def valDef(str: String) = ValDefColor + str + NoColor
private def operator(str: String) = TypeColor + str + NoColor
private def annotation(str: String) =
- if (str.trim == "@") str else AnnotationColor + str + NoColor
+ if (str.trim == "@") str else { AnnotationColor + str + NoColor }
private val tripleQs = Console.RED_B + "???" + NoColor
private val keywords: Seq[String] = for {
@@ -152,7 +153,11 @@ object SyntaxHighlighting {
var open = 1
while (open > 0 && remaining.nonEmpty) {
curr = takeChar()
- newBuf += curr
+ if (curr == '@') {
+ appendWhile('@', !typeEnders.contains(_), annotation)
+ newBuf append CommentColor
+ }
+ else newBuf += curr
if (curr == '*' && remaining.nonEmpty) {
curr = takeChar()
@@ -163,6 +168,11 @@ object SyntaxHighlighting {
newBuf += curr
if (curr == '*') open += 1
}
+
+ (curr: @switch) match {
+ case LF | FF | CR | SU => newBuf append CommentColor
+ case _ => ()
+ }
}
prev = curr
newBuf append NoColor
@@ -236,6 +246,11 @@ object SyntaxHighlighting {
newBuf += curr
closing = 0
}
+
+ (curr: @switch) match {
+ case LF | FF | CR | SU => newBuf append LiteralColor
+ case _ => ()
+ }
}
newBuf append NoColor
prev = curr
diff --git a/compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala b/compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala
index 190445d60..17eb8d39b 100644
--- a/compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala
+++ b/compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala
@@ -5,9 +5,12 @@ package reporting
import core.Contexts.Context
import core.Decorators._
import printing.Highlighting.{Blue, Red}
+import printing.SyntaxHighlighting
import diagnostic.{ErrorMessageID, Message, MessageContainer, NoExplanation}
import diagnostic.messages._
import util.SourcePosition
+import util.Chars.{ LF, CR, FF, SU }
+import scala.annotation.switch
import scala.collection.mutable
@@ -38,20 +41,37 @@ trait MessageRendering {
*/
def sourceLines(pos: SourcePosition)(implicit ctx: Context): (List[String], List[String], Int) = {
var maxLen = Int.MinValue
- def render(xs: List[Int]) =
- xs.map(pos.source.offsetToLine(_))
- .map { lineNbr =>
- val prefix = s"${lineNbr + 1} |"
- maxLen = math.max(maxLen, prefix.length)
- (prefix, pos.lineContent(lineNbr).stripLineEnd)
- }
- .map { case (prefix, line) =>
- val lnum = Red(" " * math.max(0, maxLen - prefix.length) + prefix)
- hl"$lnum$line"
- }
+ def render(offsetAndLine: (Int, String)): String = {
+ val (offset, line) = offsetAndLine
+ val lineNbr = pos.source.offsetToLine(offset)
+ val prefix = s"${lineNbr + 1} |"
+ maxLen = math.max(maxLen, prefix.length)
+ val lnum = Red(" " * math.max(0, maxLen - prefix.length) + prefix).show
+ lnum + line.stripLineEnd
+ }
+
+ def linesFrom(arr: Array[Char]): List[String] = {
+ def pred(c: Char) = (c: @switch) match {
+ case LF | CR | FF | SU => true
+ case _ => false
+ }
+ val (line, rest0) = arr.span(!pred(_))
+ val (_, rest) = rest0.span(pred)
+ new String(line) :: { if (rest.isEmpty) Nil else linesFrom(rest) }
+ }
+ val syntax =
+ if (ctx.settings.color.value != "never")
+ SyntaxHighlighting(pos.linesSlice).toArray
+ else pos.linesSlice
+ val lines = linesFrom(syntax)
val (before, after) = pos.beforeAndAfterPoint
- (render(before), render(after), maxLen)
+
+ (
+ before.zip(lines).map(render),
+ after.zip(lines.drop(before.length)).map(render),
+ maxLen
+ )
}
/** The column markers aligned under the error */
diff --git a/compiler/src/dotty/tools/dotc/util/SourcePosition.scala b/compiler/src/dotty/tools/dotc/util/SourcePosition.scala
index aad4995d8..be5c46995 100644
--- a/compiler/src/dotty/tools/dotc/util/SourcePosition.scala
+++ b/compiler/src/dotty/tools/dotc/util/SourcePosition.scala
@@ -12,9 +12,14 @@ extends interfaces.SourcePosition {
def lineContent: String = source.lineContent(point)
def point: Int = pos.point
+
/** The line of the position, starting at 0 */
def line: Int = source.offsetToLine(point)
+ /** Extracts the lines from the underlying source file as `Array[Char]`*/
+ def linesSlice: Array[Char] =
+ source.content.slice(source.startOfLine(start), source.nextLine(end))
+
/** The lines of the position */
def lines: List[Int] =
List.range(source.offsetToLine(start), source.offsetToLine(end + 1)) match {
@@ -25,9 +30,6 @@ extends interfaces.SourcePosition {
def lineOffsets: List[Int] =
lines.map(source.lineToOffset(_))
- def lineContent(lineNumber: Int): String =
- source.lineContent(source.lineToOffset(lineNumber))
-
def beforeAndAfterPoint: (List[Int], List[Int]) =
lineOffsets.partition(_ <= point)
diff --git a/doc-tool/src/dotty/tools/dottydoc/model/comment/CommentParser.scala b/doc-tool/src/dotty/tools/dottydoc/model/comment/CommentParser.scala
index b7a33a7ef..5ff28f914 100644
--- a/doc-tool/src/dotty/tools/dottydoc/model/comment/CommentParser.scala
+++ b/doc-tool/src/dotty/tools/dottydoc/model/comment/CommentParser.scala
@@ -6,6 +6,7 @@ import dotty.tools.dottydoc.util.syntax._
import dotty.tools.dotc.util.Positions._
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.Contexts.Context
+import dotty.tools.dotc.core.Decorators._
import scala.collection.mutable
import dotty.tools.dotc.config.Printers.dottydoc
import scala.util.matching.Regex
@@ -196,7 +197,7 @@ trait CommentParser extends util.MemberLookup {
)
for ((key, _) <- bodyTags) ctx.docbase.warn(
- s"Tag '@${key.name}' is not recognised",
+ hl"Tag '${"@" + key.name}' is not recognised",
// FIXME: here the position is stretched out over the entire comment,
// with the point being at the very end. This ensures that the entire
// comment will be visible in error reporting. A more fine-grained