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