langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java
author jlahoda
Thu, 06 Apr 2017 11:55:58 +0200
changeset 44569 b1accf8b2aed
parent 44459 5224425af378
child 44683 610dc2b48954
permissions -rw-r--r--
8178013: Finetuning of merged tab and shift tab completion Summary: Fixing mistakes in localization bundle, fixing completion after /help set. Reviewed-by: rfield
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     1
/*
44569
b1accf8b2aed 8178013: Finetuning of merged tab and shift tab completion
jlahoda
parents: 44459
diff changeset
     2
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     4
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    10
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    15
 * accompanied this code).
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    16
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    20
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    23
 * questions.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    24
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    25
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    26
package jdk.internal.jshell.tool;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    27
42972
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
    28
import java.io.BufferedReader;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    29
import java.io.BufferedWriter;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    30
import java.io.File;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    31
import java.io.FileNotFoundException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    32
import java.io.FileReader;
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;
42972
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
    35
import java.io.InputStreamReader;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    36
import java.io.PrintStream;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    37
import java.io.Reader;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    38
import java.io.StringReader;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    39
import java.nio.charset.Charset;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    40
import java.nio.file.FileSystems;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    41
import java.nio.file.Files;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    42
import java.nio.file.Path;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    43
import java.nio.file.Paths;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
    44
import java.text.MessageFormat;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    45
import java.util.ArrayList;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    46
import java.util.Arrays;
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
    47
import java.util.Collection;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    48
import java.util.Collections;
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
    49
import java.util.HashMap;
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
    50
import java.util.HashSet;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    51
import java.util.Iterator;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    52
import java.util.LinkedHashMap;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    53
import java.util.LinkedHashSet;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    54
import java.util.List;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
    55
import java.util.Locale;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    56
import java.util.Map;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    57
import java.util.Map.Entry;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    58
import java.util.Scanner;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    59
import java.util.Set;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    60
import java.util.function.Consumer;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    61
import java.util.function.Predicate;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    62
import java.util.prefs.Preferences;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    63
import java.util.regex.Matcher;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    64
import java.util.regex.Pattern;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    65
import java.util.stream.Collectors;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    66
import java.util.stream.Stream;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    67
import java.util.stream.StreamSupport;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    68
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    69
import jdk.internal.jshell.debug.InternalDebugControl;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    70
import jdk.internal.jshell.tool.IOContext.InputInterruptedException;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    71
import jdk.jshell.DeclarationSnippet;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    72
import jdk.jshell.Diag;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    73
import jdk.jshell.EvalException;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    74
import jdk.jshell.ExpressionSnippet;
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    75
import jdk.jshell.ImportSnippet;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    76
import jdk.jshell.JShell;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    77
import jdk.jshell.JShell.Subscription;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    78
import jdk.jshell.MethodSnippet;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    79
import jdk.jshell.Snippet;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    80
import jdk.jshell.Snippet.Status;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    81
import jdk.jshell.SnippetEvent;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    82
import jdk.jshell.SourceCodeAnalysis;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    83
import jdk.jshell.SourceCodeAnalysis.CompletionInfo;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    84
import jdk.jshell.SourceCodeAnalysis.Suggestion;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    85
import jdk.jshell.TypeDeclSnippet;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    86
import jdk.jshell.UnresolvedReferenceException;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    87
import jdk.jshell.VarSnippet;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    88
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    89
import static java.nio.file.StandardOpenOption.CREATE;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    90
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    91
import static java.nio.file.StandardOpenOption.WRITE;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    92
import java.util.MissingResourceException;
34477
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
    93
import java.util.Optional;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    94
import java.util.ResourceBundle;
41934
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
    95
import java.util.ServiceLoader;
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
    96
import java.util.Spliterators;
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
    97
import java.util.function.Function;
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
    98
import java.util.function.Supplier;
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
    99
import jdk.internal.joptsimple.*;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   100
import jdk.internal.jshell.tool.Feedback.FormatAction;
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   101
import jdk.internal.jshell.tool.Feedback.FormatCase;
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   102
import jdk.internal.jshell.tool.Feedback.FormatErrors;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   103
import jdk.internal.jshell.tool.Feedback.FormatResolve;
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   104
import jdk.internal.jshell.tool.Feedback.FormatUnresolved;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   105
import jdk.internal.jshell.tool.Feedback.FormatWhen;
41934
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
   106
import jdk.internal.editor.spi.BuildInEditorProvider;
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
   107
import jdk.internal.editor.external.ExternalEditor;
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   108
import static java.util.Arrays.asList;
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   109
import static java.util.Arrays.stream;
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   110
import static java.util.stream.Collectors.joining;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   111
import static java.util.stream.Collectors.toList;
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
   112
import static jdk.jshell.Snippet.SubKind.VAR_VALUE_SUBKIND;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   113
import static java.util.stream.Collectors.toMap;
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
   114
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_COMPA;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
   115
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_DEP;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
   116
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_EVNT;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
   117
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_FMGR;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
   118
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents: 43038
diff changeset
   119
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_WRAP;
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
   120
import static jdk.internal.jshell.tool.ContinuousCompletionProvider.STARTSWITH_MATCHER;
33362
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
/**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   123
 * Command line REPL tool for Java using the JShell API.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   124
 * @author Robert Field
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   125
 */
37745
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
   126
public class JShellTool implements MessageHandler {
33362
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
    private static final Pattern LINEBREAK = Pattern.compile("\\R");
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
   129
            static final String RECORD_SEPARATOR = "\u241E";
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   130
    private static final String RB_NAME_PREFIX  = "jdk.internal.jshell.tool.resources";
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   131
    private static final String VERSION_RB_NAME = RB_NAME_PREFIX + ".version";
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   132
    private static final String L10N_RB_NAME    = RB_NAME_PREFIX + ".l10n";
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   133
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   134
    final InputStream cmdin;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   135
    final PrintStream cmdout;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   136
    final PrintStream cmderr;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   137
    final PrintStream console;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   138
    final InputStream userin;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   139
    final PrintStream userout;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   140
    final PrintStream usererr;
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   141
    final PersistentStorage prefs;
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   142
    final Map<String, String> envvars;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   143
    final Locale locale;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   144
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   145
    final Feedback feedback = new Feedback();
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   146
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   147
    /**
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   148
     * The complete constructor for the tool (used by test harnesses).
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   149
     * @param cmdin command line input -- snippets and commands
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   150
     * @param cmdout command line output, feedback including errors
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   151
     * @param cmderr start-up errors and debugging info
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   152
     * @param console console control interaction
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   153
     * @param userin code execution input, or null to use IOContext
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   154
     * @param userout code execution output  -- System.out.printf("hi")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   155
     * @param usererr code execution error stream  -- System.err.printf("Oops")
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   156
     * @param prefs persistence implementation to use
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   157
     * @param envvars environment variable mapping to use
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   158
     * @param locale locale to use
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   159
     */
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   160
    JShellTool(InputStream cmdin, PrintStream cmdout, PrintStream cmderr,
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   161
            PrintStream console,
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36499
diff changeset
   162
            InputStream userin, PrintStream userout, PrintStream usererr,
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
   163
            PersistentStorage prefs, Map<String, String> envvars, Locale locale) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   164
        this.cmdin = cmdin;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   165
        this.cmdout = cmdout;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   166
        this.cmderr = cmderr;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   167
        this.console = console;
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   168
        this.userin = userin != null ? userin : new InputStream() {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   169
            @Override
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   170
            public int read() throws IOException {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   171
                return input.readUserInput();
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   172
            }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   173
        };
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   174
        this.userout = userout;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   175
        this.usererr = usererr;
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36499
diff changeset
   176
        this.prefs = prefs;
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   177
        this.envvars = envvars;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   178
        this.locale = locale;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   179
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   180
36992
ddebebe611a1 8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents: 36990
diff changeset
   181
    private ResourceBundle versionRB = null;
ddebebe611a1 8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents: 36990
diff changeset
   182
    private ResourceBundle outputRB  = null;
ddebebe611a1 8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents: 36990
diff changeset
   183
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   184
    private IOContext input = null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   185
    private boolean regenerateOnDeath = true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   186
    private boolean live = false;
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   187
    private Options options;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   188
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   189
    SourceCodeAnalysis analysis;
44188
3f2047e62102 8176412: jshell tool: automatic imports are excluded on /reload causing it to fail
rfield
parents: 44065
diff changeset
   190
    private JShell state = null;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   191
    Subscription shutdownSubscription = null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   192
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   193
    static final EditorSetting BUILT_IN_EDITOR = new EditorSetting(null, false);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   194
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   195
    private boolean debug = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   196
    public boolean testPrompt = false;
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
   197
    private Startup startup = null;
44065
d0a8a4c41c85 8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents: 43856
diff changeset
   198
    private boolean isCurrentlyRunningStartup = false;
42969
a48d4f74d322 8168615: JShell API: jdk.jshell.spi should be a pluggable ServiceLoader SPI
rfield
parents: 42843
diff changeset
   199
    private String executionControlSpec = null;
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   200
    private EditorSetting editor = BUILT_IN_EDITOR;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   201
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   202
    private static final String[] EDITOR_ENV_VARS = new String[] {
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   203
        "JSHELLEDITOR", "VISUAL", "EDITOR"};
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   204
43564
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   205
    // Commands and snippets which can be replayed
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   206
    private ReplayableHistory replayableHistory;
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   207
    private ReplayableHistory replayableHistoryPrevious;
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   208
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   209
    static final String STARTUP_KEY  = "STARTUP";
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   210
    static final String EDITOR_KEY   = "EDITOR";
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   211
    static final String FEEDBACK_KEY = "FEEDBACK";
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   212
    static final String MODE_KEY     = "MODE";
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   213
    static final String REPLAY_RESTORE_KEY = "REPLAY_RESTORE";
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   214
42972
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
   215
    static final Pattern BUILTIN_FILE_PATTERN = Pattern.compile("\\w+");
43136
24d62ba7ad5e 8172414: jshell not working in exploded JDK build
jlahoda
parents: 43134
diff changeset
   216
    static final String BUILTIN_FILE_PATH_FORMAT = "/jdk/jshell/tool/resources/%s.jsh";
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   217
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   218
    // match anything followed by whitespace
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   219
    private static final Pattern OPTION_PRE_PATTERN =
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   220
            Pattern.compile("\\s*(\\S+\\s+)*?");
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   221
    // match a (possibly incomplete) option flag with optional double-dash and/or internal dashes
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   222
    private static final Pattern OPTION_PATTERN =
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   223
            Pattern.compile(OPTION_PRE_PATTERN.pattern() + "(?<dd>-??)(?<flag>-([a-z][a-z\\-]*)?)");
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   224
    // match an option flag and a (possibly missing or incomplete) value
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   225
    private static final Pattern OPTION_VALUE_PATTERN =
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   226
            Pattern.compile(OPTION_PATTERN.pattern() + "\\s+(?<val>\\S*)");
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   227
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   228
    // Tool id (tid) mapping: the three name spaces
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   229
    NameSpace mainNamespace;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   230
    NameSpace startNamespace;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   231
    NameSpace errorNamespace;
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   232
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   233
    // Tool id (tid) mapping: the current name spaces
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   234
    NameSpace currentNameSpace;
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   235
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   236
    Map<Snippet, SnippetInfo> mapSnippet;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   237
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   238
    // Kinds of compiler/runtime init options
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   239
    private enum OptionKind {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   240
        CLASS_PATH("--class-path", true),
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   241
        MODULE_PATH("--module-path", true),
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   242
        ADD_MODULES("--add-modules", false),
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   243
        ADD_EXPORTS("--add-exports", false),
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   244
        TO_COMPILER("-C", false, false, true, false),
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   245
        TO_REMOTE_VM("-R", false, false, false, true),;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   246
        final String optionFlag;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   247
        final boolean onlyOne;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   248
        final boolean passFlag;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   249
        final boolean toCompiler;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   250
        final boolean toRemoteVm;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   251
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   252
        private OptionKind(String optionFlag, boolean onlyOne) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   253
            this(optionFlag, onlyOne, true, true, true);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   254
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   255
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   256
        private OptionKind(String optionFlag, boolean onlyOne, boolean passFlag,
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   257
                boolean toCompiler, boolean toRemoteVm) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   258
            this.optionFlag = optionFlag;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   259
            this.onlyOne = onlyOne;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   260
            this.passFlag = passFlag;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   261
            this.toCompiler = toCompiler;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   262
            this.toRemoteVm = toRemoteVm;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   263
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   264
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   265
    }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   266
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   267
    // compiler/runtime init option values
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   268
    private static class Options {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   269
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   270
        private Map<OptionKind, List<String>> optMap = new HashMap<>();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   271
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   272
        private String[] selectOptions(Predicate<Entry<OptionKind, List<String>>> pred) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   273
            return optMap.entrySet().stream()
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   274
                    .filter(pred)
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   275
                    .flatMap(e -> e.getValue().stream())
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   276
                    .toArray(String[]::new);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   277
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   278
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   279
        String[] remoteVmOptions() {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   280
            return selectOptions(e -> e.getKey().toRemoteVm);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   281
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   282
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   283
        String[] compilerOptions() {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   284
            return selectOptions(e -> e.getKey().toCompiler);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   285
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   286
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   287
        String[] commonOptions() {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   288
            return selectOptions(e -> e.getKey().passFlag);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   289
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   290
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   291
        void addAll(OptionKind kind, Collection<String> vals) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   292
            optMap.computeIfAbsent(kind, k -> new ArrayList<>())
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   293
                    .addAll(vals);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   294
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   295
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   296
        void override(Options newer) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   297
            newer.optMap.entrySet().stream()
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   298
                    .forEach(e -> {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   299
                        if (e.getKey().onlyOne) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   300
                            // Only one allowed, override last
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   301
                            optMap.put(e.getKey(), e.getValue());
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   302
                        } else {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   303
                            // Additive
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   304
                            addAll(e.getKey(), e.getValue());
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   305
                        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   306
                    });
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   307
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   308
    }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   309
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   310
    // base option parsing of /env, /reload, and /reset and command-line options
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   311
    private class OptionParserBase {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   312
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   313
        final OptionParser parser = new OptionParser();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   314
        private final OptionSpec<String> argClassPath = parser.accepts("class-path").withRequiredArg();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   315
        private final OptionSpec<String> argModulePath = parser.accepts("module-path").withRequiredArg();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   316
        private final OptionSpec<String> argAddModules = parser.accepts("add-modules").withRequiredArg();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   317
        private final OptionSpec<String> argAddExports = parser.accepts("add-exports").withRequiredArg();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   318
        private final NonOptionArgumentSpec<String> argNonOptions = parser.nonOptions();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   319
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   320
        private Options opts = new Options();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   321
        private List<String> nonOptions;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   322
        private boolean failed = false;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   323
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   324
        List<String> nonOptions() {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   325
            return nonOptions;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   326
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   327
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   328
        void msg(String key, Object... args) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   329
            errormsg(key, args);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   330
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   331
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   332
        Options parse(String[] args) throws OptionException {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   333
            try {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   334
                OptionSet oset = parser.parse(args);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   335
                nonOptions = oset.valuesOf(argNonOptions);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   336
                return parse(oset);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   337
            } catch (OptionException ex) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   338
                if (ex.options().isEmpty()) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   339
                    msg("jshell.err.opt.invalid", stream(args).collect(joining(", ")));
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   340
                } else {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   341
                    boolean isKnown = parser.recognizedOptions().containsKey(ex.options().iterator().next());
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   342
                    msg(isKnown
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   343
                            ? "jshell.err.opt.arg"
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   344
                            : "jshell.err.opt.unknown",
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   345
                            ex.options()
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   346
                            .stream()
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   347
                            .collect(joining(", ")));
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   348
                }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   349
                return null;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   350
            }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   351
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   352
43856
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   353
        // check that the supplied string represent valid class/module paths
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   354
        // converting any ~/ to user home
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   355
        private Collection<String> validPaths(Collection<String> vals, String context, boolean isModulePath) {
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   356
            Stream<String> result = vals.stream()
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   357
                    .map(s -> Arrays.stream(s.split(File.pathSeparator))
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   358
                        .map(sp -> toPathResolvingUserHome(sp))
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   359
                        .filter(p -> checkValidPathEntry(p, context, isModulePath))
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   360
                        .map(p -> p.toString())
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   361
                        .collect(Collectors.joining(File.pathSeparator)));
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   362
            if (failed) {
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   363
                return Collections.emptyList();
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   364
            } else {
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   365
                return result.collect(toList());
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   366
            }
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   367
        }
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   368
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   369
        // Adapted from compiler method Locations.checkValidModulePathEntry
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   370
        private boolean checkValidPathEntry(Path p, String context, boolean isModulePath) {
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   371
            if (!Files.exists(p)) {
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   372
                msg("jshell.err.file.not.found", context, p);
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   373
                failed = true;
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   374
                return false;
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   375
            }
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   376
            if (Files.isDirectory(p)) {
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   377
                // if module-path, either an exploded module or a directory of modules
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   378
                return true;
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   379
            }
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   380
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   381
            String name = p.getFileName().toString();
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   382
            int lastDot = name.lastIndexOf(".");
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   383
            if (lastDot > 0) {
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   384
                switch (name.substring(lastDot)) {
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   385
                    case ".jar":
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   386
                        return true;
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   387
                    case ".jmod":
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   388
                        if (isModulePath) {
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   389
                            return true;
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   390
                        }
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   391
                }
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   392
            }
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   393
            msg("jshell.err.arg", context, p);
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   394
            failed = true;
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   395
            return false;
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   396
        }
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   397
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   398
        Options parse(OptionSet options) {
43856
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   399
            addOptions(OptionKind.CLASS_PATH,
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   400
                    validPaths(options.valuesOf(argClassPath), "--class-path", false));
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   401
            addOptions(OptionKind.MODULE_PATH,
fcdebb803c62 8174797: jshell tool: invalid module path crashes tool
rfield
parents: 43759
diff changeset
   402
                    validPaths(options.valuesOf(argModulePath), "--module-path", true));
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   403
            addOptions(OptionKind.ADD_MODULES, options.valuesOf(argAddModules));
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   404
            addOptions(OptionKind.ADD_EXPORTS, options.valuesOf(argAddExports).stream()
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   405
                    .map(mp -> mp.contains("=") ? mp : mp + "=ALL-UNNAMED")
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   406
                    .collect(toList())
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   407
            );
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   408
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   409
            return failed ? null : opts;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   410
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   411
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   412
        void addOptions(OptionKind kind, Collection<String> vals) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   413
            if (!vals.isEmpty()) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   414
                if (kind.onlyOne && vals.size() > 1) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   415
                    msg("jshell.err.opt.one", kind.optionFlag);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   416
                    failed = true;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   417
                    return;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   418
                }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   419
                if (kind.passFlag) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   420
                    vals = vals.stream()
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   421
                            .flatMap(mp -> Stream.of(kind.optionFlag, mp))
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   422
                            .collect(toList());
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   423
                }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   424
                opts.addAll(kind, vals);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   425
            }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   426
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   427
    }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   428
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   429
    // option parsing for /reload (adds -restore -quiet)
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   430
    private class OptionParserReload extends OptionParserBase {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   431
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   432
        private final OptionSpecBuilder argRestore = parser.accepts("restore");
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   433
        private final OptionSpecBuilder argQuiet   = parser.accepts("quiet");
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   434
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   435
        private boolean restore = false;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   436
        private boolean quiet = false;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   437
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   438
        boolean restore() {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   439
            return restore;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   440
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   441
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   442
        boolean quiet() {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   443
            return quiet;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   444
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   445
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   446
        @Override
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   447
        Options parse(OptionSet options) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   448
            if (options.has(argRestore)) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   449
                restore = true;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   450
            }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   451
            if (options.has(argQuiet)) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   452
                quiet = true;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   453
            }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   454
            return super.parse(options);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   455
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   456
    }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   457
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   458
    // option parsing for command-line
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   459
    private class OptionParserCommandLine extends OptionParserBase {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   460
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   461
        private final OptionSpec<String> argStart = parser.accepts("startup").withRequiredArg();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   462
        private final OptionSpecBuilder argNoStart = parser.acceptsAll(asList("n", "no-startup"));
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   463
        private final OptionSpec<String> argFeedback = parser.accepts("feedback").withRequiredArg();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   464
        private final OptionSpec<String> argExecution = parser.accepts("execution").withRequiredArg();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   465
        private final OptionSpecBuilder argQ = parser.accepts("q");
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   466
        private final OptionSpecBuilder argS = parser.accepts("s");
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   467
        private final OptionSpecBuilder argV = parser.accepts("v");
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   468
        private final OptionSpec<String> argR = parser.accepts("R").withRequiredArg();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   469
        private final OptionSpec<String> argC = parser.accepts("C").withRequiredArg();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   470
        private final OptionSpecBuilder argHelp = parser.acceptsAll(asList("h", "help"));
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   471
        private final OptionSpecBuilder argVersion = parser.accepts("version");
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   472
        private final OptionSpecBuilder argFullVersion = parser.accepts("full-version");
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 43274
diff changeset
   473
        private final OptionSpecBuilder argShowVersion = parser.accepts("show-version");
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 43274
diff changeset
   474
        private final OptionSpecBuilder argHelpExtra = parser.acceptsAll(asList("X", "help-extra"));
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   475
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   476
        private String feedbackMode = null;
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
   477
        private Startup initialStartup = null;
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   478
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   479
        String feedbackMode() {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   480
            return feedbackMode;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   481
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   482
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
   483
        Startup startup() {
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   484
            return initialStartup;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   485
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   486
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   487
        @Override
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   488
        void msg(String key, Object... args) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   489
            startmsg(key, args);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   490
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   491
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   492
        @Override
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   493
        Options parse(OptionSet options) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   494
            if (options.has(argHelp)) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   495
                printUsage();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   496
                return null;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   497
            }
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 43274
diff changeset
   498
            if (options.has(argHelpExtra)) {
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   499
                printUsageX();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   500
                return null;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   501
            }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   502
            if (options.has(argVersion)) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   503
                cmdout.printf("jshell %s\n", version());
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   504
                return null;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   505
            }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   506
            if (options.has(argFullVersion)) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   507
                cmdout.printf("jshell %s\n", fullVersion());
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   508
                return null;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   509
            }
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 43274
diff changeset
   510
            if (options.has(argShowVersion)) {
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 43274
diff changeset
   511
                cmdout.printf("jshell %s\n", version());
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 43274
diff changeset
   512
            }
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   513
            if ((options.valuesOf(argFeedback).size() +
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   514
                    (options.has(argQ) ? 1 : 0) +
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   515
                    (options.has(argS) ? 1 : 0) +
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   516
                    (options.has(argV) ? 1 : 0)) > 1) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   517
                msg("jshell.err.opt.feedback.one");
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   518
                return null;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   519
            } else if (options.has(argFeedback)) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   520
                feedbackMode = options.valueOf(argFeedback);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   521
            } else if (options.has("q")) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   522
                feedbackMode = "concise";
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   523
            } else if (options.has("s")) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   524
                feedbackMode = "silent";
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   525
            } else if (options.has("v")) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   526
                feedbackMode = "verbose";
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   527
            }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   528
            if (options.has(argStart)) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   529
                List<String> sts = options.valuesOf(argStart);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   530
                if (options.has("no-startup")) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   531
                    startmsg("jshell.err.opt.startup.conflict");
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   532
                    return null;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   533
                }
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
   534
                initialStartup = Startup.fromFileList(sts, "--startup", new InitMessageHandler());
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
   535
                if (initialStartup == null) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
   536
                    return null;
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   537
                }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   538
            } else if (options.has(argNoStart)) {
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
   539
                initialStartup = Startup.noStartup();
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   540
            } else {
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
   541
                String packedStartup = prefs.get(STARTUP_KEY);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
   542
                initialStartup = Startup.unpack(packedStartup, new InitMessageHandler());
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   543
            }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   544
            if (options.has(argExecution)) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   545
                executionControlSpec = options.valueOf(argExecution);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   546
            }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   547
            addOptions(OptionKind.TO_REMOTE_VM, options.valuesOf(argR));
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   548
            addOptions(OptionKind.TO_COMPILER, options.valuesOf(argC));
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   549
            return super.parse(options);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   550
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   551
    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   552
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   553
    /**
43564
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   554
     * Encapsulate a history of snippets and commands which can be replayed.
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   555
     */
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   556
    private static class ReplayableHistory {
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   557
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   558
        // the history
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   559
        private List<String> hist;
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   560
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   561
        // the length of the history as of last save
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   562
        private int lastSaved;
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   563
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   564
        private ReplayableHistory(List<String> hist) {
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   565
            this.hist = hist;
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   566
            this.lastSaved = 0;
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   567
        }
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   568
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   569
        // factory for empty histories
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   570
        static ReplayableHistory emptyHistory() {
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   571
            return new ReplayableHistory(new ArrayList<>());
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   572
        }
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   573
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   574
        // factory for history stored in persistent storage
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   575
        static ReplayableHistory fromPrevious(PersistentStorage prefs) {
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   576
            // Read replay history from last jshell session
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   577
            String prevReplay = prefs.get(REPLAY_RESTORE_KEY);
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   578
            if (prevReplay == null) {
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   579
                return null;
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   580
            } else {
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   581
                return new ReplayableHistory(Arrays.asList(prevReplay.split(RECORD_SEPARATOR)));
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   582
            }
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   583
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   584
        }
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   585
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   586
        // store the history in persistent storage
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   587
        void storeHistory(PersistentStorage prefs) {
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   588
            if (hist.size() > lastSaved) {
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   589
                // Prevent history overflow by calculating what will fit, starting
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   590
                // with most recent
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   591
                int sepLen = RECORD_SEPARATOR.length();
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   592
                int length = 0;
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   593
                int first = hist.size();
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   594
                while (length < Preferences.MAX_VALUE_LENGTH && --first >= 0) {
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   595
                    length += hist.get(first).length() + sepLen;
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   596
                }
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   597
                if (first >= 0) {
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   598
                    hist = hist.subList(first + 1, hist.size());
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   599
                }
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   600
                String shist = String.join(RECORD_SEPARATOR, hist);
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   601
                prefs.put(REPLAY_RESTORE_KEY, shist);
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   602
                markSaved();
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   603
            }
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   604
            prefs.flush();
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   605
        }
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   606
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   607
        // add a snippet or command to the history
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   608
        void add(String s) {
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   609
            hist.add(s);
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   610
        }
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   611
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   612
        // return history to reloaded
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   613
        Iterable<String> iterable() {
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   614
            return hist;
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   615
        }
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   616
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   617
        // mark that persistent storage and current history are in sync
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   618
        void markSaved() {
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   619
            lastSaved = hist.size();
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   620
        }
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   621
    }
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   622
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   623
    /**
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   624
     * Is the input/output currently interactive
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   625
     *
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   626
     * @return true if console
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   627
     */
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   628
    boolean interactive() {
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   629
        return input != null && input.interactiveOutput();
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   630
    }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   631
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   632
    void debug(String format, Object... args) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   633
        if (debug) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   634
            cmderr.printf(format + "\n", args);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   635
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   636
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   637
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   638
    /**
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   639
     * Base output for command output -- no pre- or post-fix
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   640
     *
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   641
     * @param printf format
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   642
     * @param printf args
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   643
     */
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   644
    void rawout(String format, Object... args) {
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   645
        cmdout.printf(format, args);
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   646
    }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   647
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   648
    /**
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   649
     * Must show command output
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   650
     *
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   651
     * @param format printf format
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   652
     * @param args printf args
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   653
     */
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   654
    @Override
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   655
    public void hard(String format, Object... args) {
42412
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   656
        rawout(prefix(format), args);
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   657
    }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   658
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   659
    /**
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   660
     * Error command output
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   661
     *
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   662
     * @param format printf format
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   663
     * @param args printf args
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   664
     */
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   665
    void error(String format, Object... args) {
42412
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   666
        rawout(prefixError(format), args);
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   667
    }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   668
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   669
    /**
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   670
     * Should optional informative be displayed?
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   671
     * @return true if they should be displayed
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   672
     */
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   673
    @Override
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   674
    public boolean showFluff() {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   675
        return feedback.shouldDisplayCommandFluff() && interactive();
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   676
    }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   677
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   678
    /**
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   679
     * Optional output
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   680
     *
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   681
     * @param format printf format
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   682
     * @param args printf args
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   683
     */
