src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/NashornCompleter.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 39662 nashorn/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/NashornCompleter.java@e2b36a3779b9
child 47492 560fab171dc7
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
     1
/*
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
     4
 *
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    10
 *
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    15
 * accompanied this code).
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    16
 *
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    20
 *
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    23
 * questions.
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    24
 */
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    25
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    26
package jdk.nashorn.tools.jjs;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    27
32318
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
    28
import java.io.File;
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    29
import java.io.PrintWriter;
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    30
import java.util.ArrayList;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    31
import java.util.List;
32319
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
    32
import java.util.concurrent.ExecutionException;
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
    33
import java.util.concurrent.FutureTask;
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
    34
import java.util.regex.Pattern;
32318
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
    35
import javax.swing.JFileChooser;
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
    36
import javax.swing.filechooser.FileNameExtensionFilter;
32319
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
    37
import javax.swing.SwingUtilities;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    38
import jdk.internal.jline.console.completer.Completer;
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    39
import jdk.internal.jline.console.UserInterruptException;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    40
import jdk.nashorn.api.tree.AssignmentTree;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    41
import jdk.nashorn.api.tree.BinaryTree;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    42
import jdk.nashorn.api.tree.CompilationUnitTree;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    43
import jdk.nashorn.api.tree.CompoundAssignmentTree;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    44
import jdk.nashorn.api.tree.ConditionalExpressionTree;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    45
import jdk.nashorn.api.tree.ExpressionTree;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    46
import jdk.nashorn.api.tree.ExpressionStatementTree;
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
    47
import jdk.nashorn.api.tree.FunctionCallTree;
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
    48
import jdk.nashorn.api.tree.IdentifierTree;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    49
import jdk.nashorn.api.tree.InstanceOfTree;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    50
import jdk.nashorn.api.tree.MemberSelectTree;
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
    51
import jdk.nashorn.api.tree.NewTree;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    52
import jdk.nashorn.api.tree.SimpleTreeVisitorES5_1;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    53
import jdk.nashorn.api.tree.Tree;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    54
import jdk.nashorn.api.tree.UnaryTree;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    55
import jdk.nashorn.api.tree.Parser;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    56
import jdk.nashorn.api.scripting.NashornException;
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
    57
