-
Notifications
You must be signed in to change notification settings - Fork 21
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
REPL: add syntax highlighting, like the Scala 3 REPL (and Ammonite) #12273
Comments
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
@retronym passed along a link to this which might be useful: scala/scala@60ee992 oh, but Paul ripped some of that out again in scala/scala@317a105 |
scala> val unit = newCompilationUnit("class C { def foo = for (x <- (1 to 10)) yield null }"); val scanner = new syntaxAnalyzer.UnitScanner(unit); scanner.init(); while (scanner.token != ast.parser.Tokens.EOF) { val x = scanner.toString; val o = scanner.offset; scanner.
nextToken(); println((x, o, scanner.lastOffset))}
('class',0,5)
(id(C),11,12)
('{',13,14)
('def',15,18)
(id(foo),19,22)
('=',23,24)
('for',25,28)
('(',29,30)
(id(x),30,31)
('<-',32,34)
('(',35,36)
(int(1),36,37)
(id(to),38,40)
(int(10),41,43)
(')',43,44)
(')',44,45)
('yield',46,51)
('null',52,56)
('}',57,58)
val unit: $r.intp.global.CompilationUnit = <console>
val scanner: $r.intp.global.syntaxAnalyzer.UnitScanner = eof |
If you'd like to detect Here's what can be done already:
|
for the part that involves interfacing with JLine, looking at Dotty's implementation should be helpful. according to https://github.com/lampepfl/dotty/blob/master/docs/blog/_posts/2017-10-16-fourth-dotty-milestone-release.md , “we use code adapted from the Ammonite REPL to provide syntax highlighting”, so maybe look there as well (but note that Ammonite uses Scalaparse which is a dependency we don't want to take on) relevant source files include:
and files in dotty/tools/repl such as these and perhaps others:
note that The key snippets in val lineReader = LineReaderBuilder
...
.highlighter(new Highlighter)
...
...
private class Highlighter(using Context) extends reader.Highlighter {
def highlight(reader: LineReader, buffer: String): AttributedString = {
val highlighted = SyntaxHighlighting.highlight(buffer)
AttributedString.fromAnsi(highlighted)
}
... and I think that the use of scala> def inc(x: Int) = x + 1
def inc(x: Int): Int |
I have some WIP at https://github.com/SethTisue/scala/commits/repl-highlighting At present it only uses the scanner, not the parser. No tests yet. It's a start... |
How would using the parser look? Rather than talk to the parser ourselves, we should use the presentation compiler, which is designed for this sort of usage, and which the REPL already uses anyway, for doing tab completion. (That happens in Once we have a tree, we can copy the approach taken by |
As Jason has noted, detecting comments needs special handling. In Dotty, I wondered if there might be something comparable in the presentation compiler already, and, well, there sort of is: |
as @allanrenucci commented at scala/scala#7645 (comment) ,
someone want to have a look and see it how even works? does it involve external dependencies?
JLine info page on this is https://github.com/jline/jline3/wiki/Highlighting-and-parsing
The text was updated successfully, but these errors were encountered: