langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java
author rfield
Thu, 20 Oct 2016 12:53:11 -0700
changeset 41635 cb3d04878117
parent 41514 a75c2b869d8d
child 41641 a628785b9dd9
permissions -rw-r--r--
8163840: jshell tool: provide way to display configuration settings 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
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    28
import java.io.BufferedWriter;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    29
import java.io.File;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    30
import java.io.FileNotFoundException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    31
import java.io.FileReader;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    32
import java.io.IOException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    33
import java.io.InputStream;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    34
import java.io.PrintStream;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    35
import java.io.Reader;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    36
import java.io.StringReader;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    37
import java.nio.charset.Charset;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    38
import java.nio.file.AccessDeniedException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    39
import java.nio.file.FileSystems;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    40
import java.nio.file.Files;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    41
import java.nio.file.NoSuchFileException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    42
import java.nio.file.Path;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    43
import java.nio.file.Paths;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
    44
import java.text.MessageFormat;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    45
import java.util.ArrayList;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    46
import java.util.Arrays;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    47
import java.util.Collections;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    48
import java.util.Iterator;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    49
import java.util.LinkedHashMap;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    50
import java.util.LinkedHashSet;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    51
import java.util.List;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
    52
import java.util.Locale;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    53
import java.util.Map;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    54
import java.util.Map.Entry;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    55
import java.util.Scanner;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    56
import java.util.Set;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    57
import java.util.function.Consumer;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    58
import java.util.function.Predicate;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    59
import java.util.prefs.Preferences;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    60
import java.util.regex.Matcher;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    61
import java.util.regex.Pattern;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    62
import java.util.stream.Collectors;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    63
import java.util.stream.Stream;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    64
import java.util.stream.StreamSupport;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    65
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    66
import jdk.internal.jshell.debug.InternalDebugControl;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    67
import jdk.internal.jshell.tool.IOContext.InputInterruptedException;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    68
import jdk.jshell.DeclarationSnippet;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    69
import jdk.jshell.Diag;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    70
import jdk.jshell.EvalException;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    71
import jdk.jshell.ExpressionSnippet;
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    72
import jdk.jshell.ImportSnippet;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    73
import jdk.jshell.JShell;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    74
import jdk.jshell.JShell.Subscription;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    75
import jdk.jshell.MethodSnippet;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    76
import jdk.jshell.Snippet;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    77
import jdk.jshell.Snippet.Status;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    78
import jdk.jshell.SnippetEvent;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    79
import jdk.jshell.SourceCodeAnalysis;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    80
import jdk.jshell.SourceCodeAnalysis.CompletionInfo;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    81
import jdk.jshell.SourceCodeAnalysis.Suggestion;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    82
import jdk.jshell.TypeDeclSnippet;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    83
import jdk.jshell.UnresolvedReferenceException;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    84
import jdk.jshell.VarSnippet;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    85
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    86
import static java.nio.file.StandardOpenOption.CREATE;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    87
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    88
import static java.nio.file.StandardOpenOption.WRITE;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    89
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
    90
import java.util.Optional;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    91
import java.util.ResourceBundle;
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
    92
import java.util.Spliterators;
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
    93
import java.util.function.Function;
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
    94
import java.util.function.Supplier;
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
    95
import jdk.internal.joptsimple.*;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    96
import jdk.internal.jshell.tool.Feedback.FormatAction;
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    97
import jdk.internal.jshell.tool.Feedback.FormatCase;
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
    98
import jdk.internal.jshell.tool.Feedback.FormatErrors;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
    99
import jdk.internal.jshell.tool.Feedback.FormatResolve;
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   100
import jdk.internal.jshell.tool.Feedback.FormatUnresolved;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   101
import jdk.internal.jshell.tool.Feedback.FormatWhen;
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   102
import static java.util.Arrays.asList;
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   103
import static java.util.Arrays.stream;
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   104
import static java.util.stream.Collectors.joining;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   105
import static java.util.stream.Collectors.toList;
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
   106
import static jdk.jshell.Snippet.SubKind.VAR_VALUE_SUBKIND;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   107
import static java.util.stream.Collectors.toMap;
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
   108
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_COMPA;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
   109
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_DEP;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
   110
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_EVNT;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
   111
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_FMGR;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
   112
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN;
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
   113
import static jdk.internal.jshell.tool.ContinuousCompletionProvider.STARTSWITH_MATCHER;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   114
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   115
/**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   116
 * Command line REPL tool for Java using the JShell API.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   117
 * @author Robert Field
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   118
 */
37745
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
   119
public class JShellTool implements MessageHandler {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   120
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   121
    private static final String LINE_SEP = System.getProperty("line.separator");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   122
    private static final Pattern LINEBREAK = Pattern.compile("\\R");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   123
    private static final String RECORD_SEPARATOR = "\u241E";
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   124
    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
   125
    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
   126
    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
   127
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   128
    final InputStream cmdin;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   129
    final PrintStream cmdout;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   130
    final PrintStream cmderr;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   131
    final PrintStream console;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   132
    final InputStream userin;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   133
    final PrintStream userout;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   134
    final PrintStream usererr;
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36499
diff changeset
   135
    final Preferences prefs;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   136
    final Locale locale;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   137
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   138
    final Feedback feedback = new Feedback();
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   139
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   140
    /**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   141
     * The constructor for the tool (used by tool launch via main and by test
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   142
     * harnesses to capture ins and outs.
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   143
     * @param in command line input -- snippets, commands and user input
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   144
     * @param cmdout command line output, feedback including errors
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   145
     * @param cmderr start-up errors and debugging info
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   146
     * @param console console control interaction
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   147
     * @param userout code execution output  -- System.out.printf("hi")
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   148
     * @param usererr code execution error stream  -- System.err.printf("Oops")
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   149
     * @param prefs preferences to use
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   150
     * @param locale locale to use
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   151
     */
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   152
    public JShellTool(InputStream in, PrintStream cmdout, PrintStream cmderr,
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   153
            PrintStream console,
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   154
            PrintStream userout, PrintStream usererr,
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   155
            Preferences prefs, Locale locale) {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   156
        this(in, cmdout, cmderr, console, null, userout, usererr, prefs, locale);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   157
    }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   158
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   159
    /**
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   160
     * The constructor for the tool (used by tool launch via main and by test
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   161
     * harnesses to capture ins and outs.
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   162
     * @param cmdin command line input -- snippets and commands
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   163
     * @param cmdout command line output, feedback including errors
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   164
     * @param cmderr start-up errors and debugging info
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   165
     * @param console console control interaction
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   166
     * @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
   167
     * @param userout code execution output  -- System.out.printf("hi")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   168
     * @param usererr code execution error stream  -- System.err.printf("Oops")
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   169
     * @param prefs preferences to use
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   170
     * @param locale locale to use
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   171
     */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   172
    public JShellTool(InputStream cmdin, PrintStream cmdout, PrintStream cmderr,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   173
            PrintStream console,
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36499
diff changeset
   174
            InputStream userin, PrintStream userout, PrintStream usererr,
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   175
            Preferences prefs, Locale locale) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   176
        this.cmdin = cmdin;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   177
        this.cmdout = cmdout;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   178
        this.cmderr = cmderr;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   179
        this.console = console;
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   180
        this.userin = userin != null ? userin : new InputStream() {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   181
            @Override
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   182
            public int read() throws IOException {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   183
                return input.readUserInput();
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   184
            }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   185
        };
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   186
        this.userout = userout;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   187
        this.usererr = usererr;
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36499
diff changeset
   188
        this.prefs = prefs;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   189
        this.locale = locale;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   190
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   191
36992
ddebebe611a1 8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents: 36990
diff changeset
   192
    private ResourceBundle versionRB = null;
ddebebe611a1 8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents: 36990
diff changeset
   193
    private ResourceBundle outputRB  = null;
ddebebe611a1 8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents: 36990
diff changeset
   194
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   195
    private IOContext input = null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   196
    private boolean regenerateOnDeath = true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   197
    private boolean live = false;
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   198
    private boolean feedbackInitialized = false;
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   199
    private String commandLineFeedbackMode = null;
37745
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
   200
    private List<String> remoteVMOptions = new ArrayList<>();
41248
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   201
    private List<String> compilerOptions = new ArrayList<>();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   202
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   203
    SourceCodeAnalysis analysis;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   204
    JShell state = null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   205
    Subscription shutdownSubscription = null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   206
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   207
    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
   208
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   209
    private boolean debug = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   210
    public boolean testPrompt = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   211
    private String cmdlineClasspath = null;
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   212
    private String startup = null;
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   213
    private EditorSetting editor = BUILT_IN_EDITOR;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   214
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   215
    // Commands and snippets which should be replayed
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   216
    private List<String> replayableHistory;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   217
    private List<String> replayableHistoryPrevious;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   218
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   219
    static final String STARTUP_KEY  = "STARTUP";
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   220
    static final String EDITOR_KEY   = "EDITOR";
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   221
    static final String FEEDBACK_KEY = "FEEDBACK";
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   222
    static final String MODE_KEY     = "MODE";
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   223
    static final String REPLAY_RESTORE_KEY = "REPLAY_RESTORE";
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   224
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   225
    static final String DEFAULT_STARTUP =
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   226
            "\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   227
            "import java.util.*;\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   228
            "import java.io.*;\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   229
            "import java.math.*;\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   230
            "import java.net.*;\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   231
            "import java.util.concurrent.*;\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   232
            "import java.util.prefs.*;\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   233
            "import java.util.regex.*;\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   234
            "void printf(String format, Object... args) { System.out.printf(format, args); }\n";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   235
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   236
    // Tool id (tid) mapping: the three name spaces
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   237
    NameSpace mainNamespace;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   238
    NameSpace startNamespace;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   239
    NameSpace errorNamespace;
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   240
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   241
    // Tool id (tid) mapping: the current name spaces
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   242
    NameSpace currentNameSpace;
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   243
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   244
    Map<Snippet,SnippetInfo> mapSnippet;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   245
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   246
    /**
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   247
     * Is the input/output currently interactive
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   248
     *
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   249
     * @return true if console
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   250
     */
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   251
    boolean interactive() {
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   252
        return input != null && input.interactiveOutput();
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   253
    }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   254
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   255
    void debug(String format, Object... args) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   256
        if (debug) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   257
            cmderr.printf(format + "\n", args);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   258
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   259
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   260
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   261
    /**
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   262
     * Base output for command output -- no pre- or post-fix
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   263
     *
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   264
     * @param printf format
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   265
     * @param printf args
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   266
     */
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   267
    void rawout(String format, Object... args) {
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   268
        cmdout.printf(format, args);
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   269
    }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   270
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   271
    /**
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   272
     * Must show command output
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   273
     *
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   274
     * @param format printf format
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   275
     * @param args printf args
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   276
     */
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   277
    @Override
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   278
    public void hard(String format, Object... args) {
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   279
        rawout(feedback.getPre() + format + feedback.getPost(), args);
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   280
    }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   281
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   282
    /**
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   283
     * Error command output
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   284
     *
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   285
     * @param format printf format
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   286
     * @param args printf args
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   287
     */
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   288
    void error(String format, Object... args) {
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   289
        rawout(feedback.getErrorPre() + format + feedback.getErrorPost(), args);
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   290
    }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   291
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   292
    /**
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   293
     * Should optional informative be displayed?
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   294
     * @return true if they should be displayed
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   295
     */
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   296
    @Override
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   297
    public boolean showFluff() {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   298
        return feedback.shouldDisplayCommandFluff() && interactive();
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   299
    }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   300
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   301
    /**
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   302
     * Optional output
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   303
     *
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   304
     * @param format printf format
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   305
     * @param args printf args
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   306
     */
37745
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
   307
    @Override
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
   308
    public void fluff(String format, Object... args) {
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   309
        if (showFluff()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   310
            hard(format, args);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   311
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   312
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   313
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   314
    /**
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   315
     * Optional output -- with embedded per- and post-fix
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   316
     *
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   317
     * @param format printf format
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   318
     * @param args printf args
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   319
     */
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   320
    void fluffRaw(String format, Object... args) {
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   321
        if (showFluff()) {
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   322
            rawout(format, args);
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   323
        }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   324
    }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   325
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   326
    /**
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   327
     * 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
   328
     *
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   329
     * @param key the resource key
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   330
     */
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   331
    String getResourceString(String key) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   332
        if (outputRB == null) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   333
            try {
36992
ddebebe611a1 8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents: 36990
diff changeset
   334
                outputRB = ResourceBundle.getBundle(L10N_RB_NAME, locale);
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   335
            } catch (MissingResourceException mre) {
36992
ddebebe611a1 8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents: 36990
diff changeset
   336
                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
   337
                return "";
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   338
            }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   339
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   340
        String s;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   341
        try {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   342
            s = outputRB.getString(key);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   343
        } catch (MissingResourceException mre) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   344
            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
   345
            return "";
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   346
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   347
        return s;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   348
    }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   349
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   350
    /**
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   351
     * Add prefixing to embedded newlines in a string, leading with the normal
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   352
     * prefix
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   353
     *
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   354
     * @param s the string to prefix
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   355
     */
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   356
    String prefix(String s) {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   357
        return prefix(s, feedback.getPre());
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   358
    }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   359
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   360
    /**
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   361
     * Add prefixing to embedded newlines in a string
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   362
     *
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   363
     * @param s the string to prefix
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   364
     * @param leading the string to prepend
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   365
     */
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   366
    String prefix(String s, String leading) {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   367
        if (s == null || s.isEmpty()) {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   368
            return "";
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   369
        }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   370
        return leading
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   371
                + s.substring(0, s.length() - 1).replaceAll("\\R", System.getProperty("line.separator") + feedback.getPre())
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   372
                + s.substring(s.length() - 1, s.length());
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   373
    }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   374
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   375
    /**
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   376
     * 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
   377
     *
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   378
     * @param key the resource key
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   379
     */
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   380
    void hardrb(String key) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   381
        String s = prefix(getResourceString(key));
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   382
        cmdout.println(s);
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   383
    }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   384
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   385
    /**
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   386
     * Format using resource bundle look-up using MessageFormat
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   387
     *
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   388
     * @param key the resource key
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   389
     * @param args
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   390
     */
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   391
    String messageFormat(String key, Object... args) {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   392
        String rs = getResourceString(key);
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   393
        return MessageFormat.format(rs, args);
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   394
    }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   395
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   396
    /**
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   397
     * Print using resource bundle look-up, MessageFormat, and add prefix and
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   398
     * postfix
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   399
     *
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   400
     * @param key the resource key
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   401
     * @param args
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   402
     */
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   403
    @Override
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   404
    public void hardmsg(String key, Object... args) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   405
        cmdout.println(prefix(messageFormat(key, args)));
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   406
    }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   407
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   408
    /**
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   409
     * Print error using resource bundle look-up, MessageFormat, and add prefix
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   410
     * and postfix
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   411
     *
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   412
     * @param key the resource key
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   413
     * @param args
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   414
     */
