src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java
author redestad
Wed, 13 Dec 2017 21:25:49 +0100
changeset 48294 2608240fc957
parent 48275 b2190c70a1ac
child 48939 ba545e52b932
permissions -rw-r--r--
8193471: Startup regression due to JDK-8185582 Reviewed-by: rriggs, psandoz
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;
48275
b2190c70a1ac 8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents: 47840
diff changeset
   219
                boolean command = prefix.isEmpty() && text.startsWith("/");
44459
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();
47484
d0cb66d8cbf1 8189595: jshell tool: line redrawn on each completion
jlahoda
parents: 47216
diff changeset
   240
                CompletionTask ordinaryCompletion =
d0cb66d8cbf1 8189595: jshell tool: line redrawn on each completion
jlahoda
parents: 47216
diff changeset
   241
                        new OrdinaryCompletionTask(suggestions,
d0cb66d8cbf1 8189595: jshell tool: line redrawn on each completion
jlahoda
parents: 47216
diff changeset
   242
                                                   anchor[0],
d0cb66d8cbf1 8189595: jshell tool: line redrawn on each completion
jlahoda
parents: 47216
diff changeset
   243
                                                   !command && !doc.isEmpty(),
d0cb66d8cbf1 8189595: jshell tool: line redrawn on each completion
jlahoda
parents: 47216
diff changeset
   244
                                                   hasBoth);
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   245
                CompletionTask allCompletion = new AllSuggestionsCompletionTask(suggestions, anchor[0]);
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   246
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   247
                todo = new ArrayList<>();
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   248
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   249
                //the main decission tree:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   250
                if (command) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   251
                    CompletionTask shortDocumentation = new CommandSynopsisTask(doc);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   252
                    CompletionTask fullDocumentation = new CommandFullDocumentationTask(todo);
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   253
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   254
                    if (!doc.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   255
                        if (tooManyItems) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   256
                            todo.add(new NoopCompletionTask());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   257
                            todo.add(allCompletion);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   258
                        } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   259
                            todo.add(ordinaryCompletion);
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   260
                        }
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   261
                        todo.add(shortDocumentation);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   262
                        todo.add(fullDocumentation);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   263
                    } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   264
                        todo.add(new NoSuchCommandCompletionTask());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   265
                    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   266
                } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   267
                    if (doc.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   268
                        if (hasSmart) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   269
                            todo.add(ordinaryCompletion);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   270
                        } else if (tooManyItems) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   271
                            todo.add(new NoopCompletionTask());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   272
                        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   273
                        if (!hasSmart || hasBoth) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   274
                            todo.add(allCompletion);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   275
                        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   276
                    } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   277
                        CompletionTask shortDocumentation = new ExpressionSignaturesTask(doc);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   278
                        CompletionTask fullDocumentation = new ExpressionJavadocTask(todo);
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   279
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   280
                        if (hasSmart) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   281
                            todo.add(ordinaryCompletion);
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
                        todo.add(shortDocumentation);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   284
                        if (!hasSmart || hasBoth) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   285
                            todo.add(allCompletion);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   286
                        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   287
                        if (tooManyItems) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   288
                            todo.add(todo.size() - 1, fullDocumentation);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   289
                        } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   290
                            todo.add(fullDocumentation);
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   291
                        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   292
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   293
                }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   294
            }
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   295
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   296
            boolean success = false;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   297
            boolean repaint = true;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   298
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   299
            OUTER: while (!todo.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   300
                CompletionTask.Result result = todo.remove(0).perform(text, cursor);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   301
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   302
                switch (result) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   303
                    case CONTINUE:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   304
                        break;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   305
                    case SKIP_NOREPAINT:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   306
                        repaint = false;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   307
                    case SKIP:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   308
                        todo.clear();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   309
                        //intentional fall-through
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   310
                    case FINISH:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   311
                        success = true;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   312
                        //intentional fall-through
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   313
                    case NO_DATA:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   314
                        if (!todo.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   315
                            in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   316
                            in.println(todo.get(0).description());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   317
                        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   318
                        break OUTER;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   319
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   320
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   321
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   322
            completionState.actionCount = 0;
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   323
            completionState.todo = todo;
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
   324
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   325
            if (repaint) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   326
                in.redrawLine();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   327
                in.flush();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   328
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   329
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   330
            return success;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   331
        } catch (IOException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   332
            throw new IllegalStateException(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   333
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   334
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   335
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   336
    private CompletionTask.Result doPrintFullDocumentation(List<CompletionTask> todo, List<String> doc, boolean command) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   337
        if (doc != null && !doc.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   338
            Terminal term = in.getTerminal();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   339
            int pageHeight = term.getHeight() - NEEDED_LINES;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   340
            List<CompletionTask> thisTODO = new ArrayList<>();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   341
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   342
            for (Iterator<String> docIt = doc.iterator(); docIt.hasNext(); ) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   343
                String currentDoc = docIt.next();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   344
                String[] lines = currentDoc.split("\n");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   345
                int firstLine = 0;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   346
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   347
                while (firstLine < lines.length) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   348
                    boolean first = firstLine == 0;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   349
                    String[] thisPageLines =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   350
                            Arrays.copyOfRange(lines,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   351
                                               firstLine,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   352
                                               Math.min(firstLine + pageHeight, lines.length));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   353
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   354
                    thisTODO.add(new CompletionTask() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   355
                        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   356
                        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   357
                            String key =  !first ? "jshell.console.see.next.page"
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   358
                                                 : command ? "jshell.console.see.next.command.doc"
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   359
                                                           : "jshell.console.see.next.javadoc";
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   360
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   361
                            return repl.getResourceString(key);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   362
                        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   363
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   364
                        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   365
                        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   366
                            in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   367
                            for (String line : thisPageLines) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   368
                                in.println(line);
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
                            return Result.FINISH;
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
                    firstLine += pageHeight;
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
            }
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
            todo.addAll(0, thisTODO);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   379
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   380
            return CompletionTask.Result.CONTINUE;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   381
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   382
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   383
        return CompletionTask.Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   384
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   385
    //where:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   386
        private static final int NEEDED_LINES = 4;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   387
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   388
    private static String commonPrefix(String str1, String str2) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   389
        for (int i = 0; i < str2.length(); i++) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   390
            if (!str1.startsWith(str2.substring(0, i + 1))) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   391
                return str2.substring(0, i);
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
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   394
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   395
        return str2;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   396
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   397
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   398
    private interface CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   399
        public String description();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   400
        public Result perform(String text, int cursor) throws IOException;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   401
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   402
        enum Result {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   403
            NO_DATA,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   404
            CONTINUE,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   405
            FINISH,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   406
            SKIP,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   407
            SKIP_NOREPAINT;
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
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   410
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   411
    private final class NoopCompletionTask implements CompletionTask {
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
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   414
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   415
            throw new UnsupportedOperationException("Should not get here.");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   416
        }
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
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   419
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   420
            return Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   421
        }
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
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   424
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   425
    private final class NoSuchCommandCompletionTask implements CompletionTask {
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
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   428
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   429
            throw new UnsupportedOperationException("Should not get here.");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   430
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   431
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   432
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   433
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   434
            in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   435
            in.println(repl.getResourceString("jshell.console.no.such.command"));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   436
            in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   437
            return Result.SKIP;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   438
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   439
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   440
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   441
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   442
    private final class OrdinaryCompletionTask implements CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   443
        private final List<Suggestion> suggestions;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   444
        private final int anchor;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   445
        private final boolean cont;
47484
d0cb66d8cbf1 8189595: jshell tool: line redrawn on each completion
jlahoda
parents: 47216
diff changeset
   446
        private final boolean showSmart;
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   447
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   448
        public OrdinaryCompletionTask(List<Suggestion> suggestions,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   449
                                      int anchor,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   450
                                      boolean cont,
47484
d0cb66d8cbf1 8189595: jshell tool: line redrawn on each completion
jlahoda
parents: 47216
diff changeset
   451
                                      boolean showSmart) {
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   452
            this.suggestions = suggestions;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   453
            this.anchor = anchor;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   454
            this.cont = cont;
47484
d0cb66d8cbf1 8189595: jshell tool: line redrawn on each completion
jlahoda
parents: 47216
diff changeset
   455
            this.showSmart = showSmart;
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   456
        }
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
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   459
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   460
            throw new UnsupportedOperationException("Should not get here.");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   461
        }
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
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   464
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   465
            List<CharSequence> toShow;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   466
47484
d0cb66d8cbf1 8189595: jshell tool: line redrawn on each completion
jlahoda
parents: 47216
diff changeset
   467
            if (showSmart) {
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   468
                toShow =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   469
                    suggestions.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   470
                               .filter(Suggestion::matchesType)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   471
                               .map(Suggestion::continuation)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   472
                               .distinct()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   473
                               .collect(Collectors.toList());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   474
            } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   475
                toShow =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   476
                    suggestions.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   477
                               .map(Suggestion::continuation)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   478
                               .distinct()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   479
                               .collect(Collectors.toList());
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
            if (toShow.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   483
                return Result.CONTINUE;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   484
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   485
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   486
            Optional<String> prefix =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   487
                    suggestions.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   488
                               .map(Suggestion::continuation)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   489
                               .reduce(ConsoleIOContext::commonPrefix);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   490
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   491
            String prefixStr = prefix.orElse("").substring(cursor - anchor);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   492
            in.putString(prefixStr);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   493
47484
d0cb66d8cbf1 8189595: jshell tool: line redrawn on each completion
jlahoda
parents: 47216
diff changeset
   494
            boolean showItems = toShow.size() > 1 || showSmart;
44459
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
            if (showItems) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   497
                in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   498
                in.printColumns(toShow);
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
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   501
            if (!prefixStr.isEmpty())
47484
d0cb66d8cbf1 8189595: jshell tool: line redrawn on each completion
jlahoda
parents: 47216
diff changeset
   502
                return showItems ? Result.FINISH : Result.SKIP_NOREPAINT;
44459
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
            return cont ? Result.CONTINUE : Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   505
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   506
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   507
    }
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
    private final class AllSuggestionsCompletionTask implements CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   510
        private final List<Suggestion> suggestions;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   511
        private final int anchor;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   512
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   513
        public AllSuggestionsCompletionTask(List<Suggestion> suggestions,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   514
                                            int anchor) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   515
            this.suggestions = suggestions;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   516
            this.anchor = anchor;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   517
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   518
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   519
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   520
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   521
            if (suggestions.size() <= in.getAutoprintThreshold()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   522
                return repl.getResourceString("jshell.console.completion.all.completions");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   523
            } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   524
                return repl.messageFormat("jshell.console.completion.all.completions.number", suggestions.size());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   525
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   526
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   527
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   528
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   529
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   530
            List<String> candidates =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   531
                    suggestions.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   532
                               .map(Suggestion::continuation)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   533
                               .distinct()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   534
                               .collect(Collectors.toList());
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
            Optional<String> prefix =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   537
                    candidates.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   538
                              .reduce(ConsoleIOContext::commonPrefix);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   539
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   540
            String prefixStr = prefix.map(str -> str.substring(cursor - anchor)).orElse("");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   541
            in.putString(prefixStr);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   542
            if (candidates.size() > 1) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   543
                in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   544
                in.printColumns(candidates);
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
            return suggestions.isEmpty() ? Result.NO_DATA : Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   547
        }
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
    }
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
    private final class CommandSynopsisTask implements CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   552
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   553
        private final List<String> synopsis;
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
        public CommandSynopsisTask(List<String> synposis) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   556
            this.synopsis = synposis;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   557
        }
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
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   560
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   561
            return repl.getResourceString("jshell.console.see.synopsis");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   562
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   563
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   564
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   565
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   566
            try {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   567
                in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   568
                in.println(synopsis.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   569
                                   .map(l -> l.replaceAll("\n", LINE_SEPARATOR))
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   570
                                   .collect(Collectors.joining(LINE_SEPARATORS2)));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   571
            } catch (IOException ex) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   572
                throw new IllegalStateException(ex);
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
            return Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   575
        }
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
    }
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
    private final class CommandFullDocumentationTask implements CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   580
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   581
        private final List<CompletionTask> todo;
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
        public CommandFullDocumentationTask(List<CompletionTask> todo) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   584
            this.todo = todo;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   585
        }
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
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   588
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   589
            return repl.getResourceString("jshell.console.see.full.documentation");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   590
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   591
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   592
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   593
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   594
            List<String> fullDoc = repl.commandDocumentation(text, cursor, false);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   595
            return doPrintFullDocumentation(todo, fullDoc, true);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   596
        }
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
    }
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
    private final class ExpressionSignaturesTask implements CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   601
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   602
        private final List<String> doc;
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
        public ExpressionSignaturesTask(List<String> doc) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   605
            this.doc = doc;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   606
        }
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
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   609
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   610
            throw new UnsupportedOperationException("Should not get here.");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   611
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   612
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   613
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   614
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   615
            in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   616
            in.println(repl.getResourceString("jshell.console.completion.current.signatures"));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   617
            in.println(doc.stream().collect(Collectors.joining(LINE_SEPARATOR)));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   618
            return Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   619
        }
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
    }
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
    private final class ExpressionJavadocTask implements CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   624
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   625
        private final List<CompletionTask> todo;
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
        public ExpressionJavadocTask(List<CompletionTask> todo) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   628
            this.todo = todo;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   629
        }
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
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   632
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   633
            return repl.getResourceString("jshell.console.see.documentation");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   634
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   635
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   636
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   637
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   638
            //schedule showing javadoc:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   639
            Terminal term = in.getTerminal();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   640
            JavadocFormatter formatter = new JavadocFormatter(term.getWidth(),
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   641
                                                              term.isAnsiSupported());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   642
            Function<Documentation, String> convertor = d -> formatter.formatJavadoc(d.signature(), d.javadoc()) +
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   643
                             (d.javadoc() == null ? repl.messageFormat("jshell.console.no.javadoc")
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   644
                                                  : "");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   645
            List<String> doc = repl.analysis.documentation(prefix + text, cursor + prefix.length(), true)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   646
                                            .stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   647
                                            .map(convertor)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   648
                                            .collect(Collectors.toList());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   649
            return doPrintFullDocumentation(todo, doc, false);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   650
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   651
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   652
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   653
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   654
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   655
    public boolean terminalEditorRunning() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   656
        Terminal terminal = in.getTerminal();
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   657
        if (terminal instanceof SuspendableTerminal)
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   658
            return ((SuspendableTerminal) terminal).isRaw();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   659
        return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   660
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   661
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   662
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   663
    public void suspend() {
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   664
        Terminal terminal = in.getTerminal();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   665
        if (terminal instanceof SuspendableTerminal)
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   666
            ((SuspendableTerminal) terminal).suspend();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   667
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   668
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   669
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   670
    public void resume() {
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   671
        Terminal terminal = in.getTerminal();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   672
        if (terminal instanceof SuspendableTerminal)
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   673
            ((SuspendableTerminal) terminal).resume();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   674
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   675
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   676
    @Override
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   677
    public void beforeUserCode() {
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   678
        synchronized (this) {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   679
            inputBytes = null;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   680
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   681
        input.setState(State.BUFFER);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   682
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   683
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   684
    @Override
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   685
    public void afterUserCode() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   686
        input.setState(State.WAIT);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   687
    }
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
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   690
    public void replaceLastHistoryEntry(String source) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   691
        history.fullHistoryReplace(source);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   692
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   693
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   694
    private static final long ESCAPE_TIMEOUT = 100;
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
    private void fixes() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   697
        try {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   698
            int c = in.readCharacter();
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
            if (c == (-1)) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   701
                return ;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   702
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   703
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   704
            for (FixComputer computer : FIX_COMPUTERS) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   705
                if (computer.shortcut == c) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   706
                    fixes(computer);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   707
                    return ;
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
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   710
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   711
            readOutRemainingEscape(c);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   712
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   713
            in.beep();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   714
            in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   715
            in.println(repl.getResourceString("jshell.fix.wrong.shortcut"));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   716
            in.redrawLine();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   717
            in.flush();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   718
        } catch (IOException ex) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   719
            ex.printStackTrace();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   720
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   721
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   722
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   723
    private void readOutRemainingEscape(int c) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   724
        if (c == '\033') {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   725
            //escape, consume waiting input:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   726
            InputStream inp = in.getInput();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   727
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   728
            if (inp instanceof NonBlockingInputStream) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   729
                NonBlockingInputStream nbis = (NonBlockingInputStream) inp;
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
                while (nbis.isNonBlockingEnabled() && nbis.peek(ESCAPE_TIMEOUT) > 0) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   732
                    in.readCharacter();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   733
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   734
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   735
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   736
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   737
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   738
    //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
   739
    //and perform the selected one:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   740
    private void fixes(FixComputer computer) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   741
        String input = prefix + in.getCursorBuffer().toString();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   742
        int cursor = prefix.length() + in.getCursorBuffer().cursor;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   743
        FixResult candidates = computer.compute(repl, input, cursor);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   744
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   745
        try {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   746
            final boolean printError = candidates.error != null && !candidates.error.isEmpty();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   747
            if (printError) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   748
                in.println(candidates.error);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   749
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   750
            if (candidates.fixes.isEmpty()) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   751
                in.beep();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   752
                if (printError) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   753
                    in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   754
                    in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   755
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   756
            } else if (candidates.fixes.size() == 1 && !computer.showMenu) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   757
                if (printError) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   758
                    in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   759
                    in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   760
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   761
                candidates.fixes.get(0).perform(in);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   762
            } else {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   763
                List<Fix> fixes = new ArrayList<>(candidates.fixes);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   764
                fixes.add(0, new Fix() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   765
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   766
                    public String displayName() {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   767
                        return repl.messageFormat("jshell.console.do.nothing");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   768
                    }
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
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   771
                    public void perform(ConsoleReader in) throws IOException {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   772
                        in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   773
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   774
                });
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   775
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   776
                Map<Character, Fix> char2Fix = new HashMap<>();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   777
                in.println();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   778
                for (int i = 0; i < fixes.size(); i++) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   779
                    Fix fix = fixes.get(i);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   780
                    char2Fix.put((char) ('0' + i), fix);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   781
                    in.println("" + i + ": " + fixes.get(i).displayName());
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   782
                }
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   783
                in.print(repl.messageFormat("jshell.console.choice"));
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   784
                in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   785
                int 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
                read = in.readCharacter();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   788
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   789
                Fix fix = char2Fix.get((char) read);
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
                if (fix == null) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   792
                    in.beep();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   793
                    fix = fixes.get(0);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   794
                }
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.println();
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
                fix.perform(in);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   799
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   800
                in.flush();
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
        } catch (IOException ex) {
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   803
            throw new IllegalStateException(ex);
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   804
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   805
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   806
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   807
    private byte[] inputBytes;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   808
    private int inputBytesPointer;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   809
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   810
    @Override
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
   811
    public synchronized int readUserInput() throws IOException {
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   812
        while (inputBytes == null || inputBytes.length <= inputBytesPointer) {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   813
            boolean prevHandleUserInterrupt = in.getHandleUserInterrupt();
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   814
            History prevHistory = in.getHistory();
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   815
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   816
            try {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   817
                input.setState(State.WAIT);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   818
                in.setHandleUserInterrupt(true);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   819
                in.setHistory(userInputHistory);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   820
                inputBytes = (in.readLine("") + System.getProperty("line.separator")).getBytes();
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   821
                inputBytesPointer = 0;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   822
            } catch (UserInterruptException ex) {
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
   823
                throw new InterruptedIOException();
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   824
            } finally {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   825
                in.setHistory(prevHistory);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   826
                in.setHandleUserInterrupt(prevHandleUserInterrupt);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   827
                input.setState(State.BUFFER);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   828
            }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   829
        }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   830
        return inputBytes[inputBytesPointer++];
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   831
    }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   832
36160
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 possible action which the user can choose to perform.
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 interface Fix {
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
         * A name that should be shown to the user.
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 String displayName();
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
         * Perform the given action.
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
        public void perform(ConsoleReader in) throws IOException;
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
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   847
    /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   848
     * A factory for {@link Fix}es.
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
    public abstract static class FixComputer {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   851
        private final char shortcut;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   852
        private final boolean showMenu;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   853
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
         * 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
   856
         * 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
   857
         * 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
   858
         */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   859
        public FixComputer(char shortcut, boolean showMenu) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   860
            this.shortcut = shortcut;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   861
            this.showMenu = showMenu;
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
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
         * Compute possible actions for the given code.
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
        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
   868
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   869
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   870
    /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   871
     * 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
   872
     */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   873
    public static class FixResult {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   874
        public final List<Fix> fixes;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   875
        public final String 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
        public FixResult(List<Fix> fixes, String error) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   878
            this.fixes = fixes;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   879
            this.error = error;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   880
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   881
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   882
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   883
    private static final FixComputer[] FIX_COMPUTERS = new FixComputer[] {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   884
        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
   885
            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
   886
                in.redrawLine();
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   887
                in.setCursorPosition(0);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   888
                in.putString(type + "  = ");
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   889
                in.setCursorPosition(in.getCursorBuffer().cursor - 3);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   890
                in.flush();
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   891
            }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   892
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   893
            @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   894
            public FixResult compute(JShellTool repl, String code, int cursor) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   895
                String type = repl.analysis.analyzeType(code, cursor);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   896
                if (type == null) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   897
                    return new FixResult(Collections.emptyList(), null);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   898
                }
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   899
                List<Fix> fixes = new ArrayList<>();
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   900
                fixes.add(new Fix() {
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   901
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   902
                    public String displayName() {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   903
                        return repl.messageFormat("jshell.console.create.variable");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   904
                    }
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   905
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   906
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   907
                    public void perform(ConsoleReader in) throws IOException {
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   908
                        performToVar(in, type);
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   909
                    }
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   910
                });
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   911
                int idx = type.lastIndexOf(".");
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   912
                if (idx > 0) {
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   913
                    String stype = type.substring(idx + 1);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   914
                    QualifiedNames res = repl.analysis.listQualifiedNames(stype, stype.length());
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   915
                    if (res.isUpToDate() && res.getNames().contains(type)
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   916
                            && !res.isResolvable()) {
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   917
                        fixes.add(new Fix() {
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   918
                            @Override
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   919
                            public String displayName() {
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   920
                                return "import: " + type + ". " +
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   921
                                        repl.messageFormat("jshell.console.create.variable");
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   922
                            }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   923
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   924
                            @Override
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   925
                            public void perform(ConsoleReader in) throws IOException {
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47484
diff changeset
   926
                                repl.processSource("import " + type + ";");
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   927
                                in.println("Imported: " + type);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   928
                                performToVar(in, stype);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   929
                            }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   930
                        });
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   931
                    }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   932
                }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   933
                return new FixResult(fixes, null);
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   934
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   935
        },
46185
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   936
        new FixComputer('m', false) { //compute "Introduce method" Fix:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   937
            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
   938
                in.redrawLine();
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   939
                if (!code.trim().endsWith(";")) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   940
                    in.putString(";");
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
                in.putString(" }");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   943
                in.setCursorPosition(0);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   944
                String afterCursor = type.equals("void")
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   945
                        ? "() { "
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   946
                        : "() { return ";
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   947
                in.putString(type + " " + afterCursor);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   948
                // 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
   949
                in.setCursorPosition(in.getCursorBuffer().cursor - afterCursor.length());
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   950
                in.flush();
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
            private FixResult reject(JShellTool repl, String messageKey) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   954
                return new FixResult(Collections.emptyList(), repl.messageFormat(messageKey));
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   955
            }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   956
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   957
            @Override
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   958
            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
   959
                final String codeToCursor = code.substring(0, cursor);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   960
                final String type;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   961
                final CompletionInfo ci = repl.analysis.analyzeCompletion(codeToCursor);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   962
                if (!ci.remaining().isEmpty()) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   963
                    return reject(repl, "jshell.console.exprstmt");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   964
                }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   965
                switch (ci.completeness()) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   966
                    case COMPLETE:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   967
                    case COMPLETE_WITH_SEMI:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   968
                    case CONSIDERED_INCOMPLETE:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   969
                        break;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   970
                    case EMPTY:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   971
                        return reject(repl, "jshell.console.empty");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   972
                    case DEFINITELY_INCOMPLETE:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   973
                    case UNKNOWN:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   974
                    default:
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
                List<Snippet> snl = repl.analysis.sourceToSnippets(ci.source());
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   978
                if (snl.size() != 1) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   979
                    return reject(repl, "jshell.console.erroneous");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   980
                }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   981
                Snippet sn = snl.get(0);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   982
                switch (sn.kind()) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   983
                    case EXPRESSION:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   984
                        type = ((ExpressionSnippet) sn).typeName();
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   985
                        break;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   986
                    case STATEMENT:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   987
                        type = "void";
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   988
                        break;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   989
                    case VAR:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   990
                        if (sn.subKind() != SubKind.TEMP_VAR_EXPRESSION_SUBKIND) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   991
                            // 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
   992
                            return reject(repl, "jshell.console.exprstmt");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   993
                        }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   994
                        type = ((VarSnippet) sn).typeName();
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   995
                        break;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   996
                    case IMPORT:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   997
                    case METHOD:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   998
                    case TYPE_DECL:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
   999
                        return reject(repl, "jshell.console.exprstmt");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1000
                    case ERRONEOUS:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1001
                    default:
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1002
                        return reject(repl, "jshell.console.erroneous");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1003
                }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1004
                List<Fix> fixes = new ArrayList<>();
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1005
                fixes.add(new Fix() {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1006
                    @Override
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1007
                    public String displayName() {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1008
                        return repl.messageFormat("jshell.console.create.method");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1009
                    }
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
                    @Override
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1012
                    public void perform(ConsoleReader in) throws IOException {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1013
                        performToMethod(in, type, codeToCursor);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1014
                    }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1015
                });
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1016
                int idx = type.lastIndexOf(".");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1017
                if (idx > 0) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1018
                    String stype = type.substring(idx + 1);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1019
                    QualifiedNames res = repl.analysis.listQualifiedNames(stype, stype.length());
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1020
                    if (res.isUpToDate() && res.getNames().contains(type)
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1021
                            && !res.isResolvable()) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1022
                        fixes.add(new Fix() {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1023
                            @Override
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1024
                            public String displayName() {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1025
                                return "import: " + type + ". " +
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1026
                                        repl.messageFormat("jshell.console.create.method");
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1027
                            }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1028
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1029
                            @Override
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1030
                            public void perform(ConsoleReader in) throws IOException {
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47484
diff changeset
  1031
                                repl.processSource("import " + type + ";");
46185
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1032
                                in.println("Imported: " + type);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1033
                                performToMethod(in, stype, codeToCursor);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1034
                            }
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
                    }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1037
                }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1038
                return new FixResult(fixes, null);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1039
            }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45501
diff changeset
  1040
        },
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1041
        new FixComputer('i', true) { //compute "Add import" Fixes:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1042
            @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1043
            public FixResult compute(JShellTool repl, String code, int cursor) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1044
                QualifiedNames res = repl.analysis.listQualifiedNames(code, cursor);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1045
                List<Fix> fixes = new ArrayList<>();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1046
                for (String fqn : res.getNames()) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1047
                    fixes.add(new Fix() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1048
                        @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1049
                        public String displayName() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1050
                            return "import: " + fqn;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1051
                        }
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
  1052
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1053
                        @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1054
                        public void perform(ConsoleReader in) throws IOException {
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47484
diff changeset
  1055
                            repl.processSource("import " + fqn + ";");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1056
                            in.println("Imported: " + fqn);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1057
                            in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1058
                        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1059
                    });
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1060
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1061
                if (res.isResolvable()) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1062
                    return new FixResult(Collections.emptyList(),
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
  1063
                            repl.messageFormat("jshell.console.resolvable"));
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1064
                } else {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1065
                    String error = "";
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1066
                    if (fixes.isEmpty()) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
  1067
                        error = repl.messageFormat("jshell.console.no.candidate");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1068
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1069
                    if (!res.isUpToDate()) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
  1070
                        error += repl.messageFormat("jshell.console.incomplete");
36160
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
                    return new FixResult(fixes, error);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1073
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1074
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1075
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1076
    };
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
  1077
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1078
    private static final class JShellUnixTerminal extends NoInterruptUnixTerminal implements SuspendableTerminal {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1079
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1080
        private final StopDetectingInputStream input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1081
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1082
        public JShellUnixTerminal(StopDetectingInputStream input) throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1083
            this.input = input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1084
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1085
45501
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1086
        @Override
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1087
        public boolean isRaw() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1088
            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1089
                return getSettings().get("-a").contains("-icanon");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1090
            } catch (IOException | InterruptedException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1091
                return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1092
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1093
        }
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
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1096
        public InputStream wrapInIfNeeded(InputStream in) throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1097
            return input.setInputStream(super.wrapInIfNeeded(in));
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 disableInterruptCharacter() {
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
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1104
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1105
        public void enableInterruptCharacter() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1106
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1107
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1108
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1109
        public void suspend() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1110
            try {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1111
                getSettings().restore();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1112
                super.disableInterruptCharacter();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1113
            } catch (Exception ex) {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1114
                throw new IllegalStateException(ex);
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1115
            }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1116
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1117
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1118
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1119
        public void resume() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1120
            try {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1121
                init();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1122
            } catch (Exception ex) {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1123
                throw new IllegalStateException(ex);
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1124
            }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1125
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1126
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1127
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1128
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1129
    private static final class JShellWindowsTerminal extends WindowsTerminal implements SuspendableTerminal {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1130
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1131
        private final StopDetectingInputStream input;
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
        public JShellWindowsTerminal(StopDetectingInputStream input) throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1134
            this.input = input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1135
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1136
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1137
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1138
        public void init() throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1139
            super.init();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1140
            setAnsiSupported(false);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1141
        }
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
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1144
        public InputStream wrapInIfNeeded(InputStream in) throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1145
            return input.setInputStream(super.wrapInIfNeeded(in));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1146
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1147
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1148
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1149
        public void suspend() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1150
            try {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1151
                restore();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1152
                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
  1153
            } catch (Exception ex) {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1154
                throw new IllegalStateException(ex);
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1155
            }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1156
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1157
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1158
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1159
        public void resume() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1160
            try {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1161
                restore();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1162
                init();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1163
            } catch (Exception ex) {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1164
                throw new IllegalStateException(ex);
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1165
            }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1166
        }
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
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1169
        public boolean isRaw() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1170
            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
  1171
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1172
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1173
    }
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1174
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1175
    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
  1176
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1177
        private final StopDetectingInputStream input;
