langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java
author rfield
Tue, 11 Apr 2017 17:26:52 -0700
changeset 44683 610dc2b48954
parent 44459 5224425af378
child 45501 a13e5e2ee35e
permissions -rw-r--r--
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path 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;
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
    46
import java.util.function.BooleanSupplier;
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
    47
import java.util.function.Function;
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
    48
import java.util.stream.Collectors;
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
    49
import java.util.stream.Stream;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    50
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
    51
import jdk.internal.shellsupport.doc.JavadocFormatter;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    52
import jdk.internal.jline.NoInterruptUnixTerminal;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    53
import jdk.internal.jline.Terminal;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    54
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
    55
import jdk.internal.jline.TerminalSupport;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    56
import jdk.internal.jline.WindowsTerminal;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    57
import jdk.internal.jline.console.ConsoleReader;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    58
import jdk.internal.jline.console.KeyMap;
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
    59
import jdk.internal.jline.console.Operation;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    60
import jdk.internal.jline.console.UserInterruptException;
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
    61
import jdk.internal.jline.console.history.History;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
    62
import jdk.internal.jline.console.history.MemoryHistory;
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
    63
import jdk.internal.jline.extra.EditingHistory;
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
    64
import jdk.internal.jline.internal.NonBlockingInputStream;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    65
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
    66
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
    67
import jdk.internal.misc.Signal.Handler;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    68
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    69
class ConsoleIOContext extends IOContext {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    70
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
    71
    private static final String HISTORY_LINE_PREFIX = "HISTORY_LINE_";
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
    72
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    73
    final JShellTool repl;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    74
    final StopDetectingInputStream input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    75
    final ConsoleReader in;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    76
    final EditingHistory history;
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
    77
    final MemoryHistory userInputHistory = new MemoryHistory();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    78
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    79
    String prefix = "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    80
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    81
    ConsoleIOContext(JShellTool repl, InputStream cmdin, PrintStream cmdout) throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    82
        this.repl = repl;
44188
3f2047e62102 8176412: jshell tool: automatic imports are excluded on /reload causing it to fail
rfield
parents: 43773
diff changeset
    83
        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
    84
        Terminal term;
36501
93915076a341 8151570: jtreg tests leave tty in bad state
jlahoda
parents: 36160
diff changeset
    85
        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
    86
            term = new TestTerminal(input);
36501
93915076a341 8151570: jtreg tests leave tty in bad state
jlahoda
parents: 36160
diff changeset
    87
        } 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
    88
            term = new JShellWindowsTerminal(input);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    89
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    90
            term = new JShellUnixTerminal(input);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    91
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    92
        term.init();
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
    93
        List<CompletionTask> completionTODO = new ArrayList<>();
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
    94
        in = new ConsoleReader(cmdin, cmdout, term) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
    95
            @Override public KeyMap getKeys() {
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
    96
                return new CheckCompletionKeyMap(super.getKeys(), completionTODO);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
    97
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
    98
            @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
    99
            protected boolean complete() throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   100
                return ConsoleIOContext.this.complete(completionTODO);
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   101
            }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   102
        };
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   103
        in.setExpandEvents(false);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   104
        in.setHandleUserInterrupt(true);
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   105
        List<String> persistenHistory = Stream.of(repl.prefs.keys())
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   106
                                              .filter(key -> key.startsWith(HISTORY_LINE_PREFIX))
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   107
                                              .sorted()
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   108
                                              .map(key -> repl.prefs.get(key))
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   109
                                              .collect(Collectors.toList());
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   110
        in.setHistory(history = new EditingHistory(in, persistenHistory) {
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   111
            @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
   112
                return repl.analysis.analyzeCompletion(input.toString()).completeness().isComplete();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   113
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   114
        });
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   115
        in.setBellEnabled(true);
