|
| 1 | +package dotty.tools.repl |
| 2 | + |
| 3 | +import dotty.tools.dotc.core.Contexts.Context |
| 4 | +import dotty.tools.dotc.parsing.Scanners.Scanner |
| 5 | +import dotty.tools.dotc.parsing.Tokens._ |
| 6 | +import dotty.tools.dotc.printing.SyntaxHighlighting |
| 7 | +import dotty.tools.dotc.reporting.Reporter |
| 8 | +import dotty.tools.dotc.util.SourceFile |
| 9 | +import org.jline.reader |
| 10 | +import org.jline.reader.LineReader.Option |
| 11 | +import org.jline.reader.Parser.ParseContext |
| 12 | +import org.jline.reader._ |
| 13 | +import org.jline.reader.impl.history.DefaultHistory |
| 14 | +import org.jline.terminal.TerminalBuilder |
| 15 | +import org.jline.utils.AttributedString |
| 16 | + |
| 17 | +final class JLineTerminal { |
| 18 | + private val terminal = TerminalBuilder.terminal() |
| 19 | + private val history = new DefaultHistory |
| 20 | + |
| 21 | + private def blue(str: String) = Console.BLUE + str + Console.RESET |
| 22 | + private val prompt = blue("scala> ") |
| 23 | + private val newLinePrompt = blue(" | ") |
| 24 | + |
| 25 | + /** Blockingly read line from `System.in` |
| 26 | + * |
| 27 | + * This entry point into JLine handles everything to do with terminal |
| 28 | + * emulation. This includes: |
| 29 | + * |
| 30 | + * - Multi-line support |
| 31 | + * - Copy-pasting |
| 32 | + * - History |
| 33 | + * - Syntax highlighting |
| 34 | + * - Auto-completions |
| 35 | + * |
| 36 | + * @throws EndOfFileException This exception is thrown when the user types Ctrl-D. |
| 37 | + */ |
| 38 | + def readLine( |
| 39 | + completer: Completer // provide auto-completions |
| 40 | + )(implicit ctx: Context): String = { |
| 41 | + val lineReader = LineReaderBuilder.builder() |
| 42 | + .terminal(terminal) |
| 43 | + .history(history) |
| 44 | + .completer(completer) |
| 45 | + .highlighter(new Highlighter) |
| 46 | + .parser(new Parser) |
| 47 | + .variable(LineReader.SECONDARY_PROMPT_PATTERN, "%M") |
| 48 | + .option(Option.INSERT_TAB, true) // at the beginning of the line, insert tab instead of completing |
| 49 | + .option(Option.AUTO_FRESH_LINE, true) // if not at start of line before prompt, move to new line |
| 50 | + .build() |
| 51 | + |
| 52 | + lineReader.readLine(prompt) |
| 53 | + } |
| 54 | + |
| 55 | + /** Provide syntax highlighting */ |
| 56 | + private class Highlighter extends reader.Highlighter { |
| 57 | + def highlight(reader: LineReader, buffer: String): AttributedString = { |
| 58 | + val highlighted = SyntaxHighlighting(buffer).mkString |
| 59 | + AttributedString.fromAnsi(highlighted) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** Provide multi-line editing support */ |
| 64 | + private class Parser(implicit ctx: Context) extends reader.Parser { |
| 65 | + |
| 66 | + private class ParsedLine( |
| 67 | + val cursor: Int, // The cursor position within the line |
| 68 | + val line: String, // The unparsed line |
| 69 | + val word: String, // The current word being completed |
| 70 | + val wordCursor: Int // The cursor position within the current word |
| 71 | + ) extends reader.ParsedLine { |
| 72 | + // Using dummy values, not sure what they are used for |
| 73 | + def wordIndex = -1 |
| 74 | + def words = java.util.Collections.emptyList[String] |
| 75 | + } |
| 76 | + |
| 77 | + def parse(line: String, cursor: Int, context: ParseContext): reader.ParsedLine = { |
| 78 | + def parsedLine(word: String, wordCursor: Int) = |
| 79 | + new ParsedLine(cursor, line, word, wordCursor) |
| 80 | + |
| 81 | + def incomplete(): Nothing = throw new EOFError( |
| 82 | + // Using dummy values, not sure what they are used for |
| 83 | + /* line = */ -1, |
| 84 | + /* column = */ -1, |
| 85 | + /* message = */ "", |
| 86 | + /* missing = */ newLinePrompt) |
| 87 | + |
| 88 | + context match { |
| 89 | + case ParseContext.ACCEPT_LINE => |
| 90 | + // TODO: take into account cursor position |
| 91 | + if (ParseResult.isIncomplete(line)) incomplete() |
| 92 | + else parsedLine("", 0) |
| 93 | + // using dummy values, |
| 94 | + // resulting parsed line is probably unused |
| 95 | + |
| 96 | + case ParseContext.COMPLETE => |
| 97 | + // Parse to find completions (typically after a Tab). |
| 98 | + val source = new SourceFile("<completions>", line.toCharArray) |
| 99 | + val scanner = new Scanner(source)(ctx.fresh.setReporter(Reporter.NoReporter)) |
| 100 | + |
| 101 | + // Looking for the current word being completed |
| 102 | + // and the cursor position within this word |
| 103 | + while (scanner.token != EOF) { |
| 104 | + val start = scanner.offset |
| 105 | + val token = scanner.token |
| 106 | + scanner.nextToken() |
| 107 | + val end = scanner.lastOffset |
| 108 | + |
| 109 | + val isCompletable = |
| 110 | + isIdentifier(token) || isKeyword(token) // keywords can start identifiers |
| 111 | + def isCurrentWord = cursor >= start && cursor <= end |
| 112 | + if (isCompletable && isCurrentWord) { |
| 113 | + val word = line.substring(start, end) |
| 114 | + val wordCursor = cursor - start |
| 115 | + return parsedLine(word, wordCursor) |
| 116 | + } |
| 117 | + } |
| 118 | + parsedLine("", 0) // no word being completed |
| 119 | + |
| 120 | + case _ => |
| 121 | + incomplete() |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments