Skip to content

Commit 9a4a064

Browse files
slothspotallanrenucci
authored andcommitted
Add annotations highlight
- Highlight is based on MemberDef with annotations present - move scanner highlighter before parser so that annotation @inline wholdn't be highlighted as keyword - annotations are highlighted when full member definition is available
1 parent ff7b0a2 commit 9a4a064

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

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

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -369,17 +369,12 @@ object SyntaxHighlighting {
369369
override def doReport(m: MessageContainer)(implicit ctx: Context): Unit = ()
370370
}
371371

372-
private val ignoredKwds = Set(nme.ARROWkw, nme.EQ, nme.EQL, nme.COLONkw)
373-
374372
def highlight(in: String)(ctx0: Context): String = {
375373
import dotty.tools.dotc.ast.untpd._
376374

377375
implicit val ctx: Context = ctx0.fresh.setReporter(new NoReporter)
378376

379377
val source = new SourceFile("<highlighting>", in.toCharArray)
380-
val parser = new Parser(source)
381-
val trees = parser.blockStatSeq()
382-
383378
val colorAt = Array.fill(in.length)(NoColor)
384379

385380
def highlightRange(from: Int, to: Int, color: String) = {
@@ -394,18 +389,39 @@ object SyntaxHighlighting {
394389
def highlightPosition(pos: Position, color: String) =
395390
if (pos.exists) highlightRange(pos.start, pos.end, color)
396391

392+
val scanner = new Scanner(source)
393+
394+
while (scanner.token != EOF) {
395+
val isKwd = alphaKeywords.contains(scanner.token)
396+
val offsetStart = scanner.offset
397+
398+
if (scanner.token == IDENTIFIER && scanner.name == nme.???) {
399+
highlightRange(scanner.offset, scanner.offset + scanner.name.length, Console.RED_B)
400+
}
401+
scanner.nextToken()
402+
403+
if (isKwd) {
404+
val offsetEnd = scanner.lastOffset
405+
highlightPosition(Position(offsetStart, offsetEnd), KeywordColor)
406+
}
407+
}
408+
397409
val treeHighlighter = new UntypedTreeTraverser {
398410
def traverse(tree: Tree)(implicit ctx: Context): Unit = {
399411
tree match {
400-
case id: Ident if id.isType =>
412+
case id : Ident if id.isType =>
401413
highlightPosition(id.pos, TypeColor)
402414
case tpe : TypeDef =>
415+
for (annotation <- tpe.rawMods.annotations)
416+
highlightPosition(annotation.pos, AnnotationColor)
403417
highlightPosition(tpe.namePos, TypeColor)
404418
case _ : TypTree =>
405419
highlightPosition(tree.pos, TypeColor)
406420
case mod: ModuleDef =>
407421
highlightPosition(mod.namePos, TypeColor)
408422
case v : ValOrDefDef =>
423+
for (annotation <- v.rawMods.annotations)
424+
highlightPosition(annotation.pos, AnnotationColor)
409425
highlightPosition(v.namePos, ValDefColor)
410426
highlightPosition(v.tpt.pos, TypeColor)
411427
case _ : Literal =>
@@ -416,26 +432,12 @@ object SyntaxHighlighting {
416432
}
417433
}
418434

435+
val parser = new Parser(source)
436+
val trees = parser.blockStatSeq()
437+
419438
for (tree <- trees)
420439
treeHighlighter.traverse(tree)
421440

422-
val scanner = new Scanner(source)
423-
424-
while (scanner.token != EOF) {
425-
val isKwd = isKeyword(scanner.token) && !ignoredKwds.contains(scanner.name)
426-
val offsetStart = scanner.offset
427-
428-
if (scanner.token == IDENTIFIER && scanner.name == nme.???) {
429-
highlightRange(scanner.offset, scanner.offset + scanner.name.length, Console.RED_B)
430-
}
431-
scanner.nextToken()
432-
433-
if (isKwd) {
434-
val offsetEnd = scanner.lastOffset
435-
highlightPosition(Position(offsetStart, offsetEnd), KeywordColor)
436-
}
437-
}
438-
439441
val sb = new mutable.StringBuilder()
440442

441443
for (idx <- colorAt.indices) {

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,25 @@ class SyntaxHighlightingTests extends DottyTest {
5858

5959
@Test
6060
def annotations = {
61-
test("@tailrec", "<T|@tailrec>")
61+
val source =
62+
"""
63+
|@deprecated
64+
|class Foo {
65+
| @inline val bar = 42
66+
|}
67+
""".stripMargin
68+
69+
val expected =
70+
"""
71+
|<T|@deprecated>
72+
|<K|class> <T|Foo> {
73+
| <T|@inline> <K|val> <V|bar> = <L|42>
74+
|}
75+
""".stripMargin
76+
77+
test(source, expected)
78+
79+
test("@deprecated class Foo", "<T|@deprecated> <K|class> <T|Foo>")
6280
}
6381

6482
@Test

0 commit comments

Comments
 (0)