langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java
author jlahoda
Fri, 25 Aug 2017 13:48:49 +0200
changeset 47050 72ec64aeaa57
parent 46923 0172e4350f65
permissions -rw-r--r--
8185426: Jshell crashing on autocompletion Summary: Properly canceling completion on <tab> if needed. Reviewed-by: rfield
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     1
/*
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     4
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    10
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    15
 * accompanied this code).
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    16
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    20
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    23
 * questions.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    24
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    25
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    26
package jdk.internal.jshell.tool;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    27
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
    28
import jdk.jshell.SourceCodeAnalysis.Documentation;
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
    29
import jdk.jshell.SourceCodeAnalysis.QualifiedNames;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    30
import jdk.jshell.SourceCodeAnalysis.Suggestion;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    31
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    32
import java.io.IOException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    33
import java.io.InputStream;
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    34
import java.io.InterruptedIOException;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    35
import java.io.PrintStream;
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
    36
import java.util.ArrayList;
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
    37
import java.util.Arrays;
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
    38
import java.util.Collection;
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
    39
import java.util.Collections;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
    40
import java.util.HashMap;
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
    41
import java.util.Iterator;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    42
import java.util.List;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    43
import java.util.Locale;
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
    44
import java.util.Map;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    45
import java.util.Optional;
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
    46
import java.util.function.Function;
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
    47
import java.util.stream.Collectors;
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
    48
import java.util.stream.Stream;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    49
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
    50
import jdk.internal.shellsupport.doc.JavadocFormatter;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    51
import jdk.internal.jline.NoInterruptUnixTerminal;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    52
import jdk.internal.jline.Terminal;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    53
import jdk.internal.jline.TerminalFactory;
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
    54
import jdk.internal.jline.TerminalSupport;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    55
import jdk.internal.jline.WindowsTerminal;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    56
import jdk.internal.jline.console.ConsoleReader;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    57
import jdk.internal.jline.console.KeyMap;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    58
import jdk.internal.jline.console.UserInterruptException;
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
    59
import jdk.internal.jline.console.history.History;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
    60
import jdk.internal.jline.console.history.MemoryHistory;
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
    61
import jdk.internal.jline.extra.EditingHistory;
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
    62
import jdk.internal.jline.internal.NonBlockingInputStream;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    63
import jdk.internal.jshell.tool.StopDetectingInputStream.State;
41631
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
    64
import jdk.internal.misc.Signal;
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
    65
import jdk.internal.misc.Signal.Handler;
46185
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
    66
import jdk.jshell.ExpressionSnippet;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
    67
import jdk.jshell.Snippet;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
    68
import jdk.jshell.Snippet.SubKind;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
    69
import jdk.jshell.SourceCodeAnalysis.CompletionInfo;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
    70
import jdk.jshell.VarSnippet;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    71
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    72
class ConsoleIOContext extends IOContext {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    73
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
    74
    private static final String HISTORY_LINE_PREFIX = "HISTORY_LINE_";
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
    75
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    76
    final JShellTool repl;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    77
    final StopDetectingInputStream input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    78
    final ConsoleReader in;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    79
    final EditingHistory history;
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
    80
    final MemoryHistory userInputHistory = new MemoryHistory();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    81
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    82
    String prefix = "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    83
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    84
    ConsoleIOContext(JShellTool repl, InputStream cmdin, PrintStream cmdout) throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    85
        this.repl = repl;
44188
3f2047e62102 8176412: jshell tool: automatic imports are excluded on /reload causing it to fail
rfield
parents: 43773
diff changeset
    86
        this.input = new StopDetectingInputStream(() -> repl.stop(), ex -> repl.hard("Error on input: %s", ex));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    87
        Terminal term;
36501
93915076a341 8151570: jtreg tests leave tty in bad state
jlahoda
parents: 36160
diff changeset
    88
        if (System.getProperty("test.jdk") != null) {
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
    89
            term = new TestTerminal(input);
36501
93915076a341 8151570: jtreg tests leave tty in bad state
jlahoda
parents: 36160
diff changeset
    90
        } else if (System.getProperty("os.name").toLowerCase(Locale.US).contains(TerminalFactory.WINDOWS)) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    91
            term = new JShellWindowsTerminal(input);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    92
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    93
            term = new JShellUnixTerminal(input);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    94
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    95
        term.init();
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
    96
        CompletionState completionState = new CompletionState();
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
    97
        in = new ConsoleReader(cmdin, cmdout, term) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
    98
            @Override public KeyMap getKeys() {
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
    99
                return new CheckCompletionKeyMap(super.getKeys(), completionState);
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   100
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   101
            @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   102
            protected boolean complete() throws IOException {
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   103
                return ConsoleIOContext.this.complete(completionState);
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   104
            }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   105
        };
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   106
        in.setExpandEvents(false);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   107
        in.setHandleUserInterrupt(true);
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   108
        List<String> persistenHistory = Stream.of(repl.prefs.keys())
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   109
                                              .filter(key -> key.startsWith(HISTORY_LINE_PREFIX))
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   110
                                              .sorted()
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   111
                                              .map(key -> repl.prefs.get(key))
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   112
                                              .collect(Collectors.toList());
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   113
        in.setHistory(history = new EditingHistory(in, persistenHistory) {
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   114
            @Override protected boolean isComplete(CharSequence input) {
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38541
diff changeset
   115
                return repl.analysis.analyzeCompletion(input.toString()).completeness().isComplete();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   116
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   117
        });
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   118
        in.setBellEnabled(true);
38541
44e95493fd13 8131017: jshell tool: pasting code with tabs invokes tab completion
jlahoda
parents: 38521
diff changeset
   119
        in.setCopyPasteDetection(true);
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   120
        bind(FIXES_SHORTCUT, (Runnable) () -> fixes());
41631
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   121
        try {
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   122
            Signal.handle(new Signal("CONT"), new Handler() {
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   123
                @Override public void handle(Signal sig) {
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   124
                    try {
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   125
                        in.getTerminal().reset();
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   126
                        in.redrawLine();
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   127
                        in.flush();
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   128
                    } catch (Exception ex) {
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   129
                        ex.printStackTrace();
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   130
                    }
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   131
                }
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   132
            });
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   133
        } catch (IllegalArgumentException ignored) {
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   134
            //the CONT signal does not exist on this platform
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   135
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   136
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   137
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   138
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   139
    public String readLine(String prompt, String prefix) throws IOException, InputInterruptedException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   140
        this.prefix = prefix;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   141
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   142
            return in.readLine(prompt);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   143
        } catch (UserInterruptException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   144
            throw (InputInterruptedException) new InputInterruptedException().initCause(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   145
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   146
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   147
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   148
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   149
    public boolean interactiveOutput() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   150
        return true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   151
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   152
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   153
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   154
    public Iterable<String> currentSessionHistory() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   155
        return history.currentSessionEntries();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   156
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   157
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   158
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   159
    public void close() throws IOException {
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   160
        //save history:
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   161
        for (String key : repl.prefs.keys()) {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   162
            if (key.startsWith(HISTORY_LINE_PREFIX)) {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   163
                repl.prefs.remove(key);
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   164
            }
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   165
        }
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   166
        Collection<? extends String> savedHistory = history.save();
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   167
        if (!savedHistory.isEmpty()) {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   168
            int len = (int) Math.ceil(Math.log10(savedHistory.size()+1));
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   169
            String format = HISTORY_LINE_PREFIX + "%0" + len + "d";
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   170
            int index = 0;
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   171
            for (String historyLine : savedHistory) {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   172
                repl.prefs.put(String.format(format, index++), historyLine);
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   173
            }
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   174
        }
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   175
        repl.prefs.flush();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   176
        in.shutdown();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   177
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   178
            in.getTerminal().restore();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   179
        } catch (Exception ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   180
            throw new IOException(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   181
        }
38909
80e42e2d475b 8158174: jshell tool: leaks threads waiting on StopDetectingInputStream
jlahoda
parents: 38908
diff changeset
   182
        input.shutdown();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   183
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   184
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   185
    private void bind(String shortcut, Object action) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   186
        KeyMap km = in.getKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   187
        for (int i = 0; i < shortcut.length(); i++) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   188
            Object value = km.getBound(Character.toString(shortcut.charAt(i)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   189
            if (value instanceof KeyMap) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   190
                km = (KeyMap) value;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   191
            } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   192
                km.bind(shortcut.substring(i), action);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   193
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   194
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   195
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   196
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   197
    private static final String FIXES_SHORTCUT = "\033\133\132"; //Shift-TAB
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   198
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   199
    private static final String LINE_SEPARATOR = System.getProperty("line.separator");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   200
    private static final String LINE_SEPARATORS2 = LINE_SEPARATOR + LINE_SEPARATOR;
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   201
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   202
    @SuppressWarnings("fallthrough")
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   203
    private boolean complete(CompletionState completionState) {
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   204
        //The completion has multiple states (invoked by subsequent presses of <tab>).
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   205
        //On the first invocation in a given sequence, all steps are precomputed
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   206
        //and placed into the todo list (completionState.todo). The todo list is
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   207
        //then followed on both the first and subsequent completion invocations:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   208
        try {
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   209
            String text = in.getCursorBuffer().toString();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   210
            int cursor = in.getCursorBuffer().cursor;
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   211
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   212
            List<CompletionTask> todo = completionState.todo;
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   213
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   214
            if (todo.isEmpty() || completionState.actionCount != 1) {
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   215
                ConsoleIOContextTestSupport.willComputeCompletion();
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   216
                int[] anchor = new int[] {-1};
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   217
                List<Suggestion> suggestions;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   218
                List<String> doc;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   219
                boolean command = prefix.isEmpty() && text.trim().startsWith("/");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   220
                if (command) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   221
                    suggestions = repl.commandCompletionSuggestions(text, cursor, anchor);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   222
                    doc = repl.commandDocumentation(text, cursor, true);
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   223
                } else {
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   224
                    int prefixLength = prefix.length();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   225
                    suggestions = repl.analysis.completionSuggestions(prefix + text, cursor + prefixLength, anchor);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   226
                    anchor[0] -= prefixLength;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   227
                    doc = repl.analysis.documentation(prefix + text, cursor + prefix.length(), false)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   228
                                       .stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   229
                                       .map(Documentation::signature)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   230
                                       .collect(Collectors.toList());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   231
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   232
                long smartCount = suggestions.stream().filter(Suggestion::matchesType).count();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   233
                boolean hasSmart = smartCount > 0 && smartCount <= in.getAutoprintThreshold();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   234
                boolean hasBoth = hasSmart &&
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   235
                                  suggestions.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   236
                                             .map(s -> s.matchesType())
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   237
                                             .distinct()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   238
                                             .count() == 2;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   239
                boolean tooManyItems = suggestions.size() > in.getAutoprintThreshold();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   240
                CompletionTask ordinaryCompletion = new OrdinaryCompletionTask(suggestions, anchor[0], !command && !doc.isEmpty(), hasSmart);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   241
                CompletionTask allCompletion = new AllSuggestionsCompletionTask(suggestions, anchor[0]);
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   242
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   243
                todo = new ArrayList<>();
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   244
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   245
                //the main decission tree:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   246
                if (command) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   247
                    CompletionTask shortDocumentation = new CommandSynopsisTask(doc);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   248
                    CompletionTask fullDocumentation = new CommandFullDocumentationTask(todo);
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   249
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   250
                    if (!doc.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   251
                        if (tooManyItems) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   252
                            todo.add(new NoopCompletionTask());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   253
                            todo.add(allCompletion);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   254
                        } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   255
                            todo.add(ordinaryCompletion);
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   256
                        }
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   257
                        todo.add(shortDocumentation);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   258
                        todo.add(fullDocumentation);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   259
                    } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   260
                        todo.add(new NoSuchCommandCompletionTask());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   261
                    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   262
                } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   263
                    if (doc.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   264
                        if (hasSmart) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   265
                            todo.add(ordinaryCompletion);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   266
                        } else if (tooManyItems) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   267
                            todo.add(new NoopCompletionTask());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   268
                        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   269
                        if (!hasSmart || hasBoth) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   270
                            todo.add(allCompletion);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   271
                        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   272
                    } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   273
                        CompletionTask shortDocumentation = new ExpressionSignaturesTask(doc);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   274
                        CompletionTask fullDocumentation = new ExpressionJavadocTask(todo);
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   275
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   276
                        if (hasSmart) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   277
                            todo.add(ordinaryCompletion);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   278
                        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   279
                        todo.add(shortDocumentation);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   280
                        if (!hasSmart || hasBoth) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   281
                            todo.add(allCompletion);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   282
                        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   283
                        if (tooManyItems) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   284
                            todo.add(todo.size() - 1, fullDocumentation);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   285
                        } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   286
                            todo.add(fullDocumentation);
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   287
                        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   288
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   289
                }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   290
            }
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   291
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   292
            boolean success = false;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   293
            boolean repaint = true;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   294
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   295
            OUTER: while (!todo.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   296
                CompletionTask.Result result = todo.remove(0).perform(text, cursor);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   297
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   298
                switch (result) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   299
                    case CONTINUE:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   300
                        break;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   301
                    case SKIP_NOREPAINT:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   302
                        repaint = false;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   303
                    case SKIP:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   304
                        todo.clear();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   305
                        //intentional fall-through
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   306
                    case FINISH:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   307
                        success = true;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   308
                        //intentional fall-through
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   309
                    case NO_DATA:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   310
                        if (!todo.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   311
                            in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   312
                            in.println(todo.get(0).description());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   313
                        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   314
                        break OUTER;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   315
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   316
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   317
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   318
            completionState.actionCount = 0;
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   319
            completionState.todo = todo;
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   320
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   321
            if (repaint) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   322
                in.redrawLine();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   323
                in.flush();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   324
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   325
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   326
            return success;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   327
        } catch (IOException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   328
            throw new IllegalStateException(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   329
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   330
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   331
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   332
    private CompletionTask.Result doPrintFullDocumentation(List<CompletionTask> todo, List<String> doc, boolean command) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   333
        if (doc != null && !doc.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   334
            Terminal term = in.getTerminal();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   335
            int pageHeight = term.getHeight() - NEEDED_LINES;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   336
            List<CompletionTask> thisTODO = new ArrayList<>();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   337
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   338
            for (Iterator<String> docIt = doc.iterator(); docIt.hasNext(); ) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   339
                String currentDoc = docIt.next();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   340
                String[] lines = currentDoc.split("\n");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   341
                int firstLine = 0;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   342
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   343
                while (firstLine < lines.length) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   344
                    boolean first = firstLine == 0;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   345
                    String[] thisPageLines =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   346
                            Arrays.copyOfRange(lines,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   347
                                               firstLine,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   348
                                               Math.min(firstLine + pageHeight, lines.length));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   349
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   350
                    thisTODO.add(new CompletionTask() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   351
                        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   352
                        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   353
                            String key =  !first ? "jshell.console.see.next.page"
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   354
                                                 : command ? "jshell.console.see.next.command.doc"
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   355
                                                           : "jshell.console.see.next.javadoc";
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   356
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   357
                            return repl.getResourceString(key);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   358
                        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   359
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   360
                        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   361
                        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   362
                            in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   363
                            for (String line : thisPageLines) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   364
                                in.println(line);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   365
                            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   366
                            return Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   367
                        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   368
                    });
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   369
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   370
                    firstLine += pageHeight;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   371
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   372
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   373
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   374
            todo.addAll(0, thisTODO);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   375
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   376
            return CompletionTask.Result.CONTINUE;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   377
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   378
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   379
        return CompletionTask.Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   380
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   381
    //where:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   382
        private static final int NEEDED_LINES = 4;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   383
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   384
    private static String commonPrefix(String str1, String str2) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   385
        for (int i = 0; i < str2.length(); i++) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   386
            if (!str1.startsWith(str2.substring(0, i + 1))) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   387
                return str2.substring(0, i);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   388
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   389
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   390
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   391
        return str2;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   392
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   393
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   394
    private interface CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   395
        public String description();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   396
        public Result perform(String text, int cursor) throws IOException;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   397
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   398
        enum Result {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   399
            NO_DATA,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   400
            CONTINUE,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   401
            FINISH,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   402
            SKIP,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   403
            SKIP_NOREPAINT;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   404
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   405
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   406
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   407
    private final class NoopCompletionTask implements CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   408
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   409
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   410
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   411
            throw new UnsupportedOperationException("Should not get here.");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   412
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   413
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   414
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   415
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   416
            return Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   417
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   418
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   419
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   420
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   421
    private final class NoSuchCommandCompletionTask implements CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   422
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   423
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   424
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   425
            throw new UnsupportedOperationException("Should not get here.");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   426
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   427
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   428
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   429
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   430
            in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   431
            in.println(repl.getResourceString("jshell.console.no.such.command"));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   432
            in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   433
            return Result.SKIP;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   434
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   435
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   436
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   437
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   438
    private final class OrdinaryCompletionTask implements CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   439
        private final List<Suggestion> suggestions;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   440
        private final int anchor;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   441
        private final boolean cont;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   442
        private final boolean smart;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   443
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   444
        public OrdinaryCompletionTask(List<Suggestion> suggestions,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   445
                                      int anchor,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   446
                                      boolean cont,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   447
                                      boolean smart) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   448
            this.suggestions = suggestions;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   449
            this.anchor = anchor;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   450
            this.cont = cont;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   451
            this.smart = smart;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   452
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   453
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   454
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   455
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   456
            throw new UnsupportedOperationException("Should not get here.");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   457
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   458
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   459
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   460
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   461
            List<CharSequence> toShow;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   462
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   463
            if (smart) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   464
                toShow =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   465
                    suggestions.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   466
                               .filter(Suggestion::matchesType)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   467
                               .map(Suggestion::continuation)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   468
                               .distinct()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   469
                               .collect(Collectors.toList());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   470
            } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   471
                toShow =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   472
                    suggestions.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   473
                               .map(Suggestion::continuation)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   474
                               .distinct()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   475
                               .collect(Collectors.toList());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   476
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   477
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   478
            if (toShow.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   479
                return Result.CONTINUE;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   480
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   481
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   482
            Optional<String> prefix =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   483
                    suggestions.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   484
                               .map(Suggestion::continuation)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   485
                               .reduce(ConsoleIOContext::commonPrefix);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   486
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   487
            String prefixStr = prefix.orElse("").substring(cursor - anchor);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   488
            in.putString(prefixStr);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   489
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   490
            boolean showItems = toShow.size() > 1 || smart;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   491
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   492
            if (showItems) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   493
                in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   494
                in.printColumns(toShow);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   495
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   496
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   497
            if (!prefixStr.isEmpty())
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   498
                return showItems ? Result.SKIP : Result.SKIP_NOREPAINT;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   499
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   500
            return cont ? Result.CONTINUE : Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   501
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   502
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   503
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   504
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   505
    private final class AllSuggestionsCompletionTask implements CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   506
        private final List<Suggestion> suggestions;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   507
        private final int anchor;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   508
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   509
        public AllSuggestionsCompletionTask(List<Suggestion> suggestions,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   510
                                            int anchor) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   511
            this.suggestions = suggestions;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   512
            this.anchor = anchor;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   513
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   514
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   515
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   516
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   517
            if (suggestions.size() <= in.getAutoprintThreshold()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   518
                return repl.getResourceString("jshell.console.completion.all.completions");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   519
            } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   520
                return repl.messageFormat("jshell.console.completion.all.completions.number", suggestions.size());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   521
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   522
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   523
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   524
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   525
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   526
            List<String> candidates =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   527
                    suggestions.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   528
                               .map(Suggestion::continuation)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   529
                               .distinct()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   530
                               .collect(Collectors.toList());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   531
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   532
            Optional<String> prefix =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   533
                    candidates.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   534
                              .reduce(ConsoleIOContext::commonPrefix);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   535
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   536
            String prefixStr = prefix.map(str -> str.substring(cursor - anchor)).orElse("");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   537
            in.putString(prefixStr);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   538
            if (candidates.size() > 1) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   539
                in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   540
                in.printColumns(candidates);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   541
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   542
            return suggestions.isEmpty() ? Result.NO_DATA : Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   543
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   544
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   545
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   546
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   547
    private final class CommandSynopsisTask implements CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   548
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   549
        private final List<String> synopsis;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   550
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   551
        public CommandSynopsisTask(List<String> synposis) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   552
            this.synopsis = synposis;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   553
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   554
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   555
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   556
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   557
            return repl.getResourceString("jshell.console.see.synopsis");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   558
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   559
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   560
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   561
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   562
            try {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   563
                in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   564
                in.println(synopsis.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   565
                                   .map(l -> l.replaceAll("\n", LINE_SEPARATOR))
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   566
                                   .collect(Collectors.joining(LINE_SEPARATORS2)));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   567
            } catch (IOException ex) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   568
                throw new IllegalStateException(ex);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   569
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   570
            return Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   571
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   572
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   573
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   574
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   575
    private final class CommandFullDocumentationTask implements CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   576
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   577
        private final List<CompletionTask> todo;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   578
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   579
        public CommandFullDocumentationTask(List<CompletionTask> todo) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   580
            this.todo = todo;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   581
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   582
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   583
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   584
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   585
            return repl.getResourceString("jshell.console.see.full.documentation");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   586
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   587
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   588
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   589
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   590
            List<String> fullDoc = repl.commandDocumentation(text, cursor, false);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   591
            return doPrintFullDocumentation(todo, fullDoc, true);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   592
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   593
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   594
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   595
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   596
    private final class ExpressionSignaturesTask implements CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   597
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   598
        private final List<String> doc;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   599
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   600
        public ExpressionSignaturesTask(List<String> doc) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   601
            this.doc = doc;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   602
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   603
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   604
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   605
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   606
            throw new UnsupportedOperationException("Should not get here.");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   607
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   608
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   609
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   610
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   611
            in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   612
            in.println(repl.getResourceString("jshell.console.completion.current.signatures"));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   613
            in.println(doc.stream().collect(Collectors.joining(LINE_SEPARATOR)));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   614
            return Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   615
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   616
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   617
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   618
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   619
    private final class ExpressionJavadocTask implements CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   620
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   621
        private final List<CompletionTask> todo;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   622
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   623
        public ExpressionJavadocTask(List<CompletionTask> todo) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   624
            this.todo = todo;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   625
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   626
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   627
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   628
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   629
            return repl.getResourceString("jshell.console.see.documentation");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   630
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   631
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   632
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   633
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   634
            //schedule showing javadoc:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   635
            Terminal term = in.getTerminal();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   636
            JavadocFormatter formatter = new JavadocFormatter(term.getWidth(),
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   637
                                                              term.isAnsiSupported());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   638
            Function<Documentation, String> convertor = d -> formatter.formatJavadoc(d.signature(), d.javadoc()) +
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   639
                             (d.javadoc() == null ? repl.messageFormat("jshell.console.no.javadoc")
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   640
                                                  : "");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   641
            List<String> doc = repl.analysis.documentation(prefix + text, cursor + prefix.length(), true)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   642
                                            .stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   643
                                            .map(convertor)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   644
                                            .collect(Collectors.toList());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   645
            return doPrintFullDocumentation(todo, doc, false);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   646
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   647
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   648
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   649
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   650
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   651
    public boolean terminalEditorRunning() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   652
        Terminal terminal = in.getTerminal();
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   653
        if (terminal instanceof SuspendableTerminal)
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   654
            return ((SuspendableTerminal) terminal).isRaw();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   655
        return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   656
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   657
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   658
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   659
    public void suspend() {
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   660
        Terminal terminal = in.getTerminal();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   661
        if (terminal instanceof SuspendableTerminal)
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   662
            ((SuspendableTerminal) terminal).suspend();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   663
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   664
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   665
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   666
    public void resume() {
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   667
        Terminal terminal = in.getTerminal();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   668
        if (terminal instanceof SuspendableTerminal)
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   669
            ((SuspendableTerminal) terminal).resume();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   670
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   671
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   672
    @Override
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   673
    public void beforeUserCode() {
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   674
        synchronized (this) {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   675
            inputBytes = null;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   676
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   677
        input.setState(State.BUFFER);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   678
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   679
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   680
    @Override
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   681
    public void afterUserCode() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   682
        input.setState(State.WAIT);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   683
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   684
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   685
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   686
    public void replaceLastHistoryEntry(String source) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   687
        history.fullHistoryReplace(source);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   688
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   689
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   690
    private static final long ESCAPE_TIMEOUT = 100;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   691
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   692
    private void fixes() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   693
        try {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   694
            int c = in.readCharacter();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   695
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   696
            if (c == (-1)) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   697
                return ;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   698
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   699
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   700
            for (FixComputer computer : FIX_COMPUTERS) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   701
                if (computer.shortcut == c) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   702
                    fixes(computer);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   703
                    return ;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   704
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   705
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   706
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   707
            readOutRemainingEscape(c);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   708
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   709
            in.beep();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   710
            in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   711
            in.println(repl.getResourceString("jshell.fix.wrong.shortcut"));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   712
            in.redrawLine();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   713
            in.flush();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   714
        } catch (IOException ex) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   715
            ex.printStackTrace();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   716
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   717
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   718
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   719
    private void readOutRemainingEscape(int c) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   720
        if (c == '\033') {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   721
            //escape, consume waiting input:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   722
            InputStream inp = in.getInput();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   723
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   724
            if (inp instanceof NonBlockingInputStream) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   725
                NonBlockingInputStream nbis = (NonBlockingInputStream) inp;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   726
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   727
                while (nbis.isNonBlockingEnabled() && nbis.peek(ESCAPE_TIMEOUT) > 0) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   728
                    in.readCharacter();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   729
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   730
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   731
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   732
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   733
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   734
    //compute possible options/Fixes based on the selected FixComputer, present them to the user,
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   735
    //and perform the selected one:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   736
    private void fixes(FixComputer computer) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   737
        String input = prefix + in.getCursorBuffer().toString();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   738
        int cursor = prefix.length() + in.getCursorBuffer().cursor;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   739
        FixResult candidates = computer.compute(repl, input, cursor);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   740
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   741
        try {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   742
            final boolean printError = candidates.error != null && !candidates.error.isEmpty();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   743
            if (printError) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   744
                in.println(candidates.error);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   745
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   746
            if (candidates.fixes.isEmpty()) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   747
                in.beep();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   748
                if (printError) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   749
                    in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   750
                    in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   751
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   752
            } else if (candidates.fixes.size() == 1 && !computer.showMenu) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   753
                if (printError) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   754
                    in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   755
                    in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   756
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   757
                candidates.fixes.get(0).perform(in);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   758
            } else {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   759
                List<Fix> fixes = new ArrayList<>(candidates.fixes);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   760
                fixes.add(0, new Fix() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   761
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   762
                    public String displayName() {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   763
                        return repl.messageFormat("jshell.console.do.nothing");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   764
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   765
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   766
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   767
                    public void perform(ConsoleReader in) throws IOException {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   768
                        in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   769
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   770
                });
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   771
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   772
                Map<Character, Fix> char2Fix = new HashMap<>();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   773
                in.println();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   774
                for (int i = 0; i < fixes.size(); i++) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   775
                    Fix fix = fixes.get(i);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   776
                    char2Fix.put((char) ('0' + i), fix);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   777
                    in.println("" + i + ": " + fixes.get(i).displayName());
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   778
                }
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   779
                in.print(repl.messageFormat("jshell.console.choice"));
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   780
                in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   781
                int read;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   782
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   783
                read = in.readCharacter();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   784
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   785
                Fix fix = char2Fix.get((char) read);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   786
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   787
                if (fix == null) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   788
                    in.beep();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   789
                    fix = fixes.get(0);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   790
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   791
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   792
                in.println();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   793
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   794
                fix.perform(in);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   795
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   796
                in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   797
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   798
        } catch (IOException ex) {
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   799
            throw new IllegalStateException(ex);
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   800
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   801
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   802
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   803
    private byte[] inputBytes;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   804
    private int inputBytesPointer;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   805
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   806
    @Override
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
   807
    public synchronized int readUserInput() throws IOException {
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   808
        while (inputBytes == null || inputBytes.length <= inputBytesPointer) {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   809
            boolean prevHandleUserInterrupt = in.getHandleUserInterrupt();
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   810
            History prevHistory = in.getHistory();
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   811
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   812
            try {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   813
                input.setState(State.WAIT);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   814
                in.setHandleUserInterrupt(true);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   815
                in.setHistory(userInputHistory);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   816
                inputBytes = (in.readLine("") + System.getProperty("line.separator")).getBytes();
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   817
                inputBytesPointer = 0;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   818
            } catch (UserInterruptException ex) {
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
   819
                throw new InterruptedIOException();
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   820
            } finally {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   821
                in.setHistory(prevHistory);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   822
                in.setHandleUserInterrupt(prevHandleUserInterrupt);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   823
                input.setState(State.BUFFER);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   824
            }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   825
        }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   826
        return inputBytes[inputBytesPointer++];
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   827
    }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   828
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   829
    /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   830
     * A possible action which the user can choose to perform.
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   831
     */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   832
    public interface Fix {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   833
        /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   834
         * A name that should be shown to the user.
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   835
         */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   836
        public String displayName();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   837
        /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   838
         * Perform the given action.
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   839
         */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   840
        public void perform(ConsoleReader in) throws IOException;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   841
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   842
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   843
    /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   844
     * A factory for {@link Fix}es.
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   845
     */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   846
    public abstract static class FixComputer {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   847
        private final char shortcut;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   848
        private final boolean showMenu;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   849
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   850
        /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   851
         * Construct a new FixComputer. {@code shortcut} defines the key which should trigger this FixComputer.
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   852
         * If {@code showMenu} is {@code false}, and this computer returns exactly one {@code Fix},
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   853
         * no options will be show to the user, and the given {@code Fix} will be performed.
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   854
         */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   855
        public FixComputer(char shortcut, boolean showMenu) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   856
            this.shortcut = shortcut;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   857
            this.showMenu = showMenu;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   858
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   859
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   860
        /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   861
         * Compute possible actions for the given code.
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   862
         */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   863
        public abstract FixResult compute(JShellTool repl, String code, int cursor);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   864
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   865
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   866
    /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   867
     * A list of {@code Fix}es with a possible error that should be shown to the user.
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   868
     */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   869
    public static class FixResult {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   870
        public final List<Fix> fixes;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   871
        public final String error;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   872
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   873
        public FixResult(List<Fix> fixes, String error) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   874
            this.fixes = fixes;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   875
            this.error = error;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   876
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   877
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   878
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   879
    private static final FixComputer[] FIX_COMPUTERS = new FixComputer[] {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   880
        new FixComputer('v', false) { //compute "Introduce variable" Fix:
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   881
            private void performToVar(ConsoleReader in, String type) throws IOException {
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   882
                in.redrawLine();
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   883
                in.setCursorPosition(0);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   884
                in.putString(type + "  = ");
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   885
                in.setCursorPosition(in.getCursorBuffer().cursor - 3);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   886
                in.flush();
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   887
            }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   888
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   889
            @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   890
            public FixResult compute(JShellTool repl, String code, int cursor) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   891
                String type = repl.analysis.analyzeType(code, cursor);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   892
                if (type == null) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   893
                    return new FixResult(Collections.emptyList(), null);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   894
                }
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   895
                List<Fix> fixes = new ArrayList<>();
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   896
                fixes.add(new Fix() {
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   897
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   898
                    public String displayName() {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   899
                        return repl.messageFormat("jshell.console.create.variable");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   900
                    }
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   901
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   902
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   903
                    public void perform(ConsoleReader in) throws IOException {
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   904
                        performToVar(in, type);
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   905
                    }
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   906
                });
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   907
                int idx = type.lastIndexOf(".");
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   908
                if (idx > 0) {
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   909
                    String stype = type.substring(idx + 1);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   910
                    QualifiedNames res = repl.analysis.listQualifiedNames(stype, stype.length());
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   911
                    if (res.isUpToDate() && res.getNames().contains(type)
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   912
                            && !res.isResolvable()) {
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   913
                        fixes.add(new Fix() {
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   914
                            @Override
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   915
                            public String displayName() {
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   916
                                return "import: " + type + ". " +
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   917
                                        repl.messageFormat("jshell.console.create.variable");
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   918
                            }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   919
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   920
                            @Override
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   921
                            public void perform(ConsoleReader in) throws IOException {
44188
3f2047e62102 8176412: jshell tool: automatic imports are excluded on /reload causing it to fail
rfield
parents: 43773
diff changeset
   922
                                repl.processCompleteSource("import " + type + ";");
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   923
                                in.println("Imported: " + type);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   924
                                performToVar(in, stype);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   925
                            }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   926
                        });
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   927
                    }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   928
                }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   929
                return new FixResult(fixes, null);
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   930
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   931
        },
46185
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   932
        new FixComputer('m', false) { //compute "Introduce method" Fix:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   933
            private void performToMethod(ConsoleReader in, String type, String code) throws IOException {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   934
                in.redrawLine();
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   935
                if (!code.trim().endsWith(";")) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   936
                    in.putString(";");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   937
                }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   938
                in.putString(" }");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   939
                in.setCursorPosition(0);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   940
                String afterCursor = type.equals("void")
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   941
                        ? "() { "
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   942
                        : "() { return ";
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   943
                in.putString(type + " " + afterCursor);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   944
                // position the cursor where the method name should be entered (before parens)
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   945
                in.setCursorPosition(in.getCursorBuffer().cursor - afterCursor.length());
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   946
                in.flush();
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   947
            }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   948
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   949
            private FixResult reject(JShellTool repl, String messageKey) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   950
                return new FixResult(Collections.emptyList(), repl.messageFormat(messageKey));
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   951
            }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   952
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   953
            @Override
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   954
            public FixResult compute(JShellTool repl, String code, int cursor) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   955
                final String codeToCursor = code.substring(0, cursor);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   956
                final String type;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   957
                final CompletionInfo ci = repl.analysis.analyzeCompletion(codeToCursor);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   958
                if (!ci.remaining().isEmpty()) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   959
                    return reject(repl, "jshell.console.exprstmt");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   960
                }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   961
                switch (ci.completeness()) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   962
                    case COMPLETE:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   963
                    case COMPLETE_WITH_SEMI:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   964
                    case CONSIDERED_INCOMPLETE:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   965
                        break;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   966
                    case EMPTY:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   967
                        return reject(repl, "jshell.console.empty");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   968
                    case DEFINITELY_INCOMPLETE:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   969
                    case UNKNOWN:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   970
                    default:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   971
                        return reject(repl, "jshell.console.erroneous");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   972
                }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   973
                List<Snippet> snl = repl.analysis.sourceToSnippets(ci.source());
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   974
                if (snl.size() != 1) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   975
                    return reject(repl, "jshell.console.erroneous");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   976
                }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   977
                Snippet sn = snl.get(0);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   978
                switch (sn.kind()) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   979
                    case EXPRESSION:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   980
                        type = ((ExpressionSnippet) sn).typeName();
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   981
                        break;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   982
                    case STATEMENT:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   983
                        type = "void";
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   984
                        break;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   985
                    case VAR:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   986
                        if (sn.subKind() != SubKind.TEMP_VAR_EXPRESSION_SUBKIND) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   987
                            // only valid var is an expression turned into a temp var
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   988
                            return reject(repl, "jshell.console.exprstmt");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   989
                        }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   990
                        type = ((VarSnippet) sn).typeName();
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   991
                        break;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   992
                    case IMPORT:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   993
                    case METHOD:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   994
                    case TYPE_DECL:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   995
                        return reject(repl, "jshell.console.exprstmt");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   996
                    case ERRONEOUS:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   997
                    default:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   998
                        return reject(repl, "jshell.console.erroneous");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   999
                }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1000
                List<Fix> fixes = new ArrayList<>();
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1001
                fixes.add(new Fix() {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1002
                    @Override
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1003
                    public String displayName() {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1004
                        return repl.messageFormat("jshell.console.create.method");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1005
                    }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1006
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1007
                    @Override
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1008
                    public void perform(ConsoleReader in) throws IOException {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1009
                        performToMethod(in, type, codeToCursor);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1010
                    }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1011
                });
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1012
                int idx = type.lastIndexOf(".");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1013
                if (idx > 0) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1014
                    String stype = type.substring(idx + 1);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1015
                    QualifiedNames res = repl.analysis.listQualifiedNames(stype, stype.length());
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1016
                    if (res.isUpToDate() && res.getNames().contains(type)
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1017
                            && !res.isResolvable()) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1018
                        fixes.add(new Fix() {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1019
                            @Override
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1020
                            public String displayName() {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1021
                                return "import: " + type + ". " +
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1022
                                        repl.messageFormat("jshell.console.create.method");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1023
                            }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1024
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1025
                            @Override
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1026
                            public void perform(ConsoleReader in) throws IOException {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1027
                                repl.processCompleteSource("import " + type + ";");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1028
                                in.println("Imported: " + type);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1029
                                performToMethod(in, stype, codeToCursor);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1030
                            }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1031
                        });
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1032
                    }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1033
                }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1034
                return new FixResult(fixes, null);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1035
            }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1036
        },
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1037
        new FixComputer('i', true) { //compute "Add import" Fixes:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1038
            @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1039
            public FixResult compute(JShellTool repl, String code, int cursor) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1040
                QualifiedNames res = repl.analysis.listQualifiedNames(code, cursor);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1041
                List<Fix> fixes = new ArrayList<>();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1042
                for (String fqn : res.getNames()) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1043
                    fixes.add(new Fix() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1044
                        @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1045
                        public String displayName() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1046
                            return "import: " + fqn;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1047
                        }
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
  1048
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1049
                        @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1050
                        public void perform(ConsoleReader in) throws IOException {
44188
3f2047e62102 8176412: jshell tool: automatic imports are excluded on /reload causing it to fail
rfield
parents: 43773
diff changeset
  1051
                            repl.processCompleteSource("import " + fqn + ";");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1052
                            in.println("Imported: " + fqn);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1053
                            in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1054
                        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1055
                    });
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1056
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1057
                if (res.isResolvable()) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1058
                    return new FixResult(Collections.emptyList(),
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
  1059
                            repl.messageFormat("jshell.console.resolvable"));
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1060
                } else {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1061
                    String error = "";
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1062
                    if (fixes.isEmpty()) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
  1063
                        error = repl.messageFormat("jshell.console.no.candidate");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1064
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1065
                    if (!res.isUpToDate()) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
  1066
                        error += repl.messageFormat("jshell.console.incomplete");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1067
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1068
                    return new FixResult(fixes, error);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1069
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1070
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1071
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1072
    };
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1073
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1074
    private static final class JShellUnixTerminal extends NoInterruptUnixTerminal implements SuspendableTerminal {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1075
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1076
        private final StopDetectingInputStream input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1077
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1078
        public JShellUnixTerminal(StopDetectingInputStream input) throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1079
            this.input = input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1080
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1081
45501
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1082
        @Override
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1083
        public boolean isRaw() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1084
            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1085
                return getSettings().get("-a").contains("-icanon");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1086
            } catch (IOException | InterruptedException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1087
                return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1088
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1089
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1090
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1091
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1092
        public InputStream wrapInIfNeeded(InputStream in) throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1093
            return input.setInputStream(super.wrapInIfNeeded(in));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1094
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1095
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1096
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1097
        public void disableInterruptCharacter() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1098
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1099
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1100
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1101
        public void enableInterruptCharacter() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1102
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1103
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1104
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1105
        public void suspend() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1106
            try {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1107
                getSettings().restore();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1108
                super.disableInterruptCharacter();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1109
            } catch (Exception ex) {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1110
                throw new IllegalStateException(ex);
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1111
            }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1112
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1113
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1114
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1115
        public void resume() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1116
            try {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1117
                init();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1118
            } catch (Exception ex) {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1119
                throw new IllegalStateException(ex);
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1120
            }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1121
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1122
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1123
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1124
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1125
    private static final class JShellWindowsTerminal extends WindowsTerminal implements SuspendableTerminal {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1126
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1127
        private final StopDetectingInputStream input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1128
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1129
        public JShellWindowsTerminal(StopDetectingInputStream input) throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1130
            this.input = input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1131
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1132
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1133
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1134
        public void init() throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1135
            super.init();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1136
            setAnsiSupported(false);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1137
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1138
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1139
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1140
        public InputStream wrapInIfNeeded(InputStream in) throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1141
            return input.setInputStream(super.wrapInIfNeeded(in));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1142
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1143
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1144
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1145
        public void suspend() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1146
            try {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1147
                restore();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1148
                setConsoleMode(getConsoleMode() & ~ConsoleMode.ENABLE_PROCESSED_INPUT.code);
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1149
            } catch (Exception ex) {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1150
                throw new IllegalStateException(ex);
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1151
            }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1152
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1153
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1154
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1155
        public void resume() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1156
            try {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1157
                restore();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1158
                init();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1159
            } catch (Exception ex) {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1160
                throw new IllegalStateException(ex);
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1161
            }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1162
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1163
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1164
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1165
        public boolean isRaw() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1166
            return (getConsoleMode() & ConsoleMode.ENABLE_LINE_INPUT.code) == 0;
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1167
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1168
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1169
    }
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1170
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1171
    private static final class TestTerminal extends TerminalSupport {
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1172
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1173
        private final StopDetectingInputStream input;
45501
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1174
        private final int height;
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1175
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1176
        public TestTerminal(StopDetectingInputStream input) throws Exception {
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1177
            super(true);
46923
0172e4350f65 8182297: jshell tool: pasting multiple lines of code truncated
jlahoda
parents: 46185
diff changeset
  1178
            setAnsiSupported(Boolean.getBoolean("test.terminal.ansi.supported"));
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
  1179
            setEchoEnabled(false);
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1180
            this.input = input;
45501
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1181
            int h = DEFAULT_HEIGHT;
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1182
            try {
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1183
                String hp = System.getProperty("test.terminal.height");
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1184
                if (hp != null && !hp.isEmpty()) {
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1185
                    h = Integer.parseInt(hp);
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1186
                }
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1187
            } catch (Throwable ex) {
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1188
                // ignore
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1189
            }
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1190
            this.height = h;
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1191
        }
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1192
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1193
        @Override
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1194
        public InputStream wrapInIfNeeded(InputStream in) throws IOException {
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1195
            return input.setInputStream(super.wrapInIfNeeded(in));
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1196
        }
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1197
45501
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1198
        @Override
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1199
        public int getHeight() {
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1200
            return height;
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1201
        }
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1202
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1203
    }
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1204
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1205
    private interface SuspendableTerminal {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1206
        public void suspend();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1207
        public void resume();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1208
        public boolean isRaw();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1209
    }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1210
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1211
    private static final class CheckCompletionKeyMap extends KeyMap {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1212
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1213
        private final KeyMap del;
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1214
        private final CompletionState completionState;
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1215
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1216
        public CheckCompletionKeyMap(KeyMap del, CompletionState completionState) {
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1217
            super(del.getName(), del.isViKeyMap());
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1218
            this.del = del;
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1219
            this.completionState = completionState;
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1220
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1221
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1222
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1223
        public void bind(CharSequence keySeq, Object function) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1224
            del.bind(keySeq, function);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1225
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1226
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1227
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1228
        public void bindIfNotBound(CharSequence keySeq, Object function) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1229
            del.bindIfNotBound(keySeq, function);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1230
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1231
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1232
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1233
        public void from(KeyMap other) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1234
            del.from(other);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1235
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1236
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1237
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1238
        public Object getAnotherKey() {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1239
            return del.getAnotherKey();
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1240
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1241
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1242
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1243
        public Object getBound(CharSequence keySeq) {
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1244
            this.completionState.actionCount++;
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1245
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1246
            return del.getBound(keySeq);
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1247
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1248
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1249
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1250
        public void setBlinkMatchingParen(boolean on) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1251
            del.setBlinkMatchingParen(on);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1252
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1253
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1254
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1255
        public String toString() {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1256
            return "check: " + del.toString();
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1257
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1258
    }
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1259
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1260
    private static final class CompletionState {
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1261
        /**The number of actions since the last completion invocation. Will be 1 when completion is
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1262
         * invoked immediately after the last completion invocation.*/
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1263
        public int actionCount;
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1264
        /**Precomputed completion actions. Should only be reused if actionCount == 1.*/
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1265
        public List<CompletionTask> todo = Collections.emptyList();
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
  1266
    }
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1267
}