|
| 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.Parser.ParseContext |
| 11 | +import org.jline.reader._ |
| 12 | +import org.jline.reader.impl.history.DefaultHistory |
| 13 | +import org.jline.terminal.TerminalBuilder |
| 14 | +import org.jline.utils.AttributedString |
| 15 | + |
| 16 | +final class JLineTerminal extends java.io.Closeable { |
| 17 | + // import java.util.logging.{Logger, Level} |
| 18 | + // Logger.getLogger("org.jline").setLevel(Level.FINEST) |
| 19 | + |
| 20 | + private val terminal = TerminalBuilder.builder() |
| 21 | + .dumb(false) // fail early if not able to create a terminal |
| 22 | + .build() |
| 23 | + private val history = new DefaultHistory |
| 24 | + |
| 25 | + private def blue(str: String) = Console.BLUE + str + Console.RESET |
| 26 | + private val prompt = blue("scala> ") |
| 27 | + private val newLinePrompt = blue(" | ") |
| 28 | + |
| 29 | + /** Blockingly read line from `System.in` |
| 30 | + * |
| 31 | + * This entry point into JLine handles everything to do with terminal |
| 32 | + * emulation. This includes: |
| 33 | + * |
| 34 | + * - Multi-line support |
| 35 | + * - Copy-pasting |
| 36 | + * - History |
| 37 | + * - Syntax highlighting |
| 38 | + * - Auto-completions |
| 39 | + * |
| 40 | + * @throws EndOfFileException This exception is thrown when the user types Ctrl-D. |
| 41 | + */ |
| 42 | + def readLine( |
| 43 | + completer: Completer // provide auto-completions |
| 44 | + )(implicit ctx: Context): String = { |
| 45 | + import LineReader.Option._ |
| 46 | + import LineReader._ |
| 47 | + val lineReader = LineReaderBuilder.builder() |
| 48 | + .terminal(terminal) |
| 49 | + .history(history) |
| 50 | + .completer(completer) |
| 51 | + .highlighter(new Highlighter) |
| 52 | + .parser(new Parser) |
| 53 | + .variable(SECONDARY_PROMPT_PATTERN, "%M") // A short word explaining what is "missing". |
| 54 | + // This is supplied from the EOFError.getMissing() method |
| 55 | + .variable(LIST_MAX, 400) // ask user when number of completions exceed this limit (default is 100) |
| 56 | + .option(INSERT_TAB, true) // at the beginning of the line, insert tab instead of completing |
| 57 | + .option(AUTO_FRESH_LINE, true) // if not at start of line before prompt, move to new line |
| 58 | + .build() |
| 59 | + |
| 60 | + lineReader.readLine(prompt) |
| 61 | + } |
| 62 | + |
| 63 | + def close() = terminal.close() |
| 64 | + |
| 65 | + /** Provide syntax highlighting */ |
| 66 | + private class Highlighter extends reader.Highlighter { |
| 67 | + def highlight(reader: LineReader, buffer: String): AttributedString = { |
| 68 | + val highlighted = SyntaxHighlighting(buffer).mkString |
| 69 | + AttributedString.fromAnsi(highlighted) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** Provide multi-line editing support */ |
| 74 | + private class Parser(implicit ctx: Context) extends reader.Parser { |
| 75 | + |
| 76 | + /** |
| 77 | + * @param cursor The cursor position within the line |
| 78 | + * @param line The unparsed line |
| 79 | + * @param word The current word being completed |
| 80 | + * @param wordCursor The cursor position within the current word |
| 81 | + */ |
| 82 | + private class ParsedLine( |
| 83 | + val cursor: Int, val line: String, val word: String, val wordCursor: Int |
| 84 | + ) extends reader.ParsedLine { |
| 85 | + // Using dummy values, not sure what they are used for |
| 86 | + def wordIndex = -1 |
| 87 | + def words = java.util.Collections.emptyList[String] |
| 88 | + } |
| 89 | + |
| 90 | + def parse(line: String, cursor: Int, context: ParseContext): reader.ParsedLine = { |
| 91 | + def parsedLine(word: String, wordCursor: Int) = |
| 92 | + new ParsedLine(cursor, line, word, wordCursor) |
| 93 | + // Used when no word is being completed |
| 94 | + def defaultParsedLine = parsedLine("", 0) |
| 95 | + |
| 96 | + def incomplete(): Nothing = throw new EOFError( |
| 97 | + // Using dummy values, not sure what they are used for |
| 98 | + /* line = */ -1, |
| 99 | + /* column = */ -1, |
| 100 | + /* message = */ "", |
| 101 | + /* missing = */ newLinePrompt) |
| 102 | + |
| 103 | + case class TokenData(token: Token, start: Int, end: Int) |
| 104 | + def currentToken: TokenData /* | Null */ = { |
| 105 | + val source = new SourceFile("<completions>", line) |
| 106 | + val scanner = new Scanner(source)(ctx.fresh.setReporter(Reporter.NoReporter)) |
| 107 | + while (scanner.token != EOF) { |
| 108 | + val start = scanner.offset |
| 109 | + val token = scanner.token |
| 110 | + scanner.nextToken() |
| 111 | + val end = scanner.lastOffset |
| 112 | + |
| 113 | + val isCurrentToken = cursor >= start && cursor <= end |
| 114 | + if (isCurrentToken) |
| 115 | + return TokenData(token, start, end) |
| 116 | + } |
| 117 | + null |
| 118 | + } |
| 119 | + |
| 120 | + context match { |
| 121 | + case ParseContext.ACCEPT_LINE => |
| 122 | + // ENTER means SUBMIT when |
| 123 | + // - cursor is at end (discarding whitespaces) |
| 124 | + // - and, input line is complete |
| 125 | + val cursorIsAtEnd = line.indexWhere(!_.isWhitespace, from = cursor) >= 0 |
| 126 | + if (cursorIsAtEnd || ParseResult.isIncomplete(line)) incomplete() |
| 127 | + else defaultParsedLine |
| 128 | + // using dummy values, resulting parsed line is probably unused |
| 129 | + |
| 130 | + case ParseContext.COMPLETE => |
| 131 | + // Parse to find completions (typically after a Tab). |
| 132 | + def isCompletable(token: Token) = isIdentifier(token) || isKeyword(token) |
| 133 | + currentToken match { |
| 134 | + case TokenData(token, start, end) if isCompletable(token) => |
| 135 | + val word = line.substring(start, end) |
| 136 | + val wordCursor = cursor - start |
| 137 | + parsedLine(word, wordCursor) |
| 138 | + case _ => |
| 139 | + defaultParsedLine |
| 140 | + } |
| 141 | + |
| 142 | + case _ => |
| 143 | + incomplete() |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments