Skip to content

Language REPL Support

mattirn edited this page Nov 16, 2021 · 3 revisions

JLine prints command/method descriptions and syntax errors on status bar. In addition, the location of the error in the input area is highlighted.

Java

    LineReader reader = LineReaderBuilder.builder()
                    .terminal(terminal)
                    .completer(completer)
                    .parser(parser)
                    .variable(LineReader.SECONDARY_PROMPT_PATTERN, "%M%P > ")
                    .variable(LineReader.INDENTATION, 2)
                    .build();

    tailtipWidgets = new TailTipWidgets(reader, commandDescription, 5, TipType.COMPLETER);
.....
.....
CmdDesc commandDescription(CmdLine line) {
    CmdDesc out = null;
    switch (line.getDescriptionType()) {
    case COMMAND:
        out = myCommandDescription(line);
        break;
    case METHOD:
        out = methodDescription(line);
        break;
    case SYNTAX:
        out = syntaxDescription(line);
        break;
    }
    return out;
}

Functionality

After each edit, widget _tailtip-self-insert requests method/command description. Description is requested by invoking method commandDescription() for

  1. method: if the left side of the cursor we have an unclosed opening parenthesis.
  2. syntax: the last character entered is closing parenthesis
  3. command: cursor is at the end of line, args[0] is completed and it is not a case 1. or 2.

In order to avoid description recalculations the command and method descriptions are saved in permanent and temporary cache respectively. Permanent cache will be created on TailTipWidgets constructor and it is possible to populate also using method setCmdDescs(). Temporary cache will be cleared by widget _tailtip-accept-line.

Note that description request decisions are taken from partially completed statement that can contain errors like missing quotes and parenthesis. The final decision for description compilation should be done by commandDescription() method which has the knowledge of app commands and scripting language syntax. When description request is out of context for example method/syntax description is requested for command argument commandDescription() can return new CmdDesc(false)/null to ignore the request and keep/clear current description.

In addition to the methods methodDescription() and syntaxDescription() application will need also completer implementation for used scripting language. Autosuggestion as usual will call completer only when cursor is at the end of line. Thus JLine variable insert-bracket should be false and autopairing disabled in order to obtain correct behaviour.

Clone this wiki locally