Java jdk.jshell.CompletenessAnalyzer

53 views Asked by At

How do you use jdk.jshell.CompletenessAnalyzer ? this class is not public !
I want to implement my own shell with jshell with my own custom commands like /bin/bash .
how can implement auto completion ?
jshell support double tab for auto completion , but i can not understand how can implement for my own commands .
this is my example code :

import jdk.jshell.JShell;

import java.util.Scanner;

public class App {
    private static JShell jshell;

    public static void main(String[] args) {
        jshell = JShell.create();

        System.out.println("Custom Shell");
        System.out.println("Type '/exit' to exit the shell");

        Scanner scanner = new Scanner(System.in);
        String userInput;

        do {
            System.out.print("> ");
            userInput = scanner.nextLine();

            if (!userInput.equals("/exit")) {
                executeCommand(userInput);
            }
        } while (!userInput.equals("/exit"));

        jshell.close();
        scanner.close();
    }

    private static void executeCommand(String command) {
        jshell.eval(command).forEach(System.out::println);
    }
}

Note : i won't to use jline .

1

There are 1 answers

0
Joe Chacko On

I have just been reading up on the JShell API, and while searching, I came across this question.

It looks like your executeCommand() method prints and then discards the result of calling jshell.eval().

For a more sophisticated shell, you could call jshell.sourceCodeAnalysis(), to get a SourceCodeAnalysis object, which you can then use to analyze your input string to see if it is complete, or even to check for suggestions on how to complete it.

I haven't tried it yet, but what I expect to do is as follwos:

  • Use the SourceCodeAnalysis object to parse the string into a list of snippets
  • Check the state of the snippets
  • Evaluate the source of valid snippets using the original JShell object
  • Print the error messages from ErroneousSnippets
  • Do something clever with incomplete snippets to allow further typing to complete them