37745
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
   415
    @Override
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
   416
    public void errormsg(String key, Object... args) {
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
   417
        if (isRunningInteractive()) {
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
   418
            cmdout.println(prefix(messageFormat(key, args), feedback.getErrorPre()));
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
   419
        } else {
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
   420
            startmsg(key, args);
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
   421
        }
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   422
    }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   423
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   424
    /**
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   425
     * Print command-line error using resource bundle look-up, MessageFormat
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   426
     *
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   427
     * @param key the resource key
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   428
     * @param args
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   429
     */
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   430
    void startmsg(String key, Object... args) {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   431
        cmderr.println(prefix(messageFormat(key, args), ""));
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   432
    }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   433
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   434
    /**
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   435
     * Print (fluff) using resource bundle look-up, MessageFormat, and add
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   436
     * prefix and postfix
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   437
     *
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   438
     * @param key the resource key
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   439
     * @param args
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   440
     */
37745
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
   441
    @Override
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
   442
    public void fluffmsg(String key, Object... args) {
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   443
        if (showFluff()) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   444
            hardmsg(key, args);
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   445
        }
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   446
    }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   447
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   448
    <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
   449
        Map<String, String> a2b = stream.collect(toMap(a, b,
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   450
                (m1, m2) -> m1,
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   451
                () -> new LinkedHashMap<>()));
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   452
        int aLen = 0;
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   453
        for (String av : a2b.keySet()) {
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   454
            aLen = Math.max(aLen, av.length());
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   455
        }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   456
        String format = "   %-" + aLen + "s -- %s";
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   457
        String indentedNewLine = LINE_SEP + feedback.getPre()
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   458
                + String.format("   %-" + (aLen + 4) + "s", "");
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   459
        for (Entry<String, String> e : a2b.entrySet()) {
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   460
            hard(format, e.getKey(), e.getValue().replaceAll("\n", indentedNewLine));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   461
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   462
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   463
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   464
    /**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   465
     * Trim whitespace off end of string
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   466
     *
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   467
     * @param s
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   468
     * @return
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   469
     */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   470
    static String trimEnd(String s) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   471
        int last = s.length() - 1;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   472
        int i = last;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   473
        while (i >= 0 && Character.isWhitespace(s.charAt(i))) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   474
            --i;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   475
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   476
        if (i != last) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   477
            return s.substring(0, i + 1);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   478
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   479
            return s;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   480
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   481
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   482
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   483
    /**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   484
     * Normal start entry point
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   485
     * @param args
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   486
     * @throws Exception
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   487
     */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   488
    public static void main(String[] args) throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   489
        new JShellTool(System.in, System.out, System.err, System.out,
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
   490
                 System.out, System.err,
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   491
                 Preferences.userRoot().node("tool/JShell"),
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   492
                 Locale.getDefault())
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   493
                .start(args);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   494
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   495
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   496
    public void start(String[] args) throws Exception {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   497
        List<String> loadList = processCommandArgs(args);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   498
        if (loadList == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   499
            // Abort
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   500
            return;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   501
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   502
        try (IOContext in = new ConsoleIOContext(this, cmdin, console)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   503
            start(in, loadList);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   504
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   505
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   506
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   507
    private void start(IOContext in, List<String> loadList) {
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
   508
        // If startup hasn't been set by command line, set from retained/default
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
   509
        if (startup == null) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
   510
            startup = prefs.get(STARTUP_KEY, null);
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
   511
            if (startup == null) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
   512
                startup = DEFAULT_STARTUP;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
   513
            }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
   514
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
   515
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   516
        // Read retained editor setting (if any)
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   517
        editor = EditorSetting.fromPrefs(prefs);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   518
        if (editor == null) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   519
            editor = BUILT_IN_EDITOR;
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   520
        }
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   521
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   522
        resetState(); // Initialize
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   523
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   524
        // Read replay history from last jshell session into previous history
36715
ae6fa9280e0b 8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents: 36499
diff changeset
   525
        String prevReplay = prefs.get(REPLAY_RESTORE_KEY, null);
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   526
        if (prevReplay != null) {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   527
            replayableHistoryPrevious = Arrays.asList(prevReplay.split(RECORD_SEPARATOR));
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   528
        }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   529
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   530
        for (String loadFile : loadList) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   531
            runFile(loadFile, "jshell");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   532
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   533
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   534
        if (regenerateOnDeath) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   535
            hardmsg("jshell.msg.welcome", version());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   536
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   537
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   538
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   539
            while (regenerateOnDeath) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   540
                if (!live) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   541
                    resetState();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   542
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   543
                run(in);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   544
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   545
        } finally {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   546
            closeState();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   547
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   548
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   549
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   550
    /**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   551
     * Process the command line arguments.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   552
     * Set options.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   553
     * @param args the command line arguments
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   554
     * @return the list of files to be loaded
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   555
     */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   556
    private List<String> processCommandArgs(String[] args) {
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   557
        OptionParser parser = new OptionParser();
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   558
        OptionSpec<String> cp = parser.accepts("class-path").withRequiredArg();
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   559
        OptionSpec<String> st = parser.accepts("startup").withRequiredArg();
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   560
        parser.acceptsAll(asList("n", "no-startup"));
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   561
        OptionSpec<String> fb = parser.accepts("feedback").withRequiredArg();
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   562
        parser.accepts("q");
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   563
        parser.accepts("s");
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   564
        parser.accepts("v");
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   565
        OptionSpec<String> r = parser.accepts("R").withRequiredArg();
41248
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   566
        OptionSpec<String> c = parser.accepts("C").withRequiredArg();
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   567
        parser.acceptsAll(asList("h", "help"));
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   568
        parser.accepts("version");
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   569
        parser.accepts("full-version");
41248
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   570
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   571
        parser.accepts("X");
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   572
        OptionSpec<String> addExports = parser.accepts("add-exports").withRequiredArg();
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   573
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   574
        NonOptionArgumentSpec<String> loadFileSpec = parser.nonOptions();
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   575
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   576
        OptionSet options;
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   577
        try {
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   578
            options = parser.parse(args);
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   579
        } catch (OptionException ex) {
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   580
            if (ex.options().isEmpty()) {
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   581
                startmsg("jshell.err.opt.invalid", stream(args).collect(joining(", ")));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   582
            } else {
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   583
                boolean isKnown = parser.recognizedOptions().containsKey(ex.options().iterator().next());
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   584
                startmsg(isKnown
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   585
                        ? "jshell.err.opt.arg"
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   586
                        : "jshell.err.opt.unknown",
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   587
                        ex.options()
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   588
                        .stream()
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   589
                        .collect(joining(", ")));
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   590
            }
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   591
            return null;
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   592
        }
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   593
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   594
        if (options.has("help")) {
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   595
            printUsage();
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   596
            return null;
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   597
        }
41248
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   598
        if (options.has("X")) {
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   599
            printUsageX();
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   600
            return null;
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   601
        }
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   602
        if (options.has("version")) {
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   603
            cmdout.printf("jshell %s\n", version());
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   604
            return null;
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   605
        }
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   606
        if (options.has("full-version")) {
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   607
            cmdout.printf("jshell %s\n", fullVersion());
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   608
            return null;
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   609
        }
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   610
        if (options.has(cp)) {
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   611
            List<String> cps = options.valuesOf(cp);
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   612
            if (cps.size() > 1) {
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   613
                startmsg("jshell.err.opt.one", "--class-path");
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   614
                return null;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   615
            }
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   616
            cmdlineClasspath = cps.get(0);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   617
        }
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   618
        if (options.has(st)) {
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   619
            List<String> sts = options.valuesOf(st);
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   620
            if (sts.size() != 1 || options.has("no-startup")) {
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   621
                startmsg("jshell.err.opt.startup.one");
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   622
                return null;
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   623
            }
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   624
            startup = readFile(sts.get(0), "--startup");
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   625
            if (startup == null) {
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   626
                return null;
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   627
            }
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   628
        } else if (options.has("no-startup")) {
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   629
            startup = "";
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   630
        }
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   631
        if ((options.valuesOf(fb).size() +
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   632
                 (options.has("q") ? 1 : 0) +
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   633
                 (options.has("s") ? 1 : 0) +
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   634
                 (options.has("v") ? 1 : 0)) > 1) {
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   635
            startmsg("jshell.err.opt.feedback.one");
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   636
            return null;
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   637
        } else if (options.has(fb)) {
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   638
            commandLineFeedbackMode = options.valueOf(fb);
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   639
        } else if (options.has("q")) {
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   640
            commandLineFeedbackMode = "concise";
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   641
        } else if (options.has("s")) {
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   642
            commandLineFeedbackMode = "silent";
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   643
        } else if (options.has("v")) {
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   644
            commandLineFeedbackMode = "verbose";
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   645
        }
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   646
        if (options.has(r)) {
41248
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   647
            remoteVMOptions.addAll(options.valuesOf(r));
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   648
        }
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   649
        if (options.has(c)) {
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   650
            compilerOptions.addAll(options.valuesOf(c));
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   651
        }
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   652
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   653
        if (options.has(addExports)) {
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   654
            List<String> exports = options.valuesOf(addExports).stream()
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   655
                    .map(mp -> mp + "=ALL-UNNAMED")
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   656
                    .flatMap(mp -> Stream.of("--add-exports", mp))
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   657
                    .collect(toList());
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   658
            remoteVMOptions.addAll(exports);
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   659
            compilerOptions.addAll(exports);
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   660
        }
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   661
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   662
        return options.valuesOf(loadFileSpec);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   663
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   664
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   665
    private void printUsage() {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   666
        cmdout.print(getResourceString("help.usage"));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   667
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   668
41248
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   669
    private void printUsageX() {
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   670
        cmdout.print(getResourceString("help.usage.x"));
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   671
    }
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   672
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   673
    /**
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   674
     * Message handler to use during initial start-up.
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   675
     */
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   676
    private class InitMessageHandler implements MessageHandler {
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   677
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   678
        @Override
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   679
        public void fluff(String format, Object... args) {
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   680
            //ignore
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   681
        }
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   682
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   683
        @Override
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   684
        public void fluffmsg(String messageKey, Object... args) {
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   685
            //ignore
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   686
        }
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   687
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   688
        @Override
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   689
        public void hard(String format, Object... args) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   690
            //ignore
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   691
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   692
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   693
        @Override
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   694
        public void hardmsg(String messageKey, Object... args) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   695
            //ignore
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   696
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   697
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   698
        @Override
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   699
        public void errormsg(String messageKey, Object... args) {
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   700
            startmsg(messageKey, args);
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   701
        }
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   702
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   703
        @Override
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   704
        public boolean showFluff() {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   705
            return false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   706
        }
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   707
    }
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   708
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   709
    private void resetState() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   710
        closeState();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   711
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   712
        // Initialize tool id mapping
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   713
        mainNamespace = new NameSpace("main", "");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   714
        startNamespace = new NameSpace("start", "s");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   715
        errorNamespace = new NameSpace("error", "e");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   716
        mapSnippet = new LinkedHashMap<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   717
        currentNameSpace = startNamespace;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   718
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   719
        // 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
   720
        replayableHistoryPrevious = replayableHistory;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   721
        replayableHistory = new ArrayList<>();
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   722
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   723
        state = JShell.builder()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   724
                .in(userin)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   725
                .out(userout)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   726
                .err(usererr)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   727
                .tempVariableNameGenerator(()-> "$" + currentNameSpace.tidNext())
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
   728
                .idGenerator((sn, i) -> (currentNameSpace == startNamespace || state.status(sn).isActive())
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   729
                        ? currentNameSpace.tid(sn)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   730
                        : errorNamespace.tid(sn))
41248
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   731
                .remoteVMOptions(remoteVMOptions.stream().toArray(String[]::new))
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40767
diff changeset
   732
                .compilerOptions(compilerOptions.stream().toArray(String[]::new))
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   733
                .build();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   734
        shutdownSubscription = state.onShutdown((JShell deadState) -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   735
            if (deadState == state) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   736
                hardmsg("jshell.msg.terminated");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   737
                live = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   738
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   739
        });
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   740
        analysis = state.sourceCodeAnalysis();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   741
        live = true;
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   742
        if (!feedbackInitialized) {
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   743
            // One time per run feedback initialization
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   744
            feedbackInitialized = true;
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   745
            initFeedback();
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   746
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   747
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   748
        if (cmdlineClasspath != null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   749
            state.addToClasspath(cmdlineClasspath);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   750
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   751
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
   752
        startUpRun(startup);
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   753
        currentNameSpace = mainNamespace;
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   754
    }
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
   755
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
   756
    private boolean isRunningInteractive() {
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
   757
        return currentNameSpace != null && currentNameSpace == mainNamespace;
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
   758
    }
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
   759
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   760
    //where -- one-time per run initialization of feedback modes
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   761
    private void initFeedback() {
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   762
        // No fluff, no prefix, for init failures
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   763
        MessageHandler initmh = new InitMessageHandler();
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   764
        // Execute the feedback initialization code in the resource file
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   765
        startUpRun(getResourceString("startup.feedback"));
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   766
        // These predefined modes are read-only
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   767
        feedback.markModesReadOnly();
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   768
        // Restore user defined modes retained on previous run with /set mode -retain
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   769
        String encoded = prefs.get(MODE_KEY, null);
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
   770
        if (encoded != null && !encoded.isEmpty()) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
   771
            if (!feedback.restoreEncodedModes(initmh, encoded)) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
   772
                // Catastrophic corruption -- remove the retained modes
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
   773
                prefs.remove(MODE_KEY);
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
   774
            }
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   775
        }
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   776
        if (commandLineFeedbackMode != null) {
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   777
            // The feedback mode to use was specified on the command line, use it
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   778
            if (!setFeedback(initmh, new ArgTokenizer("--feedback", commandLineFeedbackMode))) {
37745
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
   779
                regenerateOnDeath = false;
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
   780
            }
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   781
            commandLineFeedbackMode = null;
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   782
        } else {
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   783
            String fb = prefs.get(FEEDBACK_KEY, null);
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   784
            if (fb != null) {
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   785
                // 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
   786
                // on a previous run with /set feedback -retain
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
   787
                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
   788
            }
37745
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 37389
diff changeset
   789
        }
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   790
    }
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   791
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   792
    //where
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   793
    private void startUpRun(String start) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   794
        try (IOContext suin = new FileScannerIOContext(new StringReader(start))) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   795
            run(suin);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   796
        } catch (Exception ex) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   797
            hardmsg("jshell.err.startup.unexpected.exception", ex);
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
   798
            ex.printStackTrace(cmdout);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   799
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   800
    }
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
    private void closeState() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   803
        live = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   804
        JShell oldState = state;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   805
        if (oldState != null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   806
            oldState.unsubscribe(shutdownSubscription); // No notification
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   807
            oldState.close();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   808
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   809
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   810
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   811
    /**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   812
     * Main loop
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   813
     * @param in the line input/editing context
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   814
     */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   815
    private void run(IOContext in) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   816
        IOContext oldInput = input;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   817
        input = in;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   818
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   819
            String incomplete = "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   820
            while (live) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   821
                String prompt;
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
   822
                if (isRunningInteractive()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   823
                    prompt = testPrompt
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   824
                                    ? incomplete.isEmpty()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   825
                                            ? "\u0005" //ENQ
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   826
                                            : "\u0006" //ACK
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   827
                                    : incomplete.isEmpty()
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   828
                                            ? feedback.getPrompt(currentNameSpace.tidNext())
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
   829
                                            : feedback.getContinuationPrompt(currentNameSpace.tidNext())
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   830
                    ;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   831
                } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   832
                    prompt = "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   833
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   834
                String raw;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   835
                try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   836
                    raw = in.readLine(prompt, incomplete);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   837
                } catch (InputInterruptedException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   838
                    //input interrupted - clearing current state
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   839
                    incomplete = "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   840
                    continue;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   841
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   842
                if (raw == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   843
                    //EOF
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   844
                    if (in.interactiveOutput()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   845
                        // End after user ctrl-D
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   846
                        regenerateOnDeath = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   847
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   848
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   849
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   850
                String trimmed = trimEnd(raw);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   851
                if (!trimmed.isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   852
                    String line = incomplete + trimmed;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   853
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   854
                    // No commands in the middle of unprocessed source
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   855
                    if (incomplete.isEmpty() && line.startsWith("/") && !line.startsWith("//") && !line.startsWith("/*")) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   856
                        processCommand(line.trim());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   857
                    } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   858
                        incomplete = processSourceCatchingReset(line);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   859
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   860
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   861
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   862
        } catch (IOException ex) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   863
            errormsg("jshell.err.unexpected.exception", ex);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   864
        } finally {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   865
            input = oldInput;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   866
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   867
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   868
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   869
    private void addToReplayHistory(String s) {
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38539
diff changeset
   870
        if (isRunningInteractive()) {
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   871
            replayableHistory.add(s);
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   872
        }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   873
    }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   874
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   875
    private String processSourceCatchingReset(String src) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   876
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   877
            input.beforeUserCode();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   878
            return processSource(src);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   879
        } catch (IllegalStateException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   880
            hard("Resetting...");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   881
            live = false; // Make double sure
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   882
            return "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   883
        } finally {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   884
            input.afterUserCode();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   885
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   886
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   887
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   888
    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
   889
        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
   890
            try {
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   891
                //handle "/-[number]"
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   892
                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
   893
                return ;
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   894
            } 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
   895
                //ignore
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   896
            }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   897
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   898
        String arg = "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   899
        int idx = cmd.indexOf(' ');
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   900
        if (idx > 0) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   901
            arg = cmd.substring(idx + 1).trim();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   902
            cmd = cmd.substring(0, idx);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   903
        }
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   904
        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
   905
        switch (candidates.length) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   906
            case 0:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   907
                if (!rerunHistoryEntryById(cmd.substring(1))) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   908
                    errormsg("jshell.err.no.such.command.or.snippet.id", cmd);
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   909
                    fluffmsg("jshell.msg.help.for.help");
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   910
                }   break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   911
            case 1:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   912
                Command command = candidates[0];
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   913
                // 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
   914
                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
   915
                    addToReplayHistory((command.command + " " + arg).trim());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   916
                }   break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   917
            default:
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   918
                errormsg("jshell.err.command.ambiguous", cmd,
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   919
                        Arrays.stream(candidates).map(c -> c.command).collect(Collectors.joining(", ")));
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   920
                fluffmsg("jshell.msg.help.for.help");
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
   921
                break;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   922
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   923
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   924
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
   925
    private Command[] findCommand(String cmd, Predicate<Command> filter) {
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
   926
        Command exact = commands.get(cmd);
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
   927
        if (exact != null)
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
   928
            return new Command[] {exact};
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
   929
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
   930
        return commands.values()
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
   931
                       .stream()
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
   932
                       .filter(filter)
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
   933
                       .filter(command -> command.command.startsWith(cmd))
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
   934
                       .toArray(size -> new Command[size]);
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
   935
    }
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
   936
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   937
    private static Path toPathResolvingUserHome(String pathString) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   938
        if (pathString.replace(File.separatorChar, '/').startsWith("~/"))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   939
            return Paths.get(System.getProperty("user.home"), pathString.substring(2));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   940
        else
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   941
            return Paths.get(pathString);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   942
    }
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
    static final class Command {
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
   945
        public final String command;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   946
        public final String helpKey;
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
   947
        public final Function<String,Boolean> run;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   948
        public final CompletionProvider completions;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   949
        public final CommandKind kind;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   950
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   951
        // NORMAL Commands
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   952
        public Command(String command, Function<String,Boolean> run, CompletionProvider completions) {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   953
            this(command, run, completions, CommandKind.NORMAL);
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   954
        }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   955
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   956
        // Special kinds of Commands
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   957
        public Command(String command, Function<String,Boolean> run, CompletionProvider completions, CommandKind kind) {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   958
            this(command, "help." + command.substring(1),
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   959
                    run, completions, kind);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   960
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   961
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   962
        // Documentation pseudo-commands
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   963
        public Command(String command, String helpKey, CommandKind kind) {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   964
            this(command, helpKey,
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   965
                    arg -> { throw new IllegalStateException(); },
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   966
                    EMPTY_COMPLETION_PROVIDER,
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   967
                    kind);
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   968
        }
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   969
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   970
        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
   971
            this.command = command;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
   972
            this.helpKey = helpKey;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   973
            this.run = run;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   974
            this.completions = completions;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   975
            this.kind = kind;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   976
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   977
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   978
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   979
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   980
    interface CompletionProvider {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   981
        List<Suggestion> completionSuggestions(String input, int cursor, int[] anchor);
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
   982
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   983
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   984
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   985
    enum CommandKind {
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   986
        NORMAL(true, true, true),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   987
        REPLAY(true, true, true),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   988
        HIDDEN(true, false, false),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   989
        HELP_ONLY(false, true, false),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   990
        HELP_SUBJECT(false, false, false);
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   991
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   992
        final boolean isRealCommand;
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   993
        final boolean showInHelp;
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   994
        final boolean shouldSuggestCompletions;
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   995
        private CommandKind(boolean isRealCommand, boolean showInHelp, boolean shouldSuggestCompletions) {
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   996
            this.isRealCommand = isRealCommand;
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   997
            this.showInHelp = showInHelp;
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   998
            this.shouldSuggestCompletions = shouldSuggestCompletions;
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
   999
        }
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1000
    }
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1001
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1002
    static final class FixedCompletionProvider implements CompletionProvider {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1003
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1004
        private final String[] alternatives;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1005
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1006
        public FixedCompletionProvider(String... alternatives) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1007
            this.alternatives = alternatives;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1008
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1009
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1010
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1011
        public List<Suggestion> completionSuggestions(String input, int cursor, int[] anchor) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1012
            List<Suggestion> result = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1013
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1014
            for (String alternative : alternatives) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1015
                if (alternative.startsWith(input)) {
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  1016
                    result.add(new ArgSuggestion(alternative));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1017
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1018
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1019
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1020
            anchor[0] = 0;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1021
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1022
            return result;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1023
        }
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
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1027
    static final CompletionProvider EMPTY_COMPLETION_PROVIDER = new FixedCompletionProvider();
38520
17e72b872ffd 8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents: 38514
diff changeset
  1028
    private static final CompletionProvider KEYWORD_COMPLETION_PROVIDER = new FixedCompletionProvider("-all ", "-start ", "-history ");
17e72b872ffd 8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents: 38514
diff changeset
  1029
    private static final CompletionProvider RELOAD_OPTIONS_COMPLETION_PROVIDER = new FixedCompletionProvider("-restore", "-quiet");
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1030
    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
  1031
    private static final CompletionProvider FILE_COMPLETION_PROVIDER = fileCompletions(p -> true);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1032
    private final Map<String, Command> commands = new LinkedHashMap<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1033
    private void registerCommand(Command cmd) {
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33714
diff changeset
  1034
        commands.put(cmd.command, cmd);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1035
    }
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1036
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1037
    private static CompletionProvider skipWordThenCompletion(CompletionProvider completionProvider) {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1038
        return (input, cursor, anchor) -> {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1039
            List<Suggestion> result = Collections.emptyList();
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1040
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1041
            int space = input.indexOf(' ');
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1042
            if (space != -1) {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1043
                String rest = input.substring(space + 1);
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1044
                result = completionProvider.completionSuggestions(rest, cursor - space - 1, anchor);
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1045
                anchor[0] += space + 1;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1046
            }
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1047
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1048
            return result;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1049
        };
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1050
    }
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1051
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1052
    private static CompletionProvider fileCompletions(Predicate<Path> accept) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1053
        return (code, cursor, anchor) -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1054
            int lastSlash = code.lastIndexOf('/');
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1055
            String path = code.substring(0, lastSlash + 1);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1056
            String prefix = lastSlash != (-1) ? code.substring(lastSlash + 1) : code;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1057
            Path current = toPathResolvingUserHome(path);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1058
            List<Suggestion> result = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1059
            try (Stream<Path> dir = Files.list(current)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1060
                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
  1061
                   .map(f -> new ArgSuggestion(f.getFileName() + (Files.isDirectory(f) ? "/" : "")))
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1062
                   .forEach(result::add);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1063
            } catch (IOException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1064
                //ignore...
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1065
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1066
            if (path.isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1067
                StreamSupport.stream(FileSystems.getDefault().getRootDirectories().spliterator(), false)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1068
                             .filter(root -> accept.test(root) && root.toString().startsWith(prefix))
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  1069
                             .map(root -> new ArgSuggestion(root.toString()))
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1070
                             .forEach(result::add);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1071
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1072
            anchor[0] = path.length();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1073
            return result;
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
    }
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
    private static CompletionProvider classPathCompletion() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1078
        return fileCompletions(p -> Files.isDirectory(p) ||
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1079
                                    p.getFileName().toString().endsWith(".zip") ||
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1080
                                    p.getFileName().toString().endsWith(".jar"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1081
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1082
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1083
    private CompletionProvider snippetCompletion(Supplier<Stream<? extends Snippet>> snippetsSupplier) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1084
        return (prefix, cursor, anchor) -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1085
            anchor[0] = 0;
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1086
            return snippetsSupplier.get()
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1087
                        .flatMap(k -> (k instanceof DeclarationSnippet)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1088
                                ? Stream.of(String.valueOf(k.id()), ((DeclarationSnippet) k).name())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1089
                                : Stream.of(String.valueOf(k.id())))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1090
                        .filter(k -> k.startsWith(prefix))
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  1091
                        .map(k -> new ArgSuggestion(k))
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1092
                        .collect(Collectors.toList());
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
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1096
    private CompletionProvider snippetKeywordCompletion(Supplier<Stream<? extends Snippet>> snippetsSupplier) {
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1097
        return (code, cursor, anchor) -> {
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1098
            List<Suggestion> result = new ArrayList<>();
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1099
            result.addAll(KEYWORD_COMPLETION_PROVIDER.completionSuggestions(code, cursor, anchor));
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1100
            result.addAll(snippetCompletion(snippetsSupplier).completionSuggestions(code, cursor, anchor));
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1101
            return result;
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1102
        };
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1103
    }
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1104
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1105
    private static CompletionProvider saveCompletion() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1106
        return (code, cursor, anchor) -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1107
            List<Suggestion> result = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1108
            int space = code.indexOf(' ');
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1109
            if (space == (-1)) {
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1110
                result.addAll(KEYWORD_COMPLETION_PROVIDER.completionSuggestions(code, cursor, anchor));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1111
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1112
            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
  1113
            anchor[0] += space + 1;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1114
            return result;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1115
        };
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1116
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1117
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1118
    private static CompletionProvider reloadCompletion() {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1119
        return (code, cursor, anchor) -> {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1120
            List<Suggestion> result = new ArrayList<>();
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1121
            int pastSpace = code.indexOf(' ') + 1; // zero if no space
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1122
            result.addAll(RELOAD_OPTIONS_COMPLETION_PROVIDER.completionSuggestions(code.substring(pastSpace), cursor - pastSpace, anchor));
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1123
            anchor[0] += pastSpace;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1124
            return result;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1125
        };
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1126
    }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1127
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1128
    private static CompletionProvider orMostSpecificCompletion(
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1129
            CompletionProvider left, CompletionProvider right) {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1130
        return (code, cursor, anchor) -> {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1131
            int[] leftAnchor = {-1};
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1132
            int[] rightAnchor = {-1};
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1133
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1134
            List<Suggestion> leftSuggestions = left.completionSuggestions(code, cursor, leftAnchor);
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1135
            List<Suggestion> rightSuggestions = right.completionSuggestions(code, cursor, rightAnchor);
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1136
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1137
            List<Suggestion> suggestions = new ArrayList<>();
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1138
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1139
            if (leftAnchor[0] >= rightAnchor[0]) {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1140
                anchor[0] = leftAnchor[0];
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1141
                suggestions.addAll(leftSuggestions);
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1142
            }
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1143
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1144
            if (leftAnchor[0] <= rightAnchor[0]) {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1145
                anchor[0] = rightAnchor[0];
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1146
                suggestions.addAll(rightSuggestions);
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1147
            }
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1148
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1149
            return suggestions;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1150
        };
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1151
    }
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1152
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1153
    // Snippet lists
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1154
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1155
    Stream<Snippet> allSnippets() {
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1156
        return state.snippets();
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1157
    }
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1158
41514
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 41248
diff changeset
  1159
    Stream<Snippet> dropableSnippets() {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1160
        return state.snippets()
41514
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 41248
diff changeset
  1161
                .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
  1162
    }
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1163
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1164
    Stream<VarSnippet> allVarSnippets() {
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1165
        return state.snippets()
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1166
                .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
  1167
                .map(sn -> (VarSnippet) sn);
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1168
    }
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1169
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1170
    Stream<MethodSnippet> allMethodSnippets() {
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1171
        return state.snippets()
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1172
                .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
  1173
                .map(sn -> (MethodSnippet) sn);
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1174
    }
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1175
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1176
    Stream<TypeDeclSnippet> allTypeSnippets() {
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1177
        return state.snippets()
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1178
                .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
  1179
                .map(sn -> (TypeDeclSnippet) sn);
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1180
    }
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1181
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1182
    // 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
  1183
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1184
    {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1185
        registerCommand(new Command("/list",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1186
                arg -> cmdList(arg),
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1187
                snippetKeywordCompletion(this::allSnippets)));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1188
        registerCommand(new Command("/edit",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1189
                arg -> cmdEdit(arg),
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1190
                snippetCompletion(this::allSnippets)));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1191
        registerCommand(new Command("/drop",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1192
                arg -> cmdDrop(arg),
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1193
                snippetCompletion(this::dropableSnippets),
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1194
                CommandKind.REPLAY));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1195
        registerCommand(new Command("/save",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1196
                arg -> cmdSave(arg),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1197
                saveCompletion()));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1198
        registerCommand(new Command("/open",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1199
                arg -> cmdOpen(arg),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1200
                FILE_COMPLETION_PROVIDER));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1201
        registerCommand(new Command("/vars",
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1202
                arg -> cmdVars(arg),
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1203
                snippetKeywordCompletion(this::allVarSnippets)));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1204
        registerCommand(new Command("/methods",
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1205
                arg -> cmdMethods(arg),
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1206
                snippetKeywordCompletion(this::allMethodSnippets)));
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1207
        registerCommand(new Command("/types",
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1208
                arg -> cmdTypes(arg),
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1209
                snippetKeywordCompletion(this::allTypeSnippets)));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1210
        registerCommand(new Command("/imports",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1211
                arg -> cmdImports(),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1212
                EMPTY_COMPLETION_PROVIDER));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1213
        registerCommand(new Command("/exit",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1214
                arg -> cmdExit(),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1215
                EMPTY_COMPLETION_PROVIDER));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1216
        registerCommand(new Command("/reset",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1217
                arg -> cmdReset(),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1218
                EMPTY_COMPLETION_PROVIDER));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1219
        registerCommand(new Command("/reload",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1220
                arg -> cmdReload(arg),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1221
                reloadCompletion()));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1222
        registerCommand(new Command("/classpath",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1223
                arg -> cmdClasspath(arg),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1224
                classPathCompletion(),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1225
                CommandKind.REPLAY));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1226
        registerCommand(new Command("/history",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1227
                arg -> cmdHistory(),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1228
                EMPTY_COMPLETION_PROVIDER));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1229
        registerCommand(new Command("/debug",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1230
                arg -> cmdDebug(arg),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1231
                EMPTY_COMPLETION_PROVIDER,
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1232
                CommandKind.HIDDEN));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1233
        registerCommand(new Command("/help",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1234
                arg -> cmdHelp(arg),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1235
                EMPTY_COMPLETION_PROVIDER));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1236
        registerCommand(new Command("/set",
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1237
                arg -> cmdSet(arg),
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1238
                new ContinuousCompletionProvider(Map.of(
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1239
                        // need more completion for format for usability
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1240
                        "format", feedback.modeCompletions(),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1241
                        "truncation", feedback.modeCompletions(),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1242
                        "feedback", feedback.modeCompletions(),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1243
                        "mode", skipWordThenCompletion(orMostSpecificCompletion(
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1244
                                feedback.modeCompletions(SET_MODE_OPTIONS_COMPLETION_PROVIDER),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1245
                                SET_MODE_OPTIONS_COMPLETION_PROVIDER)),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1246
                        "prompt", feedback.modeCompletions(),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1247
                        "editor", fileCompletions(Files::isExecutable),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1248
                        "start", FILE_COMPLETION_PROVIDER),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1249
                        STARTSWITH_MATCHER)));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1250
        registerCommand(new Command("/?",
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1251
                "help.quest",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1252
                arg -> cmdHelp(arg),
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1253
                EMPTY_COMPLETION_PROVIDER,
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1254
                CommandKind.NORMAL));
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1255
        registerCommand(new Command("/!",
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1256
                "help.bang",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1257
                arg -> cmdUseHistoryEntry(-1),
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1258
                EMPTY_COMPLETION_PROVIDER,
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1259
                CommandKind.NORMAL));
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1260
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1261
        // Documentation pseudo-commands
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1262
        registerCommand(new Command("/<id>",
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1263
                "help.id",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1264
                CommandKind.HELP_ONLY));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1265
        registerCommand(new Command("/-<n>",
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1266
                "help.previous",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1267
                CommandKind.HELP_ONLY));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1268
        registerCommand(new Command("intro",
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1269
                "help.intro",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1270
                CommandKind.HELP_SUBJECT));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1271
        registerCommand(new Command("shortcuts",
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1272
                "help.shortcuts",
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1273
                CommandKind.HELP_SUBJECT));
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1274
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1275
        commandCompletions = new ContinuousCompletionProvider(
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1276
                commands.values().stream()
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1277
                        .filter(c -> c.kind.shouldSuggestCompletions)
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1278
                        .collect(toMap(c -> c.command, c -> c.completions)),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1279
                STARTSWITH_MATCHER);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1280
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1281
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1282
    private ContinuousCompletionProvider commandCompletions;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1283
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1284
    public List<Suggestion> commandCompletionSuggestions(String code, int cursor, int[] anchor) {
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  1285
        return commandCompletions.completionSuggestions(code, cursor, anchor);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1286
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1287
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1288
    public String commandDocumentation(String code, int cursor) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1289
        code = code.substring(0, cursor);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1290
        int space = code.indexOf(' ');
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1291
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1292
        if (space != (-1)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1293
            String cmd = code.substring(0, space);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1294
            Command command = commands.get(cmd);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1295
            if (command != null) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1296
                return getResourceString(command.helpKey + ".summary");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1297
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1298
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1299
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1300
        return null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1301
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1302
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1303
    // --- Command implementations ---
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1304
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1305
    private static final String[] SET_SUBCOMMANDS = new String[]{
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1306
        "format", "truncation", "feedback", "mode", "prompt", "editor", "start"};
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1307
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1308
    final boolean cmdSet(String arg) {
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1309
        String cmd = "/set";
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1310
        ArgTokenizer at = new ArgTokenizer(cmd, arg.trim());
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1311
        String which = subCommand(cmd, at, SET_SUBCOMMANDS);
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1312
        if (which == null) {
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1313
            return false;
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1314
        }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1315
        switch (which) {
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1316
            case "_retain": {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1317
                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
  1318
                return false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1319
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1320
            case "_blank": {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1321
                // show top-level settings
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1322
                new SetEditor().set();
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1323
                showSetStart();
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1324
                setFeedback(this, at); // no args so shows feedback setting
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1325
                hardmsg("jshell.msg.set.show.mode.settings");
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1326
                return true;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1327
            }
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1328
            case "format":
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1329
                return feedback.setFormat(this, at);
38513
0ae85633d035 8154812: jshell tool: value printing truncation
rfield
parents: 37745
diff changeset
  1330
            case "truncation":
0ae85633d035 8154812: jshell tool: value printing truncation
rfield
parents: 37745
diff changeset
  1331
                return feedback.setTruncation(this, at);
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1332
            case "feedback":
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1333
                return setFeedback(this, at);
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1334
            case "mode":
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1335
                return feedback.setMode(this, at,
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1336
                        retained -> prefs.put(MODE_KEY, retained));
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1337
            case "prompt":
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1338
                return feedback.setPrompt(this, at);
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1339
            case "editor":
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1340
                return new SetEditor(at).set();
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1341
            case "start":
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1342
                return setStart(at);
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1343
            default:
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1344
                errormsg("jshell.err.arg", cmd, at.val());
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1345
                return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1346
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1347
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1348
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1349
    boolean setFeedback(MessageHandler messageHandler, ArgTokenizer at) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1350
        return feedback.setFeedback(messageHandler, at,
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1351
                fb -> prefs.put(FEEDBACK_KEY, fb));
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1352
    }
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1353
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1354
    // Find which, if any, sub-command matches.
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1355
    // Return null on error
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1356
    String subCommand(String cmd, ArgTokenizer at, String[] subs) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1357
        at.allowedOptions("-retain");
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1358
        String sub = at.next();
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1359
        if (sub == null) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1360
            // No sub-command was given
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1361
            return at.hasOption("-retain")
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1362
                    ? "_retain"
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1363
                    : "_blank";
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1364
        }
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1365
        String[] matches = Arrays.stream(subs)
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1366
                .filter(s -> s.startsWith(sub))
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1367
                .toArray(size -> new String[size]);
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1368
        if (matches.length == 0) {
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1369
            // There are no matching sub-commands
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1370
            errormsg("jshell.err.arg", cmd, sub);
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1371
            fluffmsg("jshell.msg.use.one.of", Arrays.stream(subs)
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1372
                    .collect(Collectors.joining(", "))
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1373
            );
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1374
            return null;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1375
        }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1376
        if (matches.length > 1) {
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1377
            // 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
  1378
            errormsg("jshell.err.sub.ambiguous", cmd, sub);
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1379
            fluffmsg("jshell.msg.use.one.of", Arrays.stream(matches)
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1380
                    .collect(Collectors.joining(", "))
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1381
            );
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1382
            return null;
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1383
        }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1384
        return matches[0];
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1385
    }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1386
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1387
    static class EditorSetting {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1388
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1389
        static String BUILT_IN_REP = "-default";
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1390
        static char WAIT_PREFIX = '-';
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1391
        static char NORMAL_PREFIX = '*';
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1392
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1393
        final String[] cmd;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1394
        final boolean wait;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1395
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1396
        EditorSetting(String[] cmd, boolean wait) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1397
            this.wait = wait;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1398
            this.cmd = cmd;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1399
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1400
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1401
        // returns null if not stored in preferences
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1402
        static EditorSetting fromPrefs(Preferences prefs) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1403
            // Read retained editor setting (if any)
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1404
            String editorString = prefs.get(EDITOR_KEY, "");
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1405
            if (editorString == null || editorString.isEmpty()) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1406
                return null;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1407
            } else if (editorString.equals(BUILT_IN_REP)) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1408
                return BUILT_IN_EDITOR;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1409
            } else {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1410
                boolean wait = false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1411
                char waitMarker = editorString.charAt(0);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1412
                if (waitMarker == WAIT_PREFIX || waitMarker == NORMAL_PREFIX) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1413
                    wait = waitMarker == WAIT_PREFIX;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1414
                    editorString = editorString.substring(1);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1415
                }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1416
                String[] cmd = editorString.split(RECORD_SEPARATOR);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1417
                return new EditorSetting(cmd, wait);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1418
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1419
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1420
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1421
        void toPrefs(Preferences prefs) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1422
            prefs.put(EDITOR_KEY, (this == BUILT_IN_EDITOR)
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1423
                    ? BUILT_IN_REP
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1424
                    : (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
  1425
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1426
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1427
        @Override
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1428
        public boolean equals(Object o) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1429
            if (o instanceof EditorSetting) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1430
                EditorSetting ed = (EditorSetting) o;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1431
                return Arrays.equals(cmd, ed.cmd) && wait == ed.wait;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1432
            } else {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1433
                return false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1434
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1435
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1436
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1437
        @Override
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1438
        public int hashCode() {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1439
            int hash = 7;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1440
            hash = 71 * hash + Arrays.deepHashCode(this.cmd);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1441
            hash = 71 * hash + (this.wait ? 1 : 0);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1442
            return hash;
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1443
        }
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1444
    }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1445
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1446
    class SetEditor {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1447
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1448
        private final ArgTokenizer at;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1449
        private final String[] command;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1450
        private final boolean hasCommand;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1451
        private final boolean defaultOption;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1452
        private final boolean waitOption;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1453
        private final boolean retainOption;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1454
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1455
        SetEditor(ArgTokenizer at) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1456
            at.allowedOptions("-default", "-wait", "-retain");
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1457
            String prog = at.next();
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1458
            List<String> ed = new ArrayList<>();
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1459
            while (at.val() != null) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1460
                ed.add(at.val());
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1461
                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
  1462
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1463
            this.at = at;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1464
            this.command = ed.toArray(new String[ed.size()]);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1465
            this.hasCommand = command.length > 0;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1466
            this.defaultOption = at.hasOption("-default");
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1467
            this.waitOption = at.hasOption("-wait");
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1468
            this.retainOption = at.hasOption("-retain");
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1469
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1470
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1471
        SetEditor() {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1472
            this(new ArgTokenizer("", ""));
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1473
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1474
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1475
        boolean set() {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1476
            if (!check()) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1477
                return false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1478
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1479
            if (!hasCommand && !defaultOption && !retainOption) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1480
                // No settings or -retain, so this is a query
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1481
                EditorSetting retained = EditorSetting.fromPrefs(prefs);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1482
                if (retained != null) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1483
                    // retained editor is set
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1484
                    hard("/set editor -retain %s", format(retained));
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1485
                }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1486
                if (retained == null || !retained.equals(editor)) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1487
                    // 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
  1488
                    hard("/set editor %s", format(editor));
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1489
                }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1490
                return true;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1491
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1492
            install();
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1493
            if (retainOption) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1494
                editor.toPrefs(prefs);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1495
                fluffmsg("jshell.msg.set.editor.retain", format(editor));
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1496
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1497
            return true;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1498
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1499
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1500
        private boolean check() {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1501
            if (!checkOptionsAndRemainingInput(at)) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1502
                return false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1503
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1504
            if (hasCommand && defaultOption) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1505
                errormsg("jshell.err.default.option.or.program", at.whole());
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1506
                return false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1507
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1508
            if (waitOption && !hasCommand) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1509
                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
  1510
                return false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1511
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1512
            return true;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1513
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1514
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1515
        private void install() {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1516
            if (hasCommand) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1517
                editor = new EditorSetting(command, waitOption);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1518
            } else if (defaultOption) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1519
                editor = BUILT_IN_EDITOR;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1520
            } else {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1521
                return;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1522
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1523
            fluffmsg("jshell.msg.set.editor.set", format(editor));
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1524
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1525
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1526
        private String format(EditorSetting ed) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1527
            if (ed == BUILT_IN_EDITOR) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1528
                return "-default";
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1529
            } else {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1530
                Stream<String> elems = Arrays.stream(ed.cmd);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1531
                if (ed.wait) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1532
                    elems = Stream.concat(Stream.of("-wait"), elems);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1533
                }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1534
                return elems.collect(joining(" "));
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1535
            }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1536
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1537
    }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1538
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1539
    // The sub-command:  /set start <start-file>
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1540
    boolean setStart(ArgTokenizer at) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1541
        at.allowedOptions("-default", "-none", "-retain");
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1542
        String fn = at.next();
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1543
        if (!checkOptionsAndRemainingInput(at)) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1544
            return false;
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1545
        }
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1546
        boolean defaultOption = at.hasOption("-default");
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1547
        boolean noneOption = at.hasOption("-none");
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1548
        boolean retainOption = at.hasOption("-retain");
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1549
        boolean hasFile = fn != null;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1550
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1551
        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
  1552
        if (argCount > 1) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1553
            errormsg("jshell.err.option.or.filename", at.whole());
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1554
            return false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1555
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1556
        if (argCount == 0 && !retainOption) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1557
            // no options or filename, show current setting
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1558
            showSetStart();
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1559
            return true;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1560
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1561
        if (hasFile) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1562
            String init = readFile(fn, "/set start");
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1563
            if (init == null) {
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1564
                return false;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1565
            }
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1566
            startup = init;
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1567
        } else if (defaultOption) {
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1568
            startup = DEFAULT_STARTUP;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1569
        } else if (noneOption) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1570
            startup = "";
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1571
        }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1572
        if (retainOption) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1573
            // retain startup setting
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1574
            prefs.put(STARTUP_KEY, startup);
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1575
        }
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1576
        return true;
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1577
    }
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1578
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1579
    void showSetStart() {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1580
        String retained = prefs.get(STARTUP_KEY, null);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1581
        if (retained != null) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1582
            showSetStart(true, retained);
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1583
        }
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1584
        if (retained == null || !startup.equals(retained)) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1585
            showSetStart(false, startup);
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1586
        }
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1587
    }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1588
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1589
    void showSetStart(boolean isRetained, String start) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1590
        String cmd = "/set start" + (isRetained ? " -retain " : " ");
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1591
        String stset;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1592
        if (start.equals(DEFAULT_STARTUP)) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1593
            stset = cmd + "-default";
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1594
        } else if (start.isEmpty()) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1595
            stset = cmd + "-none";
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1596
        } else {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1597
            stset = prefix("startup.jsh:\n" + start + "\n" + cmd + "startup.jsh", "");
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1598
        }
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1599
        hard(stset);
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1600
    }
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1601
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1602
    boolean cmdClasspath(String arg) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1603
        if (arg.isEmpty()) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1604
            errormsg("jshell.err.classpath.arg");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1605
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1606
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1607
            state.addToClasspath(toPathResolvingUserHome(arg).toString());
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1608
            fluffmsg("jshell.msg.classpath", arg);
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1609
            return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1610
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1611
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1612
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1613
    boolean cmdDebug(String arg) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1614
        if (arg.isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1615
            debug = !debug;
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
  1616
            InternalDebugControl.setDebugFlags(state, debug ? DBG_GEN : 0);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1617
            fluff("Debugging %s", debug ? "on" : "off");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1618
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1619
            int flags = 0;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1620
            for (char ch : arg.toCharArray()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1621
                switch (ch) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1622
                    case '0':
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1623
                        flags = 0;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1624
                        debug = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1625
                        fluff("Debugging off");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1626
                        break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1627
                    case 'r':
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1628
                        debug = true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1629
                        fluff("REPL tool debugging on");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1630
                        break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1631
                    case 'g':
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
  1632
                        flags |= DBG_GEN;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1633
                        fluff("General debugging on");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1634
                        break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1635
                    case 'f':
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
  1636
                        flags |= DBG_FMGR;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1637
                        fluff("File manager debugging on");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1638
                        break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1639
                    case 'c':
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
  1640
                        flags |= DBG_COMPA;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1641
                        fluff("Completion analysis debugging on");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1642
                        break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1643
                    case 'd':
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
  1644
                        flags |= DBG_DEP;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1645
                        fluff("Dependency debugging on");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1646
                        break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1647
                    case 'e':
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38531
diff changeset
  1648
                        flags |= DBG_EVNT;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1649
                        fluff("Event debugging on");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1650
                        break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1651
                    default:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1652
                        hard("Unknown debugging option: %c", ch);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1653
                        fluff("Use: 0 r g f c d");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1654
                        return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1655
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1656
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1657
            InternalDebugControl.setDebugFlags(state, flags);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1658
        }
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1659
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1660
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1661
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1662
    private boolean cmdExit() {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1663
        regenerateOnDeath = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1664
        live = false;
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1665
        if (!replayableHistory.isEmpty()) {
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1666
            // Prevent history overflow by calculating what will fit, starting
38912
2ddd6171351e 8155581: jshell tool: replace use of Option.get()
ntv
parents: 38908
diff changeset
  1667
            // with most recent
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1668
            int sepLen = RECORD_SEPARATOR.length();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1669
            int length = 0;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1670
            int first = replayableHistory.size();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1671
            while(length < Preferences.MAX_VALUE_LENGTH && --first >= 0) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1672
                length += replayableHistory.get(first).length() + sepLen;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1673
            }
38912
2ddd6171351e 8155581: jshell tool: replace use of Option.get()
ntv
parents: 38908
diff changeset
  1674
            String hist =  String.join(RECORD_SEPARATOR,
2ddd6171351e 8155581: jshell tool: replace use of Option.get()
ntv
parents: 38908
diff changeset
  1675
                    replayableHistory.subList(first + 1, replayableHistory.size()));
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  1676
            prefs.put(REPLAY_RESTORE_KEY, hist);
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1677
        }
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1678
        fluffmsg("jshell.msg.goodbye");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1679
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1680
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1681
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1682
    boolean cmdHelp(String arg) {
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1683
        ArgTokenizer at = new ArgTokenizer("/help", arg);
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1684
        String subject = at.next();
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1685
        if (subject != null) {
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1686
            Command[] matches = commands.values().stream()
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1687
                    .filter(c -> c.command.startsWith(subject))
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1688
                    .toArray(size -> new Command[size]);
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1689
            if (matches.length == 1) {
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1690
                String cmd = matches[0].command;
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1691
                if (cmd.equals("/set")) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1692
                    // Print the help doc for the specified sub-command
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1693
                    String which = subCommand(cmd, at, SET_SUBCOMMANDS);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1694
                    if (which == null) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1695
                        return false;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1696
                    }
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1697
                    if (!which.equals("_blank")) {
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1698
                        hardrb("help.set." + which);
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1699
                        return true;
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1700
                    }
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  1701
                }
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1702
            }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1703
            if (matches.length > 0) {
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1704
                for (Command c : matches) {
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1705
                    hard("");
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1706
                    hard("%s", c.command);
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1707
                    hard("");
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1708
                    hardrb(c.helpKey);
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1709
                }
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1710
                return true;
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1711
            } else {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1712
                errormsg("jshell.err.help.arg", arg);
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1713
            }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1714
        }
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1715
        hardmsg("jshell.msg.help.begin");
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1716
        hardPairs(commands.values().stream()
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1717
                .filter(cmd -> cmd.kind.showInHelp),
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1718
                cmd -> cmd.command + " " + getResourceString(cmd.helpKey + ".args"),
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1719
                cmd -> getResourceString(cmd.helpKey + ".summary")
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1720
        );
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1721
        hardmsg("jshell.msg.help.subject");
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1722
        hardPairs(commands.values().stream()
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1723
                .filter(cmd -> cmd.kind == CommandKind.HELP_SUBJECT),
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1724
                cmd -> cmd.command,
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1725
                cmd -> getResourceString(cmd.helpKey + ".summary")
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  1726
        );
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1727
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1728
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1729
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1730
    private boolean cmdHistory() {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1731
        cmdout.println();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1732
        for (String s : input.currentSessionHistory()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1733
            // No number prefix, confusing with snippet ids
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1734
            cmdout.printf("%s\n", s);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1735
        }
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1736
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1737
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1738
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1739
    /**
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1740
     * Avoid parameterized varargs possible heap pollution warning.
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1741
     */
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1742
    private interface SnippetPredicate<T extends Snippet> extends Predicate<T> { }
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1743
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1744
    /**
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1745
     * 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
  1746
     * Adapted from Stuart Marks
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1747
     *
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1748
     * @param supplier Supply the Snippet stream to filter
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1749
     * @param filters Filters to attempt
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1750
     * @return The non-empty filtered Stream, or null
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1751
     */
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1752
    @SafeVarargs
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1753
    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
  1754
            SnippetPredicate<T>... filters) {
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1755
        for (SnippetPredicate<T> filt : filters) {
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1756
            Iterator<T> iterator = supplier.get().filter(filt).iterator();
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1757
            if (iterator.hasNext()) {
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1758
                return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1759
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1760
        }
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1761
        return null;
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1762
    }
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1763
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1764
    private boolean inStartUp(Snippet sn) {
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1765
        return mapSnippet.get(sn).space == startNamespace;
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1766
    }
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1767
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1768
    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
  1769
        return state.status(sn).isActive();
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1770
    }
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1771
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1772
    private boolean mainActive(Snippet sn) {
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1773
        return !inStartUp(sn) && isActive(sn);
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1774
    }
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1775
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1776
    private boolean matchingDeclaration(Snippet sn, String name) {
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1777
        return sn instanceof DeclarationSnippet
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1778
                && ((DeclarationSnippet) sn).name().equals(name);
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1779
    }
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1780
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1781
    /**
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1782
     * Convert user arguments to a Stream of snippets referenced by those
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1783
     * arguments (or lack of arguments).
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1784
     *
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1785
     * @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
  1786
     * @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
  1787
     * @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
  1788
     * string
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1789
     * @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
  1790
     */
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1791
    private <T extends Snippet> Stream<T> argsOptionsToSnippets(Supplier<Stream<T>> snippetSupplier,
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1792
            Predicate<Snippet> defFilter, String rawargs, String cmd) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1793
        ArgTokenizer at = new ArgTokenizer(cmd, rawargs.trim());
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1794
        at.allowedOptions("-all", "-start");
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1795
        List<String> args = new ArrayList<>();
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1796
        String s;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1797
        while ((s = at.next()) != null) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1798
            args.add(s);
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1799
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1800
        if (!checkOptionsAndRemainingInput(at)) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1801
            return null;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1802
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1803
        if (at.optionCount() > 0 && args.size() > 0) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1804
            errormsg("jshell.err.may.not.specify.options.and.snippets", at.whole());
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1805
            return null;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1806
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1807
        if (at.optionCount() > 1) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1808
            errormsg("jshell.err.conflicting.options", at.whole());
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1809
            return null;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1810
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1811
        if (at.hasOption("-all")) {
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1812
            // 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
  1813
            return snippetSupplier.get();
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1814
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1815
        if (at.hasOption("-start")) {
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1816
            // start-up snippets
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1817
            return snippetSupplier.get()
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35000
diff changeset
  1818
                    .filter(this::inStartUp);
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1819
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1820
        if (args.isEmpty()) {
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1821
            // Default is all active user snippets
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1822
            return snippetSupplier.get()
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1823
                    .filter(defFilter);
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1824
        }
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1825
        return argsToSnippets(snippetSupplier, args);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1826
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1827
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1828
    /**
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1829
     * Convert user arguments to a Stream of snippets referenced by those
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1830
     * arguments.
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1831
     *
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1832
     * @param snippetSupplier the base list of possible snippets
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1833
     * @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
  1834
     * @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
  1835
     * arg
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1836
     */
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1837
    private <T extends Snippet> Stream<T> argsToSnippets(Supplier<Stream<T>> snippetSupplier,
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1838
            List<String> args) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1839
        Stream<T> result = null;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1840
        for (String arg : args) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1841
            // Find the best match
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1842
            Stream<T> st = layeredSnippetSearch(snippetSupplier, arg);
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1843
            if (st == null) {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1844
                Stream<Snippet> est = layeredSnippetSearch(state::snippets, arg);
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1845
                if (est == null) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1846
                    errormsg("jshell.err.no.such.snippets", arg);
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1847
                } else {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1848
                    errormsg("jshell.err.the.snippet.cannot.be.used.with.this.command",
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1849
                            arg, est.findFirst().get().source());
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1850
                }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1851
                return null;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1852
            }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1853
            if (result == null) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1854
                result = st;
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1855
            } else {
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1856
                result = Stream.concat(result, st);
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1857
            }
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1858
        }
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1859
        return result;
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1860
    }
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  1861
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1862
    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
  1863
        return nonEmptyStream(
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1864
                // the stream supplier
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1865
                snippetSupplier,
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1866
                // look for active user declarations matching the name
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1867
                sn -> isActive(sn) && matchingDeclaration(sn, arg),
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1868
                // else, look for any declarations matching the name
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1869
                sn -> matchingDeclaration(sn, arg),
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1870
                // else, look for an id of this name
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1871
                sn -> sn.id().equals(arg)
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1872
        );
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1873
    }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1874
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1875
    private boolean cmdDrop(String rawargs) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1876
        ArgTokenizer at = new ArgTokenizer("/drop", rawargs.trim());
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1877
        at.allowedOptions();
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1878
        List<String> args = new ArrayList<>();
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1879
        String s;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1880
        while ((s = at.next()) != null) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1881
            args.add(s);
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1882
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1883
        if (!checkOptionsAndRemainingInput(at)) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1884
            return false;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1885
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1886
        if (args.isEmpty()) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1887
            errormsg("jshell.err.drop.arg");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1888
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1889
        }
