Skip to content

New highlighter based on tokens and untyped trees #5137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
263c377
Add UntypedTreeTraverser
slothspot May 16, 2018
5c337a8
Introduce Parser and Scanner based highlighter
slothspot May 16, 2018
abaaa27
Small improvements in highlighter
slothspot May 16, 2018
c639ab8
Switch highlighting tests to new highlighter implementation
slothspot May 16, 2018
88bdbd4
Address review comments
slothspot May 18, 2018
9c85db2
Highlight Ident as type when it is type
slothspot May 18, 2018
90c4107
Update expected highlight for union types
slothspot May 18, 2018
02926b6
Change ctx.error to println
slothspot May 23, 2018
f1662d6
Add annotations highlight
slothspot May 23, 2018
8dc30f4
Remove old highlighter code and ignore tests with unsupported features
slothspot May 28, 2018
ffdc530
Polishing
allanrenucci Jun 1, 2018
9c73b0b
Let's turn on debug mode by default
allanrenucci Jun 1, 2018
368fc5b
Fix position error in annotation parameter list parsing
danielyli Jun 19, 2018
2645cab
Add tests for mid-expression user inputs
danielyli Jun 20, 2018
f95dfb7
Add tests for long and float literals
danielyli Jun 20, 2018
049eafe
Fix rebase breakages
allanrenucci Jun 29, 2018
0748eea
Fix position of parsed applications
smarter Jun 29, 2018
cfe896d
Fix rebase breakage
allanrenucci Sep 19, 2018
01768ca
Use tokens (instead of trees) to highlight literals
allanrenucci Sep 21, 2018
a8dc7e8
Add syntax highlighting support for string interpolator
allanrenucci Sep 21, 2018
2b67780
Highlight `inline` as a "soft" keyword
allanrenucci Sep 24, 2018
feeb420
Address review comments
allanrenucci Sep 26, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use tokens (instead of trees) to highlight literals
Tokens position is less fragile. Specially when code does not parse
  • Loading branch information
allanrenucci committed Sep 26, 2018
commit 01768ca7fcb5d5bedba73bc502ff65a9eba31fac
16 changes: 10 additions & 6 deletions compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ object SyntaxHighlighting {
scanner.nextToken()
val end = scanner.lastOffset

if (alphaKeywords.contains(token))
// Branch order is important. For example,
// `true` is at the same time a keyword and a literal
if (literalTokens.contains(token))
highlightRange(start, end, LiteralColor)
else if (alphaKeywords.contains(token))
highlightRange(start, end, KeywordColor)
else if (token == IDENTIFIER && name == nme.???)
highlightRange(start, end, Console.RED_B)
}

val treeHighlighter = new untpd.UntypedTreeTraverser {
object TreeHighlighter extends untpd.UntypedTreeTraverser {
import untpd._

def ignored(tree: NameTree) = {
Expand All @@ -72,6 +76,9 @@ object SyntaxHighlighting {
name == nme.ERROR || name == nme.CONSTRUCTOR
}

def highlight(trees: List[Tree])(implicit ctx: Context): Unit =
trees.foreach(traverse)

def traverse(tree: Tree)(implicit ctx: Context): Unit = {
tree match {
case tree: NameTree if ignored(tree) =>
Expand All @@ -85,8 +92,6 @@ object SyntaxHighlighting {
highlightPosition(tree.pos, TypeColor)
case _: TypTree =>
highlightPosition(tree.pos, TypeColor)
case _: Literal =>
highlightPosition(tree.pos, LiteralColor)
case _ =>
}
traverseChildren(tree)
Expand All @@ -95,8 +100,7 @@ object SyntaxHighlighting {

val parser = new Parser(source)
val trees = parser.blockStatSeq()
for (tree <- trees)
treeHighlighter.traverse(tree)
TreeHighlighter.highlight(trees)

val highlighted = new StringBuilder()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SyntaxHighlightingTests extends DottyTest {
@Test
@Ignore("Comments are currently not supported")
def comments = {
test("//a", "<C|//a>")
test("// a", "<C|// a>")
test("/** a */", "<C|/** a */>")
test("/* a */", "<C|/* a */>")
}
Expand All @@ -50,7 +50,7 @@ class SyntaxHighlightingTests extends DottyTest {
test("1.1", "<L|1.1>")
test("1.1.toString", "<L|1.1>.toString")
test("1L", "<L|1L>")
test("1Lx", "1Lx")
test("1Lx", "<L|1L>x")
test("1f", "<L|1f>")
test("1.1f", "<L|1.1f>")
test("1.1fx", "1.1fx")
Expand Down Expand Up @@ -94,9 +94,9 @@ class SyntaxHighlightingTests extends DottyTest {
test("var", "<K|var>")
test("var foo", "<K|var> <V|foo>")
test("var foo:", "<K|var> <V|foo>:")
test("var foo: Int", "<K|var> <V|foo>: <T|int>")
test("var foo: Int =", "<K|var> <V|foo>: <T|int> =")
test("var foo: Int = 123", "<K|var> <V|foo>: <T|int> = <L|123>")
test("var foo: Int", "<K|var> <V|foo>: <T|Int>")
test("var foo: Int =", "<K|var> <V|foo>: <T|Int> =")
test("var foo: Int = 123", "<K|var> <V|foo>: <T|Int> = <L|123>")

test("def", "<K|def>")
test("def foo", "<K|def> <V|foo>")
Expand Down