45501
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1178
        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
  1179
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1180
        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
  1181
            super(true);
46923
0172e4350f65 8182297: jshell tool: pasting multiple lines of code truncated
jlahoda
parents: 46185
diff changeset
  1182
            setAnsiSupported(Boolean.getBoolean("test.terminal.ansi.supported"));
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
  1183
            setEchoEnabled(false);
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1184
            this.input = input;
45501
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1185
            int h = DEFAULT_HEIGHT;
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1186
            try {
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1187
                String hp = System.getProperty("test.terminal.height");
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1188
                if (hp != null && !hp.isEmpty()) {
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1189
                    h = Integer.parseInt(hp);
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1190
                }
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1191
            } catch (Throwable ex) {
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1192
                // ignore
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1193
            }
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1194
            this.height = h;
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1195
        }
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
        @Override
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1198
        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
  1199
            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
  1200
        }
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1201
45501
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1202
        @Override
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1203
        public int getHeight() {
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1204
            return height;
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1205
        }
a13e5e2ee35e 8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents: 44459
diff changeset
  1206
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1207
    }
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1208
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1209
    private interface SuspendableTerminal {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1210
        public void suspend();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1211
        public void resume();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1212
        public boolean isRaw();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1213
    }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1214
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1215
    private static final class CheckCompletionKeyMap extends KeyMap {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1216
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1217
        private final KeyMap del;
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1218
        private final CompletionState completionState;
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1219
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1220
        public CheckCompletionKeyMap(KeyMap del, CompletionState completionState) {
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1221
            super(del.getName(), del.isViKeyMap());
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1222
            this.del = del;
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1223
            this.completionState = completionState;
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1224
        }
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
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1227
        public void bind(CharSequence keySeq, Object function) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1228
            del.bind(keySeq, function);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1229
        }
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
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1232
        public void bindIfNotBound(CharSequence keySeq, Object function) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1233
            del.bindIfNotBound(keySeq, function);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1234
        }
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
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1237
        public void from(KeyMap other) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1238
            del.from(other);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1239
        }
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
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1242
        public Object getAnotherKey() {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1243
            return del.getAnotherKey();
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1244
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1245
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1246
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1247
        public Object getBound(CharSequence keySeq) {
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1248
            this.completionState.actionCount++;
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1249
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1250
            return del.getBound(keySeq);
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1251
        }
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
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1254
        public void setBlinkMatchingParen(boolean on) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1255
            del.setBlinkMatchingParen(on);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1256
        }
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
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1259
        public String toString() {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1260
            return "check: " + del.toString();
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1261
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1262
    }
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1263
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1264
    private static final class CompletionState {
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1265
        /**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
  1266
         * invoked immediately after the last completion invocation.*/
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1267
        public int actionCount;
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1268
        /**Precomputed completion actions. Should only be reused if actionCount == 1.*/
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1269
        public List<CompletionTask> todo = Collections.emptyList();
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
  1270
    }
47050
72ec64aeaa57 8185426: Jshell crashing on autocompletion
jlahoda
parents: 46923
diff changeset
  1271
}