37745
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
   684
    @Override
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
   685
    public void fluff(String format, Object... args) {
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   686
        if (showFluff()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   687
            hard(format, args);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   688
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   689
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   690
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   691
    /**
41934
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
   692
     * Resource bundle look-up
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   693
     *
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   694
     * @param key the resource key
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   695
     */
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   696
    String getResourceString(String key) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   697
        if (outputRB == null) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   698
            try {
36992
ddebebe611a1 8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents: 36990
diff changeset
   699
                outputRB = ResourceBundle.getBundle(L10N_RB_NAME, locale);
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   700
            } catch (MissingResourceException mre) {
36992
ddebebe611a1 8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents: 36990
diff changeset
   701
                error("Cannot find ResourceBundle: %s for locale: %s", L10N_RB_NAME, locale);
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   702
                return "";
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   703
            }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   704
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   705
        String s;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   706
        try {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   707
            s = outputRB.getString(key);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   708
        } catch (MissingResourceException mre) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   709
            error("Missing resource: %s in %s", key, L10N_RB_NAME);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   710
            return "";
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   711
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   712
        return s;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   713
    }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   714
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   715
    /**
42412
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   716
     * Add normal prefixing/postfixing to embedded newlines in a string,
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   717
     * bracketing with normal prefix/postfix
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   718
     *
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   719
     * @param s the string to prefix
42412
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   720
     * @return the pre/post-fixed and bracketed string
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   721
     */
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   722
    String prefix(String s) {
42412
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   723
         return prefix(s, feedback.getPre(), feedback.getPost());
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   724
    }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   725
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   726
    /**
42412
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   727
     * Add error prefixing/postfixing to embedded newlines in a string,
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   728
     * bracketing with error prefix/postfix
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   729
     *
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   730
     * @param s the string to prefix
42412
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   731
     * @return the pre/post-fixed and bracketed string
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   732
     */
42412
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   733
    String prefixError(String s) {
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   734
         return prefix(s, feedback.getErrorPre(), feedback.getErrorPost());
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   735
    }
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   736
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   737
    /**
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   738
     * Add prefixing/postfixing to embedded newlines in a string,
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   739
     * bracketing with prefix/postfix
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   740
     *
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   741
     * @param s the string to prefix
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   742
     * @param pre the string to prepend to each line
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   743
     * @param post the string to append to each line (replacing newline)
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   744
     * @return the pre/post-fixed and bracketed string
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   745
     */
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   746
    String prefix(String s, String pre, String post) {
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   747
        if (s == null) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   748
            return "";
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   749
        }
42412
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   750
        String pp = s.replaceAll("\\R", post + pre);
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
   751
        if (pp.endsWith(post + pre)) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
   752
            // prevent an extra prefix char and blank line when the string
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
   753
            // already terminates with newline
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
   754
            pp = pp.substring(0, pp.length() - (post + pre).length());
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
   755
        }
42412
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   756
        return pre + pp + post;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   757
    }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   758
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   759
    /**
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   760
     * Print using resource bundle look-up and adding prefix and postfix
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   761
     *
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   762
     * @param key the resource key
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   763
     */
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   764
    void hardrb(String key) {
42412
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   765
        hard(getResourceString(key));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   766
    }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   767
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   768
    /**
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   769
     * Format using resource bundle look-up using MessageFormat
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   770
     *
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   771
     * @param key the resource key
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   772
     * @param args
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   773
     */
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   774
    String messageFormat(String key, Object... args) {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   775
        String rs = getResourceString(key);
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   776
        return MessageFormat.format(rs, args);
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   777
    }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   778
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   779
    /**
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   780
     * Print using resource bundle look-up, MessageFormat, and add prefix and
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   781
     * postfix
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   782
     *
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   783
     * @param key the resource key
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   784
     * @param args
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   785
     */
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   786
    @Override
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   787
    public void hardmsg(String key, Object... args) {
42412
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   788
        hard(messageFormat(key, args));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   789
    }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   790
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   791
    /**
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   792
     * Print error using resource bundle look-up, MessageFormat, and add prefix
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   793
     * and postfix
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   794
     *
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   795
     * @param key the resource key
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   796
     * @param args
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   797
     */
37745
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
   798
    @Override
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
   799
    public void errormsg(String key, Object... args) {
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
   800
        if (isRunningInteractive()) {
42412
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   801
            rawout(prefixError(messageFormat(key, args)));
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
   802
        } else {
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
   803
            startmsg(key, args);
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
   804
        }
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   805
    }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   806
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   807
    /**
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   808
     * Print command-line error using resource bundle look-up, MessageFormat
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   809
     *
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   810
     * @param key the resource key
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   811
     * @param args
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   812
     */
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   813
    void startmsg(String key, Object... args) {
42412
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   814
        cmderr.println(messageFormat(key, args));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   815
    }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   816
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   817
    /**
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   818
     * Print (fluff) using resource bundle look-up, MessageFormat, and add
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   819
     * prefix and postfix
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   820
     *
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   821
     * @param key the resource key
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   822
     * @param args
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   823
     */
37745
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
   824
    @Override
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
   825
    public void fluffmsg(String key, Object... args) {
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   826
        if (showFluff()) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   827
            hardmsg(key, args);
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   828
        }
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   829
    }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   830
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   831
    <T> void hardPairs(Stream<T> stream, Function<T, String> a, Function<T, String> b) {
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   832
        Map<String, String> a2b = stream.collect(toMap(a, b,
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   833
                (m1, m2) -> m1,
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
   834
                LinkedHashMap::new));
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   835
        for (Entry<String, String> e : a2b.entrySet()) {
42412
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   836
            hard("%s", e.getKey());
ca6f4f1914b2 8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents: 42265
diff changeset
   837
            rawout(prefix(e.getValue(), feedback.getPre() + "\t", feedback.getPost()));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   838
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   839
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   840
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   841
    /**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   842
     * Trim whitespace off end of string
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   843
     *
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   844
     * @param s
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   845
     * @return
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   846
     */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   847
    static String trimEnd(String s) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   848
        int last = s.length() - 1;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   849
        int i = last;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   850
        while (i >= 0 && Character.isWhitespace(s.charAt(i))) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   851
            --i;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   852
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   853
        if (i != last) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   854
            return s.substring(0, i + 1);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   855
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   856
            return s;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   857
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   858
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   859
43757
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   860
    /**
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   861
     * The entry point into the JShell tool.
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   862
     *
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   863
     * @param args the command-line arguments
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   864
     * @throws Exception catastrophic fatal exception
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   865
     */
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   866
    public void start(String[] args) throws Exception {
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   867
        OptionParserCommandLine commandLineArgs = new OptionParserCommandLine();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   868
        options = commandLineArgs.parse(args);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   869
        if (options == null) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   870
            // Abort
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   871
            return;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   872
        }
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   873
        startup = commandLineArgs.startup();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   874
        // initialize editor settings
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   875
        configEditor();
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   876
        // initialize JShell instance
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   877
        resetState();
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   878
        // Read replay history from last jshell session into previous history
43564
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   879
        replayableHistoryPrevious = ReplayableHistory.fromPrevious(prefs);
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   880
        // load snippet/command files given on command-line
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   881
        for (String loadFile : commandLineArgs.nonOptions()) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   882
            runFile(loadFile, "jshell");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   883
        }
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   884
        // if we survived that...
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   885
        if (regenerateOnDeath) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   886
            // initialize the predefined feedback modes
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   887
            initFeedback(commandLineArgs.feedbackMode());
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   888
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   889
        // check again, as feedback setting could have failed
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   890
        if (regenerateOnDeath) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   891
            // if we haven't died, and the feedback mode wants fluff, print welcome
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   892
            if (feedback.shouldDisplayCommandFluff()) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   893
                hardmsg("jshell.msg.welcome", version());
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   894
            }
