langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java
author rfield
Mon, 04 Apr 2016 10:31:20 -0700
changeset 36990 ec0b843a7af5
parent 36715 ae6fa9280e0b
child 38521 f2fe39ab9256
permissions -rw-r--r--
8147515: JShell: Internationalize Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     1
/*
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
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
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    28
import jdk.jshell.SourceCodeAnalysis.CompletionInfo;
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.awt.event.ActionListener;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    33
import java.io.IOException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    34
import java.io.InputStream;
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;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    37
import java.lang.reflect.Method;
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
    38
import java.util.ArrayList;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
    39
import java.util.Collections;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
    40
import java.util.HashMap;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    41
import java.util.List;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    42
import java.util.Locale;
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
    43
import java.util.Map;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    44
import java.util.Objects;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    45
import java.util.Optional;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    46
import java.util.function.Supplier;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    47
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    48
import jdk.internal.jline.NoInterruptUnixTerminal;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    49
import jdk.internal.jline.Terminal;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    50
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
    51
import jdk.internal.jline.TerminalSupport;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    52
import jdk.internal.jline.WindowsTerminal;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    53
import jdk.internal.jline.console.ConsoleReader;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    54
import jdk.internal.jline.console.KeyMap;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    55
import jdk.internal.jline.console.UserInterruptException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    56
import jdk.internal.jline.console.completer.Completer;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    57
import jdk.internal.jshell.tool.StopDetectingInputStream.State;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    58
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    59
class ConsoleIOContext extends IOContext {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    60
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    61
    final JShellTool repl;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    62
    final StopDetectingInputStream input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    63
    final ConsoleReader in;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    64
    final EditingHistory history;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    65
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    66
    String prefix = "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    67
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    68
    ConsoleIOContext(JShellTool repl, InputStream cmdin, PrintStream cmdout) throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    69
        this.repl = repl;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    70
        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
    71
        Terminal term;
36501
93915076a341 8151570: jtreg tests leave tty in bad state
jlahoda
parents: 36160
diff changeset
    72
        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
    73
            term = new TestTerminal(input);
36501
93915076a341 8151570: jtreg tests leave tty in bad state
jlahoda
parents: 36160
diff changeset
    74
        } 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
    75
            term = new JShellWindowsTerminal(input);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    76
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    77
            term = new JShellUnixTerminal(input);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    78
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    79
        term.init();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    80
        in = new ConsoleReader(cmdin, cmdout, term);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    81
        in.setExpandEvents(false);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    82
        in.setHandleUserInterrupt(true);
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
    83
        in.setHistory(history = new EditingHistory(repl.prefs) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    84
            @Override protected CompletionInfo analyzeCompletion(String input) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    85
                return repl.analysis.analyzeCompletion(input);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    86
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    87
        });
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    88
        in.setBellEnabled(true);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    89
        in.addCompleter(new Completer() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    90
            private String lastTest;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    91
            private int lastCursor;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    92
            private boolean allowSmart = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    93
            @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
    94
                int[] anchor = new int[] {-1};
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    95
                List<Suggestion> suggestions;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    96
                if (prefix.isEmpty() && test.trim().startsWith("/")) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    97
                    suggestions = repl.commandCompletionSuggestions(test, cursor, anchor);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    98
                } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    99
                    int prefixLength = prefix.length();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   100
                    suggestions = repl.analysis.completionSuggestions(prefix + test, cursor + prefixLength, anchor);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   101
                    anchor[0] -= prefixLength;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   102
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   103
                if (!Objects.equals(lastTest, test) || lastCursor != cursor)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   104
                    allowSmart = true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   105
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   106
                boolean smart = allowSmart &&
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   107
                                suggestions.stream()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   108
                                           .anyMatch(s -> s.isSmart);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   109
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   110
                lastTest = test;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   111
                lastCursor = cursor;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   112
                allowSmart = !allowSmart;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   113
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   114
                suggestions.stream()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   115
                           .filter(s -> !smart || s.isSmart)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   116
                           .map(s -> s.continuation)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   117
                           .forEach(result::add);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   118
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   119
                boolean onlySmart = suggestions.stream()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   120
                                               .allMatch(s -> s.isSmart);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   121
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   122
                if (smart && !onlySmart) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   123
                    Optional<String> prefix =
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   124
                            suggestions.stream()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   125
                                       .map(s -> s.continuation)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   126
                                       .reduce(ConsoleIOContext::commonPrefix);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   127
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   128
                    String prefixStr = prefix.orElse("").substring(cursor - anchor[0]);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   129
                    try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   130
                        in.putString(prefixStr);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   131
                        cursor += prefixStr.length();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   132
                    } catch (IOException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   133
                        throw new IllegalStateException(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   134
                    }
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   135
                    result.add(repl.messageFormat("jshell.console.see.more"));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   136
                    return cursor; //anchor should not be used.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   137
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   138
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   139
                if (result.isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   140
                    try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   141
                        //provide "empty completion" feedback
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   142
                        //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
   143
                        in.beep();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   144
                    } catch (IOException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   145
                        throw new UncheckedIOException(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   146
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   147
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   148
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   149
                return anchor[0];
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   150
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   151
        });
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   152
        bind(DOCUMENTATION_SHORTCUT, (ActionListener) evt -> documentation(repl));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   153
        bind(CTRL_UP, (ActionListener) evt -> moveHistoryToSnippet(((EditingHistory) in.getHistory())::previousSnippet));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   154
        bind(CTRL_DOWN, (ActionListener) evt -> moveHistoryToSnippet(((EditingHistory) in.getHistory())::nextSnippet));
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   155
        for (FixComputer computer : FIX_COMPUTERS) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   156
            for (String shortcuts : SHORTCUT_FIXES) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   157
                bind(shortcuts + computer.shortcut, (ActionListener) evt -> fixes(computer));
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   158
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   159
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   160
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   161
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   162
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   163
    public String readLine(String prompt, String prefix) throws IOException, InputInterruptedException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   164
        this.prefix = prefix;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   165
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   166
            return in.readLine(prompt);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   167
        } catch (UserInterruptException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   168
            throw (InputInterruptedException) new InputInterruptedException().initCause(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   169
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   170
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   171
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   172
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   173
    public boolean interactiveOutput() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   174
        return true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   175
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   176
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   177
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   178
    public Iterable<String> currentSessionHistory() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   179
        return history.currentSessionEntries();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   180
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   181
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   182
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   183
    public void close() throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   184
        history.save();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   185
        in.shutdown();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   186
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   187
            in.getTerminal().restore();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   188
        } catch (Exception ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   189
            throw new IOException(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   190
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   191
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   192
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   193
    private void moveHistoryToSnippet(Supplier<Boolean> action) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   194
        if (!action.get()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   195
            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   196
                in.beep();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   197
            } catch (IOException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   198
                throw new IllegalStateException(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   199
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   200
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   201
            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   202
                //could use:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   203
                //in.resetPromptLine(in.getPrompt(), in.getHistory().current().toString(), -1);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   204
                //but that would mean more re-writing on the screen, (and prints an additional
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   205
                //empty line), so using setBuffer directly:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   206
                Method setBuffer = in.getClass().getDeclaredMethod("setBuffer", String.class);
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
                setBuffer.setAccessible(true);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   209
                setBuffer.invoke(in, in.getHistory().current().toString());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   210
                in.flush();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   211
            } catch (ReflectiveOperationException | IOException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   212
                throw new IllegalStateException(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   213
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   214
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   215
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   216
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   217
    private void bind(String shortcut, Object action) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   218
        KeyMap km = in.getKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   219
        for (int i = 0; i < shortcut.length(); i++) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   220
            Object value = km.getBound(Character.toString(shortcut.charAt(i)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   221
            if (value instanceof KeyMap) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   222
                km = (KeyMap) value;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   223
            } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   224
                km.bind(shortcut.substring(i), action);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   225
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   226
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   227
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   228
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   229
    private static final String DOCUMENTATION_SHORTCUT = "\033\133\132"; //Shift-TAB
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   230
    private static final String CTRL_UP = "\033\133\061\073\065\101"; //Ctrl-UP
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   231
    private static final String CTRL_DOWN = "\033\133\061\073\065\102"; //Ctrl-DOWN
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   232
    private static final String[] SHORTCUT_FIXES = {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   233
        "\033\015", //Alt-Enter (Linux)
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   234
        "\033\133\061\067\176", //F6/Alt-F1 (Mac)
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   235
        "\u001BO3P" //Alt-F1 (Linux)
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   236
    };
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   237
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   238
    private void documentation(JShellTool repl) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   239
        String buffer = in.getCursorBuffer().buffer.toString();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   240
        int cursor = in.getCursorBuffer().cursor;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   241
        String doc;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   242
        if (prefix.isEmpty() && buffer.trim().startsWith("/")) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   243
            doc = repl.commandDocumentation(buffer, cursor);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   244
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   245
            doc = repl.analysis.documentation(prefix + buffer, cursor + prefix.length());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   246
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   247
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   248
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   249
            if (doc != null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   250
                in.println();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   251
                in.println(doc);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   252
                in.redrawLine();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   253
                in.flush();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   254
            } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   255
                in.beep();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   256
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   257
        } catch (IOException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   258
            throw new IllegalStateException(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   259
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   260
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   261
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   262
    private static String commonPrefix(String str1, String str2) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   263
        for (int i = 0; i < str2.length(); i++) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   264
            if (!str1.startsWith(str2.substring(0, i + 1))) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   265
                return str2.substring(0, i);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   266
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   267
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   268
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   269
        return str2;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   270
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   271
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   272
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   273
    public boolean terminalEditorRunning() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   274
        Terminal terminal = in.getTerminal();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   275
        if (terminal instanceof JShellUnixTerminal)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   276
            return ((JShellUnixTerminal) terminal).isRaw();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   277
        return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   278
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   279
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   280
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   281
    public void suspend() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   282
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   283
            in.getTerminal().restore();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   284
        } catch (Exception ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   285
            throw new IllegalStateException(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   286
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   287
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   288
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   289
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   290
    public void resume() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   291
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   292
            in.getTerminal().init();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   293
        } catch (Exception ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   294
            throw new IllegalStateException(ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   295
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   296
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   297
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   298
    public void beforeUserCode() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   299
        input.setState(State.BUFFER);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   300
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   301
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   302
    public void afterUserCode() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   303
        input.setState(State.WAIT);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   304
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   305
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   306
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   307
    public void replaceLastHistoryEntry(String source) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   308
        history.fullHistoryReplace(source);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   309
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   310
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   311
    //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
   312
    //and perform the selected one:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   313
    private void fixes(FixComputer computer) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   314
        String input = prefix + in.getCursorBuffer().toString();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   315
        int cursor = prefix.length() + in.getCursorBuffer().cursor;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   316
        FixResult candidates = computer.compute(repl, input, cursor);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   317
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   318
        try {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   319
            final boolean printError = candidates.error != null && !candidates.error.isEmpty();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   320
            if (printError) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   321
                in.println(candidates.error);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   322
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   323
            if (candidates.fixes.isEmpty()) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   324
                in.beep();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   325
                if (printError) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   326
                    in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   327
                    in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   328
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   329
            } else if (candidates.fixes.size() == 1 && !computer.showMenu) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   330
                if (printError) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   331
                    in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   332
                    in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   333
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   334
                candidates.fixes.get(0).perform(in);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   335
            } else {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   336
                List<Fix> fixes = new ArrayList<>(candidates.fixes);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   337
                fixes.add(0, new Fix() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   338
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   339
                    public String displayName() {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   340
                        return repl.messageFormat("jshell.console.do.nothing");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   341
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   342
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   343
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   344
                    public void perform(ConsoleReader in) throws IOException {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   345
                        in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   346
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   347
                });
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   348
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   349
                Map<Character, Fix> char2Fix = new HashMap<>();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   350
                in.println();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   351
                for (int i = 0; i < fixes.size(); i++) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   352
                    Fix fix = fixes.get(i);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   353
                    char2Fix.put((char) ('0' + i), fix);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   354
                    in.println("" + i + ": " + fixes.get(i).displayName());
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   355
                }
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   356
                in.print(repl.messageFormat("jshell.console.choice"));
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   357
                in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   358
                int read;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   359
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   360
                read = in.readCharacter();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   361
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   362
                Fix fix = char2Fix.get((char) read);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   363
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   364
                if (fix == null) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   365
                    in.beep();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   366
                    fix = fixes.get(0);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   367
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   368
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   369
                in.println();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   370
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   371
                fix.perform(in);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   372
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   373
                in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   374
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   375
        } catch (IOException ex) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   376
            ex.printStackTrace();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   377
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   378
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   379
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   380
    /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   381
     * 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
   382
     */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   383
    public interface Fix {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   384
        /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   385
         * A name that should be shown to the user.
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   386
         */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   387
        public String displayName();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   388
        /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   389
         * Perform the given action.
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   390
         */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   391
        public void perform(ConsoleReader in) throws IOException;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   392
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   393
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   394
    /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   395
     * A factory for {@link Fix}es.
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   396
     */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   397
    public abstract static class FixComputer {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   398
        private final char shortcut;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   399
        private final boolean showMenu;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   400
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   401
        /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   402
         * 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
   403
         * 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
   404
         * 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
   405
         */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   406
        public FixComputer(char shortcut, boolean showMenu) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   407
            this.shortcut = shortcut;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   408
            this.showMenu = showMenu;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   409
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   410
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   411
        /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   412
         * Compute possible actions for the given code.
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   413
         */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   414
        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
   415
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   416
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   417
    /**
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   418
     * 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
   419
     */
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   420
    public static class FixResult {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   421
        public final List<Fix> fixes;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   422
        public final String error;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   423
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   424
        public FixResult(List<Fix> fixes, String error) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   425
            this.fixes = fixes;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   426
            this.error = error;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   427
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   428
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   429
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   430
    private static final FixComputer[] FIX_COMPUTERS = new FixComputer[] {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   431
        new FixComputer('v', false) { //compute "Introduce variable" Fix:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   432
            @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   433
            public FixResult compute(JShellTool repl, String code, int cursor) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   434
                String type = repl.analysis.analyzeType(code, cursor);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   435
                if (type == null) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   436
                    return new FixResult(Collections.emptyList(), null);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   437
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   438
                return new FixResult(Collections.singletonList(new Fix() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   439
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   440
                    public String displayName() {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   441
                        return repl.messageFormat("jshell.console.create.variable");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   442
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   443
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   444
                    public void perform(ConsoleReader in) throws IOException {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   445
                        in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   446
                        in.setCursorPosition(0);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   447
                        in.putString(type + "  = ");
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   448
                        in.setCursorPosition(in.getCursorBuffer().cursor - 3);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   449
                        in.flush();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   450
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   451
                }), null);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   452
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   453
        },
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   454
        new FixComputer('i', true) { //compute "Add import" Fixes:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   455
            @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   456
            public FixResult compute(JShellTool repl, String code, int cursor) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   457
                QualifiedNames res = repl.analysis.listQualifiedNames(code, cursor);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   458
                List<Fix> fixes = new ArrayList<>();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   459
                for (String fqn : res.getNames()) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   460
                    fixes.add(new Fix() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   461
                        @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   462
                        public String displayName() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   463
                            return "import: " + fqn;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   464
                        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   465
                        @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   466
                        public void perform(ConsoleReader in) throws IOException {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   467
                            repl.state.eval("import " + fqn + ";");
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   468
                            in.println("Imported: " + fqn);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   469
                            in.redrawLine();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   470
                        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   471
                    });
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   472
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   473
                if (res.isResolvable()) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   474
                    return new FixResult(Collections.emptyList(),
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   475
                            repl.messageFormat("jshell.console.resolvable"));
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   476
                } else {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   477
                    String error = "";
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   478
                    if (fixes.isEmpty()) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   479
                        error = repl.messageFormat("jshell.console.no.candidate");
36160
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
                    if (!res.isUpToDate()) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36715
diff changeset
   482
                        error += repl.messageFormat("jshell.console.incomplete");
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   483
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   484
                    return new FixResult(fixes, error);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   485
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   486
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 33362
diff changeset
   487
        }
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
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   490
    private static final class JShellUnixTerminal extends NoInterruptUnixTerminal {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   491
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   492
        private final StopDetectingInputStream input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   493
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   494
        public JShellUnixTerminal(StopDetectingInputStream input) throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   495
            this.input = input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   496
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   497
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   498
        public boolean isRaw() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   499
            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   500
                return getSettings().get("-a").contains("-icanon");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   501
            } catch (IOException | InterruptedException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   502
                return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   503
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   504
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   505
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   506
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   507
        public InputStream wrapInIfNeeded(InputStream in) throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   508
            return input.setInputStream(super.wrapInIfNeeded(in));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   509
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   510
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   511
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   512
        public void disableInterruptCharacter() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   513
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   514
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   515
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   516
        public void enableInterruptCharacter() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   517
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   518
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   519
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   520
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   521
    private static final class JShellWindowsTerminal extends WindowsTerminal {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   522
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   523
        private final StopDetectingInputStream input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   524
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   525
        public JShellWindowsTerminal(StopDetectingInputStream input) throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   526
            this.input = input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   527
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   528
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   529
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   530
        public void init() throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   531
            super.init();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   532
            setAnsiSupported(false);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   533
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   534
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   535
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   536
        public InputStream wrapInIfNeeded(InputStream in) throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   537
            return input.setInputStream(super.wrapInIfNeeded(in));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   538
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   539
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   540
    }
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   541
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   542
    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
   543
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   544
        private final StopDetectingInputStream input;
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   545
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   546
        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
   547
            super(true);
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   548
            setAnsiSupported(false);
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   549
            setEchoEnabled(true);
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   550
            this.input = input;
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   551
        }
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   552
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   553
        @Override
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   554
        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
   555
            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
   556
        }
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   557
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36501
diff changeset
   558
    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   559
}