Is there any way to delete history in JShell?

Lindstorm :

In JShell, is there any way to remove a statement that you executed like it never happened?

For example, suppose we have the following code.

 JShell shell = JShell.create();
 shell.eval("int x = 5;");
 shell.eval("x = 7;");

Is there something in JShell that goes back one step? That is, something like a method goBackOneStep() where, after executing it, sets x back to 5.

 shell.goBackOneStep();
 shell.eval("x") // x is evaluated to 5

I noticed in the JShell API there is a method called drop(Snippet) which seems to remove the Snippet from the history, but it does not remove any effect the Snippet had on the state.

Deadpool :

jshell drop jshell drop means changing the status of snippets to DROPPED and making them inactive (so they are no more active and you can not use them until you create them again)

Drops a snippet, making it inactive. Provide either the name or the ID of an import, class, method, or variable. For more than one snippet, separate the names and IDs with a space. Use the /list command to see the IDs of code snippets.

DROPPED

The snippet is inactive because of an explicit call to the JShell.drop(Snippet).

In the below code you have two snippets and by using drop you made them inactive

JShell shell = JShell.create();
          shell.eval("int x = 5;");
          shell.eval("x = 7;");

    shell.snippets().forEach(s1->shell.drop(s1));

    shell.snippets().forEach(s2->{
        System.out.println(shell.status(s2));   //DROPPED
    });

   shell.snippets().forEach(System.out::println);  //we print all active and inactive snippets

In the same way you can also see all active and dropped snippets in jshell console

/list [option]

Displays a list of snippets and their ID. If no option is entered, then all active snippets are displayed, but startup snippets aren’t.

/list -all

Displays all snippets, including startup snippets and snippets that failed, were overwritten, or were dropped. IDs that begin with s are startup snippets. IDs that begin with e are snippets that failed.

Is there something in JShell that goes back one step?

In jshell each input is exactly one snippet so there is no way to rollback of snippets.

The input should be exactly one complete snippet of source code, that is, one expression, statement, variable declaration, method declaration, class declaration, or import. To break arbitrary input into individual complete snippets, use SourceCodeAnalysis.analyzeCompletion(String).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=97146&siteId=1