Skip to content

Commit d169e36

Browse files
committed
Added ability to highlight comments in repl
1 parent 791d9bb commit d169e36

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

compiler/src/dotty/tools/dotc/parsing/Scanners.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ object Scanners {
177177
/** All doc comments kept by their end position in a `Map` */
178178
private[this] var docstringMap: SortedMap[Int, Comment] = SortedMap.empty
179179

180+
/* All comments positions will be stored in this */
181+
var commentPositions:mutable.MutableList[Position] = mutable.MutableList()
182+
180183
private[this] def addComment(comment: Comment): Unit = {
181184
val lookahead = lookaheadReader()
182185
def nextPos: Int = (lookahead.getc(): @switch) match {
@@ -616,6 +619,7 @@ object Scanners {
616619
if (keepComments) {
617620
val pos = Position(start, charOffset - 1, start)
618621
val comment = Comment(pos, flushBuf(commentBuf))
622+
commentPositions += pos
619623

620624
if (comment.isDocComment) {
621625
addComment(comment)

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import dotty.tools.dotc.util.Positions.Position
1111
import dotty.tools.dotc.util.SourceFile
1212

1313
import java.util.Arrays
14+
import scala.collection.mutable
1415

1516
/** This object provides functions for syntax highlighting in the REPL */
1617
object SyntaxHighlighting {
@@ -48,6 +49,11 @@ object SyntaxHighlighting {
4849
highlightRange(pos.start, pos.end, color)
4950
}
5051

52+
def highlightComments(commentsPos: mutable.MutableList[Position]): Unit = {
53+
for (pos <- commentsPos)
54+
highlightPosition(pos,CommentColor)
55+
}
56+
5157
val scanner = new Scanner(source)
5258
while (scanner.token != EOF) {
5359
val start = scanner.offset
@@ -81,6 +87,8 @@ object SyntaxHighlighting {
8187
}
8288
}
8389

90+
highlightComments(scanner.commentPositions)
91+
8492
object TreeHighlighter extends untpd.UntypedTreeTraverser {
8593
import untpd._
8694

compiler/src/dotty/tools/repl/ReplDriver.scala

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,7 @@ class ReplDriver(settings: Array[String],
282282
x.symbol
283283
}
284284
.foreach { sym =>
285-
// FIXME syntax highlighting on comment is currently not working
286-
// out.println(SyntaxHighlighting.highlight("// defined " + sym.showUser))
287-
val message = "// defined " + sym.showUser
288-
if (ctx.settings.color.value != "never") {
289-
println(SyntaxHighlighting.CommentColor + message + SyntaxHighlighting.NoColor)
290-
} else {
291-
println(message)
292-
}
285+
out.println(SyntaxHighlighting.highlight("// defined " + sym.showUser))
293286
}
294287

295288

compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class SyntaxHighlightingTests extends DottyTest {
2828
}
2929

3030
@Test
31-
@Ignore("Comments are currently not supported")
3231
def comments = {
3332
test("// a", "<C|// a>")
3433
test("/** a */", "<C|/** a */>")

0 commit comments

Comments
 (0)