43564
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   895
            // Be sure history is always saved so that user code isn't lost
43757
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   896
            Thread shutdownHook = new Thread() {
43564
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   897
                @Override
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   898
                public void run() {
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   899
                    replayableHistory.storeHistory(prefs);
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   900
                }
43757
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   901
            };
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   902
            Runtime.getRuntime().addShutdownHook(shutdownHook);
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   903
            // execute from user input
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   904
            try (IOContext in = new ConsoleIOContext(this, cmdin, console)) {
43757
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   905
                while (regenerateOnDeath) {
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   906
                    if (!live) {
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   907
                        resetState();
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   908
                    }
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   909
                    run(in);
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   910
                }
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   911
            } finally {
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   912
                replayableHistory.storeHistory(prefs);
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   913
                closeState();
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   914
                try {
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   915
                    Runtime.getRuntime().removeShutdownHook(shutdownHook);
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   916
                } catch (Exception ex) {
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   917
                    // ignore, this probably caused by VM aready being shutdown
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   918
                    // and this is the last act anyhow
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   919
                }
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   920
            }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   921
        }
43757
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
   922
        closeState();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   923
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   924
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   925
    private EditorSetting configEditor() {
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   926
        // Read retained editor setting (if any)
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   927
        editor = EditorSetting.fromPrefs(prefs);
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   928
        if (editor != null) {
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   929
            return editor;
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   930
        }
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   931
        // Try getting editor setting from OS environment variables
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   932
        for (String envvar : EDITOR_ENV_VARS) {
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   933
            String v = envvars.get(envvar);
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   934
            if (v != null) {
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   935
                return editor = new EditorSetting(v.split("\\s+"), false);
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   936
            }
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   937
        }
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   938
        // Default to the built-in editor
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   939
        return editor = BUILT_IN_EDITOR;
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   940
    }
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
   941
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   942
    private void printUsage() {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   943
        cmdout.print(getResourceString("help.usage"));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   944
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   945
41248
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   946
    private void printUsageX() {
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   947
        cmdout.print(getResourceString("help.usage.x"));
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   948
    }
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   949
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   950
    /**
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   951
     * Message handler to use during initial start-up.
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   952
     */
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   953
    private class InitMessageHandler implements MessageHandler {
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   954
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   955
        @Override
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   956
        public void fluff(String format, Object... args) {
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   957
            //ignore
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   958
        }
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   959
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   960
        @Override
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   961
        public void fluffmsg(String messageKey, Object... args) {
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   962
            //ignore
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   963
        }
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   964
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   965
        @Override
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   966
        public void hard(String format, Object... args) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   967
            //ignore
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   968
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   969
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   970
        @Override
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   971
        public void hardmsg(String messageKey, Object... args) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   972
            //ignore
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   973
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   974
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   975
        @Override
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   976
        public void errormsg(String messageKey, Object... args) {
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   977
            startmsg(messageKey, args);
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   978
        }
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   979
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   980
        @Override
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   981
        public boolean showFluff() {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   982
            return false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   983
        }
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   984
    }
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   985
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   986
    private void resetState() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   987
        closeState();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   988
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   989
        // Initialize tool id mapping
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   990
        mainNamespace = new NameSpace("main", "");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   991
        startNamespace = new NameSpace("start", "s");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   992
        errorNamespace = new NameSpace("error", "e");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   993
        mapSnippet = new LinkedHashMap<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   994
        currentNameSpace = startNamespace;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   995
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   996
        // Reset the replayable history, saving the old for restore
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   997
        replayableHistoryPrevious = replayableHistory;
43564
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
   998
        replayableHistory = ReplayableHistory.emptyHistory();
42969
a48d4f74d322 8168615: JShell API: jdk.jshell.spi should be a pluggable ServiceLoader SPI
rfield
parents: 42843
diff changeset
   999
        JShell.Builder builder =
a48d4f74d322 8168615: JShell API: jdk.jshell.spi should be a pluggable ServiceLoader SPI
rfield
parents: 42843
diff changeset
  1000
               JShell.builder()
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1001
                .in(userin)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1002
                .out(userout)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1003
                .err(usererr)
42969
a48d4f74d322 8168615: JShell API: jdk.jshell.spi should be a pluggable ServiceLoader SPI
rfield
parents: 42843
diff changeset
  1004
                .tempVariableNameGenerator(() -> "$" + currentNameSpace.tidNext())
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  1005
                .idGenerator((sn, i) -> (currentNameSpace == startNamespace || state.status(sn).isActive())
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1006
                        ? currentNameSpace.tid(sn)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1007
                        : errorNamespace.tid(sn))
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1008
                .remoteVMOptions(options.remoteVmOptions())
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1009
                .compilerOptions(options.compilerOptions());
42969
a48d4f74d322 8168615: JShell API: jdk.jshell.spi should be a pluggable ServiceLoader SPI
rfield
parents: 42843
diff changeset
  1010
        if (executionControlSpec != null) {
a48d4f74d322 8168615: JShell API: jdk.jshell.spi should be a pluggable ServiceLoader SPI
rfield
parents: 42843
diff changeset
  1011
            builder.executionEngine(executionControlSpec);
a48d4f74d322 8168615: JShell API: jdk.jshell.spi should be a pluggable ServiceLoader SPI
rfield
parents: 42843
diff changeset
  1012
        }
a48d4f74d322 8168615: JShell API: jdk.jshell.spi should be a pluggable ServiceLoader SPI
rfield
parents: 42843
diff changeset
  1013
        state = builder.build();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1014
        shutdownSubscription = state.onShutdown((JShell deadState) -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1015
            if (deadState == state) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1016
                hardmsg("jshell.msg.terminated");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1017
                live = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1018
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1019
        });
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1020
        analysis = state.sourceCodeAnalysis();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1021
        live = true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1022
44065
d0a8a4c41c85 8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents: 43856
diff changeset
  1023
        // Run the start-up script.
d0a8a4c41c85 8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents: 43856
diff changeset
  1024
        // Avoid an infinite loop running start-up while running start-up.
d0a8a4c41c85 8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents: 43856
diff changeset
  1025
        // This could, otherwise, occur when /env /reset or /reload commands are
d0a8a4c41c85 8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents: 43856
diff changeset
  1026
        // in the start-up script.
d0a8a4c41c85 8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents: 43856
diff changeset
  1027
        if (!isCurrentlyRunningStartup) {
d0a8a4c41c85 8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents: 43856
diff changeset
  1028
            try {
d0a8a4c41c85 8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents: 43856
diff changeset
  1029
                isCurrentlyRunningStartup = true;
d0a8a4c41c85 8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents: 43856
diff changeset
  1030
                startUpRun(startup.toString());
d0a8a4c41c85 8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents: 43856
diff changeset
  1031
            } finally {
d0a8a4c41c85 8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents: 43856
diff changeset
  1032
                isCurrentlyRunningStartup = false;
d0a8a4c41c85 8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents: 43856
diff changeset
  1033
            }
d0a8a4c41c85 8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents: 43856
diff changeset
  1034
        }