38541
44e95493fd13 8131017: jshell tool: pasting code with tabs invokes tab completion
jlahoda
parents: 38521
diff changeset
   116
        in.setCopyPasteDetection(true);
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   117
        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
   118
        try {
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   119
            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
   120
                @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
   121
                    try {
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   122
                        in.getTerminal().reset();
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   123
                        in.redrawLine();
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   124
                        in.flush();
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   125
                    } catch (Exception ex) {
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   126
                        ex.printStackTrace();
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   127
                    }
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   128
                }
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   129
            });
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   130
        } catch (IllegalArgumentException ignored) {
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   131
            //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
   132
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   133
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   134
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   135
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   136
    public String readLine(String prompt, String prefix) throws IOException, InputInterruptedException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   137
        this.prefix = prefix;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   138
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   139
            return in.readLine(prompt);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   140
        } catch (UserInterruptException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   141
            throw (InputInterruptedException) new InputInterruptedException().initCause(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   142
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   143
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   144
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   145
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   146
    public boolean interactiveOutput() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   147
        return true;
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
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   150
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   151
    public Iterable<String> currentSessionHistory() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   152
        return history.currentSessionEntries();
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
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   155
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   156
    public void close() throws IOException {
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   157
        //save history:
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   158
        for (String key : repl.prefs.keys()) {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   159
            if (key.startsWith(HISTORY_LINE_PREFIX)) {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   160
                repl.prefs.remove(key);
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   161
            }
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   162
        }
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   163
        Collection<? extends String> savedHistory = history.save();
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   164
        if (!savedHistory.isEmpty()) {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   165
            int len = (int) Math.ceil(Math.log10(savedHistory.size()+1));
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   166
            String format = HISTORY_LINE_PREFIX + "%0" + len + "d";
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   167
            int index = 0;
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   168
            for (String historyLine : savedHistory) {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   169
                repl.prefs.put(String.format(format, index++), historyLine);
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   170
            }
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   171
        }
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   172
        repl.prefs.flush();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   173
        in.shutdown();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   174
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   175
            in.getTerminal().restore();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   176
        } catch (Exception ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   177
            throw new IOException(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   178
        }
38909
80e42e2d475b 8158174: jshell tool: leaks threads waiting on StopDetectingInputStream
jlahoda
parents: 38908
diff changeset
   179
        input.shutdown();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   180
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   181
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   182
    private void bind(String shortcut, Object action) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   183
        KeyMap km = in.getKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   184
        for (int i = 0; i < shortcut.length(); i++) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   185
            Object value = km.getBound(Character.toString(shortcut.charAt(i)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   186
            if (value instanceof KeyMap) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   187
                km = (KeyMap) value;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   188
            } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   189
                km.bind(shortcut.substring(i), action);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   190
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   191
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   192
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   193
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   194
    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
   195
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   196
    private static final String LINE_SEPARATOR = System.getProperty("line.separator");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   197
    private static final String LINE_SEPARATORS2 = LINE_SEPARATOR + LINE_SEPARATOR;
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   198
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   199
    @SuppressWarnings("fallthrough")
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   200
    private boolean complete(List<CompletionTask> todo) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   201
        //The completion has multiple states (invoked by subsequent presses of <tab>).
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   202
        //On the first invocation in a given sequence, all steps are precomputed
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   203
        //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
   204
        //and subsequent <tab> presses:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   205
        try {
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   206
            String text = in.getCursorBuffer().toString();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   207
            int cursor = in.getCursorBuffer().cursor;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   208
            if (todo.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   209
                int[] anchor = new int[] {-1};
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   210
                List<Suggestion> suggestions;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   211
                List<String> doc;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   212
                boolean command = prefix.isEmpty() && text.trim().startsWith("/");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   213
                if (command) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   214
                    suggestions = repl.commandCompletionSuggestions(text, cursor, anchor);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   215
                    doc = repl.commandDocumentation(text, cursor, true);
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   216
                } else {
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   217
                    int prefixLength = prefix.length();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   218
                    suggestions = repl.analysis.completionSuggestions(prefix + text, cursor + prefixLength, anchor);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   219
                    anchor[0] -= prefixLength;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   220
                    doc = repl.analysis.documentation(prefix + text, cursor + prefix.length(), false)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   221
                                       .stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   222
                                       .map(Documentation::signature)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   223
                                       .collect(Collectors.toList());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   224
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   225
                long smartCount = suggestions.stream().filter(Suggestion::matchesType).count();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   226
                boolean hasSmart = smartCount > 0 && smartCount <= in.getAutoprintThreshold();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   227
                boolean hasBoth = hasSmart &&
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   228
                                  suggestions.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   229
                                             .map(s -> s.matchesType())
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   230
                                             .distinct()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   231
                                             .count() == 2;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   232
                boolean tooManyItems = suggestions.size() > in.getAutoprintThreshold();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   233
                CompletionTask ordinaryCompletion = new OrdinaryCompletionTask(suggestions, anchor[0], !command && !doc.isEmpty(), hasSmart);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   234
                CompletionTask allCompletion = new AllSuggestionsCompletionTask(suggestions, anchor[0]);
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   235
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   236
                //the main decission tree:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   237
                if (command) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   238
                    CompletionTask shortDocumentation = new CommandSynopsisTask(doc);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   239
                    CompletionTask fullDocumentation = new CommandFullDocumentationTask(todo);
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   240
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   241
                    if (!doc.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   242
                        if (tooManyItems) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   243
                            todo.add(new NoopCompletionTask());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   244
                            todo.add(allCompletion);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   245
                        } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   246
                            todo.add(ordinaryCompletion);
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   247
                        }
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   248
                        todo.add(shortDocumentation);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   249
                        todo.add(fullDocumentation);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   250
                    } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   251
                        todo.add(new NoSuchCommandCompletionTask());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   252
                    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   253
                } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   254
                    if (doc.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   255
                        if (hasSmart) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   256
                            todo.add(ordinaryCompletion);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   257
                        } else if (tooManyItems) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   258
                            todo.add(new NoopCompletionTask());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   259
                        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   260
                        if (!hasSmart || hasBoth) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   261
                            todo.add(allCompletion);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   262
                        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   263
                    } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   264
                        CompletionTask shortDocumentation = new ExpressionSignaturesTask(doc);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   265
                        CompletionTask fullDocumentation = new ExpressionJavadocTask(todo);
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   266
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   267
                        if (hasSmart) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   268
                            todo.add(ordinaryCompletion);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   269
                        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   270
                        todo.add(shortDocumentation);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   271
                        if (!hasSmart || hasBoth) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   272
                            todo.add(allCompletion);
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
                        if (tooManyItems) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   275
                            todo.add(todo.size() - 1, fullDocumentation);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   276
                        } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   277
                            todo.add(fullDocumentation);
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   278
                        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   279
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   280
                }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   281
            }
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   282
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   283
            boolean success = false;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   284
            boolean repaint = true;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   285
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   286
            OUTER: while (!todo.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   287
                CompletionTask.Result result = todo.remove(0).perform(text, cursor);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   288
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   289
                switch (result) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   290
                    case CONTINUE:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   291
                        break;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   292
                    case SKIP_NOREPAINT:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   293
                        repaint = false;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   294
                    case SKIP:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   295
                        todo.clear();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   296
                        //intentional fall-through
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   297
                    case FINISH:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   298
                        success = true;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   299
                        //intentional fall-through
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   300
                    case NO_DATA:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   301
                        if (!todo.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   302
                            in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   303
                            in.println(todo.get(0).description());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   304
                        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   305
                        break OUTER;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   306
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   307
            }
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
            if (repaint) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   310
                in.redrawLine();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   311
                in.flush();
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
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   314
            return success;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   315
        } catch (IOException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   316
            throw new IllegalStateException(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   317
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   318
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   319
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   320
    private CompletionTask.Result doPrintFullDocumentation(List<CompletionTask> todo, List<String> doc, boolean command) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   321
        if (doc != null && !doc.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   322
            Terminal term = in.getTerminal();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   323
            int pageHeight = term.getHeight() - NEEDED_LINES;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   324
            List<CompletionTask> thisTODO = new ArrayList<>();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   325
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   326
            for (Iterator<String> docIt = doc.iterator(); docIt.hasNext(); ) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   327
                String currentDoc = docIt.next();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   328
                String[] lines = currentDoc.split("\n");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   329
                int firstLine = 0;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   330
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   331
                while (firstLine < lines.length) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   332
                    boolean first = firstLine == 0;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   333
                    String[] thisPageLines =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   334
                            Arrays.copyOfRange(lines,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   335
                                               firstLine,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   336
                                               Math.min(firstLine + pageHeight, lines.length));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   337
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   338
                    thisTODO.add(new CompletionTask() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   339
                        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   340
                        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   341
                            String key =  !first ? "jshell.console.see.next.page"
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   342
                                                 : command ? "jshell.console.see.next.command.doc"
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   343
                                                           : "jshell.console.see.next.javadoc";
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   344
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   345
                            return repl.getResourceString(key);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   346
                        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   347
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   348
                        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   349
                        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   350
                            in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   351
                            for (String line : thisPageLines) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   352
                                in.println(line);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   353
                            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   354
                            return Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   355
                        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   356
                    });
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   357
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   358
                    firstLine += pageHeight;
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
            todo.addAll(0, thisTODO);
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
            return CompletionTask.Result.CONTINUE;
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
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   367
        return CompletionTask.Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   368
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   369
    //where:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   370
        private static final int NEEDED_LINES = 4;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   371
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   372
    private static String commonPrefix(String str1, String str2) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   373
        for (int i = 0; i < str2.length(); i++) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   374
            if (!str1.startsWith(str2.substring(0, i + 1))) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   375
                return str2.substring(0, i);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   376
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   377
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   378
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   379
        return str2;
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
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   382
    private interface CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   383
        public String description();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   384
        public Result perform(String text, int cursor) throws IOException;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   385
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   386
        enum Result {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   387
            NO_DATA,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   388
            CONTINUE,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   389
            FINISH,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   390
            SKIP,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   391
            SKIP_NOREPAINT;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   392
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   393
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   394
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   395
    private final class NoopCompletionTask implements CompletionTask {
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
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   398
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   399
            throw new UnsupportedOperationException("Should not get here.");
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
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   402
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   403
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   404
            return Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   405
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   406
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   407
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   408
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   409
    private final class NoSuchCommandCompletionTask implements CompletionTask {
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
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   412
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   413
            throw new UnsupportedOperationException("Should not get here.");
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
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   416
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   417
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   418
            in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   419
            in.println(repl.getResourceString("jshell.console.no.such.command"));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   420
            in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   421
            return Result.SKIP;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   422
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   423
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   424
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   425
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   426
    private final class OrdinaryCompletionTask implements CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   427
        private final List<Suggestion> suggestions;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   428
        private final int anchor;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   429
        private final boolean cont;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   430
        private final boolean smart;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   431
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   432
        public OrdinaryCompletionTask(List<Suggestion> suggestions,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   433
                                      int anchor,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   434
                                      boolean cont,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   435
                                      boolean smart) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   436
            this.suggestions = suggestions;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   437
            this.anchor = anchor;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   438
            this.cont = cont;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   439
            this.smart = smart;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   440
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   441
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   442
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   443
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   444
            throw new UnsupportedOperationException("Should not get here.");
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
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   447
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   448
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   449
            List<CharSequence> toShow;
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
            if (smart) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   452
                toShow =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   453
                    suggestions.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   454
                               .filter(Suggestion::matchesType)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   455
                               .map(Suggestion::continuation)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   456
                               .distinct()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   457
                               .collect(Collectors.toList());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   458
            } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   459
                toShow =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   460
                    suggestions.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   461
                               .map(Suggestion::continuation)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   462
                               .distinct()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   463
                               .collect(Collectors.toList());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   464
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   465
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   466
            if (toShow.isEmpty()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   467
                return Result.CONTINUE;
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
            Optional<String> prefix =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   471
                    suggestions.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   472
                               .map(Suggestion::continuation)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   473
                               .reduce(ConsoleIOContext::commonPrefix);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   474
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   475
            String prefixStr = prefix.orElse("").substring(cursor - anchor);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   476
            in.putString(prefixStr);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   477
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   478
            boolean showItems = toShow.size() > 1 || smart;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   479
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   480
            if (showItems) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   481
                in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   482
                in.printColumns(toShow);
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
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   485
            if (!prefixStr.isEmpty())
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   486
                return showItems ? Result.SKIP : Result.SKIP_NOREPAINT;
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
            return cont ? Result.CONTINUE : Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   489
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   490
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
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   493
    private final class AllSuggestionsCompletionTask implements CompletionTask {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   494
        private final List<Suggestion> suggestions;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   495
        private final int anchor;
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
        public AllSuggestionsCompletionTask(List<Suggestion> suggestions,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   498
                                            int anchor) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   499
            this.suggestions = suggestions;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   500
            this.anchor = anchor;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   501
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   502
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   503
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   504
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   505
            if (suggestions.size() <= in.getAutoprintThreshold()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   506
                return repl.getResourceString("jshell.console.completion.all.completions");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   507
            } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   508
                return repl.messageFormat("jshell.console.completion.all.completions.number", suggestions.size());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   509
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   510
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   511
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   512
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   513
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   514
            List<String> candidates =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   515
                    suggestions.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   516
                               .map(Suggestion::continuation)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   517
                               .distinct()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   518
                               .collect(Collectors.toList());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   519
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   520
            Optional<String> prefix =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   521
                    candidates.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   522
                              .reduce(ConsoleIOContext::commonPrefix);
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
            String prefixStr = prefix.map(str -> str.substring(cursor - anchor)).orElse("");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   525
            in.putString(prefixStr);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   526
            if (candidates.size() > 1) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   527
                in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   528
                in.printColumns(candidates);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   529
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   530
            return suggestions.isEmpty() ? Result.NO_DATA : Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   531
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   532
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
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   535
    private final class CommandSynopsisTask implements CompletionTask {
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
        private final List<String> synopsis;
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
        public CommandSynopsisTask(List<String> synposis) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   540
            this.synopsis = synposis;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   541
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   542
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   543
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   544
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   545
            return repl.getResourceString("jshell.console.see.synopsis");
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
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   548
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   549
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   550
            try {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   551
                in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   552
                in.println(synopsis.stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   553
                                   .map(l -> l.replaceAll("\n", LINE_SEPARATOR))
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   554
                                   .collect(Collectors.joining(LINE_SEPARATORS2)));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   555
            } catch (IOException ex) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   556
                throw new IllegalStateException(ex);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   557
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   558
            return Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   559
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   560
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
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   563
    private final class CommandFullDocumentationTask implements CompletionTask {
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
        private final List<CompletionTask> todo;
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
        public CommandFullDocumentationTask(List<CompletionTask> todo) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   568
            this.todo = todo;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   569
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   570
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   571
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   572
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   573
            return repl.getResourceString("jshell.console.see.full.documentation");
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
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   576
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   577
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   578
            List<String> fullDoc = repl.commandDocumentation(text, cursor, false);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   579
            return doPrintFullDocumentation(todo, fullDoc, true);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   580
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   581
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   582
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   583
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   584
    private final class ExpressionSignaturesTask implements CompletionTask {
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
        private final List<String> doc;
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
        public ExpressionSignaturesTask(List<String> doc) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   589
            this.doc = doc;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   590
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   591
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   592
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   593
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   594
            throw new UnsupportedOperationException("Should not get here.");
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
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   597
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   598
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   599
            in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   600
            in.println(repl.getResourceString("jshell.console.completion.current.signatures"));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   601
            in.println(doc.stream().collect(Collectors.joining(LINE_SEPARATOR)));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   602
            return Result.FINISH;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   603
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   604
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   605
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   606
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   607
    private final class ExpressionJavadocTask implements CompletionTask {
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
        private final List<CompletionTask> todo;
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
        public ExpressionJavadocTask(List<CompletionTask> todo) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   612
            this.todo = todo;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   613
        }
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
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   616
        public String description() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   617
            return repl.getResourceString("jshell.console.see.documentation");
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
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   620
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   621
        public Result perform(String text, int cursor) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   622
            //schedule showing javadoc:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   623
            Terminal term = in.getTerminal();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   624
            JavadocFormatter formatter = new JavadocFormatter(term.getWidth(),
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   625
                                                              term.isAnsiSupported());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   626
            Function<Documentation, String> convertor = d -> formatter.formatJavadoc(d.signature(), d.javadoc()) +
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   627
                             (d.javadoc() == null ? repl.messageFormat("jshell.console.no.javadoc")
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   628
                                                  : "");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   629
            List<String> doc = repl.analysis.documentation(prefix + text, cursor + prefix.length(), true)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   630
                                            .stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   631
                                            .map(convertor)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   632
                                            .collect(Collectors.toList());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   633
            return doPrintFullDocumentation(todo, doc, false);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   634
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   635
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   636
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   637
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   638
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   639
    public boolean terminalEditorRunning() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   640
        Terminal terminal = in.getTerminal();
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   641
        if (terminal instanceof SuspendableTerminal)
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   642
            return ((SuspendableTerminal) terminal).isRaw();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   643
        return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   644
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   645
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   646
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   647
    public void suspend() {
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   648
        Terminal terminal = in.getTerminal();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   649
        if (terminal instanceof SuspendableTerminal)
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   650
            ((SuspendableTerminal) terminal).suspend();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   651
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   652
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   653
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   654
    public void resume() {
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   655
        Terminal terminal = in.getTerminal();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   656
        if (terminal instanceof SuspendableTerminal)
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   657
            ((SuspendableTerminal) terminal).resume();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   658
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   659
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   660
    @Override
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   661
    public void beforeUserCode() {
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   662
        synchronized (this) {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   663
            inputBytes = null;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   664
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   665
        input.setState(State.BUFFER);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   666
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   667
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   668
    @Override
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   669
    public void afterUserCode() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   670
        input.setState(State.WAIT);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   671
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   672
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   673
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   674
    public void replaceLastHistoryEntry(String source) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   675
        history.fullHistoryReplace(source);
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
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   678
    private static final long ESCAPE_TIMEOUT = 100;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   679
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   680
    private void fixes() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   681
        try {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   682
            int c = in.readCharacter();
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
            if (c == (-1)) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   685
                return ;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   686
            }
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
            for (FixComputer computer : FIX_COMPUTERS) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   689
                if (computer.shortcut == c) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   690
                    fixes(computer);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   691
                    return ;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   692
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   693
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   694
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   695
            readOutRemainingEscape(c);
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
            in.beep();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   698
            in.println();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   699
            in.println(repl.getResourceString("jshell.fix.wrong.shortcut"));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   700
            in.redrawLine();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   701
            in.flush();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   702
        } catch (IOException ex) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   703
            ex.printStackTrace();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   704
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   705
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   706
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   707
    private void readOutRemainingEscape(int c) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   708
        if (c == '\033') {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   709
            //escape, consume waiting input:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   710
            InputStream inp = in.getInput();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   711
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   712
            if (inp instanceof NonBlockingInputStream) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   713
                NonBlockingInputStream nbis = (NonBlockingInputStream) inp;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   714
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   715
                while (nbis.isNonBlockingEnabled() && nbis.peek(ESCAPE_TIMEOUT) > 0) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   716
                    in.readCharacter();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   717
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   718
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   719
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   720
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   721
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   722
    //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
   723
    //and perform the selected one:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   724
    private void fixes(FixComputer computer) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   725
        String input = prefix + in.getCursorBuffer().toString();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   726
        int cursor = prefix.length() + in.getCursorBuffer().cursor;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   727
        FixResult candidates = computer.compute(repl, input, cursor);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   728
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   729
        try {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   730
            final boolean printError = candidates.error != null && !candidates.error.isEmpty();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   731
            if (printError) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   732
                in.println(candidates.error);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   733
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   734
            if (candidates.fixes.isEmpty()) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   735
                in.beep();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   736
                if (printError) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   737
                    in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   738
                    in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   739
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   740
            } else if (candidates.fixes.size() == 1 && !computer.showMenu) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   741
                if (printError) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   742
                    in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   743
                    in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   744
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   745
                candidates.fixes.get(0).perform(in);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   746
            } else {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   747
                List<Fix> fixes = new ArrayList<>(candidates.fixes);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   748
                fixes.add(0, new Fix() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   749
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   750
                    public String displayName() {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   751
                        return repl.messageFormat("jshell.console.do.nothing");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   752
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   753
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   754
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   755
                    public void perform(ConsoleReader in) throws IOException {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   756
                        in.redrawLine();
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
                });
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   759
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   760
                Map<Character, Fix> char2Fix = new HashMap<>();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   761
                in.println();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   762
                for (int i = 0; i < fixes.size(); i++) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   763
                    Fix fix = fixes.get(i);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   764
                    char2Fix.put((char) ('0' + i), fix);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   765
                    in.println("" + i + ": " + fixes.get(i).displayName());
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   766
                }
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   767
                in.print(repl.messageFormat("jshell.console.choice"));
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   768
                in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   769
                int read;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   770
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   771
                read = in.readCharacter();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   772
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   773
                Fix fix = char2Fix.get((char) 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
                if (fix == null) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   776
                    in.beep();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   777
                    fix = fixes.get(0);
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
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   780
                in.println();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   781
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   782
                fix.perform(in);
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.flush();
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
        } catch (IOException ex) {
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
   787
            throw new IllegalStateException(ex);
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   788
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   789
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   790
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   791
    private byte[] inputBytes;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   792
    private int inputBytesPointer;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   793
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   794
    @Override
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
   795
    public synchronized int readUserInput() throws IOException {
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   796
        while (inputBytes == null || inputBytes.length <= inputBytesPointer) {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   797
            boolean prevHandleUserInterrupt = in.getHandleUserInterrupt();
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   798
            History prevHistory = in.getHistory();
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   799
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   800
            try {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   801
                input.setState(State.WAIT);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   802
                in.setHandleUserInterrupt(true);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   803
                in.setHistory(userInputHistory);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   804
                inputBytes = (in.readLine("") + System.getProperty("line.separator")).getBytes();
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   805
                inputBytesPointer = 0;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   806
            } catch (UserInterruptException ex) {
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
   807
                throw new InterruptedIOException();
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   808
            } finally {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   809
                in.setHistory(prevHistory);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   810
                in.setHandleUserInterrupt(prevHandleUserInterrupt);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   811
                input.setState(State.BUFFER);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   812
            }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   813
        }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   814
        return inputBytes[inputBytesPointer++];
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   815
    }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   816
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   817
    /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   818
     * 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
   819
     */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   820
    public interface Fix {
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 name that should be shown to the user.
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 String displayName();
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
         * Perform the given action.
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 void perform(ConsoleReader in) throws IOException;
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
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
     * A factory for {@link Fix}es.
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
    public abstract static class FixComputer {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   835
        private final char shortcut;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   836
        private final boolean showMenu;
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
        /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   839
         * 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
   840
         * 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
   841
         * 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
   842
         */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   843
        public FixComputer(char shortcut, boolean showMenu) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   844
            this.shortcut = shortcut;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   845
            this.showMenu = showMenu;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   846
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   847
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   848
        /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   849
         * Compute possible actions for the given code.
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
        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
   852
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   853
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   854
    /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   855
     * 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
   856
     */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   857
    public static class FixResult {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   858
        public final List<Fix> fixes;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   859
        public final String error;
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 FixResult(List<Fix> fixes, String error) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   862
            this.fixes = fixes;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   863
            this.error = 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
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   866
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   867
    private static final FixComputer[] FIX_COMPUTERS = new FixComputer[] {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   868
        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
   869
            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
   870
                in.redrawLine();
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   871
                in.setCursorPosition(0);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   872
                in.putString(type + "  = ");
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   873
                in.setCursorPosition(in.getCursorBuffer().cursor - 3);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   874
                in.flush();
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   875
            }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   876
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   877
            @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   878
            public FixResult compute(JShellTool repl, String code, int cursor) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   879
                String type = repl.analysis.analyzeType(code, cursor);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   880
                if (type == null) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   881
                    return new FixResult(Collections.emptyList(), null);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   882
                }
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   883
                List<Fix> fixes = new ArrayList<>();
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   884
                fixes.add(new Fix() {
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   885
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   886
                    public String displayName() {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   887
                        return repl.messageFormat("jshell.console.create.variable");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   888
                    }
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   889
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   890
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   891
                    public void perform(ConsoleReader in) throws IOException {
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   892
                        performToVar(in, type);
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   893
                    }
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   894
                });
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   895
                int idx = type.lastIndexOf(".");
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   896
                if (idx > 0) {
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   897
                    String stype = type.substring(idx + 1);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   898
                    QualifiedNames res = repl.analysis.listQualifiedNames(stype, stype.length());
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   899
                    if (res.isUpToDate() && res.getNames().contains(type)
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   900
                            && !res.isResolvable()) {
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   901
                        fixes.add(new Fix() {
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   902
                            @Override
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   903
                            public String displayName() {
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   904
                                return "import: " + type + ". " +
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   905
                                        repl.messageFormat("jshell.console.create.variable");
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   906
                            }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   907
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   908
                            @Override
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   909
                            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
   910
                                repl.processCompleteSource("import " + type + ";");
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   911
                                in.println("Imported: " + type);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   912
                                performToVar(in, stype);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   913
                            }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   914
                        });
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   915
                    }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   916
                }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   917
                return new FixResult(fixes, null);
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   918
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   919
        },
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   920
        new FixComputer('i', true) { //compute "Add import" Fixes:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   921
            @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   922
            public FixResult compute(JShellTool repl, String code, int cursor) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   923
                QualifiedNames res = repl.analysis.listQualifiedNames(code, cursor);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   924
                List<Fix> fixes = new ArrayList<>();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   925
                for (String fqn : res.getNames()) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   926
                    fixes.add(new Fix() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   927
                        @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   928
                        public String displayName() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   929
                            return "import: " + fqn;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   930
                        }
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   931
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   932
                        @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   933
                        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
   934
                            repl.processCompleteSource("import " + fqn + ";");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   935
                            in.println("Imported: " + fqn);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   936
                            in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   937
                        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   938
                    });
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   939
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   940
                if (res.isResolvable()) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   941
                    return new FixResult(Collections.emptyList(),
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   942
                            repl.messageFormat("jshell.console.resolvable"));
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   943
                } else {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   944
                    String error = "";
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   945
                    if (fixes.isEmpty()) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   946
                        error = repl.messageFormat("jshell.console.no.candidate");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   947
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   948
                    if (!res.isUpToDate()) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   949
                        error += repl.messageFormat("jshell.console.incomplete");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   950
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   951
                    return new FixResult(fixes, error);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   952
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   953
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   954
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   955
    };
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   956
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   957
    private static final class JShellUnixTerminal extends NoInterruptUnixTerminal implements SuspendableTerminal {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   958
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   959
        private final StopDetectingInputStream input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   960
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   961
        public JShellUnixTerminal(StopDetectingInputStream input) throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   962
            this.input = input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   963
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   964
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   965
        public boolean isRaw() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   966
            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   967
                return getSettings().get("-a").contains("-icanon");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   968
            } catch (IOException | InterruptedException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   969
                return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   970
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   971
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   972
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   973
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   974
        public InputStream wrapInIfNeeded(InputStream in) throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   975
            return input.setInputStream(super.wrapInIfNeeded(in));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   976
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   977
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   978
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   979
        public void disableInterruptCharacter() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   980
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   981
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   982
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   983
        public void enableInterruptCharacter() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   984
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   985
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   986
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   987
        public void suspend() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   988
            try {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   989
                getSettings().restore();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   990
                super.disableInterruptCharacter();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   991
            } catch (Exception ex) {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   992
                throw new IllegalStateException(ex);
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   993
            }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   994
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   995
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   996
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   997
        public void resume() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   998
            try {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   999
                init();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1000
            } catch (Exception ex) {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1001
                throw new IllegalStateException(ex);
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1002
            }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1003
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1004
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1005
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1006
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1007
    private static final class JShellWindowsTerminal extends WindowsTerminal implements SuspendableTerminal {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1008
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1009
        private final StopDetectingInputStream input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1010
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1011
        public JShellWindowsTerminal(StopDetectingInputStream input) throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1012
            this.input = input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1013
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1014
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1015
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1016
        public void init() throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1017
            super.init();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1018
            setAnsiSupported(false);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1019
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1020
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1021
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1022
        public InputStream wrapInIfNeeded(InputStream in) throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1023
            return input.setInputStream(super.wrapInIfNeeded(in));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1024
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1025
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1026
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1027
        public void suspend() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1028
            try {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1029
                restore();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1030
                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
  1031
            } catch (Exception ex) {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1032
                throw new IllegalStateException(ex);
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1033
            }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1034
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1035
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1036
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1037
        public void resume() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1038
            try {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1039
                restore();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1040
                init();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1041
            } catch (Exception ex) {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1042
                throw new IllegalStateException(ex);
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1043
            }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1044
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1045
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1046
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1047
        public boolean isRaw() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1048
            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
  1049
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1050
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1051
    }
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1052
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1053
    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
  1054
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1055
        private final StopDetectingInputStream input;
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1056
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1057
        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
  1058
            super(true);
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1059
            setAnsiSupported(false);
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
  1060
            setEchoEnabled(false);
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1061
            this.input = input;
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1062
        }
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1063
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1064
        @Override
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1065
        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
  1066
            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
  1067
        }
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1068
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
  1069
    }
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1070
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1071
    private interface SuspendableTerminal {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1072
        public void suspend();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1073
        public void resume();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1074
        public boolean isRaw();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1075
    }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
  1076
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1077
    private static final class CheckCompletionKeyMap extends KeyMap {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1078
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1079
        private final KeyMap del;
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
  1080
        private final List<CompletionTask> completionTODO;
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1081
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
  1082
        public CheckCompletionKeyMap(KeyMap del, List<CompletionTask> completionTODO) {
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1083
            super(del.getName(), del.isViKeyMap());
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1084
            this.del = del;
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
  1085
            this.completionTODO = completionTODO;
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1086
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1087
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1088
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1089
        public void bind(CharSequence keySeq, Object function) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1090
            del.bind(keySeq, function);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1091
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1092
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1093
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1094
        public void bindIfNotBound(CharSequence keySeq, Object function) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1095
            del.bindIfNotBound(keySeq, function);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1096
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1097
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1098
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1099
        public void from(KeyMap other) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1100
            del.from(other);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1101
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1102
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1103
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1104
        public Object getAnotherKey() {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1105
            return del.getAnotherKey();
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1106
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1107
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1108
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1109
        public Object getBound(CharSequence keySeq) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1110
            Object res = del.getBound(keySeq);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1111
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1112
            if (res != Operation.COMPLETE) {
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
  1113
                completionTODO.clear();
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1114
            }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1115
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1116
            return res;
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1117
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1118
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1119
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1120
        public void setBlinkMatchingParen(boolean on) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1121
            del.setBlinkMatchingParen(on);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1122
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1123
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1124
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1125
        public String toString() {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1126
            return "check: " + del.toString();
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1127
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
  1128
    }
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44188
diff changeset
  1129
    }