41514
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 41248
diff changeset
  1890
        Stream<Snippet> stream = argsToSnippets(this::dropableSnippets, args);
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1891
        if (stream == null) {
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1892
            // Snippet not found. Error already printed
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1893
            fluffmsg("jshell.msg.see.classes.etc");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1894
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1895
        }
41514
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 41248
diff changeset
  1896
        List<Snippet> snippets = stream.collect(toList());
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1897
        if (snippets.size() > args.size()) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1898
            // One of the args references more thean one snippet
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1899
            errormsg("jshell.err.drop.ambiguous");
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1900
            fluffmsg("jshell.msg.use.one.of", snippets.stream()
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1901
                    .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
  1902
                    .collect(Collectors.joining(", "))
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1903
            );
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1904
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1905
        }
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1906
        snippets.stream()
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  1907
                .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
  1908
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1909
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1910
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1911
    private boolean cmdEdit(String arg) {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  1912
        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
  1913
                this::mainActive, arg, "/edit");
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1914
        if (stream == null) {
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1915
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1916
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1917
        Set<String> srcSet = new LinkedHashSet<>();
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1918
        stream.forEachOrdered(sn -> {
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1919
            String src = sn.source();
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1920
            switch (sn.subKind()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1921
                case VAR_VALUE_SUBKIND:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1922
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1923
                case ASSIGNMENT_SUBKIND:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1924
                case OTHER_EXPRESSION_SUBKIND:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1925
                case TEMP_VAR_EXPRESSION_SUBKIND:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1926
                    if (!src.endsWith(";")) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1927
                        src = src + ";";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1928
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1929
                    srcSet.add(src);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1930
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1931
                default:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1932
                    srcSet.add(src);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1933
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1934
            }
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  1935
        });
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1936
        StringBuilder sb = new StringBuilder();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1937
        for (String s : srcSet) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1938
            sb.append(s);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1939
            sb.append('\n');
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1940
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1941
        String src = sb.toString();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1942
        Consumer<String> saveHandler = new SaveHandler(src, srcSet);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1943
        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
  1944
        if (editor == BUILT_IN_EDITOR) {
37007
6023a9a9d58a 8153716: JShell tool: should warn when failed to launch editor
rfield
parents: 36992
diff changeset
  1945
            try {
6023a9a9d58a 8153716: JShell tool: should warn when failed to launch editor
rfield
parents: 36992
diff changeset
  1946
                EditPad.edit(errorHandler, src, saveHandler);
6023a9a9d58a 8153716: JShell tool: should warn when failed to launch editor
rfield
parents: 36992
diff changeset
  1947
            } catch (RuntimeException ex) {
6023a9a9d58a 8153716: JShell tool: should warn when failed to launch editor
rfield
parents: 36992
diff changeset
  1948
                errormsg("jshell.err.cant.launch.editor", ex);
6023a9a9d58a 8153716: JShell tool: should warn when failed to launch editor
rfield
parents: 36992
diff changeset
  1949
                fluffmsg("jshell.msg.try.set.editor");
6023a9a9d58a 8153716: JShell tool: should warn when failed to launch editor
rfield
parents: 36992
diff changeset
  1950
                return false;
6023a9a9d58a 8153716: JShell tool: should warn when failed to launch editor
rfield
parents: 36992
diff changeset
  1951
            }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1952
        } else {
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1953
            ExternalEditor.edit(editor.cmd, errorHandler, src, saveHandler, input,
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 41514
diff changeset
  1954
                    editor.wait, this::hardrb);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1955
        }
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  1956
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1957
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1958
    //where
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1959
    // receives editor requests to save
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1960
    private class SaveHandler implements Consumer<String> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1961
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1962
        String src;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1963
        Set<String> currSrcs;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1964
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1965
        SaveHandler(String src, Set<String> ss) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1966
            this.src = src;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1967
            this.currSrcs = ss;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1968
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1969
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1970
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1971
        public void accept(String s) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1972
            if (!s.equals(src)) { // quick check first
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1973
                src = s;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1974
                try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1975
                    Set<String> nextSrcs = new LinkedHashSet<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1976
                    boolean failed = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1977
                    while (true) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1978
                        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
  1979
                        if (!an.completeness().isComplete()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1980
                            break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1981
                        }
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  1982
                        String tsrc = trimNewlines(an.source());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1983
                        if (!failed && !currSrcs.contains(tsrc)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1984
                            failed = processCompleteSource(tsrc);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1985
                        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1986
                        nextSrcs.add(tsrc);
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  1987
                        if (an.remaining().isEmpty()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1988
                            break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1989
                        }
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  1990
                        s = an.remaining();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1991
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1992
                    currSrcs = nextSrcs;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1993
                } catch (IllegalStateException ex) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  1994
                    hardmsg("jshell.msg.resetting");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1995
                    resetState();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1996
                    currSrcs = new LinkedHashSet<>(); // re-process everything
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1997
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1998
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1999
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2000
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2001
        private String trimNewlines(String s) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2002
            int b = 0;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2003
            while (b < s.length() && s.charAt(b) == '\n') {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2004
                ++b;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2005
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2006
            int e = s.length() -1;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2007
            while (e >= 0 && s.charAt(e) == '\n') {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2008
                --e;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2009
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2010
            return s.substring(b, e + 1);
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
    }
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
    private boolean cmdList(String arg) {
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2015
        if (arg.length() >= 2 && "-history".startsWith(arg)) {
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2016
            return cmdHistory();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2017
        }
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2018
        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
  2019
                this::mainActive, arg, "/list");
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2020
        if (stream == null) {
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2021
            return false;
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2022
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2023
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2024
        // prevent double newline on empty list
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2025
        boolean[] hasOutput = new boolean[1];
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2026
        stream.forEachOrdered(sn -> {
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2027
            if (!hasOutput[0]) {
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2028
                cmdout.println();
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2029
                hasOutput[0] = true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2030
            }
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34477
diff changeset
  2031
            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
  2032
        });
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2033
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2034
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2035
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2036
    private boolean cmdOpen(String filename) {
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
  2037
        return runFile(filename, "/open");
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2038
    }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2039
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2040
    private boolean runFile(String filename, String context) {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2041
        if (!filename.isEmpty()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2042
            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2043
                run(new FileScannerIOContext(toPathResolvingUserHome(filename).toString()));
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2044
                return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2045
            } catch (FileNotFoundException e) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2046
                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
  2047
            } catch (Exception e) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2048
                errormsg("jshell.err.file.exception", context, filename, e);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2049
            }
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2050
        } else {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2051
            errormsg("jshell.err.file.filename", context);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2052
        }
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2053
        return false;
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2054
    }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2055
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2056
    /**
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2057
     * Read an external file. Error messages accessed via keyPrefix
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2058
     *
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2059
     * @param filename file to access or null
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2060
     * @param context printable non-natural language context for errors
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2061
     * @return contents of file as string
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2062
     */
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2063
    String readFile(String filename, String context) {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2064
        if (filename != null) {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2065
            try {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2066
                byte[] encoded = Files.readAllBytes(Paths.get(filename));
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2067
                return new String(encoded);
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2068
            } catch (AccessDeniedException e) {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2069
                errormsg("jshell.err.file.not.accessible", context, filename, e.getMessage());
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2070
            } catch (NoSuchFileException e) {
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
  2071
                errormsg("jshell.err.file.not.found", context, filename);
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2072
            } catch (Exception e) {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2073
                errormsg("jshell.err.file.exception", context, filename, e);
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2074
            }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2075
        } else {
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2076
            errormsg("jshell.err.file.filename", context);
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2077
        }
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2078
        return null;
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2079
33362
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
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2082
    private boolean cmdReset() {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2083
        live = false;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2084
        fluffmsg("jshell.msg.resetting.state");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2085
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2086
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2087
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2088
    private boolean cmdReload(String rawargs) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2089
        ArgTokenizer at = new ArgTokenizer("/reload", rawargs.trim());
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2090
        at.allowedOptions("-restore", "-quiet");
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2091
        if (!checkOptionsAndRemainingInput(at)) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2092
            return false;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2093
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2094
        Iterable<String> history;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2095
        if (at.hasOption("-restore")) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2096
            if (replayableHistoryPrevious == null) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2097
                errormsg("jshell.err.reload.no.previous");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2098
                return false;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2099
            }
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2100
            history = replayableHistoryPrevious;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2101
            fluffmsg("jshell.err.reload.restarting.previous.state");
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2102
        } else {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2103
            history = replayableHistory;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2104
            fluffmsg("jshell.err.reload.restarting.state");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2105
        }
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2106
        boolean echo = !at.hasOption("-quiet");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2107
        resetState();
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2108
        run(new ReloadIOContext(history,
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2109
                echo ? cmdout : null));
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2110
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2111
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2112
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2113
    private boolean cmdSave(String rawargs) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2114
        ArgTokenizer at = new ArgTokenizer("/save", rawargs.trim());
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2115
        at.allowedOptions("-all", "-start", "-history");
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2116
        String filename = at.next();
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2117
        if (filename == null) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2118
            errormsg("jshell.err.file.filename", "/save");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2119
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2120
        }
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2121
        if (!checkOptionsAndRemainingInput(at)) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2122
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2123
        }
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2124
        if (at.optionCount() > 1) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2125
            errormsg("jshell.err.conflicting.options", at.whole());
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2126
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2127
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2128
        try (BufferedWriter writer = Files.newBufferedWriter(toPathResolvingUserHome(filename),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2129
                Charset.defaultCharset(),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2130
                CREATE, TRUNCATE_EXISTING, WRITE)) {
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2131
            if (at.hasOption("-history")) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2132
                for (String s : input.currentSessionHistory()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2133
                    writer.write(s);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2134
                    writer.write("\n");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2135
                }
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2136
            } else if (at.hasOption("-start")) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2137
                writer.append(startup);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2138
            } else {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2139
                String sources = (at.hasOption("-all")
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2140
                        ? state.snippets()
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2141
                        : state.snippets().filter(this::mainActive))
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2142
                        .map(Snippet::source)
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2143
                        .collect(Collectors.joining("\n"));
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2144
                writer.write(sources);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2145
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2146
        } catch (FileNotFoundException e) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2147
            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
  2148
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2149
        } catch (Exception e) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2150
            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
  2151
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2152
        }
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2153
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2154
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2155
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2156
    private boolean cmdVars(String arg) {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2157
        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
  2158
                this::isActive, arg, "/vars");
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2159
        if (stream == null) {
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2160
            return false;
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2161
        }
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2162
        stream.forEachOrdered(vk ->
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2163
        {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2164
            String val = state.status(vk) == Status.VALID
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2165
                    ? state.varValue(vk)
40516
9e0e107c39dd 8154374: JShell: setContextClassLoader() for remote Snippet class loader
rfield
parents: 40498
diff changeset
  2166
                    : getResourceString("jshell.msg.vars.not.active");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2167
            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
  2168
        });
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2169
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2170
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2171
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2172
    private boolean cmdMethods(String arg) {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2173
        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
  2174
                this::isActive, arg, "/methods");
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2175
        if (stream == null) {
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2176
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2177
        }
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2178
        stream.forEachOrdered(mk
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2179
                -> 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
  2180
        );
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2181
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2182
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2183
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2184
    private boolean cmdTypes(String arg) {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2185
        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
  2186
                this::isActive, arg, "/types");
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2187
        if (stream == null) {
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2188
            return false;
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2189
        }
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2190
        stream.forEachOrdered(ck
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 38513
diff changeset
  2191
        -> {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2192
            String kind;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2193
            switch (ck.subKind()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2194
                case INTERFACE_SUBKIND:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2195
                    kind = "interface";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2196
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2197
                case CLASS_SUBKIND:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2198
                    kind = "class";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2199
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2200
                case ENUM_SUBKIND:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2201
                    kind = "enum";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2202
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2203
                case ANNOTATION_TYPE_SUBKIND:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2204
                    kind = "@interface";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2205
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2206
                default:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2207
                    assert false : "Wrong kind" + ck.subKind();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2208
                    kind = "class";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2209
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2210
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2211
            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
  2212
        });
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2213
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2214
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2215
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2216
    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
  2217
        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
  2218
            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
  2219
        });
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2220
        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
  2221
    }
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
  2222
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2223
    private boolean cmdUseHistoryEntry(int index) {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2224
        List<Snippet> keys = state.snippets().collect(toList());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2225
        if (index < 0)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2226
            index += keys.size();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2227
        else
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2228
            index--;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2229
        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
  2230
            rerunSnippet(keys.get(index));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2231
        } else {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2232
            errormsg("jshell.err.out.of.range");
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2233
            return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2234
        }
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2235
        return true;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2236
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2237
38539
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2238
    boolean checkOptionsAndRemainingInput(ArgTokenizer at) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2239
        String junk = at.remainder();
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2240
        if (!junk.isEmpty()) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2241
            errormsg("jshell.err.unexpected.at.end", junk, at.whole());
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2242
            return false;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2243
        } else {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2244
            String bad = at.badOptions();
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2245
            if (!bad.isEmpty()) {
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2246
                errormsg("jshell.err.unknown.option", bad, at.whole());
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2247
                return false;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2248
            }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2249
        }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2250
        return true;
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2251
    }