d0a8a4c41c85 8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents: 43856
diff changeset
  1035
        // Record subsequent snippets in the main namespace.
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1036
        currentNameSpace = mainNamespace;
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1037
    }
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1038
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
  1039
    private boolean isRunningInteractive() {
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
  1040
        return currentNameSpace != null && currentNameSpace == mainNamespace;
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
  1041
    }
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
  1042
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1043
    //where -- one-time per run initialization of feedback modes
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1044
    private void initFeedback(String initMode) {
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1045
        // No fluff, no prefix, for init failures
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1046
        MessageHandler initmh = new InitMessageHandler();
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1047
        // Execute the feedback initialization code in the resource file
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1048
        startUpRun(getResourceString("startup.feedback"));
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1049
        // These predefined modes are read-only
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1050
        feedback.markModesReadOnly();
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1051
        // Restore user defined modes retained on previous run with /set mode -retain
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
  1052
        String encoded = prefs.get(MODE_KEY);
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1053
        if (encoded != null && !encoded.isEmpty()) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1054
            if (!feedback.restoreEncodedModes(initmh, encoded)) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1055
                // Catastrophic corruption -- remove the retained modes
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1056
                prefs.remove(MODE_KEY);
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1057
            }
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1058
        }
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1059
        if (initMode != null) {
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1060
            // The feedback mode to use was specified on the command line, use it
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1061
            if (!setFeedback(initmh, new ArgTokenizer("--feedback", initMode))) {
37745
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
  1062
                regenerateOnDeath = false;
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
  1063
            }
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1064
        } else {
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
  1065
            String fb = prefs.get(FEEDBACK_KEY);
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1066
            if (fb != null) {
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1067
                // Restore the feedback mode to use that was retained
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1068
                // on a previous run with /set feedback -retain
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1069
                setFeedback(initmh, new ArgTokenizer("previous retain feedback", "-retain " + fb));
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1070
            }
37745
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
  1071
        }
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1072
    }
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1073
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1074
    //where
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1075
    private void startUpRun(String start) {
42972
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  1076
        try (IOContext suin = new ScannerIOContext(new StringReader(start))) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1077
            run(suin);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1078
        } catch (Exception ex) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1079
            hardmsg("jshell.err.startup.unexpected.exception", ex);
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1080
            ex.printStackTrace(cmdout);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1081
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1082
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1083
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1084
    private void closeState() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1085
        live = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1086
        JShell oldState = state;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1087
        if (oldState != null) {
43757
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
  1088
            state = null;
7193f6ef25db 8173893: JShell: reduce memory leaks
rfield
parents: 43564
diff changeset
  1089
            analysis = null;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1090
            oldState.unsubscribe(shutdownSubscription); // No notification
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1091
            oldState.close();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1092
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1093
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1094
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1095
    /**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1096
     * Main loop
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1097
     * @param in the line input/editing context
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1098
     */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1099
    private void run(IOContext in) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1100
        IOContext oldInput = input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1101
        input = in;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1102
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1103
            String incomplete = "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1104
            while (live) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1105
                String prompt;
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
  1106
                if (isRunningInteractive()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1107
                    prompt = testPrompt
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1108
                                    ? incomplete.isEmpty()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1109
                                            ? "\u0005" //ENQ
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1110
                                            : "\u0006" //ACK
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1111
                                    : incomplete.isEmpty()
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1112
                                            ? feedback.getPrompt(currentNameSpace.tidNext())
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1113
                                            : feedback.getContinuationPrompt(currentNameSpace.tidNext())
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1114
                    ;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1115
                } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1116
                    prompt = "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1117
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1118
                String raw;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1119
                try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1120
                    raw = in.readLine(prompt, incomplete);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1121
                } catch (InputInterruptedException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1122
                    //input interrupted - clearing current state
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1123
                    incomplete = "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1124
                    continue;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1125
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1126
                if (raw == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1127
                    //EOF
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1128
                    if (in.interactiveOutput()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1129
                        // End after user ctrl-D
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1130
                        regenerateOnDeath = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1131
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1132
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1133
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1134
                String trimmed = trimEnd(raw);
43274
6b5ed4f9fb81 8173073: jshell tool: blank lines removed from multi-line snippets
rfield
parents: 43264
diff changeset
  1135
                if (!trimmed.isEmpty() || !incomplete.isEmpty()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1136
                    String line = incomplete + trimmed;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1137
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1138
                    // No commands in the middle of unprocessed source
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1139
                    if (incomplete.isEmpty() && line.startsWith("/") && !line.startsWith("//") && !line.startsWith("/*")) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1140
                        processCommand(line.trim());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1141
                    } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1142
                        incomplete = processSourceCatchingReset(line);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1143
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1144
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1145
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1146
        } catch (IOException ex) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1147
            errormsg("jshell.err.unexpected.exception", ex);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1148
        } finally {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1149
            input = oldInput;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1150
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1151
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1152
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1153
    private void addToReplayHistory(String s) {
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
  1154
        if (isRunningInteractive()) {
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1155
            replayableHistory.add(s);
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1156
        }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1157
    }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1158
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1159
    private String processSourceCatchingReset(String src) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1160
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1161
            input.beforeUserCode();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1162
            return processSource(src);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1163
        } catch (IllegalStateException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1164
            hard("Resetting...");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1165
            live = false; // Make double sure
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1166
            return "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1167
        } finally {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1168
            input.afterUserCode();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1169
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1170
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1171
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1172
    private void processCommand(String cmd) {
34477
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  1173
        if (cmd.startsWith("/-")) {
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  1174
            try {
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  1175
                //handle "/-[number]"
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  1176
                cmdUseHistoryEntry(Integer.parseInt(cmd.substring(1)));
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  1177
                return ;
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  1178
            } catch (NumberFormatException ex) {
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  1179
                //ignore
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  1180
            }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1181
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1182
        String arg = "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1183
        int idx = cmd.indexOf(' ');
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1184
        if (idx > 0) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1185
            arg = cmd.substring(idx + 1).trim();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1186
            cmd = cmd.substring(0, idx);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1187
        }
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1188
        Command[] candidates = findCommand(cmd, c -> c.kind.isRealCommand);
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1189
        switch (candidates.length) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1190
            case 0:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1191
                if (!rerunHistoryEntryById(cmd.substring(1))) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1192
                    errormsg("jshell.err.no.such.command.or.snippet.id", cmd);
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1193
                    fluffmsg("jshell.msg.help.for.help");
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1194
                }   break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1195
            case 1:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1196
                Command command = candidates[0];
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1197
                // If comand was successful and is of a replayable kind, add it the replayable history
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1198
                if (command.run.apply(arg) && command.kind == CommandKind.REPLAY) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1199
                    addToReplayHistory((command.command + " " + arg).trim());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1200
                }   break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1201
            default:
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1202
                errormsg("jshell.err.command.ambiguous", cmd,
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1203
                        Arrays.stream(candidates).map(c -> c.command).collect(Collectors.joining(", ")));
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1204
                fluffmsg("jshell.msg.help.for.help");
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1205
                break;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1206
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1207
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1208
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
  1209
    private Command[] findCommand(String cmd, Predicate<Command> filter) {
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
  1210
        Command exact = commands.get(cmd);
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
  1211
        if (exact != null)
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
  1212
            return new Command[] {exact};
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
  1213
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
  1214
        return commands.values()
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
  1215
                       .stream()
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
  1216
                       .filter(filter)
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
  1217
                       .filter(command -> command.command.startsWith(cmd))
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  1218
                       .toArray(Command[]::new);
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
  1219
    }
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
  1220
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  1221
    static Path toPathResolvingUserHome(String pathString) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1222
        if (pathString.replace(File.separatorChar, '/').startsWith("~/"))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1223
            return Paths.get(System.getProperty("user.home"), pathString.substring(2));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1224
        else
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1225
            return Paths.get(pathString);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1226
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1227
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1228
    static final class Command {
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
  1229
        public final String command;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1230
        public final String helpKey;
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1231
        public final Function<String,Boolean> run;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1232
        public final CompletionProvider completions;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1233
        public final CommandKind kind;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1234
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1235
        // NORMAL Commands
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1236
        public Command(String command, Function<String,Boolean> run, CompletionProvider completions) {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1237
            this(command, run, completions, CommandKind.NORMAL);
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1238
        }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1239
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1240
        // Special kinds of Commands
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1241
        public Command(String command, Function<String,Boolean> run, CompletionProvider completions, CommandKind kind) {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1242
            this(command, "help." + command.substring(1),
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1243
                    run, completions, kind);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1244
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1245
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1246
        // Documentation pseudo-commands
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1247
        public Command(String command, String helpKey, CommandKind kind) {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1248
            this(command, helpKey,
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1249
                    arg -> { throw new IllegalStateException(); },
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1250
                    EMPTY_COMPLETION_PROVIDER,
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1251
                    kind);
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1252
        }
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1253
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1254
        public Command(String command, String helpKey, Function<String,Boolean> run, CompletionProvider completions, CommandKind kind) {
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
  1255
            this.command = command;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1256
            this.helpKey = helpKey;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1257
            this.run = run;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1258
            this.completions = completions;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1259
            this.kind = kind;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1260
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1261
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1262
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1263
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1264
    interface CompletionProvider {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1265
        List<Suggestion> completionSuggestions(String input, int cursor, int[] anchor);
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1266
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1267
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1268
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1269
    enum CommandKind {
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1270
        NORMAL(true, true, true),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1271
        REPLAY(true, true, true),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1272
        HIDDEN(true, false, false),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1273
        HELP_ONLY(false, true, false),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1274
        HELP_SUBJECT(false, false, false);
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1275
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1276
        final boolean isRealCommand;
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1277
        final boolean showInHelp;
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1278
        final boolean shouldSuggestCompletions;
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1279
        private CommandKind(boolean isRealCommand, boolean showInHelp, boolean shouldSuggestCompletions) {
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1280
            this.isRealCommand = isRealCommand;
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1281
            this.showInHelp = showInHelp;
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1282
            this.shouldSuggestCompletions = shouldSuggestCompletions;
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1283
        }
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1284
    }
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1285
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1286
    static final class FixedCompletionProvider implements CompletionProvider {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1287
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1288
        private final String[] alternatives;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1289
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1290
        public FixedCompletionProvider(String... alternatives) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1291
            this.alternatives = alternatives;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1292
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1293
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1294
        // Add more options to an existing provider
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1295
        public FixedCompletionProvider(FixedCompletionProvider base, String... alternatives) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1296
            List<String> l = new ArrayList<>(Arrays.asList(base.alternatives));
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1297
            l.addAll(Arrays.asList(alternatives));
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1298
            this.alternatives = l.toArray(new String[l.size()]);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1299
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1300
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1301
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1302
        public List<Suggestion> completionSuggestions(String input, int cursor, int[] anchor) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1303
            List<Suggestion> result = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1304
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1305
            for (String alternative : alternatives) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1306
                if (alternative.startsWith(input)) {
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  1307
                    result.add(new ArgSuggestion(alternative));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1308
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1309
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1310
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1311
            anchor[0] = 0;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1312
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1313
            return result;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1314
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1315
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1316
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1317
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1318
    static final CompletionProvider EMPTY_COMPLETION_PROVIDER = new FixedCompletionProvider();
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1319
    private static final CompletionProvider SNIPPET_HISTORY_OPTION_COMPLETION_PROVIDER = new FixedCompletionProvider("-all", "-start ", "-history");
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1320
    private static final CompletionProvider SAVE_OPTION_COMPLETION_PROVIDER = new FixedCompletionProvider("-all ", "-start ", "-history ");
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1321
    private static final CompletionProvider SNIPPET_OPTION_COMPLETION_PROVIDER = new FixedCompletionProvider("-all", "-start " );
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1322
    private static final FixedCompletionProvider COMMAND_LINE_LIKE_OPTIONS_COMPLETION_PROVIDER = new FixedCompletionProvider(
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1323
            "-class-path ", "-module-path ", "-add-modules ", "-add-exports ");
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1324
    private static final CompletionProvider RELOAD_OPTIONS_COMPLETION_PROVIDER = new FixedCompletionProvider(
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1325
            COMMAND_LINE_LIKE_OPTIONS_COMPLETION_PROVIDER,
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1326
            "-restore ", "-quiet ");
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1327
    private static final CompletionProvider SET_MODE_OPTIONS_COMPLETION_PROVIDER = new FixedCompletionProvider("-command", "-quiet", "-delete");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1328
    private static final CompletionProvider FILE_COMPLETION_PROVIDER = fileCompletions(p -> true);
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1329
    private static final Map<String, CompletionProvider> ARG_OPTIONS = new HashMap<>();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1330
    static {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1331
        ARG_OPTIONS.put("-class-path", classPathCompletion());
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1332
        ARG_OPTIONS.put("-module-path", fileCompletions(Files::isDirectory));
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1333
        ARG_OPTIONS.put("-add-modules", EMPTY_COMPLETION_PROVIDER);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1334
        ARG_OPTIONS.put("-add-exports", EMPTY_COMPLETION_PROVIDER);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1335
    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1336
    private final Map<String, Command> commands = new LinkedHashMap<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1337
    private void registerCommand(Command cmd) {
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
  1338
        commands.put(cmd.command, cmd);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1339
    }
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1340
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1341
    private static CompletionProvider skipWordThenCompletion(CompletionProvider completionProvider) {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1342
        return (input, cursor, anchor) -> {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1343
            List<Suggestion> result = Collections.emptyList();
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1344
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1345
            int space = input.indexOf(' ');
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1346
            if (space != -1) {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1347
                String rest = input.substring(space + 1);
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1348
                result = completionProvider.completionSuggestions(rest, cursor - space - 1, anchor);
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1349
                anchor[0] += space + 1;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1350
            }
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1351
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1352
            return result;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1353
        };
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1354
    }
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1355
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1356
    private static CompletionProvider fileCompletions(Predicate<Path> accept) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1357
        return (code, cursor, anchor) -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1358
            int lastSlash = code.lastIndexOf('/');
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1359
            String path = code.substring(0, lastSlash + 1);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1360
            String prefix = lastSlash != (-1) ? code.substring(lastSlash + 1) : code;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1361
            Path current = toPathResolvingUserHome(path);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1362
            List<Suggestion> result = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1363
            try (Stream<Path> dir = Files.list(current)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1364
                dir.filter(f -> accept.test(f) && f.getFileName().toString().startsWith(prefix))
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  1365
                   .map(f -> new ArgSuggestion(f.getFileName() + (Files.isDirectory(f) ? "/" : "")))
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1366
                   .forEach(result::add);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1367
            } catch (IOException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1368
                //ignore...
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1369
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1370
            if (path.isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1371
                StreamSupport.stream(FileSystems.getDefault().getRootDirectories().spliterator(), false)
42415
85388bae723d 8165564: langtools\test\jdk\jshell\CommandCompletionTest.java fails on some windows
jlahoda
parents: 42412
diff changeset
  1372
                             .filter(root -> Files.exists(root))
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1373
                             .filter(root -> accept.test(root) && root.toString().startsWith(prefix))
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  1374
                             .map(root -> new ArgSuggestion(root.toString()))
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1375
                             .forEach(result::add);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1376
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1377
            anchor[0] = path.length();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1378
            return result;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1379
        };
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1380
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1381
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1382
    private static CompletionProvider classPathCompletion() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1383
        return fileCompletions(p -> Files.isDirectory(p) ||
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1384
                                    p.getFileName().toString().endsWith(".zip") ||
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1385
                                    p.getFileName().toString().endsWith(".jar"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1386
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1387
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1388
    // Completion based on snippet supplier
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1389
    private CompletionProvider snippetCompletion(Supplier<Stream<? extends Snippet>> snippetsSupplier) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1390
        return (prefix, cursor, anchor) -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1391
            anchor[0] = 0;
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1392
            int space = prefix.lastIndexOf(' ');
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1393
            Set<String> prior = new HashSet<>(Arrays.asList(prefix.split(" ")));
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1394
            if (prior.contains("-all") || prior.contains("-history")) {
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1395
                return Collections.emptyList();
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1396
            }
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1397
            String argPrefix = prefix.substring(space + 1);
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1398
            return snippetsSupplier.get()
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1399
                        .filter(k -> !prior.contains(String.valueOf(k.id()))
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1400
                                && (!(k instanceof DeclarationSnippet)
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1401
                                     || !prior.contains(((DeclarationSnippet) k).name())))
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1402
                        .flatMap(k -> (k instanceof DeclarationSnippet)
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1403
                                ? Stream.of(String.valueOf(k.id()) + " ", ((DeclarationSnippet) k).name() + " ")
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1404
                                : Stream.of(String.valueOf(k.id()) + " "))
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1405
                        .filter(k -> k.startsWith(argPrefix))
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  1406
                        .map(ArgSuggestion::new)
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1407
                        .collect(Collectors.toList());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1408
        };
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1409
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1410
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1411
    // Completion based on snippet supplier with -all -start (and sometimes -history) options
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1412
    private CompletionProvider snippetWithOptionCompletion(CompletionProvider optionProvider,
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1413
            Supplier<Stream<? extends Snippet>> snippetsSupplier) {
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1414
        return (code, cursor, anchor) -> {
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1415
            List<Suggestion> result = new ArrayList<>();
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1416
            int pastSpace = code.lastIndexOf(' ') + 1; // zero if no space
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1417
            if (pastSpace == 0) {
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1418
                result.addAll(optionProvider.completionSuggestions(code, cursor, anchor));
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1419
            }
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1420
            result.addAll(snippetCompletion(snippetsSupplier).completionSuggestions(code, cursor, anchor));
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1421
            anchor[0] += pastSpace;
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1422
            return result;
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1423
        };
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1424
    }
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1425
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1426
    // Completion of help, commands and subjects
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1427
    private CompletionProvider helpCompletion() {
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1428
        return (code, cursor, anchor) -> {
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1429
            List<Suggestion> result;
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1430
            int pastSpace = code.indexOf(' ') + 1; // zero if no space
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1431
            if (pastSpace == 0) {
44454
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  1432
                // initially suggest commands (with slash) and subjects,
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  1433
                // however, if their subject starts without slash, include
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  1434
                // commands without slash
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  1435
                boolean noslash = code.length() > 0 && !code.startsWith("/");
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1436
                result = new FixedCompletionProvider(commands.values().stream()
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1437
                        .filter(cmd -> cmd.kind.showInHelp || cmd.kind == CommandKind.HELP_SUBJECT)
44454
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  1438
                        .map(c -> ((noslash && c.command.startsWith("/"))
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  1439
                                ? c.command.substring(1)
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  1440
                                : c.command) + " ")
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  1441
                        .toArray(String[]::new))
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1442
                        .completionSuggestions(code, cursor, anchor);
44569
b1accf8b2aed 8178013: Finetuning of merged tab and shift tab completion
jlahoda
parents: 44459
diff changeset
  1443
            } else if (code.startsWith("/se") || code.startsWith("se")) {
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1444
                result = new FixedCompletionProvider(SET_SUBCOMMANDS)
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1445
                        .completionSuggestions(code.substring(pastSpace), cursor - pastSpace, anchor);
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1446
            } else {
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1447
                result = Collections.emptyList();
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1448
            }
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1449
            anchor[0] += pastSpace;
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1450
            return result;
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1451
        };
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1452
    }
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1453
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1454
    private static CompletionProvider saveCompletion() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1455
        return (code, cursor, anchor) -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1456
            List<Suggestion> result = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1457
            int space = code.indexOf(' ');
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1458
            if (space == (-1)) {
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1459
                result.addAll(SAVE_OPTION_COMPLETION_PROVIDER.completionSuggestions(code, cursor, anchor));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1460
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1461
            result.addAll(FILE_COMPLETION_PROVIDER.completionSuggestions(code.substring(space + 1), cursor - space - 1, anchor));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1462
            anchor[0] += space + 1;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1463
            return result;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1464
        };
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1465
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1466
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1467
    // command-line-like option completion -- options with values
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1468
    private static CompletionProvider optionCompletion(CompletionProvider provider) {
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1469
        return (code, cursor, anchor) -> {
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1470
            Matcher ovm = OPTION_VALUE_PATTERN.matcher(code);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1471
            if (ovm.matches()) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1472
                String flag = ovm.group("flag");
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1473
                List<CompletionProvider> ps = ARG_OPTIONS.entrySet().stream()
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1474
                        .filter(es -> es.getKey().startsWith(flag))
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1475
                        .map(es -> es.getValue())
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1476
                        .collect(toList());
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1477
                if (ps.size() == 1) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1478
                    int pastSpace = ovm.start("val");
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1479
                    List<Suggestion> result = ps.get(0).completionSuggestions(
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1480
                            ovm.group("val"), cursor - pastSpace, anchor);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1481
                    anchor[0] += pastSpace;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1482
                    return result;
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1483
                }
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1484
            }
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1485
            Matcher om = OPTION_PATTERN.matcher(code);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1486
            if (om.matches()) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1487
                int pastSpace = om.start("flag");
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1488
                List<Suggestion> result = provider.completionSuggestions(
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1489
                        om.group("flag"), cursor - pastSpace, anchor);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1490
                if (!om.group("dd").isEmpty()) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1491
                    result = result.stream()
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1492
                            .map(sug -> new Suggestion() {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1493
                                @Override
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1494
                                public String continuation() {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1495
                                    return "-" + sug.continuation();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1496
                                }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1497
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1498
                                @Override
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1499
                                public boolean matchesType() {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1500
                                    return false;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1501
                                }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1502
                            })
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1503
                            .collect(toList());
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1504
                    --pastSpace;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1505
                }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1506
                anchor[0] += pastSpace;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1507
                return result;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1508
            }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1509
            Matcher opp = OPTION_PRE_PATTERN.matcher(code);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1510
            if (opp.matches()) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1511
                int pastSpace = opp.end();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1512
                List<Suggestion> result = provider.completionSuggestions(
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1513
                        "", cursor - pastSpace, anchor);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1514
                anchor[0] += pastSpace;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1515
                return result;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1516
            }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1517
            return Collections.emptyList();
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1518
        };
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1519
    }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1520
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1521
    // /reload command completion
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1522
    private static CompletionProvider reloadCompletion() {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1523
        return optionCompletion(RELOAD_OPTIONS_COMPLETION_PROVIDER);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1524
    }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1525
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1526
    // /env command completion
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1527
    private static CompletionProvider envCompletion() {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1528
        return optionCompletion(COMMAND_LINE_LIKE_OPTIONS_COMPLETION_PROVIDER);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1529
    }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1530
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1531
    private static CompletionProvider orMostSpecificCompletion(
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1532
            CompletionProvider left, CompletionProvider right) {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1533
        return (code, cursor, anchor) -> {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1534
            int[] leftAnchor = {-1};
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1535
            int[] rightAnchor = {-1};
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1536
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1537
            List<Suggestion> leftSuggestions = left.completionSuggestions(code, cursor, leftAnchor);
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1538
            List<Suggestion> rightSuggestions = right.completionSuggestions(code, cursor, rightAnchor);
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1539
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1540
            List<Suggestion> suggestions = new ArrayList<>();
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1541
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1542
            if (leftAnchor[0] >= rightAnchor[0]) {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1543
                anchor[0] = leftAnchor[0];
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1544
                suggestions.addAll(leftSuggestions);
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1545
            }
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1546
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1547
            if (leftAnchor[0] <= rightAnchor[0]) {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1548
                anchor[0] = rightAnchor[0];
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1549
                suggestions.addAll(rightSuggestions);
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1550
            }
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1551
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1552
            return suggestions;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1553
        };
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1554
    }
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1555
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1556
    // Snippet lists
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1557
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1558
    Stream<Snippet> allSnippets() {
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1559
        return state.snippets();
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1560
    }
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1561
41514
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 41248
diff changeset
  1562
    Stream<Snippet> dropableSnippets() {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1563
        return state.snippets()
41514
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 41248
diff changeset
  1564
                .filter(sn -> state.status(sn).isActive());
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1565
    }
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1566
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1567
    Stream<VarSnippet> allVarSnippets() {
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1568
        return state.snippets()
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1569
                .filter(sn -> sn.kind() == Snippet.Kind.VAR)
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1570
                .map(sn -> (VarSnippet) sn);
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1571
    }
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1572
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1573
    Stream<MethodSnippet> allMethodSnippets() {
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1574
        return state.snippets()
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1575
                .filter(sn -> sn.kind() == Snippet.Kind.METHOD)
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1576
                .map(sn -> (MethodSnippet) sn);
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1577
    }
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1578
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1579
    Stream<TypeDeclSnippet> allTypeSnippets() {
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1580
        return state.snippets()
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1581
                .filter(sn -> sn.kind() == Snippet.Kind.TYPE_DECL)
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1582
                .map(sn -> (TypeDeclSnippet) sn);
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1583
    }
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1584
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1585
    // Table of commands -- with command forms, argument kinds, helpKey message, implementation, ...
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1586
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1587
    {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1588
        registerCommand(new Command("/list",
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  1589
                this::cmdList,
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1590
                snippetWithOptionCompletion(SNIPPET_HISTORY_OPTION_COMPLETION_PROVIDER,
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1591
                        this::allSnippets)));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1592
        registerCommand(new Command("/edit",
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  1593
                this::cmdEdit,
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1594
                snippetWithOptionCompletion(SNIPPET_OPTION_COMPLETION_PROVIDER,
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1595
                        this::allSnippets)));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1596
        registerCommand(new Command("/drop",
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  1597
                this::cmdDrop,
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1598
                snippetCompletion(this::dropableSnippets),
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1599
                CommandKind.REPLAY));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1600
        registerCommand(new Command("/save",
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  1601
                this::cmdSave,
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1602
                saveCompletion()));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1603
        registerCommand(new Command("/open",
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  1604
                this::cmdOpen,
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1605
                FILE_COMPLETION_PROVIDER));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1606
        registerCommand(new Command("/vars",
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  1607
                this::cmdVars,
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1608
                snippetWithOptionCompletion(SNIPPET_OPTION_COMPLETION_PROVIDER,
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1609
                        this::allVarSnippets)));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1610
        registerCommand(new Command("/methods",
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  1611
                this::cmdMethods,
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1612
                snippetWithOptionCompletion(SNIPPET_OPTION_COMPLETION_PROVIDER,
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1613
                        this::allMethodSnippets)));
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1614
        registerCommand(new Command("/types",
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  1615
                this::cmdTypes,
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1616
                snippetWithOptionCompletion(SNIPPET_OPTION_COMPLETION_PROVIDER,
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1617
                        this::allTypeSnippets)));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1618
        registerCommand(new Command("/imports",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1619
                arg -> cmdImports(),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1620
                EMPTY_COMPLETION_PROVIDER));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1621
        registerCommand(new Command("/exit",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1622
                arg -> cmdExit(),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1623
                EMPTY_COMPLETION_PROVIDER));
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1624
        registerCommand(new Command("/env",
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1625
                arg -> cmdEnv(arg),
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1626
                envCompletion()));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1627
        registerCommand(new Command("/reset",
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1628
                arg -> cmdReset(arg),
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1629
                envCompletion()));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1630
        registerCommand(new Command("/reload",
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  1631
                this::cmdReload,
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1632
                reloadCompletion()));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1633
        registerCommand(new Command("/history",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1634
                arg -> cmdHistory(),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1635
                EMPTY_COMPLETION_PROVIDER));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1636
        registerCommand(new Command("/debug",
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  1637
                this::cmdDebug,
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1638
                EMPTY_COMPLETION_PROVIDER,
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1639
                CommandKind.HIDDEN));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1640
        registerCommand(new Command("/help",
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  1641
                this::cmdHelp,
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1642
                helpCompletion()));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1643
        registerCommand(new Command("/set",
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  1644
                this::cmdSet,
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1645
                new ContinuousCompletionProvider(Map.of(
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1646
                        // need more completion for format for usability
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1647
                        "format", feedback.modeCompletions(),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1648
                        "truncation", feedback.modeCompletions(),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1649
                        "feedback", feedback.modeCompletions(),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1650
                        "mode", skipWordThenCompletion(orMostSpecificCompletion(
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1651
                                feedback.modeCompletions(SET_MODE_OPTIONS_COMPLETION_PROVIDER),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1652
                                SET_MODE_OPTIONS_COMPLETION_PROVIDER)),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1653
                        "prompt", feedback.modeCompletions(),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1654
                        "editor", fileCompletions(Files::isExecutable),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1655
                        "start", FILE_COMPLETION_PROVIDER),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1656
                        STARTSWITH_MATCHER)));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1657
        registerCommand(new Command("/?",
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1658
                "help.quest",
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  1659
                this::cmdHelp,
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 42259
diff changeset
  1660
                helpCompletion(),
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1661
                CommandKind.NORMAL));
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1662
        registerCommand(new Command("/!",
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1663
                "help.bang",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1664
                arg -> cmdUseHistoryEntry(-1),
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1665
                EMPTY_COMPLETION_PROVIDER,
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1666
                CommandKind.NORMAL));
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1667
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1668
        // Documentation pseudo-commands
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1669
        registerCommand(new Command("/<id>",
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1670
                "help.id",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1671
                CommandKind.HELP_ONLY));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1672
        registerCommand(new Command("/-<n>",
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1673
                "help.previous",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1674
                CommandKind.HELP_ONLY));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1675
        registerCommand(new Command("intro",
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1676
                "help.intro",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1677
                CommandKind.HELP_SUBJECT));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1678
        registerCommand(new Command("shortcuts",
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1679
                "help.shortcuts",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1680
                CommandKind.HELP_SUBJECT));
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1681
        registerCommand(new Command("context",
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1682
                "help.context",
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  1683
                CommandKind.HELP_SUBJECT));
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1684
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1685
        commandCompletions = new ContinuousCompletionProvider(
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1686
                commands.values().stream()
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1687
                        .filter(c -> c.kind.shouldSuggestCompletions)
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1688
                        .collect(toMap(c -> c.command, c -> c.completions)),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1689
                STARTSWITH_MATCHER);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1690
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1691
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1692
    private ContinuousCompletionProvider commandCompletions;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1693
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1694
    public List<Suggestion> commandCompletionSuggestions(String code, int cursor, int[] anchor) {
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1695
        return commandCompletions.completionSuggestions(code, cursor, anchor);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1696
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1697
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44454
diff changeset
  1698
    public List<String> commandDocumentation(String code, int cursor, boolean shortDescription) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1699
        code = code.substring(0, cursor);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1700
        int space = code.indexOf(' ');
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44454
diff changeset
  1701
        String prefix = space != (-1) ? code.substring(0, space) : code;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44454
diff changeset
  1702
        List<String> result = new ArrayList<>();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44454
diff changeset
  1703
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44454
diff changeset
  1704
        List<Entry<String, Command>> toShow =
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44454
diff changeset
  1705
                commands.entrySet()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44454
diff changeset
  1706
                        .stream()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44454
diff changeset
  1707
                        .filter(e -> e.getKey().startsWith(prefix))
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44454
diff changeset
  1708
                        .filter(e -> e.getValue().kind.showInHelp)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44454
diff changeset
  1709
                        .sorted((e1, e2) -> e1.getKey().compareTo(e2.getKey()))
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44454
diff changeset
  1710
                        .collect(Collectors.toList());
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44454
diff changeset
  1711
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44454
diff changeset
  1712
        if (toShow.size() == 1) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44454
diff changeset
  1713
            result.add(getResourceString(toShow.get(0).getValue().helpKey + (shortDescription ? ".summary" : "")));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44454
diff changeset
  1714
        } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44454
diff changeset
  1715
            for (Entry<String, Command> e : toShow) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44454
diff changeset
  1716
                result.add(e.getKey() + "\n" +getResourceString(e.getValue().helpKey + (shortDescription ? ".summary" : "")));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1717
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1718
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1719
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44454
diff changeset
  1720
        return result;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1721
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1722
44188
3f2047e62102 8176412: jshell tool: automatic imports are excluded on /reload causing it to fail
rfield
parents: 44065
diff changeset
  1723
    // Attempt to stop currently running evaluation
3f2047e62102 8176412: jshell tool: automatic imports are excluded on /reload causing it to fail
rfield
parents: 44065
diff changeset
  1724
    void stop() {
3f2047e62102 8176412: jshell tool: automatic imports are excluded on /reload causing it to fail
rfield
parents: 44065
diff changeset
  1725
        state.stop();
3f2047e62102 8176412: jshell tool: automatic imports are excluded on /reload causing it to fail
rfield
parents: 44065
diff changeset
  1726
    }
3f2047e62102 8176412: jshell tool: automatic imports are excluded on /reload causing it to fail
rfield
parents: 44065
diff changeset
  1727
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1728
    // --- Command implementations ---
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1729
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1730
    private static final String[] SET_SUBCOMMANDS = new String[]{
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1731
        "format", "truncation", "feedback", "mode", "prompt", "editor", "start"};
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1732
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1733
    final boolean cmdSet(String arg) {
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1734
        String cmd = "/set";
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1735
        ArgTokenizer at = new ArgTokenizer(cmd, arg.trim());
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1736
        String which = subCommand(cmd, at, SET_SUBCOMMANDS);
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1737
        if (which == null) {
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1738
            return false;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1739
        }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1740
        switch (which) {
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1741
            case "_retain": {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1742
                errormsg("jshell.err.setting.to.retain.must.be.specified", at.whole());
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1743
                return false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1744
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1745
            case "_blank": {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1746
                // show top-level settings
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1747
                new SetEditor().set();
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1748
                showSetStart();
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1749
                setFeedback(this, at); // no args so shows feedback setting
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1750
                hardmsg("jshell.msg.set.show.mode.settings");
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1751
                return true;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1752
            }
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1753
            case "format":
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1754
                return feedback.setFormat(this, at);
38513
0ae85633d035 8154812: jshell tool: value printing truncation
rfield
parents: 37745
diff changeset
  1755
            case "truncation":
0ae85633d035 8154812: jshell tool: value printing truncation
rfield
parents: 37745
diff changeset
  1756
                return feedback.setTruncation(this, at);
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1757
            case "feedback":
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1758
                return setFeedback(this, at);
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1759
            case "mode":
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1760
                return feedback.setMode(this, at,
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1761
                        retained -> prefs.put(MODE_KEY, retained));
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1762
            case "prompt":
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1763
                return feedback.setPrompt(this, at);
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1764
            case "editor":
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1765
                return new SetEditor(at).set();
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1766
            case "start":
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1767
                return setStart(at);
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1768
            default:
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1769
                errormsg("jshell.err.arg", cmd, at.val());
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1770
                return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1771
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1772
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1773
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1774
    boolean setFeedback(MessageHandler messageHandler, ArgTokenizer at) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1775
        return feedback.setFeedback(messageHandler, at,
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1776
                fb -> prefs.put(FEEDBACK_KEY, fb));
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1777
    }
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1778
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1779
    // Find which, if any, sub-command matches.
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1780
    // Return null on error
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1781
    String subCommand(String cmd, ArgTokenizer at, String[] subs) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1782
        at.allowedOptions("-retain");
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1783
        String sub = at.next();
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1784
        if (sub == null) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1785
            // No sub-command was given
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1786
            return at.hasOption("-retain")
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1787
                    ? "_retain"
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1788
                    : "_blank";
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1789
        }
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1790
        String[] matches = Arrays.stream(subs)
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1791
                .filter(s -> s.startsWith(sub))
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  1792
                .toArray(String[]::new);
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1793
        if (matches.length == 0) {
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1794
            // There are no matching sub-commands
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1795
            errormsg("jshell.err.arg", cmd, sub);
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1796
            fluffmsg("jshell.msg.use.one.of", Arrays.stream(subs)
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1797
                    .collect(Collectors.joining(", "))
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1798
            );
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1799
            return null;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1800
        }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1801
        if (matches.length > 1) {
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1802
            // More than one sub-command matches the initial characters provided
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1803
            errormsg("jshell.err.sub.ambiguous", cmd, sub);
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1804
            fluffmsg("jshell.msg.use.one.of", Arrays.stream(matches)
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1805
                    .collect(Collectors.joining(", "))
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1806
            );
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1807
            return null;
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1808
        }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1809
        return matches[0];
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1810
    }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1811
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1812
    static class EditorSetting {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1813
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1814
        static String BUILT_IN_REP = "-default";
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1815
        static char WAIT_PREFIX = '-';
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1816
        static char NORMAL_PREFIX = '*';
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1817
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1818
        final String[] cmd;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1819
        final boolean wait;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1820
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1821
        EditorSetting(String[] cmd, boolean wait) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1822
            this.wait = wait;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1823
            this.cmd = cmd;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1824
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1825
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1826
        // returns null if not stored in preferences
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
  1827
        static EditorSetting fromPrefs(PersistentStorage prefs) {
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1828
            // Read retained editor setting (if any)
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
  1829
            String editorString = prefs.get(EDITOR_KEY);
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1830
            if (editorString == null || editorString.isEmpty()) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1831
                return null;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1832
            } else if (editorString.equals(BUILT_IN_REP)) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1833
                return BUILT_IN_EDITOR;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1834
            } else {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1835
                boolean wait = false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1836
                char waitMarker = editorString.charAt(0);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1837
                if (waitMarker == WAIT_PREFIX || waitMarker == NORMAL_PREFIX) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1838
                    wait = waitMarker == WAIT_PREFIX;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1839
                    editorString = editorString.substring(1);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1840
                }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1841
                String[] cmd = editorString.split(RECORD_SEPARATOR);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1842
                return new EditorSetting(cmd, wait);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1843
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1844
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1845
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
  1846
        static void removePrefs(PersistentStorage prefs) {
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
  1847
            prefs.remove(EDITOR_KEY);
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
  1848
        }
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
  1849
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
  1850
        void toPrefs(PersistentStorage prefs) {
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1851
            prefs.put(EDITOR_KEY, (this == BUILT_IN_EDITOR)
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1852
                    ? BUILT_IN_REP
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1853
                    : (wait ? WAIT_PREFIX : NORMAL_PREFIX) + String.join(RECORD_SEPARATOR, cmd));
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1854
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1855
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1856
        @Override
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1857
        public boolean equals(Object o) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1858
            if (o instanceof EditorSetting) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1859
                EditorSetting ed = (EditorSetting) o;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1860
                return Arrays.equals(cmd, ed.cmd) && wait == ed.wait;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1861
            } else {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1862
                return false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1863
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1864
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1865
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1866
        @Override
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1867
        public int hashCode() {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1868
            int hash = 7;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1869
            hash = 71 * hash + Arrays.deepHashCode(this.cmd);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1870
            hash = 71 * hash + (this.wait ? 1 : 0);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1871
            return hash;
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1872
        }
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1873
    }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1874
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1875
    class SetEditor {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1876
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1877
        private final ArgTokenizer at;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1878
        private final String[] command;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1879
        private final boolean hasCommand;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1880
        private final boolean defaultOption;
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
  1881
        private final boolean deleteOption;
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1882
        private final boolean waitOption;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1883
        private final boolean retainOption;
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
  1884
        private final int primaryOptionCount;
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1885
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1886
        SetEditor(ArgTokenizer at) {
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
  1887
            at.allowedOptions("-default", "-wait", "-retain", "-delete");
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1888
            String prog = at.next();
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1889
            List<String> ed = new ArrayList<>();
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1890
            while (at.val() != null) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1891
                ed.add(at.val());
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1892
                at.nextToken();  // so that options are not interpreted as jshell options
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1893
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1894
            this.at = at;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1895
            this.command = ed.toArray(new String[ed.size()]);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1896
            this.hasCommand = command.length > 0;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1897
            this.defaultOption = at.hasOption("-default");
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
  1898
            this.deleteOption = at.hasOption("-delete");
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1899
            this.waitOption = at.hasOption("-wait");
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1900
            this.retainOption = at.hasOption("-retain");
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
  1901
            this.primaryOptionCount = (hasCommand? 1 : 0) + (defaultOption? 1 : 0) + (deleteOption? 1 : 0);
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1902
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1903
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1904
        SetEditor() {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1905
            this(new ArgTokenizer("", ""));
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1906
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1907
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1908
        boolean set() {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1909
            if (!check()) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1910
                return false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1911
            }
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
  1912
            if (primaryOptionCount == 0 && !retainOption) {
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1913
                // No settings or -retain, so this is a query
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1914
                EditorSetting retained = EditorSetting.fromPrefs(prefs);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1915
                if (retained != null) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1916
                    // retained editor is set
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1917
                    hard("/set editor -retain %s", format(retained));
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1918
                }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1919
                if (retained == null || !retained.equals(editor)) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1920
                    // editor is not retained or retained is different from set
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1921
                    hard("/set editor %s", format(editor));
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1922
                }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1923
                return true;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1924
            }
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
  1925
            if (retainOption && deleteOption) {
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
  1926
                EditorSetting.removePrefs(prefs);
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
  1927
            }
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1928
            install();
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
  1929
            if (retainOption && !deleteOption) {
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1930
                editor.toPrefs(prefs);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1931
                fluffmsg("jshell.msg.set.editor.retain", format(editor));
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1932
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1933
            return true;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1934
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1935
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1936
        private boolean check() {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1937
            if (!checkOptionsAndRemainingInput(at)) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1938
                return false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1939
            }
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
  1940
            if (primaryOptionCount > 1) {
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1941
                errormsg("jshell.err.default.option.or.program", at.whole());
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1942
                return false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1943
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1944
            if (waitOption && !hasCommand) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1945
                errormsg("jshell.err.wait.applies.to.external.editor", at.whole());
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1946
                return false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1947
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1948
            return true;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1949
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1950
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1951
        private void install() {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1952
            if (hasCommand) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1953
                editor = new EditorSetting(command, waitOption);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1954
            } else if (defaultOption) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1955
                editor = BUILT_IN_EDITOR;
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
  1956
            } else if (deleteOption) {
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 41635
diff changeset
  1957
                configEditor();
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1958
            } else {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1959
                return;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1960
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1961
            fluffmsg("jshell.msg.set.editor.set", format(editor));
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1962
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1963
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1964
        private String format(EditorSetting ed) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1965
            if (ed == BUILT_IN_EDITOR) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1966
                return "-default";
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1967
            } else {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1968
                Stream<String> elems = Arrays.stream(ed.cmd);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1969
                if (ed.wait) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1970
                    elems = Stream.concat(Stream.of("-wait"), elems);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1971
                }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1972
                return elems.collect(joining(" "));
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1973
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1974
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1975
    }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1976
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1977
    // The sub-command:  /set start <start-file>
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1978
    boolean setStart(ArgTokenizer at) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1979
        at.allowedOptions("-default", "-none", "-retain");
42972
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  1980
        List<String> fns = new ArrayList<>();
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  1981
        while (at.next() != null) {
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  1982
            fns.add(at.val());
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  1983
        }
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1984
        if (!checkOptionsAndRemainingInput(at)) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1985
            return false;
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1986
        }
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1987
        boolean defaultOption = at.hasOption("-default");
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1988
        boolean noneOption = at.hasOption("-none");
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1989
        boolean retainOption = at.hasOption("-retain");
42972
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  1990
        boolean hasFile = !fns.isEmpty();
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1991
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1992
        int argCount = (defaultOption ? 1 : 0) + (noneOption ? 1 : 0) + (hasFile ? 1 : 0);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1993
        if (argCount > 1) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1994
            errormsg("jshell.err.option.or.filename", at.whole());
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1995
            return false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1996
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1997
        if (argCount == 0 && !retainOption) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1998
            // no options or filename, show current setting
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1999
            showSetStart();
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2000
            return true;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2001
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2002
        if (hasFile) {
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2003
            startup = Startup.fromFileList(fns, "/set start", this);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2004
            if (startup == null) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2005
                return false;
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2006
            }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2007
        } else if (defaultOption) {
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2008
            startup = Startup.defaultStartup(this);
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2009
        } else if (noneOption) {
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2010
            startup = Startup.noStartup();
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2011
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2012
        if (retainOption) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2013
            // retain startup setting
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2014
            prefs.put(STARTUP_KEY, startup.storedForm());
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2015
        }
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  2016
        return true;
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  2017
    }
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  2018
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2019
    // show the "/set start" settings (retained and, if different, current)
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2020
    // as commands (and file contents).  All commands first, then contents.
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2021
    void showSetStart() {
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2022
        StringBuilder sb = new StringBuilder();
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42827
diff changeset
  2023
        String retained = prefs.get(STARTUP_KEY);
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2024
        if (retained != null) {
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2025
            Startup retainedStart = Startup.unpack(retained, this);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2026
            boolean currentDifferent = !startup.equals(retainedStart);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2027
            sb.append(retainedStart.show(true));
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2028
            if (currentDifferent) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2029
                sb.append(startup.show(false));
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2030
            }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2031
            sb.append(retainedStart.showDetail());
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2032
            if (currentDifferent) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2033
                sb.append(startup.showDetail());
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2034
            }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2035
        } else {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2036
            sb.append(startup.show(false));
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2037
            sb.append(startup.showDetail());
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  2038
        }
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2039
        hard(sb.toString());
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  2040
    }
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  2041
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2042
    boolean cmdDebug(String arg) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2043
        if (arg.isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2044
            debug = !debug;
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
  2045
            InternalDebugControl.setDebugFlags(state, debug ? DBG_GEN : 0);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2046
            fluff("Debugging %s", debug ? "on" : "off");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2047
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2048
            int flags = 0;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2049
            for (char ch : arg.toCharArray()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2050
                switch (ch) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2051
                    case '0':
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2052
                        flags = 0;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2053
                        debug = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2054
                        fluff("Debugging off");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2055
                        break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2056
                    case 'r':
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2057
                        debug = true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2058
                        fluff("REPL tool debugging on");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2059
                        break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2060
                    case 'g':
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
  2061
                        flags |= DBG_GEN;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2062
                        fluff("General debugging on");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2063
                        break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2064
                    case 'f':
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
  2065
                        flags |= DBG_FMGR;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2066
                        fluff("File manager debugging on");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2067
                        break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2068
                    case 'c':
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
  2069
                        flags |= DBG_COMPA;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2070
                        fluff("Completion analysis debugging on");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2071
                        break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2072
                    case 'd':
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
  2073
                        flags |= DBG_DEP;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2074
                        fluff("Dependency debugging on");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2075
                        break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2076
                    case 'e':
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
  2077
                        flags |= DBG_EVNT;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2078
                        fluff("Event debugging on");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2079
                        break;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents: 43038
diff changeset
  2080
                    case 'w':
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents: 43038
diff changeset
  2081
                        flags |= DBG_WRAP;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents: 43038
diff changeset
  2082
                        fluff("Wrap debugging on");
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents: 43038
diff changeset
  2083
                        break;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2084
                    default:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2085
                        hard("Unknown debugging option: %c", ch);
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents: 43038
diff changeset
  2086
                        fluff("Use: 0 r g f c d e w");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2087
                        return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2088
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2089
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2090
            InternalDebugControl.setDebugFlags(state, flags);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2091
        }
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2092
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2093
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2094
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2095
    private boolean cmdExit() {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2096
        regenerateOnDeath = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2097
        live = false;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2098
        fluffmsg("jshell.msg.goodbye");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2099
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2100
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2101
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2102
    boolean cmdHelp(String arg) {
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2103
        ArgTokenizer at = new ArgTokenizer("/help", arg);
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2104
        String subject = at.next();
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2105
        if (subject != null) {
44454
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  2106
            // check if the requested subject is a help subject or
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  2107
            // a command, with or without slash
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2108
            Command[] matches = commands.values().stream()
44454
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  2109
                    .filter(c -> c.command.startsWith(subject)
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  2110
                              || c.command.substring(1).startsWith(subject))
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  2111
                    .toArray(Command[]::new);
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2112
            if (matches.length == 1) {
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  2113
                String cmd = matches[0].command;
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2114
                if (cmd.equals("/set")) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2115
                    // Print the help doc for the specified sub-command
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2116
                    String which = subCommand(cmd, at, SET_SUBCOMMANDS);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2117
                    if (which == null) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2118
                        return false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2119
                    }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2120
                    if (!which.equals("_blank")) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2121
                        hardrb("help.set." + which);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2122
                        return true;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2123
                    }
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  2124
                }
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2125
            }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2126
            if (matches.length > 0) {
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2127
                for (Command c : matches) {
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2128
                    hard("");
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2129
                    hard("%s", c.command);
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2130
                    hard("");
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2131
                    hardrb(c.helpKey);
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2132
                }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2133
                return true;
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2134
            } else {
44454
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  2135
                // failing everything else, check if this is the start of
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  2136
                // a /set sub-command name
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  2137
                String[] subs = Arrays.stream(SET_SUBCOMMANDS)
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  2138
                        .filter(s -> s.startsWith(subject))
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  2139
                        .toArray(String[]::new);
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  2140
                if (subs.length > 0) {
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  2141
                    for (String sub : subs) {
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  2142
                        hardrb("help.set." + sub);
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  2143
                        hard("");
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  2144
                    }
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  2145
                    return true;
74af976d6798 8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents: 44188
diff changeset
  2146
                }
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2147
                errormsg("jshell.err.help.arg", arg);
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2148
            }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2149
        }
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2150
        hardmsg("jshell.msg.help.begin");
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2151
        hardPairs(commands.values().stream()
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2152
                .filter(cmd -> cmd.kind.showInHelp),
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2153
                cmd -> cmd.command + " " + getResourceString(cmd.helpKey + ".args"),
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2154
                cmd -> getResourceString(cmd.helpKey + ".summary")
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2155
        );
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2156
        hardmsg("jshell.msg.help.subject");
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2157
        hardPairs(commands.values().stream()
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2158
                .filter(cmd -> cmd.kind == CommandKind.HELP_SUBJECT),
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2159
                cmd -> cmd.command,
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2160
                cmd -> getResourceString(cmd.helpKey + ".summary")
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2161
        );
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2162
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2163
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2164
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2165
    private boolean cmdHistory() {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2166
        cmdout.println();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2167
        for (String s : input.currentSessionHistory()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2168
            // No number prefix, confusing with snippet ids
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2169
            cmdout.printf("%s\n", s);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2170
        }
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2171
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2172
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2173
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2174
    /**
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2175
     * Avoid parameterized varargs possible heap pollution warning.
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2176
     */
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2177
    private interface SnippetPredicate<T extends Snippet> extends Predicate<T> { }
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2178
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2179
    /**
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2180
     * Apply filters to a stream until one that is non-empty is found.
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2181
     * Adapted from Stuart Marks
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2182
     *
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2183
     * @param supplier Supply the Snippet stream to filter
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2184
     * @param filters Filters to attempt
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2185
     * @return The non-empty filtered Stream, or null
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2186
     */
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2187
    @SafeVarargs
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2188
    private static <T extends Snippet> Stream<T> nonEmptyStream(Supplier<Stream<T>> supplier,
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2189
            SnippetPredicate<T>... filters) {
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2190
        for (SnippetPredicate<T> filt : filters) {
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2191
            Iterator<T> iterator = supplier.get().filter(filt).iterator();
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2192
            if (iterator.hasNext()) {
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2193
                return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2194
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2195
        }
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2196
        return null;
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2197
    }
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2198
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  2199
    private boolean inStartUp(Snippet sn) {
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  2200
        return mapSnippet.get(sn).space == startNamespace;
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  2201
    }
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  2202
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  2203
    private boolean isActive(Snippet sn) {
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  2204
        return state.status(sn).isActive();
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  2205
    }
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  2206
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2207
    private boolean mainActive(Snippet sn) {
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  2208
        return !inStartUp(sn) && isActive(sn);
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2209
    }
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2210
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2211
    private boolean matchingDeclaration(Snippet sn, String name) {
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2212
        return sn instanceof DeclarationSnippet
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2213
                && ((DeclarationSnippet) sn).name().equals(name);
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2214
    }
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2215
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2216
    /**
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2217
     * Convert user arguments to a Stream of snippets referenced by those
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2218
     * arguments (or lack of arguments).
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2219
     *
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2220
     * @param snippets the base list of possible snippets
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2221
     * @param defFilter the filter to apply to the arguments if no argument
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2222
     * @param rawargs the user's argument to the command, maybe be the empty
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2223
     * string
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2224
     * @return a Stream of referenced snippets or null if no matches are found
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2225
     */
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2226
    private <T extends Snippet> Stream<T> argsOptionsToSnippets(Supplier<Stream<T>> snippetSupplier,
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2227
            Predicate<Snippet> defFilter, String rawargs, String cmd) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2228
        ArgTokenizer at = new ArgTokenizer(cmd, rawargs.trim());
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2229
        at.allowedOptions("-all", "-start");
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2230
        List<String> args = new ArrayList<>();
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2231
        String s;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2232
        while ((s = at.next()) != null) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2233
            args.add(s);
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2234
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2235
        if (!checkOptionsAndRemainingInput(at)) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2236
            return null;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2237
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2238
        if (at.optionCount() > 0 && args.size() > 0) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2239
            errormsg("jshell.err.may.not.specify.options.and.snippets", at.whole());
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2240
            return null;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2241
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2242
        if (at.optionCount() > 1) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2243
            errormsg("jshell.err.conflicting.options", at.whole());
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2244
            return null;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2245
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2246
        if (at.hasOption("-all")) {
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2247
            // all snippets including start-up, failed, and overwritten
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2248
            return snippetSupplier.get();
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2249
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2250
        if (at.hasOption("-start")) {
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  2251
            // start-up snippets
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2252
            return snippetSupplier.get()
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  2253
                    .filter(this::inStartUp);
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2254
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2255
        if (args.isEmpty()) {
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2256
            // Default is all active user snippets
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2257
            return snippetSupplier.get()
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2258
                    .filter(defFilter);
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2259
        }
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2260
        return argsToSnippets(snippetSupplier, args);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2261
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2262
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2263
    /**
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2264
     * Convert user arguments to a Stream of snippets referenced by those
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2265
     * arguments.
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2266
     *
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2267
     * @param snippetSupplier the base list of possible snippets
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2268
     * @param args the user's argument to the command, maybe be the empty list
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2269
     * @return a Stream of referenced snippets or null if no matches to specific
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2270
     * arg
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2271
     */
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2272
    private <T extends Snippet> Stream<T> argsToSnippets(Supplier<Stream<T>> snippetSupplier,
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2273
            List<String> args) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2274
        Stream<T> result = null;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2275
        for (String arg : args) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2276
            // Find the best match
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2277
            Stream<T> st = layeredSnippetSearch(snippetSupplier, arg);
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2278
            if (st == null) {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2279
                Stream<Snippet> est = layeredSnippetSearch(state::snippets, arg);
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2280
                if (est == null) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2281
                    errormsg("jshell.err.no.such.snippets", arg);
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2282
                } else {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2283
                    errormsg("jshell.err.the.snippet.cannot.be.used.with.this.command",
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2284
                            arg, est.findFirst().get().source());
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2285
                }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2286
                return null;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2287
            }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2288
            if (result == null) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2289
                result = st;
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2290
            } else {
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2291
                result = Stream.concat(result, st);
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2292
            }
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2293
        }
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2294
        return result;
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2295
    }
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2296
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2297
    private <T extends Snippet> Stream<T> layeredSnippetSearch(Supplier<Stream<T>> snippetSupplier, String arg) {
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2298
        return nonEmptyStream(
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2299
                // the stream supplier
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2300
                snippetSupplier,
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2301
                // look for active user declarations matching the name
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2302
                sn -> isActive(sn) && matchingDeclaration(sn, arg),
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2303
                // else, look for any declarations matching the name
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2304
                sn -> matchingDeclaration(sn, arg),
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2305
                // else, look for an id of this name
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2306
                sn -> sn.id().equals(arg)
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2307
        );
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2308
    }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2309
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2310
    private boolean cmdDrop(String rawargs) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2311
        ArgTokenizer at = new ArgTokenizer("/drop", rawargs.trim());
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2312
        at.allowedOptions();
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2313
        List<String> args = new ArrayList<>();
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2314
        String s;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2315
        while ((s = at.next()) != null) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2316
            args.add(s);
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2317
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2318
        if (!checkOptionsAndRemainingInput(at)) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2319
            return false;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2320
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2321
        if (args.isEmpty()) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2322
            errormsg("jshell.err.drop.arg");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2323
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2324
        }
41514
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 41248
diff changeset
  2325
        Stream<Snippet> stream = argsToSnippets(this::dropableSnippets, args);
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2326
        if (stream == null) {
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2327
            // Snippet not found. Error already printed
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2328
            fluffmsg("jshell.msg.see.classes.etc");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2329
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2330
        }
41514
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 41248
diff changeset
  2331
        List<Snippet> snippets = stream.collect(toList());
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2332
        if (snippets.size() > args.size()) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2333
            // One of the args references more thean one snippet
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2334
            errormsg("jshell.err.drop.ambiguous");
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2335
            fluffmsg("jshell.msg.use.one.of", snippets.stream()
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2336
                    .map(sn -> String.format("\n/drop %-5s :   %s", sn.id(), sn.source().replace("\n", "\n       ")))
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2337
                    .collect(Collectors.joining(", "))
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2338
            );
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2339
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2340
        }
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2341
        snippets.stream()
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2342
                .forEach(sn -> state.drop(sn).forEach(this::handleEvent));
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2343
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2344
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2345
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2346
    private boolean cmdEdit(String arg) {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2347
        Stream<Snippet> stream = argsOptionsToSnippets(state::snippets,
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2348
                this::mainActive, arg, "/edit");
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2349
        if (stream == null) {
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2350
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2351
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2352
        Set<String> srcSet = new LinkedHashSet<>();
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2353
        stream.forEachOrdered(sn -> {
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2354
            String src = sn.source();
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2355
            switch (sn.subKind()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2356
                case VAR_VALUE_SUBKIND:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2357
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2358
                case ASSIGNMENT_SUBKIND:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2359
                case OTHER_EXPRESSION_SUBKIND:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2360
                case TEMP_VAR_EXPRESSION_SUBKIND:
42259
e4d04fcd0826 8143006: jshell tool: /edit doesn't process each line as same as inputs for jshell
rfield
parents: 41937
diff changeset
  2361
                case UNKNOWN_SUBKIND:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2362
                    if (!src.endsWith(";")) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2363
                        src = src + ";";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2364
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2365
                    srcSet.add(src);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2366
                    break;
43264
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 43263
diff changeset
  2367
                case STATEMENT_SUBKIND:
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 43263
diff changeset
  2368
                    if (src.endsWith("}")) {
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 43263
diff changeset
  2369
                        // Could end with block or, for example, new Foo() {...}
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 43263
diff changeset
  2370
                        // so, we need deeper analysis to know if it needs a semicolon
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 43263
diff changeset
  2371
                        src = analysis.analyzeCompletion(src).source();
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 43263
diff changeset
  2372
                    } else if (!src.endsWith(";")) {
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 43263
diff changeset
  2373
                        src = src + ";";
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 43263
diff changeset
  2374
                    }
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 43263
diff changeset
  2375
                    srcSet.add(src);
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 43263
diff changeset
  2376
                    break;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2377
                default:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2378
                    srcSet.add(src);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2379
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2380
            }
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2381
        });
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2382
        StringBuilder sb = new StringBuilder();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2383
        for (String s : srcSet) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2384
            sb.append(s);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2385
            sb.append('\n');
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2386
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2387
        String src = sb.toString();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2388
        Consumer<String> saveHandler = new SaveHandler(src, srcSet);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2389
        Consumer<String> errorHandler = s -> hard("Edit Error: %s", s);
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  2390
        if (editor == BUILT_IN_EDITOR) {
41934
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2391
            return builtInEdit(src, saveHandler, errorHandler);
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2392
        } else {
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2393
            // Changes have occurred in temp edit directory,
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2394
            // transfer the new sources to JShell (unless the editor is
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2395
            // running directly in JShell's window -- don't make a mess)
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2396
            String[] buffer = new String[1];
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2397
            Consumer<String> extSaveHandler = s -> {
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2398
                if (input.terminalEditorRunning()) {
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2399
                    buffer[0] = s;
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2400
                } else {
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2401
                    saveHandler.accept(s);
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2402
                }
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2403
            };
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2404
            ExternalEditor.edit(editor.cmd, src,
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2405
                    errorHandler, extSaveHandler,
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2406
                    () -> input.suspend(),
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2407
                    () -> input.resume(),
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2408
                    editor.wait,
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2409
                    () -> hardrb("jshell.msg.press.return.to.leave.edit.mode"));
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2410
            if (buffer[0] != null) {
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2411
                saveHandler.accept(buffer[0]);
37007
6023a9a9d58a 8153716: JShell tool: should warn when failed to launch editor
rfield
parents: 36992
diff changeset
  2412
            }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2413
        }
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2414
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2415
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2416
    //where
41934
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2417
    // start the built-in editor
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2418
    private boolean builtInEdit(String initialText,
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2419
            Consumer<String> saveHandler, Consumer<String> errorHandler) {
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2420
        try {
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2421
            ServiceLoader<BuildInEditorProvider> sl
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2422
                    = ServiceLoader.load(BuildInEditorProvider.class);
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2423
            // Find the highest ranking provider
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2424
            BuildInEditorProvider provider = null;
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2425
            for (BuildInEditorProvider p : sl) {
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2426
                if (provider == null || p.rank() > provider.rank()) {
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2427
                    provider = p;
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2428
                }
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2429
            }
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2430
            if (provider != null) {
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2431
                provider.edit(getResourceString("jshell.label.editpad"),
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2432
                        initialText, saveHandler, errorHandler);
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2433
                return true;
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2434
            } else {
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2435
                errormsg("jshell.err.no.builtin.editor");
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2436
            }
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2437
        } catch (RuntimeException ex) {
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2438
            errormsg("jshell.err.cant.launch.editor", ex);
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2439
        }
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2440
        fluffmsg("jshell.msg.try.set.editor");
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2441
        return false;
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2442
    }
a4da50688dc7 8167636: jshell tool: Edit Pad should be in its own module
rfield
parents: 41865
diff changeset
  2443
    //where
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2444
    // receives editor requests to save
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2445
    private class SaveHandler implements Consumer<String> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2446
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2447
        String src;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2448
        Set<String> currSrcs;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2449
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2450
        SaveHandler(String src, Set<String> ss) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2451
            this.src = src;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2452
            this.currSrcs = ss;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2453
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2454
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2455
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2456
        public void accept(String s) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2457
            if (!s.equals(src)) { // quick check first
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2458
                src = s;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2459
                try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2460
                    Set<String> nextSrcs = new LinkedHashSet<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2461
                    boolean failed = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2462
                    while (true) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2463
                        CompletionInfo an = analysis.analyzeCompletion(s);
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  2464
                        if (!an.completeness().isComplete()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2465
                            break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2466
                        }
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  2467
                        String tsrc = trimNewlines(an.source());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2468
                        if (!failed && !currSrcs.contains(tsrc)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2469
                            failed = processCompleteSource(tsrc);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2470
                        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2471
                        nextSrcs.add(tsrc);
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  2472
                        if (an.remaining().isEmpty()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2473
                            break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2474
                        }
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  2475
                        s = an.remaining();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2476
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2477
                    currSrcs = nextSrcs;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2478
                } catch (IllegalStateException ex) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2479
                    hardmsg("jshell.msg.resetting");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2480
                    resetState();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2481
                    currSrcs = new LinkedHashSet<>(); // re-process everything
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2482
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2483
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2484
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2485
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2486
        private String trimNewlines(String s) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2487
            int b = 0;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2488
            while (b < s.length() && s.charAt(b) == '\n') {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2489
                ++b;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2490
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2491
            int e = s.length() -1;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2492
            while (e >= 0 && s.charAt(e) == '\n') {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2493
                --e;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2494
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2495
            return s.substring(b, e + 1);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2496
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2497
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2498
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2499
    private boolean cmdList(String arg) {
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2500
        if (arg.length() >= 2 && "-history".startsWith(arg)) {
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2501
            return cmdHistory();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2502
        }
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2503
        Stream<Snippet> stream = argsOptionsToSnippets(state::snippets,
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2504
                this::mainActive, arg, "/list");
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2505
        if (stream == null) {
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2506
            return false;
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2507
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2508
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2509
        // prevent double newline on empty list
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2510
        boolean[] hasOutput = new boolean[1];
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2511
        stream.forEachOrdered(sn -> {
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2512
            if (!hasOutput[0]) {
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2513
                cmdout.println();
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2514
                hasOutput[0] = true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2515
            }
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2516
            cmdout.printf("%4s : %s\n", sn.id(), sn.source().replace("\n", "\n       "));
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2517
        });
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2518
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2519
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2520
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2521
    private boolean cmdOpen(String filename) {
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
  2522
        return runFile(filename, "/open");
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2523
    }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2524
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2525
    private boolean runFile(String filename, String context) {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2526
        if (!filename.isEmpty()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2527
            try {
42972
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2528
                Path path = toPathResolvingUserHome(filename);
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2529
                Reader reader;
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2530
                String resource;
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2531
                if (!Files.exists(path) && (resource = getResource(filename)) != null) {
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2532
                    // Not found as file, but found as resource
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2533
                    reader = new StringReader(resource);
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2534
                } else {
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2535
                    reader = new FileReader(path.toString());
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2536
                }
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2537
                run(new ScannerIOContext(reader));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2538
                return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2539
            } catch (FileNotFoundException e) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2540
                errormsg("jshell.err.file.not.found", context, filename, e.getMessage());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2541
            } catch (Exception e) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2542
                errormsg("jshell.err.file.exception", context, filename, e);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2543
            }
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2544
        } else {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2545
            errormsg("jshell.err.file.filename", context);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2546
        }
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2547
        return false;
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2548
    }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2549
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2550
    static String getResource(String name) {
42972
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2551
        if (BUILTIN_FILE_PATTERN.matcher(name).matches()) {
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2552
            try {
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2553
                return readResource(name);
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2554
            } catch (Throwable t) {
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2555
                // Fall-through to null
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2556
            }
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2557
        }
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2558
        return null;
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2559
    }
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2560
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2561
    // Read a built-in file from resources
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2562
    static String readResource(String name) throws IOException {
42972
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2563
        // Attempt to find the file as a resource
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2564
        String spec = String.format(BUILTIN_FILE_PATH_FORMAT, name);
43136
24d62ba7ad5e 8172414: jshell not working in exploded JDK build
jlahoda
parents: 43134
diff changeset
  2565
24d62ba7ad5e 8172414: jshell not working in exploded JDK build
jlahoda
parents: 43134
diff changeset
  2566
        try (InputStream in = JShellTool.class.getResourceAsStream(spec);
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2567
                BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2568
            return reader.lines().collect(Collectors.joining("\n", "", "\n"));
43136
24d62ba7ad5e 8172414: jshell not working in exploded JDK build
jlahoda
parents: 43134
diff changeset
  2569
        }
42972
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2570
    }
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  2571
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2572
    private boolean cmdReset(String rawargs) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2573
        if (!parseCommandLineLikeFlags(rawargs, new OptionParserBase())) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2574
            return false;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2575
        }
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2576
        live = false;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2577
        fluffmsg("jshell.msg.resetting.state");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2578
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2579
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2580
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2581
    private boolean cmdReload(String rawargs) {
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2582
        OptionParserReload ap = new OptionParserReload();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2583
        if (!parseCommandLineLikeFlags(rawargs, ap)) {
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2584
            return false;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2585
        }
43564
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
  2586
        ReplayableHistory history;
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2587
        if (ap.restore()) {
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2588
            if (replayableHistoryPrevious == null) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2589
                errormsg("jshell.err.reload.no.previous");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2590
                return false;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2591
            }
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2592
            history = replayableHistoryPrevious;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2593
            fluffmsg("jshell.err.reload.restarting.previous.state");
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2594
        } else {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2595
            history = replayableHistory;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2596
            fluffmsg("jshell.err.reload.restarting.state");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2597
        }
43564
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
  2598
        boolean success = doReload(history, !ap.quiet());
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
  2599
        if (success && ap.restore()) {
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
  2600
            // if we are restoring from previous, then if nothing was added
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
  2601
            // before time of exit, there is nothing to save
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
  2602
            replayableHistory.markSaved();
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
  2603
        }
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
  2604
        return success;
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2605
    }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2606
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2607
    private boolean cmdEnv(String rawargs) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2608
        if (rawargs.trim().isEmpty()) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2609
            // No arguments, display current settings (as option flags)
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2610
            StringBuilder sb = new StringBuilder();
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2611
            for (String a : options.commonOptions()) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2612
                sb.append(
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2613
                        a.startsWith("-")
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2614
                            ? sb.length() > 0
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2615
                                    ? "\n   "
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2616
                                    :   "   "
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2617
                            : " ");
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2618
                sb.append(a);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2619
            }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2620
            if (sb.length() > 0) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2621
                rawout(prefix(sb.toString()));
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2622
            }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2623
            return false;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2624
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2625
        if (!parseCommandLineLikeFlags(rawargs, new OptionParserBase())) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2626
            return false;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2627
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2628
        fluffmsg("jshell.msg.set.restore");
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2629
        return doReload(replayableHistory, false);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2630
    }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2631
