langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java
author jlahoda
Mon, 13 Feb 2017 11:57:56 +0100
changeset 43773 8d8593871575
parent 43583 d16e490ec827
child 44188 3f2047e62102
permissions -rw-r--r--
8174245: Javadoc is not working for some methods Summary: Parsing source file as if they were part of their corresponding modules. Reviewed-by: rfield
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     1
/*
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
     2
 * Copyright (c) 2015, 2016, 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;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    36
import java.io.UncheckedIOException;
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
    37
import java.util.ArrayList;
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
    38
import java.util.Arrays;
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
    39
import java.util.Collection;
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
    40
import java.util.Collections;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
    41
import java.util.HashMap;
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
    42
import java.util.Iterator;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    43
import java.util.List;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    44
import java.util.Locale;
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
    45
import java.util.Map;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    46
import java.util.Objects;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    47
import java.util.Optional;
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
    48
import java.util.concurrent.atomic.AtomicBoolean;
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
    49
import java.util.function.Function;
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
    50
import java.util.stream.Collectors;
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
    51
import java.util.stream.Stream;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    52
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
    53
import jdk.internal.shellsupport.doc.JavadocFormatter;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    54
import jdk.internal.jline.NoInterruptUnixTerminal;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    55
import jdk.internal.jline.Terminal;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    56
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
    57
import jdk.internal.jline.TerminalSupport;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    58
import jdk.internal.jline.WindowsTerminal;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    59
import jdk.internal.jline.console.ConsoleReader;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    60
import jdk.internal.jline.console.KeyMap;
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
    61
import jdk.internal.jline.console.Operation;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    62
import jdk.internal.jline.console.UserInterruptException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    63
import jdk.internal.jline.console.completer.Completer;
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
    64
import jdk.internal.jline.console.history.History;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
    65
import jdk.internal.jline.console.history.MemoryHistory;
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
    66
import jdk.internal.jline.extra.EditingHistory;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    67
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
    68
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
    69
import jdk.internal.misc.Signal.Handler;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    70
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    71
class ConsoleIOContext extends IOContext {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    72
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
    73
    private static final String HISTORY_LINE_PREFIX = "HISTORY_LINE_";
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
    74
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    75
    final JShellTool repl;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    76
    final StopDetectingInputStream input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    77
    final ConsoleReader in;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    78
    final EditingHistory history;
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
    79
    final MemoryHistory userInputHistory = new MemoryHistory();
33362
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
    String prefix = "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    82
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    83
    ConsoleIOContext(JShellTool repl, InputStream cmdin, PrintStream cmdout) throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    84
        this.repl = repl;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    85
        this.input = new StopDetectingInputStream(() -> repl.state.stop(), ex -> repl.hard("Error on input: %s", ex));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    86
        Terminal term;
36501
93915076a341 8151570: jtreg tests leave tty in bad state
jlahoda
parents: 36160
diff changeset
    87
        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
    88
            term = new TestTerminal(input);
36501
93915076a341 8151570: jtreg tests leave tty in bad state
jlahoda
parents: 36160
diff changeset
    89
        } 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
    90
            term = new JShellWindowsTerminal(input);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    91
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    92
            term = new JShellUnixTerminal(input);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    93
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    94
        term.init();
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
    95
        AtomicBoolean allowSmart = new AtomicBoolean();
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
    96
        in = new ConsoleReader(cmdin, cmdout, term) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
    97
            @Override public KeyMap getKeys() {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
    98
                return new CheckCompletionKeyMap(super.getKeys(), allowSmart);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
    99
            }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   100
        };
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   101
        in.setExpandEvents(false);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   102
        in.setHandleUserInterrupt(true);
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   103
        List<String> persistenHistory = Stream.of(repl.prefs.keys())
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   104
                                              .filter(key -> key.startsWith(HISTORY_LINE_PREFIX))
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   105
                                              .sorted()
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   106
                                              .map(key -> repl.prefs.get(key))
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   107
                                              .collect(Collectors.toList());
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   108
        in.setHistory(history = new EditingHistory(in, persistenHistory) {
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   109
            @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
   110
                return repl.analysis.analyzeCompletion(input.toString()).completeness().isComplete();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   111
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   112
        });
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   113
        in.setBellEnabled(true);
38541
44e95493fd13 8131017: jshell tool: pasting code with tabs invokes tab completion
jlahoda
parents: 38521
diff changeset
   114
        in.setCopyPasteDetection(true);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   115
        in.addCompleter(new Completer() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   116
            @Override public int complete(String test, int cursor, List<CharSequence> result) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   117
                int[] anchor = new int[] {-1};
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   118
                List<Suggestion> suggestions;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   119
                if (prefix.isEmpty() && test.trim().startsWith("/")) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   120
                    suggestions = repl.commandCompletionSuggestions(test, cursor, anchor);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   121
                } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   122
                    int prefixLength = prefix.length();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   123
                    suggestions = repl.analysis.completionSuggestions(prefix + test, cursor + prefixLength, anchor);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   124
                    anchor[0] -= prefixLength;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   125
                }
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   126
                boolean smart = allowSmart.get() &&
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   127
                                suggestions.stream()
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42258
diff changeset
   128
                                           .anyMatch(Suggestion::matchesType);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   129
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   130
                allowSmart.set(!allowSmart.get());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   131
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   132
                suggestions.stream()
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38541
diff changeset
   133
                           .filter(s -> !smart || s.matchesType())
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42258
diff changeset
   134
                           .map(Suggestion::continuation)
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   135
                           .forEach(result::add);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   136
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   137
                boolean onlySmart = suggestions.stream()
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42258
diff changeset
   138
                                               .allMatch(Suggestion::matchesType);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   139
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   140
                if (smart && !onlySmart) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   141
                    Optional<String> prefix =
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   142
                            suggestions.stream()
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42258
diff changeset
   143
                                       .map(Suggestion::continuation)
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   144
                                       .reduce(ConsoleIOContext::commonPrefix);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   145
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   146
                    String prefixStr = prefix.orElse("").substring(cursor - anchor[0]);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   147
                    try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   148
                        in.putString(prefixStr);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   149
                        cursor += prefixStr.length();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   150
                    } catch (IOException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   151
                        throw new IllegalStateException(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   152
                    }
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   153
                    result.add(repl.messageFormat("jshell.console.see.more"));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   154
                    return cursor; //anchor should not be used.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   155
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   156
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   157
                if (result.isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   158
                    try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   159
                        //provide "empty completion" feedback
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   160
                        //XXX: this only works correctly when there is only one Completer:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   161
                        in.beep();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   162
                    } catch (IOException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   163
                        throw new UncheckedIOException(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   164
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   165
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   166
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   167
                return anchor[0];
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   168
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   169
        });
41934
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
   170
        bind(DOCUMENTATION_SHORTCUT, (Runnable) () -> documentation(repl));
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   171
        for (FixComputer computer : FIX_COMPUTERS) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   172
            for (String shortcuts : SHORTCUT_FIXES) {
41934
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
   173
                bind(shortcuts + computer.shortcut, (Runnable) () -> fixes(computer));
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   174
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   175
        }
41631
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   176
        try {
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   177
            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
   178
                @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
   179
                    try {
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   180
                        in.getTerminal().reset();
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   181
                        in.redrawLine();
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   182
                        in.flush();
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   183
                    } catch (Exception ex) {
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   184
                        ex.printStackTrace();
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   185
                    }
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   186
                }
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   187
            });
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   188
        } catch (IllegalArgumentException ignored) {
a348d1cc8d9d 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 41628
diff changeset
   189
            //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
   190
        }
33362
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
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   194
    public String readLine(String prompt, String prefix) throws IOException, InputInterruptedException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   195
        this.prefix = prefix;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   196
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   197
            return in.readLine(prompt);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   198
        } catch (UserInterruptException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   199
            throw (InputInterruptedException) new InputInterruptedException().initCause(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   200
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   201
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   202
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   203
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   204
    public boolean interactiveOutput() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   205
        return true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   206
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   207
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   208
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   209
    public Iterable<String> currentSessionHistory() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   210
        return history.currentSessionEntries();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   211
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   212
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   213
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   214
    public void close() throws IOException {
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   215
        //save history:
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   216
        for (String key : repl.prefs.keys()) {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   217
            if (key.startsWith(HISTORY_LINE_PREFIX)) {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   218
                repl.prefs.remove(key);
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   219
            }
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   220
        }
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   221
        Collection<? extends String> savedHistory = history.save();
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   222
        if (!savedHistory.isEmpty()) {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   223
            int len = (int) Math.ceil(Math.log10(savedHistory.size()+1));
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   224
            String format = HISTORY_LINE_PREFIX + "%0" + len + "d";
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   225
            int index = 0;
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   226
            for (String historyLine : savedHistory) {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   227
                repl.prefs.put(String.format(format, index++), historyLine);
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   228
            }
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 36990
diff changeset
   229
        }
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   230
        repl.prefs.flush();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   231
        in.shutdown();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   232
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   233
            in.getTerminal().restore();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   234
        } catch (Exception ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   235
            throw new IOException(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   236
        }
38909
80e42e2d475b 8158174: jshell tool: leaks threads waiting on StopDetectingInputStream
jlahoda
parents: 38908
diff changeset
   237
        input.shutdown();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   238
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   239
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   240
    private void bind(String shortcut, Object action) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   241
        KeyMap km = in.getKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   242
        for (int i = 0; i < shortcut.length(); i++) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   243
            Object value = km.getBound(Character.toString(shortcut.charAt(i)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   244
            if (value instanceof KeyMap) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   245
                km = (KeyMap) value;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   246
            } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   247
                km.bind(shortcut.substring(i), action);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   248
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   249
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   250
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   251
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   252
    private static final String DOCUMENTATION_SHORTCUT = "\033\133\132"; //Shift-TAB
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   253
    private static final String[] SHORTCUT_FIXES = {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   254
        "\033\015", //Alt-Enter (Linux)
39811
b8d3d2487379 8162255: 'Alt-Enter v'/'Alt-Enter i' not working on some terminals
jlahoda
parents: 38909
diff changeset
   255
        "\033\012", //Alt-Enter (Linux)
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   256
        "\033\133\061\067\176", //F6/Alt-F1 (Mac)
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   257
        "\u001BO3P" //Alt-F1 (Linux)
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   258
    };
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   259
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   260
    private String lastDocumentationBuffer;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   261
    private int lastDocumentationCursor = (-1);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   262
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   263
    private void documentation(JShellTool repl) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   264
        String buffer = in.getCursorBuffer().buffer.toString();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   265
        int cursor = in.getCursorBuffer().cursor;
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   266
        boolean firstInvocation = !buffer.equals(lastDocumentationBuffer) || cursor != lastDocumentationCursor;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   267
        lastDocumentationBuffer = buffer;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   268
        lastDocumentationCursor = cursor;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   269
        List<String> doc;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   270
        String seeMore;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   271
        Terminal term = in.getTerminal();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   272
        if (prefix.isEmpty() && buffer.trim().startsWith("/")) {
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   273
            doc = Arrays.asList(repl.commandDocumentation(buffer, cursor, firstInvocation));
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   274
            seeMore = "jshell.console.see.help";
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   275
        } else {
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   276
            JavadocFormatter formatter = new JavadocFormatter(term.getWidth(),
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   277
                                                              term.isAnsiSupported());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   278
            Function<Documentation, String> convertor;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   279
            if (firstInvocation) {
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42258
diff changeset
   280
                convertor = Documentation::signature;
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   281
            } else {
42258
a1aafd5ea6ec 8169561: jshell tool: double shift-tab on variable crashes tool
jlahoda
parents: 41996
diff changeset
   282
                convertor = d -> formatter.formatJavadoc(d.signature(), d.javadoc()) +
a1aafd5ea6ec 8169561: jshell tool: double shift-tab on variable crashes tool
jlahoda
parents: 41996
diff changeset
   283
                                 (d.javadoc() == null ? repl.messageFormat("jshell.console.no.javadoc")
a1aafd5ea6ec 8169561: jshell tool: double shift-tab on variable crashes tool
jlahoda
parents: 41996
diff changeset
   284
                                                      : "");
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   285
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   286
            doc = repl.analysis.documentation(prefix + buffer, cursor + prefix.length(), !firstInvocation)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   287
                               .stream()
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   288
                               .map(convertor)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   289
                               .collect(Collectors.toList());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   290
            seeMore = "jshell.console.see.javadoc";
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   291
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   292
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   293
        try {
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   294
            if (doc != null && !doc.isEmpty()) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   295
                if (firstInvocation) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   296
                    in.println();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   297
                    in.println(doc.stream().collect(Collectors.joining("\n")));
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   298
                    in.println(repl.messageFormat(seeMore));
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   299
                    in.redrawLine();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   300
                    in.flush();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   301
                } else {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   302
                    in.println();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   303
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   304
                    int height = term.getHeight();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   305
                    String lastNote = "";
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   306
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   307
                    PRINT_DOC: for (Iterator<String> docIt = doc.iterator(); docIt.hasNext(); ) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   308
                        String currentDoc = docIt.next();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   309
                        String[] lines = currentDoc.split("\n");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   310
                        int firstLine = 0;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   311
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   312
                        PRINT_PAGE: while (true) {
43773
8d8593871575 8174245: Javadoc is not working for some methods
jlahoda
parents: 43583
diff changeset
   313
                            in.print(lastNote.replaceAll(".", " ") + ConsoleReader.RESET_LINE);
8d8593871575 8174245: Javadoc is not working for some methods
jlahoda
parents: 43583
diff changeset
   314
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   315
                            int toPrint = height - 1;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   316
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   317
                            while (toPrint > 0 && firstLine < lines.length) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   318
                                in.println(lines[firstLine++]);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   319
                                toPrint--;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   320
                            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   321
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   322
                            if (firstLine >= lines.length) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   323
                                break;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   324
                            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   325
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   326
                            lastNote = repl.getResourceString("jshell.console.see.next.page");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   327
                            in.print(lastNote + ConsoleReader.RESET_LINE);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   328
                            in.flush();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   329
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   330
                            while (true) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   331
                                int r = in.readCharacter();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   332
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   333
                                switch (r) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   334
                                    case ' ': continue PRINT_PAGE;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   335
                                    case 'q':
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   336
                                    case 3:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   337
                                        break PRINT_DOC;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   338
                                    default:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   339
                                        in.beep();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   340
                                        break;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   341
                                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   342
                            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   343
                        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   344
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   345
                        if (docIt.hasNext()) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   346
                            lastNote = repl.getResourceString("jshell.console.see.next.javadoc");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   347
                            in.print(lastNote + ConsoleReader.RESET_LINE);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   348
                            in.flush();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   349
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   350
                            while (true) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   351
                                int r = in.readCharacter();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   352
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   353
                                switch (r) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   354
                                    case ' ': continue PRINT_DOC;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   355
                                    case 'q':
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   356
                                    case 3:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   357
                                        break PRINT_DOC;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   358
                                    default:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   359
                                        in.beep();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   360
                                        break;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   361
                                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   362
                            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   363
                        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   364
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   365
                    //clear the "press space" line:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   366
                    in.getCursorBuffer().buffer.replace(0, buffer.length(), lastNote);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   367
                    in.getCursorBuffer().cursor = 0;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   368
                    in.killLine();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   369
                    in.getCursorBuffer().buffer.append(buffer);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   370
                    in.getCursorBuffer().cursor = cursor;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   371
                    in.redrawLine();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   372
                    in.flush();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41631
diff changeset
   373
                }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   374
            } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   375
                in.beep();
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
        } catch (IOException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   378
            throw new IllegalStateException(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   379
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   380
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   381
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   382
    private static String commonPrefix(String str1, String str2) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   383
        for (int i = 0; i < str2.length(); i++) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   384
            if (!str1.startsWith(str2.substring(0, i + 1))) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   385
                return str2.substring(0, i);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   386
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   387
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   388
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   389
        return str2;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   390
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   391
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   392
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   393
    public boolean terminalEditorRunning() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   394
        Terminal terminal = in.getTerminal();
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   395
        if (terminal instanceof SuspendableTerminal)
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   396
            return ((SuspendableTerminal) terminal).isRaw();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   397
        return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   398
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   399
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   400
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   401
    public void suspend() {
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   402
        Terminal terminal = in.getTerminal();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   403
        if (terminal instanceof SuspendableTerminal)
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   404
            ((SuspendableTerminal) terminal).suspend();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   405
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   406
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   407
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   408
    public void resume() {
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   409
        Terminal terminal = in.getTerminal();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   410
        if (terminal instanceof SuspendableTerminal)
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   411
            ((SuspendableTerminal) terminal).resume();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   412
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   413
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   414
    @Override
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   415
    public void beforeUserCode() {
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   416
        synchronized (this) {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   417
            inputBytes = null;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   418
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   419
        input.setState(State.BUFFER);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   420
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   421
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   422
    @Override
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   423
    public void afterUserCode() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   424
        input.setState(State.WAIT);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   425
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   426
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   427
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   428
    public void replaceLastHistoryEntry(String source) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   429
        history.fullHistoryReplace(source);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   430
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   431
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   432
    //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
   433
    //and perform the selected one:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   434
    private void fixes(FixComputer computer) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   435
        String input = prefix + in.getCursorBuffer().toString();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   436
        int cursor = prefix.length() + in.getCursorBuffer().cursor;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   437
        FixResult candidates = computer.compute(repl, input, cursor);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   438
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   439
        try {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   440
            final boolean printError = candidates.error != null && !candidates.error.isEmpty();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   441
            if (printError) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   442
                in.println(candidates.error);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   443
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   444
            if (candidates.fixes.isEmpty()) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   445
                in.beep();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   446
                if (printError) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   447
                    in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   448
                    in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   449
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   450
            } else if (candidates.fixes.size() == 1 && !computer.showMenu) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   451
                if (printError) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   452
                    in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   453
                    in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   454
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   455
                candidates.fixes.get(0).perform(in);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   456
            } else {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   457
                List<Fix> fixes = new ArrayList<>(candidates.fixes);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   458
                fixes.add(0, new Fix() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   459
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   460
                    public String displayName() {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   461
                        return repl.messageFormat("jshell.console.do.nothing");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   462
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   463
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   464
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   465
                    public void perform(ConsoleReader in) throws IOException {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   466
                        in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   467
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   468
                });
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   469
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   470
                Map<Character, Fix> char2Fix = new HashMap<>();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   471
                in.println();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   472
                for (int i = 0; i < fixes.size(); i++) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   473
                    Fix fix = fixes.get(i);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   474
                    char2Fix.put((char) ('0' + i), fix);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   475
                    in.println("" + i + ": " + fixes.get(i).displayName());
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   476
                }
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   477
                in.print(repl.messageFormat("jshell.console.choice"));
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   478
                in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   479
                int read;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   480
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   481
                read = in.readCharacter();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   482
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   483
                Fix fix = char2Fix.get((char) read);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   484
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   485
                if (fix == null) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   486
                    in.beep();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   487
                    fix = fixes.get(0);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   488
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   489
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   490
                in.println();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   491
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   492
                fix.perform(in);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   493
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   494
                in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   495
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   496
        } catch (IOException ex) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   497
            ex.printStackTrace();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   498
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   499
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   500
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   501
    private byte[] inputBytes;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   502
    private int inputBytesPointer;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   503
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   504
    @Override
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
   505
    public synchronized int readUserInput() throws IOException {
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   506
        while (inputBytes == null || inputBytes.length <= inputBytesPointer) {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   507
            boolean prevHandleUserInterrupt = in.getHandleUserInterrupt();
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   508
            History prevHistory = in.getHistory();
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   509
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   510
            try {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   511
                input.setState(State.WAIT);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   512
                in.setHandleUserInterrupt(true);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   513
                in.setHistory(userInputHistory);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   514
                inputBytes = (in.readLine("") + System.getProperty("line.separator")).getBytes();
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   515
                inputBytesPointer = 0;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   516
            } catch (UserInterruptException ex) {
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
   517
                throw new InterruptedIOException();
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   518
            } finally {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   519
                in.setHistory(prevHistory);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   520
                in.setHandleUserInterrupt(prevHandleUserInterrupt);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   521
                input.setState(State.BUFFER);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   522
            }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   523
        }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   524
        return inputBytes[inputBytesPointer++];
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   525
    }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39811
diff changeset
   526
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   527
    /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   528
     * 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
   529
     */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   530
    public interface Fix {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   531
        /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   532
         * A name that should be shown to the user.
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   533
         */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   534
        public String displayName();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   535
        /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   536
         * Perform the given action.
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   537
         */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   538
        public void perform(ConsoleReader in) throws IOException;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   539
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   540
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   541
    /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   542
     * A factory for {@link Fix}es.
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   543
     */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   544
    public abstract static class FixComputer {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   545
        private final char shortcut;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   546
        private final boolean showMenu;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   547
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   548
        /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   549
         * 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
   550
         * 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
   551
         * 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
   552
         */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   553
        public FixComputer(char shortcut, boolean showMenu) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   554
            this.shortcut = shortcut;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   555
            this.showMenu = showMenu;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   556
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   557
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   558
        /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   559
         * Compute possible actions for the given code.
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   560
         */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   561
        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
   562
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   563
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   564
    /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   565
     * 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
   566
     */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   567
    public static class FixResult {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   568
        public final List<Fix> fixes;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   569
        public final String error;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   570
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   571
        public FixResult(List<Fix> fixes, String error) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   572
            this.fixes = fixes;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   573
            this.error = error;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   574
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   575
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   576
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   577
    private static final FixComputer[] FIX_COMPUTERS = new FixComputer[] {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   578
        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
   579
            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
   580
                in.redrawLine();
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   581
                in.setCursorPosition(0);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   582
                in.putString(type + "  = ");
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   583
                in.setCursorPosition(in.getCursorBuffer().cursor - 3);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   584
                in.flush();
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   585
            }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   586
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   587
            @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   588
            public FixResult compute(JShellTool repl, String code, int cursor) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   589
                String type = repl.analysis.analyzeType(code, cursor);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   590
                if (type == null) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   591
                    return new FixResult(Collections.emptyList(), null);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   592
                }
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   593
                List<Fix> fixes = new ArrayList<>();
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   594
                fixes.add(new Fix() {
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   595
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   596
                    public String displayName() {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   597
                        return repl.messageFormat("jshell.console.create.variable");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   598
                    }
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   599
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   600
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   601
                    public void perform(ConsoleReader in) throws IOException {
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   602
                        performToVar(in, type);
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   603
                    }
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   604
                });
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   605
                int idx = type.lastIndexOf(".");
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   606
                if (idx > 0) {
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   607
                    String stype = type.substring(idx + 1);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   608
                    QualifiedNames res = repl.analysis.listQualifiedNames(stype, stype.length());
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   609
                    if (res.isUpToDate() && res.getNames().contains(type)
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   610
                            && !res.isResolvable()) {
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   611
                        fixes.add(new Fix() {
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   612
                            @Override
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   613
                            public String displayName() {
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   614
                                return "import: " + type + ". " +
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   615
                                        repl.messageFormat("jshell.console.create.variable");
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   616
                            }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   617
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   618
                            @Override
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   619
                            public void perform(ConsoleReader in) throws IOException {
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   620
                                repl.state.eval("import " + type + ";");
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   621
                                in.println("Imported: " + type);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   622
                                performToVar(in, stype);
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   623
                            }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   624
                        });
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   625
                    }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   626
                }
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   627
                return new FixResult(fixes, null);
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   628
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   629
        },
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   630
        new FixComputer('i', true) { //compute "Add import" Fixes:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   631
            @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   632
            public FixResult compute(JShellTool repl, String code, int cursor) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   633
                QualifiedNames res = repl.analysis.listQualifiedNames(code, cursor);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   634
                List<Fix> fixes = new ArrayList<>();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   635
                for (String fqn : res.getNames()) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   636
                    fixes.add(new Fix() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   637
                        @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   638
                        public String displayName() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   639
                            return "import: " + fqn;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   640
                        }
41996
389212e0746c 8166333: jshell tool: shortcut var does not import its type
rfield
parents: 41934
diff changeset
   641
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   642
                        @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   643
                        public void perform(ConsoleReader in) throws IOException {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   644
                            repl.state.eval("import " + fqn + ";");
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   645
                            in.println("Imported: " + fqn);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   646
                            in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   647
                        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   648
                    });
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   649
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   650
                if (res.isResolvable()) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   651
                    return new FixResult(Collections.emptyList(),
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   652
                            repl.messageFormat("jshell.console.resolvable"));
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   653
                } else {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   654
                    String error = "";
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   655
                    if (fixes.isEmpty()) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   656
                        error = repl.messageFormat("jshell.console.no.candidate");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   657
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   658
                    if (!res.isUpToDate()) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   659
                        error += repl.messageFormat("jshell.console.incomplete");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   660
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   661
                    return new FixResult(fixes, error);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   662
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   663
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   664
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   665
    };
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   666
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   667
    private static final class JShellUnixTerminal extends NoInterruptUnixTerminal implements SuspendableTerminal {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   668
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   669
        private final StopDetectingInputStream input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   670
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   671
        public JShellUnixTerminal(StopDetectingInputStream input) throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   672
            this.input = input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   673
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   674
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   675
        public boolean isRaw() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   676
            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   677
                return getSettings().get("-a").contains("-icanon");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   678
            } catch (IOException | InterruptedException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   679
                return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   680
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   681
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   682
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   683
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   684
        public InputStream wrapInIfNeeded(InputStream in) throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   685
            return input.setInputStream(super.wrapInIfNeeded(in));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   686
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   687
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   688
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   689
        public void disableInterruptCharacter() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   690
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   691
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   692
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   693
        public void enableInterruptCharacter() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   694
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   695
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   696
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   697
        public void suspend() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   698
            try {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   699
                getSettings().restore();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   700
                super.disableInterruptCharacter();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   701
            } catch (Exception ex) {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   702
                throw new IllegalStateException(ex);
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   703
            }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   704
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   705
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   706
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   707
        public void resume() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   708
            try {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   709
                init();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   710
            } catch (Exception ex) {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   711
                throw new IllegalStateException(ex);
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   712
            }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   713
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   714
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   715
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   716
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   717
    private static final class JShellWindowsTerminal extends WindowsTerminal implements SuspendableTerminal {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   718
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   719
        private final StopDetectingInputStream input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   720
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   721
        public JShellWindowsTerminal(StopDetectingInputStream input) throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   722
            this.input = input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   723
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   724
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   725
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   726
        public void init() throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   727
            super.init();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   728
            setAnsiSupported(false);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   729
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   730
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   731
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   732
        public InputStream wrapInIfNeeded(InputStream in) throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   733
            return input.setInputStream(super.wrapInIfNeeded(in));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   734
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   735
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   736
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   737
        public void suspend() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   738
            try {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   739
                restore();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   740
                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
   741
            } catch (Exception ex) {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   742
                throw new IllegalStateException(ex);
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   743
            }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   744
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   745
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   746
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   747
        public void resume() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   748
            try {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   749
                restore();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   750
                init();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   751
            } catch (Exception ex) {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   752
                throw new IllegalStateException(ex);
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   753
            }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   754
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   755
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   756
        @Override
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   757
        public boolean isRaw() {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   758
            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
   759
        }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   760
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   761
    }
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   762
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   763
    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
   764
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   765
        private final StopDetectingInputStream input;
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   766
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   767
        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
   768
            super(true);
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   769
            setAnsiSupported(false);
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   770
            setEchoEnabled(true);
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   771
            this.input = input;
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   772
        }
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   773
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   774
        @Override
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   775
        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
   776
            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
   777
        }
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   778
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   779
    }
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   780
43583
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   781
    private interface SuspendableTerminal {
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   782
        public void suspend();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   783
        public void resume();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   784
        public boolean isRaw();
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   785
    }
