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