43564
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
  2632
    private boolean doReload(ReplayableHistory history, boolean echo) {
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2633
        resetState();
43564
4287765963d5 8173652: jshell tool: store history on fatal exit
rfield
parents: 43367
diff changeset
  2634
        run(new ReloadIOContext(history.iterable(),
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2635
                echo ? cmdout : null));
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2636
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2637
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2638
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2639
    private boolean parseCommandLineLikeFlags(String rawargs, OptionParserBase ap) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2640
        String[] args = Arrays.stream(rawargs.split("\\s+"))
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2641
                .filter(s -> !s.isEmpty())
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2642
                .toArray(String[]::new);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2643
        Options opts = ap.parse(args);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2644
        if (opts == null) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2645
            return false;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2646
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2647
        if (!ap.nonOptions().isEmpty()) {
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2648
            errormsg("jshell.err.unexpected.at.end", ap.nonOptions(), rawargs);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2649
            return false;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2650
        }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2651
        options.override(opts);
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2652
        return true;
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2653
    }
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
  2654
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2655
    private boolean cmdSave(String rawargs) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2656
        ArgTokenizer at = new ArgTokenizer("/save", rawargs.trim());
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2657
        at.allowedOptions("-all", "-start", "-history");
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2658
        String filename = at.next();
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2659
        if (filename == null) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2660
            errormsg("jshell.err.file.filename", "/save");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2661
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2662
        }
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2663
        if (!checkOptionsAndRemainingInput(at)) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2664
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2665
        }
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2666
        if (at.optionCount() > 1) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2667
            errormsg("jshell.err.conflicting.options", at.whole());
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2668
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2669
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2670
        try (BufferedWriter writer = Files.newBufferedWriter(toPathResolvingUserHome(filename),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2671
                Charset.defaultCharset(),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2672
                CREATE, TRUNCATE_EXISTING, WRITE)) {
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2673
            if (at.hasOption("-history")) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2674
                for (String s : input.currentSessionHistory()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2675
                    writer.write(s);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2676
                    writer.write("\n");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2677
                }
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2678
            } else if (at.hasOption("-start")) {
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents: 43136
diff changeset
  2679
                writer.append(startup.toString());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2680
            } else {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2681
                String sources = (at.hasOption("-all")
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2682
                        ? state.snippets()
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2683
                        : state.snippets().filter(this::mainActive))
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2684
                        .map(Snippet::source)
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2685
                        .collect(Collectors.joining("\n"));
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2686
                writer.write(sources);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2687
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2688
        } catch (FileNotFoundException e) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2689
            errormsg("jshell.err.file.not.found", "/save", filename, e.getMessage());
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2690
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2691
        } catch (Exception e) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2692
            errormsg("jshell.err.file.exception", "/save", filename, e);
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2693
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2694
        }
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2695
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2696
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2697
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2698
    private boolean cmdVars(String arg) {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2699
        Stream<VarSnippet> stream = argsOptionsToSnippets(this::allVarSnippets,
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2700
                this::isActive, arg, "/vars");
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2701
        if (stream == null) {
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2702
            return false;
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2703
        }
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2704
        stream.forEachOrdered(vk ->
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2705
        {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2706
            String val = state.status(vk) == Status.VALID
41937
1313399705e9 8161969: jshell tool: /var value is not truncated per feedback setting
rfield
parents: 41934
diff changeset
  2707
                    ? feedback.truncateVarValue(state.varValue(vk))
40516
9e0e107c39dd 8154374: JShell: setContextClassLoader() for remote Snippet class loader
rfield
parents: 40498
diff changeset
  2708
                    : getResourceString("jshell.msg.vars.not.active");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2709
            hard("  %s %s = %s", vk.typeName(), vk.name(), val);
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2710
        });
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2711
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2712
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2713
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2714
    private boolean cmdMethods(String arg) {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2715
        Stream<MethodSnippet> stream = argsOptionsToSnippets(this::allMethodSnippets,
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2716
                this::isActive, arg, "/methods");
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2717
        if (stream == null) {
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2718
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2719
        }
43759
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  2720
        stream.forEachOrdered(meth -> {
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  2721
            String sig = meth.signature();
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  2722
            int i = sig.lastIndexOf(")") + 1;
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  2723
            if (i <= 0) {
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  2724
                hard("  %s", meth.name());
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  2725
            } else {
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  2726
                hard("  %s %s%s", sig.substring(i), meth.name(), sig.substring(0, i));
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  2727
            }
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  2728
            printSnippetStatus(meth, true);
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  2729
        });
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2730
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2731
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2732
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2733
    private boolean cmdTypes(String arg) {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2734
        Stream<TypeDeclSnippet> stream = argsOptionsToSnippets(this::allTypeSnippets,
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2735
                this::isActive, arg, "/types");
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2736
        if (stream == null) {
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2737
            return false;
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2738
        }
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2739
        stream.forEachOrdered(ck
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2740
        -> {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2741
            String kind;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2742
            switch (ck.subKind()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2743
                case INTERFACE_SUBKIND:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2744
                    kind = "interface";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2745
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2746
                case CLASS_SUBKIND:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2747
                    kind = "class";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2748
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2749
                case ENUM_SUBKIND:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2750
                    kind = "enum";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2751
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2752
                case ANNOTATION_TYPE_SUBKIND:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2753
                    kind = "@interface";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2754
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2755
                default:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2756
                    assert false : "Wrong kind" + ck.subKind();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2757
                    kind = "class";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2758
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2759
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2760
            hard("  %s %s", kind, ck.name());
43759
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  2761
            printSnippetStatus(ck, true);
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2762
        });
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2763
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2764
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2765
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2766
    private boolean cmdImports() {
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
  2767
        state.imports().forEach(ik -> {
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
  2768
            hard("  import %s%s", ik.isStatic() ? "static " : "", ik.fullname());
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
  2769
        });
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2770
        return true;
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
  2771
    }
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
  2772
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2773
    private boolean cmdUseHistoryEntry(int index) {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2774
        List<Snippet> keys = state.snippets().collect(toList());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2775
        if (index < 0)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2776
            index += keys.size();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2777
        else
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2778
            index--;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2779
        if (index >= 0 && index < keys.size()) {
34477
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2780
            rerunSnippet(keys.get(index));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2781
        } else {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2782
            errormsg("jshell.err.out.of.range");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2783
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2784
        }
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2785
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2786
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2787
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2788
    boolean checkOptionsAndRemainingInput(ArgTokenizer at) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2789
        String junk = at.remainder();
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2790
        if (!junk.isEmpty()) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2791
            errormsg("jshell.err.unexpected.at.end", junk, at.whole());
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2792
            return false;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2793
        } else {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2794
            String bad = at.badOptions();
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2795
            if (!bad.isEmpty()) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2796
                errormsg("jshell.err.unknown.option", bad, at.whole());
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2797
                return false;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2798
            }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2799
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2800
        return true;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2801
    }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2802
34477
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2803
    private boolean rerunHistoryEntryById(String id) {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2804
        Optional<Snippet> snippet = state.snippets()
34477
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2805
            .filter(s -> s.id().equals(id))
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2806
            .findFirst();
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2807
        return snippet.map(s -> {
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2808
            rerunSnippet(s);
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2809
            return true;
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2810
        }).orElse(false);
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2811
    }
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2812
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2813
    private void rerunSnippet(Snippet snippet) {
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2814
        String source = snippet.source();
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2815
        cmdout.printf("%s\n", source);
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2816
        input.replaceLastHistoryEntry(source);
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2817
        processSourceCatchingReset(source);
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2818
    }
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2819
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2820
    /**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2821
     * Filter diagnostics for only errors (no warnings, ...)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2822
     * @param diagnostics input list
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2823
     * @return filtered list
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2824
     */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2825
    List<Diag> errorsOnly(List<Diag> diagnostics) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2826
        return diagnostics.stream()
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42415
diff changeset
  2827
                .filter(Diag::isError)
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2828
                .collect(toList());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2829
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2830
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2831
    void displayDiagnostics(String source, Diag diag, List<String> toDisplay) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2832
        for (String line : diag.getMessage(null).split("\\r?\\n")) { // TODO: Internationalize
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2833
            if (!line.trim().startsWith("location:")) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2834
                toDisplay.add(line);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2835
            }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2836
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2837
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2838
        int pstart = (int) diag.getStartPosition();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2839
        int pend = (int) diag.getEndPosition();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2840
        Matcher m = LINEBREAK.matcher(source);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2841
        int pstartl = 0;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2842
        int pendl = -2;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2843
        while (m.find(pstartl)) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2844
            pendl = m.start();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2845
            if (pendl >= pstart) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2846
                break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2847
            } else {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2848
                pstartl = m.end();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2849
            }
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2850
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2851
        if (pendl < pstart) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2852
            pendl = source.length();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2853
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2854
        toDisplay.add(source.substring(pstartl, pendl));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2855
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2856
        StringBuilder sb = new StringBuilder();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2857
        int start = pstart - pstartl;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2858
        for (int i = 0; i < start; ++i) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2859
            sb.append(' ');
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2860
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2861
        sb.append('^');
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2862
        boolean multiline = pend > pendl;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2863
        int end = (multiline ? pendl : pend) - pstartl - 1;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2864
        if (end > start) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2865
            for (int i = start + 1; i < end; ++i) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2866
                sb.append('-');
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2867
            }
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2868
            if (multiline) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2869
                sb.append("-...");
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2870
            } else {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2871
                sb.append('^');
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2872
            }
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2873
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2874
        toDisplay.add(sb.toString());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2875
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2876
        debug("printDiagnostics start-pos = %d ==> %d -- wrap = %s", diag.getStartPosition(), start, this);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2877
        debug("Code: %s", diag.getCode());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2878
        debug("Pos: %d (%d - %d)", diag.getPosition(),
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2879
                diag.getStartPosition(), diag.getEndPosition());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2880
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2881
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2882
    private String processSource(String srcInput) throws IllegalStateException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2883
        while (true) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2884
            CompletionInfo an = analysis.analyzeCompletion(srcInput);
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  2885
            if (!an.completeness().isComplete()) {
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  2886
                return an.remaining();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2887
            }
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  2888
            boolean failed = processCompleteSource(an.source());
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  2889
            if (failed || an.remaining().isEmpty()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2890
                return "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2891
            }
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  2892
            srcInput = an.remaining();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2893
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2894
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2895
    //where