d16e490ec827 8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents: 43363
diff changeset
   786
43363
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   787
    private static final class CheckCompletionKeyMap extends KeyMap {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   788
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   789
        private final KeyMap del;
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   790
        private final AtomicBoolean allowSmart;
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   791
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   792
        public CheckCompletionKeyMap(KeyMap del, AtomicBoolean allowSmart) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   793
            super(del.getName(), del.isViKeyMap());
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   794
            this.del = del;
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   795
            this.allowSmart = allowSmart;
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   796
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   797
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   798
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   799
        public void bind(CharSequence keySeq, Object function) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   800
            del.bind(keySeq, function);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   801
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   802
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   803
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   804
        public void bindIfNotBound(CharSequence keySeq, Object function) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   805
            del.bindIfNotBound(keySeq, function);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   806
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   807
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   808
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   809
        public void from(KeyMap other) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   810
            del.from(other);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   811
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   812
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   813
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   814
        public Object getAnotherKey() {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   815
            return del.getAnotherKey();
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   816
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   817
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   818
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   819
        public Object getBound(CharSequence keySeq) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   820
            Object res = del.getBound(keySeq);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   821
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   822
            if (res != Operation.COMPLETE) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   823
                allowSmart.set(true);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   824
            }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   825
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   826
            return res;
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   827
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   828
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   829
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   830
        public void setBlinkMatchingParen(boolean on) {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   831
            del.setBlinkMatchingParen(on);
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   832
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   833
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   834
        @Override
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   835
        public String toString() {
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   836
            return "check: " + del.toString();
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   837
        }
a4ed2006a4c5 8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents: 42843
diff changeset
   838
    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   839
}