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