44188
3f2047e62102 8176412: jshell tool: automatic imports are excluded on /reload causing it to fail
rfield
parents: 44065
diff changeset
  2896
    boolean processCompleteSource(String source) throws IllegalStateException {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2897
        debug("Compiling: %s", source);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2898
        boolean failed = false;
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2899
        boolean isActive = false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2900
        List<SnippetEvent> events = state.eval(source);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2901
        for (SnippetEvent e : events) {
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2902
            // Report the event, recording failure
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2903
            failed |= handleEvent(e);
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2904
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2905
            // If any main snippet is active, this should be replayable
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2906
            // also ignore var value queries
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2907
            isActive |= e.causeSnippet() == null &&
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  2908
                    e.status().isActive() &&
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2909
                    e.snippet().subKind() != VAR_VALUE_SUBKIND;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2910
        }
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2911
        // If this is an active snippet and it didn't cause the backend to die,
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2912
        // add it to the replayable history
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2913
        if (isActive && live) {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2914
            addToReplayHistory(source);
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2915
        }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2916
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2917
        return failed;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2918
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2919
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2920
    // Handle incoming snippet events -- return true on failure
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2921
    private boolean handleEvent(SnippetEvent ste) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2922
        Snippet sn = ste.snippet();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2923
        if (sn == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2924
            debug("Event with null key: %s", ste);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2925
            return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2926
        }
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2927
        List<Diag> diagnostics = state.diagnostics(sn).collect(toList());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2928
        String source = sn.source();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2929
        if (ste.causeSnippet() == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2930
            // main event
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2931
            for (Diag d : diagnostics) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2932
                hardmsg(d.isError()? "jshell.msg.error" : "jshell.msg.warning");
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2933
                List<String> disp = new ArrayList<>();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2934
                displayDiagnostics(source, d, disp);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2935
                disp.stream()
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  2936
                        .forEach(l -> hard("%s", l));
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2937
            }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2938
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2939
            if (ste.status() != Status.REJECTED) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2940
                if (ste.exception() != null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2941
                    if (ste.exception() instanceof EvalException) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2942
                        printEvalException((EvalException) ste.exception());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2943
                        return true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2944
                    } else if (ste.exception() instanceof UnresolvedReferenceException) {
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2945
                        printUnresolvedException((UnresolvedReferenceException) ste.exception());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2946
                    } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2947
                        hard("Unexpected execution exception: %s", ste.exception());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2948
                        return true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2949
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2950
                } else {
43759
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  2951
                    new DisplayEvent(ste, FormatWhen.PRIMARY, ste.value(), diagnostics)
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  2952
                            .displayDeclarationAndValue();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2953
                }
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2954
            } else {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2955
                if (diagnostics.isEmpty()) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2956
                    errormsg("jshell.err.failed");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2957
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2958
                return true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2959
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2960
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2961
            // Update
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2962
            if (sn instanceof DeclarationSnippet) {
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2963
                List<Diag> other = errorsOnly(diagnostics);
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2964
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2965
                // display update information
43759
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  2966
                new DisplayEvent(ste, FormatWhen.UPDATE, ste.value(), other)
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  2967
                        .displayDeclarationAndValue();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2968
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2969
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2970
        return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2971
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2972
    //where
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2973
    void printStackTrace(StackTraceElement[] stes) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2974
        for (StackTraceElement ste : stes) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2975
            StringBuilder sb = new StringBuilder();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2976
            String cn = ste.getClassName();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2977
            if (!cn.isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2978
                int dot = cn.lastIndexOf('.');
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2979
                if (dot > 0) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2980
                    sb.append(cn.substring(dot + 1));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2981
                } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2982
                    sb.append(cn);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2983
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2984
                sb.append(".");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2985
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2986
            if (!ste.getMethodName().isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2987
                sb.append(ste.getMethodName());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2988
                sb.append(" ");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2989
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2990
            String fileName = ste.getFileName();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2991
            int lineNumber = ste.getLineNumber();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2992
            String loc = ste.isNativeMethod()
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2993
                    ? getResourceString("jshell.msg.native.method")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2994
                    : fileName == null
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2995
                            ? getResourceString("jshell.msg.unknown.source")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2996
                            : lineNumber >= 0
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2997
                                    ? fileName + ":" + lineNumber
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2998
                                    : fileName;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2999
            hard("      at %s(%s)", sb, loc);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3000
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3001
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3002
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3003
    //where
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3004
    void printUnresolvedException(UnresolvedReferenceException ex) {
43759
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3005
        printSnippetStatus(ex.getSnippet(), false);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3006
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3007
    //where
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3008
    void printEvalException(EvalException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3009
        if (ex.getMessage() == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3010
            hard("%s thrown", ex.getExceptionClassName());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3011
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3012
            hard("%s thrown: %s", ex.getExceptionClassName(), ex.getMessage());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3013
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3014
        printStackTrace(ex.getStackTrace());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3015
    }
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3016
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3017
    private FormatAction toAction(Status status, Status previousStatus, boolean isSignatureChange) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3018
        FormatAction act;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3019
        switch (status) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3020
            case VALID:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3021
            case RECOVERABLE_DEFINED:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3022
            case RECOVERABLE_NOT_DEFINED:
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  3023
                if (previousStatus.isActive()) {
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3024
                    act = isSignatureChange
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3025
                            ? FormatAction.REPLACED
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3026
                            : FormatAction.MODIFIED;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3027
                } else {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3028
                    act = FormatAction.ADDED;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3029
                }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3030
                break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3031
            case OVERWRITTEN:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3032
                act = FormatAction.OVERWROTE;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3033
                break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3034
            case DROPPED:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3035
                act = FormatAction.DROPPED;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3036
                break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3037
            case REJECTED:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3038
            case NONEXISTENT:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3039
            default:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3040
                // Should not occur
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3041
                error("Unexpected status: " + previousStatus.toString() + "=>" + status.toString());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3042
                act = FormatAction.DROPPED;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3043
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3044
        return act;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3045
    }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3046
43759
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3047
    void printSnippetStatus(DeclarationSnippet sn, boolean resolve) {
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3048
        List<Diag> otherErrors = errorsOnly(state.diagnostics(sn).collect(toList()));
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3049
        new DisplayEvent(sn, state.status(sn), resolve, otherErrors)
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3050
                .displayDeclarationAndValue();
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3051
    }
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3052
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3053
    class DisplayEvent {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3054
        private final Snippet sn;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3055
        private final FormatAction action;
43759
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3056
        private final FormatWhen update;
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3057
        private final String value;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3058
        private final List<String> errorLines;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3059
        private final FormatResolve resolution;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3060
        private final String unresolved;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3061
        private final FormatUnresolved unrcnt;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3062
        private final FormatErrors errcnt;
43759
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3063
        private final boolean resolve;
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3064
43759
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3065
        DisplayEvent(SnippetEvent ste, FormatWhen update, String value, List<Diag> errors) {
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3066
            this(ste.snippet(), ste.status(), false,
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3067
                    toAction(ste.status(), ste.previousStatus(), ste.isSignatureChange()),
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3068
                    update, value, errors);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3069
        }
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3070
43759
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3071
        DisplayEvent(Snippet sn, Status status, boolean resolve, List<Diag> errors) {
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3072
            this(sn, status, resolve, FormatAction.USED, FormatWhen.UPDATE, null, errors);
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3073
        }
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3074
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3075
        private DisplayEvent(Snippet sn, Status status, boolean resolve,
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3076
                FormatAction action, FormatWhen update, String value, List<Diag> errors) {
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3077
            this.sn = sn;
43759
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3078
            this.resolve =resolve;
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3079
            this.action = action;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3080
            this.update = update;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3081
            this.value = value;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3082
            this.errorLines = new ArrayList<>();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3083
            for (Diag d : errors) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3084
                displayDiagnostics(sn.source(), d, errorLines);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3085
            }
43759
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3086
            if (resolve) {
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3087
                // resolve needs error lines indented
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3088
                for (int i = 0; i < errorLines.size(); ++i) {
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3089
                    errorLines.set(i, "    " + errorLines.get(i));
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3090
                }
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3091
            }
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  3092
            long unresolvedCount;
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3093
            if (sn instanceof DeclarationSnippet && (status == Status.RECOVERABLE_DEFINED || status == Status.RECOVERABLE_NOT_DEFINED)) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3094
                resolution = (status == Status.RECOVERABLE_NOT_DEFINED)
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3095
                        ? FormatResolve.NOTDEFINED
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3096
                        : FormatResolve.DEFINED;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3097
                unresolved = unresolved((DeclarationSnippet) sn);
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  3098
                unresolvedCount = state.unresolvedDependencies((DeclarationSnippet) sn).count();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3099
            } else {
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3100
                resolution = FormatResolve.OK;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3101
                unresolved = "";
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3102
                unresolvedCount = 0;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3103
            }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3104
            unrcnt = unresolvedCount == 0
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3105
                    ? FormatUnresolved.UNRESOLVED0
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3106
                    : unresolvedCount == 1
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3107
                        ? FormatUnresolved.UNRESOLVED1
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3108
                        : FormatUnresolved.UNRESOLVED2;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3109
            errcnt = errors.isEmpty()
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3110
                    ? FormatErrors.ERROR0
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3111
                    : errors.size() == 1
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3112
                        ? FormatErrors.ERROR1
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3113
                        : FormatErrors.ERROR2;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3114
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3115
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3116
        private String unresolved(DeclarationSnippet key) {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  3117
            List<String> unr = state.unresolvedDependencies(key).collect(toList());
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3118
            StringBuilder sb = new StringBuilder();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3119
            int fromLast = unr.size();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3120
            if (fromLast > 0) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3121
                sb.append(" ");
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3122
            }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3123
            for (String u : unr) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3124
                --fromLast;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3125
                sb.append(u);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3126
                switch (fromLast) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3127
                    // No suffix
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3128
                    case 0:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3129
                        break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3130
                    case 1:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3131
                        sb.append(", and ");
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3132
                        break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3133
                    default:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3134
                        sb.append(", ");
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3135
                        break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3136
                }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3137
            }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3138
            return sb.toString();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3139
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3140
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3141
        private void custom(FormatCase fcase, String name) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3142
            custom(fcase, name, null);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3143
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3144
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3145
        private void custom(FormatCase fcase, String name, String type) {
43759
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3146
            if (resolve) {
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3147
                String resolutionErrors = feedback.format("resolve", fcase, action, update,
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3148
                        resolution, unrcnt, errcnt,
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3149
                        name, type, value, unresolved, errorLines);
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3150
                if (!resolutionErrors.trim().isEmpty()) {
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3151
                    hard("    %s", resolutionErrors);
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3152
                }
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3153
            } else if (interactive()) {
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3154
                String display = feedback.format(fcase, action, update,
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3155
                        resolution, unrcnt, errcnt,
61535ac55add 8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents: 43757
diff changeset
  3156
                        name, type, value, unresolved, errorLines);
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3157
                cmdout.print(display);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3158
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3159
        }
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3160
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3161
        @SuppressWarnings("fallthrough")
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3162
        private void displayDeclarationAndValue() {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3163
            switch (sn.subKind()) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3164
                case CLASS_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3165
                    custom(FormatCase.CLASS, ((TypeDeclSnippet) sn).name());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3166
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3167
                case INTERFACE_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3168
                    custom(FormatCase.INTERFACE, ((TypeDeclSnippet) sn).name());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3169
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3170
                case ENUM_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3171
                    custom(FormatCase.ENUM, ((TypeDeclSnippet) sn).name());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3172
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3173
                case ANNOTATION_TYPE_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3174
                    custom(FormatCase.ANNOTATION, ((TypeDeclSnippet) sn).name());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3175
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3176
                case METHOD_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3177
                    custom(FormatCase.METHOD, ((MethodSnippet) sn).name(), ((MethodSnippet) sn).parameterTypes());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3178
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3179
                case VAR_DECLARATION_SUBKIND: {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3180
                    VarSnippet vk = (VarSnippet) sn;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3181
                    custom(FormatCase.VARDECL, vk.name(), vk.typeName());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3182
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3183
                }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3184
                case VAR_DECLARATION_WITH_INITIALIZER_SUBKIND: {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3185
                    VarSnippet vk = (VarSnippet) sn;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3186
                    custom(FormatCase.VARINIT, vk.name(), vk.typeName());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3187
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3188
                }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3189
                case TEMP_VAR_EXPRESSION_SUBKIND: {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3190
                    VarSnippet vk = (VarSnippet) sn;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3191
                    custom(FormatCase.EXPRESSION, vk.name(), vk.typeName());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3192
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3193
                }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3194
                case OTHER_EXPRESSION_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3195
                    error("Unexpected expression form -- value is: %s", (value));
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3196
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3197
                case VAR_VALUE_SUBKIND: {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3198
                    ExpressionSnippet ek = (ExpressionSnippet) sn;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3199
                    custom(FormatCase.VARVALUE, ek.name(), ek.typeName());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3200
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3201
                }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3202
                case ASSIGNMENT_SUBKIND: {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3203
                    ExpressionSnippet ek = (ExpressionSnippet) sn;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3204
                    custom(FormatCase.ASSIGNMENT, ek.name(), ek.typeName());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3205
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3206
                }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3207
                case SINGLE_TYPE_IMPORT_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3208
                case TYPE_IMPORT_ON_DEMAND_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3209
                case SINGLE_STATIC_IMPORT_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3210
                case STATIC_IMPORT_ON_DEMAND_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3211
                    custom(FormatCase.IMPORT, ((ImportSnippet) sn).name());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3212
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3213
                case STATEMENT_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3214
                    custom(FormatCase.STATEMENT, null);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3215
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3216
            }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  3217
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3218
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3219
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3220
    /** The current version number as a string.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3221
     */
36992
ddebebe611a1 8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents: 36990
diff changeset
  3222
    String version() {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3223
        return version("release");  // mm.nn.oo[-milestone]
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3224
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3225
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3226
    /** The current full version number as a string.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3227
     */
36992
ddebebe611a1 8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents: 36990
diff changeset
  3228
    String fullVersion() {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3229
        return version("full"); // mm.mm.oo[-milestone]-build
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3230
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3231
36992
ddebebe611a1 8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents: 36990
diff changeset
  3232
    private String version(String key) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3233
        if (versionRB == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3234
            try {
36992
ddebebe611a1 8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents: 36990
diff changeset
  3235
                versionRB = ResourceBundle.getBundle(VERSION_RB_NAME, locale);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3236
            } catch (MissingResourceException e) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3237
                return "(version info not available)";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3238
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3239
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3240
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3241
            return versionRB.getString(key);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3242
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3243
        catch (MissingResourceException e) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3244
            return "(version info not available)";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3245
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3246
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3247
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3248
    class NameSpace {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3249
        final String spaceName;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3250
        final String prefix;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3251
        private int nextNum;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3252
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3253
        NameSpace(String spaceName, String prefix) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3254
            this.spaceName = spaceName;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3255
            this.prefix = prefix;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3256
            this.nextNum = 1;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3257
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3258
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3259
        String tid(Snippet sn) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3260
            String tid = prefix + nextNum++;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3261
            mapSnippet.put(sn, new SnippetInfo(sn, this, tid));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3262
            return tid;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3263
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3264
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3265
        String tidNext() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3266
            return prefix + nextNum;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3267
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3268
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3269
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3270
    static class SnippetInfo {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3271
        final Snippet snippet;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3272
        final NameSpace space;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3273
        final String tid;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3274
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3275
        SnippetInfo(Snippet snippet, NameSpace space, String tid) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3276
            this.snippet = snippet;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3277
            this.space = space;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3278
            this.tid = tid;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3279
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3280
    }
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3281
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  3282
    static class ArgSuggestion implements Suggestion {
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3283
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3284
        private final String continuation;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3285
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3286
        /**
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3287
         * Create a {@code Suggestion} instance.
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3288
         *
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3289
         * @param continuation a candidate continuation of the user's input
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3290
         */
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3291
        public ArgSuggestion(String continuation) {
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3292
            this.continuation = continuation;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3293
        }
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3294
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3295
        /**
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3296
         * The candidate continuation of the given user's input.
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3297
         *
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3298
         * @return the continuation string
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3299
         */
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3300
        @Override
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3301
        public String continuation() {
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3302
            return continuation;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3303
        }
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3304
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3305
        /**
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3306
         * Indicates whether input continuation matches the target type and is thus
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3307
         * more likely to be the desired continuation. A matching continuation is
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3308
         * preferred.
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3309
         *
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3310
         * @return {@code false}, non-types analysis
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3311
         */
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3312
        @Override
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3313
        public boolean matchesType() {
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3314
            return false;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3315
        }
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  3316
    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3317
}
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3318
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3319
abstract class NonInteractiveIOContext extends IOContext {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3320
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3321
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3322
    public boolean interactiveOutput() {
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3323
        return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3324
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3325
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3326
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3327
    public Iterable<String> currentSessionHistory() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3328
        return Collections.emptyList();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3329
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3330
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3331
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3332
    public boolean terminalEditorRunning() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3333
        return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3334
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3335
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3336
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3337
    public void suspend() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3338
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3339
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3340
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3341
    public void resume() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3342
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3343
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3344
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3345
    public void beforeUserCode() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3346
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3347
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3348
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3349
    public void afterUserCode() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3350
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3351
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3352
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3353
    public void replaceLastHistoryEntry(String source) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3354
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3355
}
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3356
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3357
class ScannerIOContext extends NonInteractiveIOContext {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3358
    private final Scanner scannerIn;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3359
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3360
    ScannerIOContext(Scanner scannerIn) {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3361
        this.scannerIn = scannerIn;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3362
    }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3363
42972
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  3364
    ScannerIOContext(Reader rdr) throws FileNotFoundException {
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  3365
        this(new Scanner(rdr));
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  3366
    }
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42969
diff changeset
  3367
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3368
    @Override
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3369
    public String readLine(String prompt, String prefix) {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3370
        if (scannerIn.hasNextLine()) {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3371
            return scannerIn.nextLine();
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3372
        } else {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3373
            return null;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3374
        }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3375
    }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3376
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3377
    @Override
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3378
    public void close() {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3379
        scannerIn.close();
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3380
    }
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  3381
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  3382
    @Override
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  3383
    public int readUserInput() {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  3384
        return -1;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  3385
    }
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3386
}
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3387
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3388
class ReloadIOContext extends NonInteractiveIOContext {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3389
    private final Iterator<String> it;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3390
    private final PrintStream echoStream;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3391
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3392
    ReloadIOContext(Iterable<String> history, PrintStream echoStream) {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3393
        this.it = history.iterator();
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3394
        this.echoStream = echoStream;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3395
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3396
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3397
    @Override
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3398
    public String readLine(String prompt, String prefix) {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3399
        String s = it.hasNext()
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3400
                ? it.next()
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3401
                : null;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3402
        if (echoStream != null && s != null) {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3403
            String p = "-: ";
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3404
            String p2 = "\n   ";
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3405
            echoStream.printf("%s%s\n", p, s.replace("\n", p2));
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3406
        }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3407
        return s;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3408
    }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3409
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3410
    @Override
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  3411
    public void close() {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3412
    }
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  3413
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  3414
    @Override
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  3415
    public int readUserInput() {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  3416
        return -1;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  3417
    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  3418
}