src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/Console.java
author erikj
Mon, 21 Oct 2019 09:26:14 -0700
changeset 58720 ae0af9fb3dbb
parent 52938 5ff7480c9e28
permissions -rw-r--r--
8232572: Add hooks for custom output dir in Bundles.gmk Reviewed-by: tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     1
/*
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     4
 *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    10
 *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    15
 * accompanied this code).
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    16
 *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    20
 *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    23
 * questions.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    24
 */
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    25
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    26
package jdk.nashorn.tools.jjs;
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    27
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
    28
import java.io.File;
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    29
import java.io.IOException;
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    30
import java.io.InputStream;
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    31
import java.io.PrintStream;
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    32
import java.io.Writer;
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    33
import java.nio.file.Files;
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    34
import java.util.ArrayList;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    35
import java.util.List;
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
    36
import java.util.function.Function;
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    37
import java.util.stream.Collectors;
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    38
import java.util.stream.StreamSupport;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    39
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    40
import jdk.internal.org.jline.reader.Candidate;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    41
import jdk.internal.org.jline.reader.CompletingParsedLine;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    42
import jdk.internal.org.jline.reader.EOFError;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    43
import jdk.internal.org.jline.reader.History;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    44
import jdk.internal.org.jline.reader.LineReader;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    45
import jdk.internal.org.jline.reader.LineReader.Option;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    46
import jdk.internal.org.jline.reader.LineReaderBuilder;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    47
import jdk.internal.org.jline.reader.Parser;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    48
import jdk.internal.org.jline.reader.Parser.ParseContext;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    49
import jdk.internal.org.jline.reader.Widget;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    50
import jdk.internal.org.jline.reader.impl.LineReaderImpl;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    51
import jdk.internal.org.jline.reader.impl.completer.ArgumentCompleter.ArgumentLine;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    52
import jdk.internal.org.jline.terminal.Attributes.LocalFlag;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    53
import jdk.internal.org.jline.terminal.Terminal;
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    54
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    55
class Console implements AutoCloseable {
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
    56
    private static final String DOCUMENTATION_SHORTCUT = "\033\133\132"; //Shift-TAB
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    57
    private final LineReader in;
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    58
    private final File historyFile;
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    59
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
    60
    Console(final InputStream cmdin, final PrintStream cmdout, final File historyFile,
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    61
            final NashornCompleter completer, final Function<String, String> docHelper) throws IOException {
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    62
        this.historyFile = historyFile;
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    63
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    64
        Parser parser = (line, cursor, context) -> {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    65
            if (context == ParseContext.COMPLETE) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    66
                List<Candidate> candidates = new ArrayList<>();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    67
                int anchor = completer.complete(line, cursor, candidates);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    68
                anchor = Math.min(anchor, line.length());
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    69
                return new CompletionLine(line.substring(anchor), cursor, candidates);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    70
            } else if (!completer.isComplete(line)) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    71
                throw new EOFError(cursor, cursor, line);
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    72
            }
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    73
            return new ArgumentLine(line, cursor);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    74
        };
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    75
        in = LineReaderBuilder.builder()
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    76
                              .option(Option.DISABLE_EVENT_EXPANSION, true)
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    77
                              .completer((in, line, candidates) -> candidates.addAll(((CompletionLine) line).candidates))
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    78
                              .parser(parser)
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    79
                              .build();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    80
        if (historyFile.exists()) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    81
            StringBuilder line = new StringBuilder();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    82
            for (String h : Files.readAllLines(historyFile.toPath())) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    83
                if (line.length() > 0) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    84
                    line.append("\n");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    85
                }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    86
                line.append(h);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    87
                try {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    88
                    parser.parse(line.toString(), line.length());
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    89
                    in.getHistory().add(line.toString());
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    90
                    line.delete(0, line.length());
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    91
                } catch (EOFError e) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    92
                    //continue;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    93
                }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    94
            }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    95
            if (line.length() > 0) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    96
                in.getHistory().add(line.toString());
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    97
            }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
    98
        }
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
    99
        Runtime.getRuntime().addShutdownHook(new Thread((Runnable)this::saveHistory));
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   100
        bind(DOCUMENTATION_SHORTCUT, ()->showDocumentation(docHelper));
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   101
    }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   102
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   103
    String readLine(final String prompt, final String continuationPrompt) throws IOException {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   104
        in.setVariable(LineReader.SECONDARY_PROMPT_PATTERN, continuationPrompt);
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   105
        return in.readLine(prompt);
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   106
    }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   107
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   108
    String readUserLine(final String prompt) throws IOException {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   109
        Parser prevParser = in.getParser();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   110
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   111
        try {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   112
            ((LineReaderImpl) in).setParser((line, cursor, context) -> new ArgumentLine(line, cursor));
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   113
            return in.readLine(prompt);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   114
        } finally {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   115
            ((LineReaderImpl) in).setParser(prevParser);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   116
        }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   117
    }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   118
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   119
    @Override
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   120
    public void close() {
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
   121
        saveHistory();
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   122
    }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   123
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
   124
    private void saveHistory() {
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
   125
        try (Writer out = Files.newBufferedWriter(historyFile.toPath())) {
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
   126
            String lineSeparator = System.getProperty("line.separator");
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
   127
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   128
            out.write(StreamSupport.stream(getHistory().spliterator(), false)
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   129
                                   .map(e -> e.line())
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   130
                                   .collect(Collectors.joining(lineSeparator)));
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
   131
        } catch (final IOException exp) {}
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
   132
    }
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   133
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   134
    History getHistory() {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   135
        return in.getHistory();
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   136
    }
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   137
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   138
    boolean terminalEditorRunning() {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   139
        Terminal terminal = in.getTerminal();
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   140
        return !terminal.getAttributes().getLocalFlag(LocalFlag.ICANON);
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   141
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   142
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   143
    void suspend() {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   144
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   145
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   146
    void resume() {
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   147
    }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   148
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   149
    private void bind(String shortcut, Widget action) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   150
        in.getKeyMaps().get(LineReader.MAIN).bind(action, shortcut);
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   151
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   152
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   153
    private boolean showDocumentation(final Function<String, String> docHelper) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   154
        final String buffer = in.getBuffer().toString();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   155
        final int cursor = in.getBuffer().cursor();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   156
        final String doc = docHelper.apply(buffer.substring(0, cursor));
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   157
        if (doc != null) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   158
            in.getTerminal().writer().println();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   159
            in.printAbove(doc);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   160
            return true;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   161
        } else {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   162
            return false;
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   163
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   164
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   165
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   166
    private static final class CompletionLine extends ArgumentLine implements CompletingParsedLine {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   167
        public final List<Candidate> candidates;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   168
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   169
        public CompletionLine(String word, int cursor, List<Candidate> candidates) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   170
            super(word, cursor);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   171
            this.candidates = candidates;
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   172
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   173
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   174
        public CharSequence escape(CharSequence candidate, boolean complete) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   175
            return candidate;
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   176
        }
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   177
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   178
        public int rawWordCursor() {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   179
            return word().length();
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   180
        }
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   181
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   182
        public int rawWordLength() {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 50338
diff changeset
   183
            return word().length();
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   184
        }
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   185
    }
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   186
}