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