import jdk.nashorn.tools.PartialParser;
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    58
import jdk.nashorn.internal.objects.NativeSyntaxError;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    59
import jdk.nashorn.internal.objects.Global;
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    60
import jdk.nashorn.internal.runtime.ECMAException;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    61
import jdk.nashorn.internal.runtime.Context;
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    62
import jdk.nashorn.internal.runtime.ScriptEnvironment;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    63
import jdk.nashorn.internal.runtime.ScriptRuntime;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    64
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    65
/**
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    66
 * A simple source completer for nashorn. Handles code completion for
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    67
 * expressions as well as handles incomplete single line code.
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    68
 */
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    69
final class NashornCompleter implements Completer {
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    70
    private final Context context;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    71
    private final Global global;
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    72
    private final ScriptEnvironment env;
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
    73
    private final PartialParser partialParser;
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
    74
    private final PropertiesHelper propsHelper;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    75
    private final Parser parser;
32318
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
    76
    private static final boolean BACKSLASH_FILE_SEPARATOR = File.separatorChar == '\\';
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    77
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
    78
    NashornCompleter(final Context context, final Global global,
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
    79
            final PartialParser partialParser, final PropertiesHelper propsHelper) {
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    80
        this.context = context;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    81
        this.global = global;
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    82
        this.env = context.getEnv();
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
    83
        this.partialParser = partialParser;
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
    84
        this.propsHelper = propsHelper;
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    85
        this.parser = createParser(env);
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    86
    }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    87
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    88
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    89
    /**
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    90
     * Is this a ECMAScript SyntaxError thrown for parse issue at the given line and column?
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    91
     *
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    92
     * @param exp Throwable to check
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    93
     * @param line line number to check
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    94
     * @param column column number to check
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    95
     *
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    96
     * @return true if the given Throwable is a ECMAScript SyntaxError at given line, column
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    97
     */
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    98
    boolean isSyntaxErrorAt(final Throwable exp, final int line, final int column) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    99
        if (exp instanceof ECMAException) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   100
            final ECMAException eexp = (ECMAException)exp;
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   101
            if (eexp.getThrown() instanceof NativeSyntaxError) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   102
                return isParseErrorAt(eexp.getCause(), line, column);
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   103
            }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   104
        }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   105
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   106
        return false;
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   107
    }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   108
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   109
    /**
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   110
     * Is this a parse error at the given line and column?
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   111
     *
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   112
     * @param exp Throwable to check
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   113
     * @param line line number to check
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   114
     * @param column column number to check
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   115
     *
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   116
     * @return true if the given Throwable is a parser error at given line, column
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   117
     */
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   118
    boolean isParseErrorAt(final Throwable exp, final int line, final int column) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   119
        if (exp instanceof NashornException) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   120
            final NashornException nexp = (NashornException)exp;
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   121
            return nexp.getLineNumber() == line && nexp.getColumnNumber() == column;
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   122
        }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   123
        return false;
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   124
    }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   125
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   126
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   127
    /**
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   128
     * Read more lines of code if we got SyntaxError at EOF and we can it fine by
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   129
     * by reading more lines of code from the user. This is used for multiline editing.
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   130
     *
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   131
     * @param firstLine First line of code from the user
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   132
     * @param exp       Exception thrown by evaluting first line code
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   133
     * @param in        Console to get read more lines from the user
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   134
     * @param prompt    Prompt to be printed to read more lines from the user
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   135
     * @param err       PrintWriter to print any errors in the proecess of reading
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   136
     *
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   137
     * @return Complete code read from the user including the first line. This is null
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   138
     *         if any error or the user discarded multiline editing by Ctrl-C.
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   139
     */
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   140
    String readMoreLines(final String firstLine, final Exception exp, final Console in,
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   141
            final String prompt, final PrintWriter err) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   142
        int line = 1;
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   143
        final StringBuilder buf = new StringBuilder(firstLine);
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   144
        while (true) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   145
            buf.append('\n');
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   146
            String curLine = null;
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   147
            try {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   148
                curLine = in.readLine(prompt);
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   149
                buf.append(curLine);
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   150
                line++;
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   151
            } catch (final Throwable th) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   152
                if (th instanceof UserInterruptException) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   153
                    // Ctrl-C from user - discard the whole thing silently!
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   154
                    return null;
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   155
                } else {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   156
                    // print anything else -- but still discard the code
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   157
                    err.println(th);
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   158
                    if (env._dump_on_error) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   159
                        th.printStackTrace(err);
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   160
                    }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   161
                    return null;
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   162
                }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   163
            }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   164
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   165
            final String allLines = buf.toString();
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   166
            try {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   167
                parser.parse("<shell>", allLines, null);
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   168
            } catch (final Exception pexp) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   169
                // Do we have a parse error at the end of current line?
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   170
                // If so, read more lines from the console.
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   171
                if (isParseErrorAt(pexp, line, curLine.length())) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   172
                    continue;
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   173
                } else {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   174
                    // print anything else and bail out!
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   175
                    err.println(pexp);
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   176
                    if (env._dump_on_error) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   177
                        pexp.printStackTrace(err);
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   178
                    }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   179
                    return null;
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   180
                }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   181
            }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   182
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   183
            // We have complete parseable code!
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   184
            return buf.toString();
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   185
        }
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   186
    }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   187
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 32320
diff changeset
   188
    public boolean isComplete(String input) {
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 32320
diff changeset
   189
        try {
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 32320
diff changeset
   190
            parser.parse("<shell>", input, null);
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 32320
diff changeset
   191
        } catch (final Exception pexp) {
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 32320
diff changeset
   192
            // Do we have a parse error at the end of current line?
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 32320
diff changeset
   193
            // If so, read more lines from the console.
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 32320
diff changeset
   194
            int line = input.split("\n").length;
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 32320
diff changeset
   195
            int lastLineLen = input.length() - (input.lastIndexOf("\n") + 1);
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 32320
diff changeset
   196
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 32320
diff changeset
   197
            if (isParseErrorAt(pexp, line, lastLineLen)) {
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 32320
diff changeset
   198
                return false;
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 32320
diff changeset
   199
            }
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 32320
diff changeset
   200
        }
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 32320
diff changeset
   201
        return true;
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 32320
diff changeset
   202
    }
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 32320
diff changeset
   203
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   204
    // Pattern to match a unfinished member selection expression. object part and "."
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   205
    // but property name missing pattern.
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   206
    private static final Pattern SELECT_PROP_MISSING = Pattern.compile(".*\\.\\s*");
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   207
32318
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   208
    // Pattern to match load call
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   209
    private static final Pattern LOAD_CALL = Pattern.compile("\\s*load\\s*\\(\\s*");
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   210
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   211
    @Override
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   212
    public int complete(final String test, final int cursor, final List<CharSequence> result) {
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   213
        // check that cursor is at the end of test string. Do not complete in the middle!
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   214
        if (cursor != test.length()) {
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   215
            return cursor;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   216
        }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   217
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   218
        // get the start of the last expression embedded in the given code
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   219
        // using the partial parsing support - so that we can complete expressions
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   220
        // inside statements, function call argument lists, array index etc.
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   221
        final int exprStart = partialParser.getLastExpressionStart(context, test);
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   222
        if (exprStart == -1) {
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   223
            return cursor;
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   224
        }
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   225
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   226
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   227
        // extract the last expression string
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   228
        final String exprStr = test.substring(exprStart);
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   229
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   230
        // do we have an incomplete member selection expression that misses property name?
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   231
        final boolean endsWithDot = SELECT_PROP_MISSING.matcher(exprStr).matches();
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   232
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   233
        // If this is an incomplete member selection, then it is not legal code.
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   234
        // Make it legal by adding a random property name "x" to it.
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   235
        final String completeExpr = endsWithDot? exprStr + "x" : exprStr;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   236
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   237
        final ExpressionTree topExpr = getTopLevelExpression(parser, completeExpr);
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   238
        if (topExpr == null) {
32318
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   239
            // special case for load call that looks like "load(" with optional whitespaces
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   240
            if (LOAD_CALL.matcher(test).matches()) {
32319
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   241
                String name = readFileName(context.getErr());
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   242
                if (name != null) {
32318
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   243
                    // handle '\' file separator
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   244
                    if (BACKSLASH_FILE_SEPARATOR) {
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   245
                        name = name.replace("\\", "\\\\");
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   246
                    }
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   247
                    result.add("\"" + name + "\")");
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   248
                    return cursor + name.length() + 3;
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   249
                }
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   250
            }
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   251
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   252
            // did not parse to be a top level expression, no suggestions!
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   253
            return cursor;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   254
        }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   255
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   256
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   257
        // Find 'right most' expression of the top level expression
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   258
        final Tree rightMostExpr = getRightMostExpression(topExpr);
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   259
        if (rightMostExpr instanceof MemberSelectTree) {
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   260
            return completeMemberSelect(exprStr, cursor, result, (MemberSelectTree)rightMostExpr, endsWithDot);
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   261
        } else if (rightMostExpr instanceof IdentifierTree) {
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   262
            return completeIdentifier(exprStr, cursor, result, (IdentifierTree)rightMostExpr);
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   263
        } else {
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   264
            // expression that we cannot handle for completion
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   265
            return cursor;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   266
        }
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   267
    }
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   268
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   269
    // Internals only below this point
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   270
32319
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   271
    // read file name from the user using by showing a swing file chooser diablog
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   272
    private static String readFileName(final PrintWriter err) {
32320
3e408f16c1c9 8134397: Features that require AWT, swing should handle headless mode properly
sundar
parents: 32319
diff changeset
   273
        // if running on AWT Headless mode, don't attempt swing dialog box!
3e408f16c1c9 8134397: Features that require AWT, swing should handle headless mode properly
sundar
parents: 32319
diff changeset
   274
        if (Main.HEADLESS) {
3e408f16c1c9 8134397: Features that require AWT, swing should handle headless mode properly
sundar
parents: 32319
diff changeset
   275
            return null;
3e408f16c1c9 8134397: Features that require AWT, swing should handle headless mode properly
sundar
parents: 32319
diff changeset
   276
        }
3e408f16c1c9 8134397: Features that require AWT, swing should handle headless mode properly
sundar
parents: 32319
diff changeset
   277
32319
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   278
        final FutureTask<String> fileChooserTask = new FutureTask<String>(() -> {
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   279
            // show a file chooser dialog box
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   280
            final JFileChooser chooser = new JFileChooser();
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   281
            chooser.setFileFilter(new FileNameExtensionFilter("JavaScript Files", "js"));
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   282
            final int retVal = chooser.showOpenDialog(null);
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   283
            return retVal == JFileChooser.APPROVE_OPTION ?
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   284
                chooser.getSelectedFile().getAbsolutePath() : null;
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   285
        });
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   286
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   287
        SwingUtilities.invokeLater(fileChooserTask);
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   288
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   289
        try {
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   290
            return fileChooserTask.get();
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   291
        } catch (final ExecutionException | InterruptedException e) {
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   292
            err.println(e);
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   293
            if (Main.DEBUG) {
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   294
                e.printStackTrace();
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   295
            }
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   296
        }
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   297
        return null;
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   298
    }
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   299
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   300
    // fill properties of the incomplete member expression
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   301
    private int completeMemberSelect(final String exprStr, final int cursor, final List<CharSequence> result,
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   302
                final MemberSelectTree select, final boolean endsWithDot) {
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   303
        final ExpressionTree objExpr = select.getExpression();
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   304
        final String objExprCode = exprStr.substring((int)objExpr.getStartPosition(), (int)objExpr.getEndPosition());
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   305
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   306
        // try to evaluate the object expression part as a script
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   307
        Object obj = null;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   308
        try {
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   309
            obj = context.eval(global, objExprCode, global, "<suggestions>");
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
   310
        } catch (Exception exp) {
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
   311
            // throw away the exception - this is during tab-completion
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
   312
            if (Main.DEBUG) {
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
   313
                exp.printStackTrace();
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
   314
            }
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   315
        }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   316
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   317
        if (obj != null && obj != ScriptRuntime.UNDEFINED) {
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   318
            if (endsWithDot) {
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   319
                // no user specified "prefix". List all properties of the object
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
   320
                result.addAll(propsHelper.getProperties(obj));
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   321
                return cursor;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   322
            } else {
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   323
                // list of properties matching the user specified prefix
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   324
                final String prefix = select.getIdentifier();
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
   325
                result.addAll(propsHelper.getProperties(obj, prefix));
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   326
                return cursor - prefix.length();
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   327
            }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   328
        }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   329
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   330
        return cursor;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   331
    }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   332
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   333
    // fill properties for the given (partial) identifer
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   334
    private int completeIdentifier(final String test, final int cursor, final List<CharSequence> result,
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   335
                final IdentifierTree ident) {
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   336
        final String name = ident.getName();
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
   337
        result.addAll(propsHelper.getProperties(global, name));
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   338
        return cursor - name.length();
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   339
    }
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   340
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   341
    // returns ExpressionTree if the given code parses to a top level expression.
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   342
    // Or else returns null.
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   343
    private ExpressionTree getTopLevelExpression(final Parser parser, final String code) {
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   344
        try {
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   345
            final CompilationUnitTree cut = parser.parse("<code>", code, null);
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   346
            final List<? extends Tree> stats = cut.getSourceElements();
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   347
            if (stats.size() == 1) {
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   348
                final Tree stat = stats.get(0);
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   349
                if (stat instanceof ExpressionStatementTree) {
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   350
                    return ((ExpressionStatementTree)stat).getExpression();
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   351
                }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   352
            }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   353
        } catch (final NashornException ignored) {
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   354
            // ignore any parser error. This is for completion anyway!
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   355
            // And user will get that error later when the expression is evaluated.
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   356
        }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   357
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   358
        return null;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   359
    }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   360
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   361
    // get the right most expreesion of the given expression
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   362
    private Tree getRightMostExpression(final ExpressionTree expr) {
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   363
        return expr.accept(new SimpleTreeVisitorES5_1<Tree, Void>() {
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   364
            @Override
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   365
            public Tree visitAssignment(final AssignmentTree at, final Void v) {
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   366
                return getRightMostExpression(at.getExpression());
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   367
            }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   368
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   369
            @Override
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   370
            public Tree visitCompoundAssignment(final CompoundAssignmentTree cat, final Void v) {
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   371
                return getRightMostExpression(cat.getExpression());
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   372
            }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   373
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   374
            @Override
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   375
            public Tree visitConditionalExpression(final ConditionalExpressionTree cet, final Void v) {
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   376
                return getRightMostExpression(cet.getFalseExpression());
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   377
            }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   378
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   379
            @Override
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   380
            public Tree visitBinary(final BinaryTree bt, final Void v) {
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   381
                return getRightMostExpression(bt.getRightOperand());
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   382
            }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   383
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   384
            @Override
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   385
            public Tree visitIdentifier(final IdentifierTree ident, final Void v) {
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   386
                return ident;
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   387
            }
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   388
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   389
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   390
            @Override
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   391
            public Tree visitInstanceOf(final InstanceOfTree it, final Void v) {
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   392
                return it.getType();
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   393
            }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   394
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   395
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   396
            @Override
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   397
            public Tree visitMemberSelect(final MemberSelectTree select, final Void v) {
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   398
                return select;
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   399
            }
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   400
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   401
            @Override
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   402
            public Tree visitNew(final NewTree nt, final Void v) {
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   403
                final ExpressionTree call = nt.getConstructorExpression();
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   404
                if (call instanceof FunctionCallTree) {
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   405
                    final ExpressionTree func = ((FunctionCallTree)call).getFunctionSelect();
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   406
                    // Is this "new Foo" or "new obj.Foo" with no user arguments?
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   407
                    // If so, we may be able to do completion of constructor name.
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   408
                    if (func.getEndPosition() == nt.getEndPosition()) {
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   409
                        return func;
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   410
                    }
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   411
                }
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   412
                return null;
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   413
            }
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   414
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   415
            @Override
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   416
            public Tree visitUnary(final UnaryTree ut, final Void v) {
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   417
                return getRightMostExpression(ut.getExpression());
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   418
            }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   419
        }, null);
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   420
    }
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   421
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   422
    // create a Parser instance that uses compatible command line options of the
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   423
    // current ScriptEnvironment being used for REPL.
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   424
    private static Parser createParser(final ScriptEnvironment env) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   425
        final List<String> args = new ArrayList<>();
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   426
        if (env._const_as_var) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   427
            args.add("--const-as-var");
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   428
        }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   429
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   430
        if (env._no_syntax_extensions) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   431
            args.add("-nse");
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   432
        }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   433
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   434
        if (env._scripting) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   435
            args.add("-scripting");
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   436
        }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   437
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   438
        if (env._strict) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   439
            args.add("-strict");
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   440
        }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   441
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 38488
diff changeset
   442
        if (env._es6) {
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 38488
diff changeset
   443
            args.add("--language=es6");
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 38488
diff changeset
   444
        }
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 38488
diff changeset
   445
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   446
        return Parser.create(args.toArray(new String[0]));
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   447
    }
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   448
}