71874886920f 8157517: jshell tool: allow undoing operations
rfield
parents: 38535
diff changeset
  2252
34477
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2253
    private boolean rerunHistoryEntryById(String id) {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2254
        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
  2255
            .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
  2256
            .findFirst();
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2257
        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
  2258
            rerunSnippet(s);
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2259
            return true;
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2260
        }).orElse(false);
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2261
    }
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2262
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2263
    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
  2264
        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
  2265
        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
  2266
        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
  2267
        processSourceCatchingReset(source);
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2268
    }
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
  2269
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2270
    /**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2271
     * Filter diagnostics for only errors (no warnings, ...)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2272
     * @param diagnostics input list
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2273
     * @return filtered list
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2274
     */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2275
    List<Diag> errorsOnly(List<Diag> diagnostics) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2276
        return diagnostics.stream()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2277
                .filter(d -> d.isError())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2278
                .collect(toList());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2279
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2280
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2281
    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
  2282
        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
  2283
            if (!line.trim().startsWith("location:")) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2284
                toDisplay.add(line);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2285
            }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2286
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2287
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2288
        int pstart = (int) diag.getStartPosition();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2289
        int pend = (int) diag.getEndPosition();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2290
        Matcher m = LINEBREAK.matcher(source);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2291
        int pstartl = 0;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2292
        int pendl = -2;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2293
        while (m.find(pstartl)) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2294
            pendl = m.start();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2295
            if (pendl >= pstart) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2296
                break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2297
            } else {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2298
                pstartl = m.end();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2299
            }
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2300
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2301
        if (pendl < pstart) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2302
            pendl = source.length();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2303
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2304
        toDisplay.add(source.substring(pstartl, pendl));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2305
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2306
        StringBuilder sb = new StringBuilder();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2307
        int start = pstart - pstartl;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2308
        for (int i = 0; i < start; ++i) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2309
            sb.append(' ');
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2310
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2311
        sb.append('^');
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2312
        boolean multiline = pend > pendl;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2313
        int end = (multiline ? pendl : pend) - pstartl - 1;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2314
        if (end > start) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2315
            for (int i = start + 1; i < end; ++i) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2316
                sb.append('-');
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2317
            }
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2318
            if (multiline) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2319
                sb.append("-...");
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2320
            } else {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2321
                sb.append('^');
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2322
            }
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2323
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2324
        toDisplay.add(sb.toString());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2325
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2326
        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
  2327
        debug("Code: %s", diag.getCode());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2328
        debug("Pos: %d (%d - %d)", diag.getPosition(),
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2329
                diag.getStartPosition(), diag.getEndPosition());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2330
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2331
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2332
    private String processSource(String srcInput) throws IllegalStateException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2333
        while (true) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2334
            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
  2335
            if (!an.completeness().isComplete()) {
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  2336
                return an.remaining();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2337
            }
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  2338
            boolean failed = processCompleteSource(an.source());
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  2339
            if (failed || an.remaining().isEmpty()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2340
                return "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2341
            }
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  2342
            srcInput = an.remaining();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2343
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2344
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2345
    //where
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2346
    private boolean processCompleteSource(String source) throws IllegalStateException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2347
        debug("Compiling: %s", source);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2348
        boolean failed = false;
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2349
        boolean isActive = false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2350
        List<SnippetEvent> events = state.eval(source);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2351
        for (SnippetEvent e : events) {
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2352
            // Report the event, recording failure
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2353
            failed |= handleEvent(e);
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2354
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2355
            // 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
  2356
            // also ignore var value queries
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2357
            isActive |= e.causeSnippet() == null &&
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  2358
                    e.status().isActive() &&
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2359
                    e.snippet().subKind() != VAR_VALUE_SUBKIND;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2360
        }
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2361
        // 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
  2362
        // add it to the replayable history
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2363
        if (isActive && live) {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2364
            addToReplayHistory(source);
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2365
        }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2366
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2367
        return failed;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2368
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2369
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2370
    // Handle incoming snippet events -- return true on failure
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2371
    private boolean handleEvent(SnippetEvent ste) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2372
        Snippet sn = ste.snippet();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2373
        if (sn == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2374
            debug("Event with null key: %s", ste);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2375
            return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2376
        }
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2377
        List<Diag> diagnostics = state.diagnostics(sn).collect(toList());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2378
        String source = sn.source();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2379
        if (ste.causeSnippet() == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2380
            // main event
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2381
            for (Diag d : diagnostics) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2382
                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
  2383
                List<String> disp = new ArrayList<>();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2384
                displayDiagnostics(source, d, disp);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2385
                disp.stream()
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
  2386
                        .forEach(l -> hard("%s", l));
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2387
            }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2388
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2389
            if (ste.status() != Status.REJECTED) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2390
                if (ste.exception() != null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2391
                    if (ste.exception() instanceof EvalException) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2392
                        printEvalException((EvalException) ste.exception());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2393
                        return true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2394
                    } else if (ste.exception() instanceof UnresolvedReferenceException) {
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2395
                        printUnresolvedException((UnresolvedReferenceException) ste.exception());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2396
                    } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2397
                        hard("Unexpected execution exception: %s", ste.exception());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2398
                        return true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2399
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2400
                } else {
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2401
                    new DisplayEvent(ste, false, ste.value(), diagnostics).displayDeclarationAndValue();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2402
                }
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2403
            } else {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2404
                if (diagnostics.isEmpty()) {
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2405
                    errormsg("jshell.err.failed");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2406
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2407
                return true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2408
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2409
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2410
            // Update
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2411
            if (sn instanceof DeclarationSnippet) {
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2412
                List<Diag> other = errorsOnly(diagnostics);
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 36160
diff changeset
  2413
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2414
                // display update information
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2415
                new DisplayEvent(ste, true, ste.value(), other).displayDeclarationAndValue();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2416
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2417
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2418
        return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2419
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2420
    //where
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2421
    void printStackTrace(StackTraceElement[] stes) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2422
        for (StackTraceElement ste : stes) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2423
            StringBuilder sb = new StringBuilder();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2424
            String cn = ste.getClassName();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2425
            if (!cn.isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2426
                int dot = cn.lastIndexOf('.');
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2427
                if (dot > 0) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2428
                    sb.append(cn.substring(dot + 1));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2429
                } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2430
                    sb.append(cn);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2431
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2432
                sb.append(".");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2433
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2434
            if (!ste.getMethodName().isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2435
                sb.append(ste.getMethodName());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2436
                sb.append(" ");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2437
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2438
            String fileName = ste.getFileName();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2439
            int lineNumber = ste.getLineNumber();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2440
            String loc = ste.isNativeMethod()
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2441
                    ? getResourceString("jshell.msg.native.method")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2442
                    : fileName == null
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36718
diff changeset
  2443
                            ? getResourceString("jshell.msg.unknown.source")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2444
                            : lineNumber >= 0
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2445
                                    ? fileName + ":" + lineNumber
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2446
                                    : fileName;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2447
            hard("      at %s(%s)", sb, loc);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2448
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2449
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2450
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2451
    //where
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2452
    void printUnresolvedException(UnresolvedReferenceException ex) {
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents: 36494
diff changeset
  2453
        DeclarationSnippet corralled =  ex.getSnippet();
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2454
        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
  2455
        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
  2456
                .displayDeclarationAndValue();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2457
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2458
    //where
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2459
    void printEvalException(EvalException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2460
        if (ex.getMessage() == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2461
            hard("%s thrown", ex.getExceptionClassName());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2462
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2463
            hard("%s thrown: %s", ex.getExceptionClassName(), ex.getMessage());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2464
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2465
        printStackTrace(ex.getStackTrace());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2466
    }
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2467
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2468
    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
  2469
        FormatAction act;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2470
        switch (status) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2471
            case VALID:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2472
            case RECOVERABLE_DEFINED:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2473
            case RECOVERABLE_NOT_DEFINED:
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38613
diff changeset
  2474
                if (previousStatus.isActive()) {
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2475
                    act = isSignatureChange
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2476
                            ? FormatAction.REPLACED
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2477
                            : FormatAction.MODIFIED;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2478
                } else {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2479
                    act = FormatAction.ADDED;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2480
                }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2481
                break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2482
            case OVERWRITTEN:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2483
                act = FormatAction.OVERWROTE;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2484
                break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2485
            case DROPPED:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2486
                act = FormatAction.DROPPED;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2487
                break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2488
            case REJECTED:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2489
            case NONEXISTENT:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2490
            default:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2491
                // Should not occur
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2492
                error("Unexpected status: " + previousStatus.toString() + "=>" + status.toString());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2493
                act = FormatAction.DROPPED;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2494
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2495
        return act;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2496
    }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2497
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2498
    class DisplayEvent {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2499
        private final Snippet sn;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2500
        private final FormatAction action;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2501
        private final boolean update;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2502
        private final String value;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2503
        private final List<String> errorLines;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2504
        private final FormatResolve resolution;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2505
        private final String unresolved;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2506
        private final FormatUnresolved unrcnt;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2507
        private final FormatErrors errcnt;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2508
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2509
        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
  2510
            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
  2511
        }
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2512
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2513
        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
  2514
            this.sn = sn;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2515
            this.action = action;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2516
            this.update = update;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2517
            this.value = value;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2518
            this.errorLines = new ArrayList<>();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2519
            for (Diag d : errors) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2520
                displayDiagnostics(sn.source(), d, errorLines);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2521
            }
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2522
            long unresolvedCount;
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2523
            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
  2524
                resolution = (status == Status.RECOVERABLE_NOT_DEFINED)
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2525
                        ? FormatResolve.NOTDEFINED
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2526
                        : FormatResolve.DEFINED;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2527
                unresolved = unresolved((DeclarationSnippet) sn);
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2528
                unresolvedCount = state.unresolvedDependencies((DeclarationSnippet) sn).count();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2529
            } else {
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2530
                resolution = FormatResolve.OK;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2531
                unresolved = "";
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2532
                unresolvedCount = 0;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2533
            }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2534
            unrcnt = unresolvedCount == 0
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2535
                    ? FormatUnresolved.UNRESOLVED0
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2536
                    : unresolvedCount == 1
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2537
                        ? FormatUnresolved.UNRESOLVED1
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2538
                        : FormatUnresolved.UNRESOLVED2;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2539
            errcnt = errors.isEmpty()
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2540
                    ? FormatErrors.ERROR0
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2541
                    : errors.size() == 1
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2542
                        ? FormatErrors.ERROR1
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2543
                        : FormatErrors.ERROR2;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2544
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2545
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2546
        private String unresolved(DeclarationSnippet key) {
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 38912
diff changeset
  2547
            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
  2548
            StringBuilder sb = new StringBuilder();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2549
            int fromLast = unr.size();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2550
            if (fromLast > 0) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2551
                sb.append(" ");
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2552
            }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2553
            for (String u : unr) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2554
                --fromLast;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2555
                sb.append(u);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2556
                switch (fromLast) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2557
                    // No suffix
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2558
                    case 0:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2559
                        break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2560
                    case 1:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2561
                        sb.append(", and ");
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2562
                        break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2563
                    default:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2564
                        sb.append(", ");
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2565
                        break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2566
                }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2567
            }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2568
            return sb.toString();
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2569
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2570
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2571
        private void custom(FormatCase fcase, String name) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2572
            custom(fcase, name, null);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2573
        }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2574
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2575
        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
  2576
            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
  2577
                    resolution, unrcnt, errcnt,
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2578
                    name, type, value, unresolved, errorLines);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2579
            if (interactive()) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2580
                cmdout.print(display);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2581
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2582
        }
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2583
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2584
        @SuppressWarnings("fallthrough")
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2585
        private void displayDeclarationAndValue() {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2586
            switch (sn.subKind()) {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2587
                case CLASS_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2588
                    custom(FormatCase.CLASS, ((TypeDeclSnippet) sn).name());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2589
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2590
                case INTERFACE_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2591
                    custom(FormatCase.INTERFACE, ((TypeDeclSnippet) sn).name());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2592
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2593
                case ENUM_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2594
                    custom(FormatCase.ENUM, ((TypeDeclSnippet) sn).name());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2595
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2596
                case ANNOTATION_TYPE_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2597
                    custom(FormatCase.ANNOTATION, ((TypeDeclSnippet) sn).name());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2598
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2599
                case METHOD_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2600
                    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
  2601
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2602
                case VAR_DECLARATION_SUBKIND: {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2603
                    VarSnippet vk = (VarSnippet) sn;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2604
                    custom(FormatCase.VARDECL, vk.name(), vk.typeName());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2605
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2606
                }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2607
                case VAR_DECLARATION_WITH_INITIALIZER_SUBKIND: {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2608
                    VarSnippet vk = (VarSnippet) sn;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2609
                    custom(FormatCase.VARINIT, vk.name(), vk.typeName());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2610
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2611
                }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2612
                case TEMP_VAR_EXPRESSION_SUBKIND: {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2613
                    VarSnippet vk = (VarSnippet) sn;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2614
                    custom(FormatCase.EXPRESSION, vk.name(), vk.typeName());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2615
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2616
                }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2617
                case OTHER_EXPRESSION_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2618
                    error("Unexpected expression form -- value is: %s", (value));
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2619
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2620
                case VAR_VALUE_SUBKIND: {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2621
                    ExpressionSnippet ek = (ExpressionSnippet) sn;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2622
                    custom(FormatCase.VARVALUE, ek.name(), ek.typeName());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2623
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2624
                }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2625
                case ASSIGNMENT_SUBKIND: {
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2626
                    ExpressionSnippet ek = (ExpressionSnippet) sn;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2627
                    custom(FormatCase.ASSIGNMENT, ek.name(), ek.typeName());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2628
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2629
                }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2630
                case SINGLE_TYPE_IMPORT_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2631
                case TYPE_IMPORT_ON_DEMAND_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2632
                case SINGLE_STATIC_IMPORT_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2633
                case STATIC_IMPORT_ON_DEMAND_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2634
                    custom(FormatCase.IMPORT, ((ImportSnippet) sn).name());
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2635
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2636
                case STATEMENT_SUBKIND:
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2637
                    custom(FormatCase.STATEMENT, null);
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2638
                    break;
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2639
            }
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36715
diff changeset
  2640
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2641
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2642
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2643
    /** The current version number as a string.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2644
     */
36992
ddebebe611a1 8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents: 36990
diff changeset
  2645
    String version() {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2646
        return version("release");  // mm.nn.oo[-milestone]
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2647
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2648
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2649
    /** The current full version number as a string.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2650
     */
36992
ddebebe611a1 8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents: 36990
diff changeset
  2651
    String fullVersion() {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2652
        return version("full"); // mm.mm.oo[-milestone]-build
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2653
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2654
36992
ddebebe611a1 8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents: 36990
diff changeset
  2655
    private String version(String key) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2656
        if (versionRB == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2657
            try {
36992
ddebebe611a1 8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents: 36990
diff changeset
  2658
                versionRB = ResourceBundle.getBundle(VERSION_RB_NAME, locale);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2659
            } catch (MissingResourceException e) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2660
                return "(version info not available)";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2661
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2662
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2663
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2664
            return versionRB.getString(key);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2665
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2666
        catch (MissingResourceException e) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2667
            return "(version info not available)";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2668
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2669
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2670
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2671
    class NameSpace {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2672
        final String spaceName;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2673
        final String prefix;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2674
        private int nextNum;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2675
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2676
        NameSpace(String spaceName, String prefix) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2677
            this.spaceName = spaceName;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2678
            this.prefix = prefix;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2679
            this.nextNum = 1;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2680
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2681
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2682
        String tid(Snippet sn) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2683
            String tid = prefix + nextNum++;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2684
            mapSnippet.put(sn, new SnippetInfo(sn, this, tid));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2685
            return tid;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2686
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2687
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2688
        String tidNext() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2689
            return prefix + nextNum;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2690
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2691
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2692
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2693
    static class SnippetInfo {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2694
        final Snippet snippet;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2695
        final NameSpace space;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2696
        final String tid;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2697
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2698
        SnippetInfo(Snippet snippet, NameSpace space, String tid) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2699
            this.snippet = snippet;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2700
            this.space = space;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2701
            this.tid = tid;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2702
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2703
    }
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2704
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40598
diff changeset
  2705
    static class ArgSuggestion implements Suggestion {
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2706
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2707
        private final String continuation;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2708
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2709
        /**
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2710
         * Create a {@code Suggestion} instance.
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2711
         *
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2712
         * @param continuation a candidate continuation of the user's input
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2713
         */
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2714
        public ArgSuggestion(String continuation) {
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2715
            this.continuation = continuation;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2716
        }
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2717
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2718
        /**
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2719
         * The candidate continuation of the given user's input.
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2720
         *
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2721
         * @return the continuation string
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2722
         */
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2723
        @Override
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2724
        public String continuation() {
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2725
            return continuation;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2726
        }
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2727
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2728
        /**
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2729
         * Indicates whether input continuation matches the target type and is thus
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2730
         * more likely to be the desired continuation. A matching continuation is
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2731
         * preferred.
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2732
         *
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2733
         * @return {@code false}, non-types analysis
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2734
         */
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2735
        @Override
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2736
        public boolean matchesType() {
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2737
            return false;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2738
        }
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40304
diff changeset
  2739
    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2740
}
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2741
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2742
abstract class NonInteractiveIOContext extends IOContext {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2743
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2744
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2745
    public boolean interactiveOutput() {
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2746
        return false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2747
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2748
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2749
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2750
    public Iterable<String> currentSessionHistory() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2751
        return Collections.emptyList();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2752
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2753
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2754
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2755
    public boolean terminalEditorRunning() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2756
        return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2757
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2758
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2759
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2760
    public void suspend() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2761
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2762
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2763
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2764
    public void resume() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2765
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2766
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2767
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2768
    public void beforeUserCode() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2769
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2770
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2771
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2772
    public void afterUserCode() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2773
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2774
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2775
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2776
    public void replaceLastHistoryEntry(String source) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2777
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2778
}
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2779
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2780
class ScannerIOContext extends NonInteractiveIOContext {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2781
    private final Scanner scannerIn;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2782
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2783
    ScannerIOContext(Scanner scannerIn) {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2784
        this.scannerIn = scannerIn;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2785
    }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2786
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2787
    @Override
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2788
    public String readLine(String prompt, String prefix) {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2789
        if (scannerIn.hasNextLine()) {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2790
            return scannerIn.nextLine();
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2791
        } else {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2792
            return null;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2793
        }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2794
    }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2795
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2796
    @Override
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2797
    public void close() {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2798
        scannerIn.close();
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2799
    }
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  2800
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  2801
    @Override
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  2802
    public int readUserInput() {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  2803
        return -1;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  2804
    }
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2805
}
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2806
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2807
class FileScannerIOContext extends ScannerIOContext {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2808
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2809
    FileScannerIOContext(String fn) throws FileNotFoundException {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2810
        this(new FileReader(fn));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2811
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2812
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2813
    FileScannerIOContext(Reader rdr) throws FileNotFoundException {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2814
        super(new Scanner(rdr));
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2815
    }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2816
}
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2817
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2818
class ReloadIOContext extends NonInteractiveIOContext {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2819
    private final Iterator<String> it;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2820
    private final PrintStream echoStream;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2821
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2822
    ReloadIOContext(Iterable<String> history, PrintStream echoStream) {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2823
        this.it = history.iterator();
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2824
        this.echoStream = echoStream;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2825
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2826
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2827
    @Override
34999
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2828
    public String readLine(String prompt, String prefix) {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2829
        String s = it.hasNext()
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2830
                ? it.next()
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2831
                : null;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2832
        if (echoStream != null && s != null) {
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2833
            String p = "-: ";
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2834
            String p2 = "\n   ";
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2835
            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
  2836
        }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2837
        return s;
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2838
    }
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2839
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2840
    @Override
aacf94dab449 8081845: JShell: Need way to refresh relative to external state
rfield
parents: 34570
diff changeset
  2841
    public void close() {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2842
    }
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  2843
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  2844
    @Override
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  2845
    public int readUserInput() {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  2846
        return -1;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 40765
diff changeset
  2847
    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  2848
}