Skip to content

Commit 3918121

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 50d0fa8 commit 3918121

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
@@ -374,17 +374,12 @@ object SyntaxHighlighting {
374374
override def doReport(m: MessageContainer)(implicit ctx: Context): Unit = ()
375375
}
376376

377-
private val ignoredKwds = Set(nme.ARROWkw, nme.EQ, nme.EQL, nme.COLONkw)
378-
379377
def highlight(in: String)(ctx0: Context): String = {
380378
import dotty.tools.dotc.ast.untpd._
381379

382380
implicit val ctx: Context = ctx0.fresh.setReporter(new NoReporter)
383381

384382
val source = new SourceFile("<highlighting>", in.toCharArray)
385-
val parser = new Parser(source)
386-
val trees = parser.blockStatSeq()
387-
388383
val colorAt = Array.fill(in.length)(NoColor)
389384

390385
def highlightRange(from: Int, to: Int, color: String) = {
@@ -399,18 +394,39 @@ object SyntaxHighlighting {
399394
def highlightPosition(pos: Position, color: String) =
400395
if (pos.exists) highlightRange(pos.start, pos.end, color)
401396

397+
val scanner = new Scanner(source)
398+
399+
while (scanner.token != EOF) {
400+
val isKwd = alphaKeywords.contains(scanner.token)
401+
val offsetStart = scanner.offset
402+
403+
if (scanner.token == IDENTIFIER && scanner.name == nme.???) {
404+
highlightRange(scanner.offset, scanner.offset + scanner.name.length, Console.RED_B)
405+
}
406+
scanner.nextToken()
407+
408+
if (isKwd) {
409+
val offsetEnd = scanner.lastOffset
410+
highlightPosition(Position(offsetStart, offsetEnd), KeywordColor)
411+
}
412+
}
413+
402414
val treeHighlighter = new UntypedTreeTraverser {
403415
def traverse(tree: Tree)(implicit ctx: Context): Unit = {
404416
tree match {
405-
case id: Ident if id.isType =>
417+
case id : Ident if id.isType =>
406418
highlightPosition(id.pos, TypeColor)
407419
case tpe : TypeDef =>
420+
for (annotation <- tpe.rawMods.annotations)
421+
highlightPosition(annotation.pos, AnnotationColor)
408422
highlightPosition(tpe.namePos, TypeColor)
409423
case _ : TypTree =>
410424
highlightPosition(tree.pos, TypeColor)
411425
case mod: ModuleDef =>
412426
highlightPosition(mod.namePos, TypeColor)
413427
case v : ValOrDefDef =>
428+
for (annotation <- v.rawMods.annotations)
429+
highlightPosition(annotation.pos, AnnotationColor)
414430
highlightPosition(v.namePos, ValDefColor)
415431
highlightPosition(v.tpt.pos, TypeColor)
416432
case _ : Literal =>
@@ -421,26 +437,12 @@ object SyntaxHighlighting {
421437
}
422438
}
423439

440+
val parser = new Parser(source)
441+
val trees = parser.blockStatSeq()
442+
424443
for (tree <- trees)
425444
treeHighlighter.traverse(tree)
426445

427-
val scanner = new Scanner(source)
428-
429-
while (scanner.token != EOF) {
430-
val isKwd = isKeyword(scanner.token) && !ignoredKwds.contains(scanner.name)
431-
val offsetStart = scanner.offset
432-
433-
if (scanner.token == IDENTIFIER && scanner.name == nme.???) {
434-
highlightRange(scanner.offset, scanner.offset + scanner.name.length, Console.RED_B)
435-
}
436-
scanner.nextToken()
437-
438-
if (isKwd) {
439-
val offsetEnd = scanner.lastOffset
440-
highlightPosition(Position(offsetStart, offsetEnd), KeywordColor)
441-
}
442-
}
443-
444446
val sb = new mutable.StringBuilder()
445447

446448
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)