src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/NashornCompleter.java
author sundar
Fri, 03 Nov 2017 19:53:09 +0530
changeset 47492 560fab171dc7
parent 47216 71c04702a3d5
child 52938 5ff7480c9e28
permissions -rw-r--r--
8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop Reviewed-by: jlaskey, hannesw
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;
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
    32
import java.util.Objects;
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
    33
import java.util.regex.Pattern;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    34
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
    35
import jdk.internal.jline.console.UserInterruptException;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    36
import jdk.nashorn.api.tree.AssignmentTree;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    37
import jdk.nashorn.api.tree.BinaryTree;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    38
import jdk.nashorn.api.tree.CompilationUnitTree;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    39
import jdk.nashorn.api.tree.CompoundAssignmentTree;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    40
import jdk.nashorn.api.tree.ConditionalExpressionTree;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    41
import jdk.nashorn.api.tree.ExpressionTree;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    42
import jdk.nashorn.api.tree.ExpressionStatementTree;
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
    43
import jdk.nashorn.api.tree.FunctionCallTree;
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
    44
import jdk.nashorn.api.tree.IdentifierTree;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    45
import jdk.nashorn.api.tree.InstanceOfTree;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    46
import jdk.nashorn.api.tree.MemberSelectTree;
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
    47
import jdk.nashorn.api.tree.NewTree;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    48
import jdk.nashorn.api.tree.SimpleTreeVisitorES5_1;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    49
import jdk.nashorn.api.tree.Tree;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    50
import jdk.nashorn.api.tree.UnaryTree;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    51
import jdk.nashorn.api.tree.Parser;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    52
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
    53
import jdk.nashorn.tools.PartialParser;
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    54
import jdk.nashorn.internal.objects.NativeSyntaxError;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    55
import jdk.nashorn.internal.objects.Global;
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    56
import jdk.nashorn.internal.runtime.ECMAException;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    57
import jdk.nashorn.internal.runtime.Context;
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    58
import jdk.nashorn.internal.runtime.ScriptEnvironment;
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
    59
import jdk.nashorn.internal.runtime.ScriptFunction;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    60
import jdk.nashorn.internal.runtime.ScriptRuntime;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    61
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    62
/**
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    63
 * 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
    64
 * 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
    65
 */
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    66
final class NashornCompleter implements Completer {
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    67
    private final Context context;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    68
    private final Global global;
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    69
    private final ScriptEnvironment env;
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
    70
    private final PartialParser partialParser;
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
    71
    private final PropertiesHelper propsHelper;
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
    72
    private final ScriptFunction fileChooserFunc;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    73
    private final Parser parser;
32318
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
    74
    private static final boolean BACKSLASH_FILE_SEPARATOR = File.separatorChar == '\\';
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    75
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
    76
    NashornCompleter(final Context context, final Global global,
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
    77
            final PartialParser partialParser, final PropertiesHelper propsHelper,
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
    78
            final ScriptFunction fileChooserFunc) {
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    79
        this.context = context;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
    80
        this.global = global;
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    81
        this.env = context.getEnv();
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
    82
        this.partialParser = partialParser;
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
    83
        this.propsHelper = propsHelper;
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
    84
        this.fileChooserFunc = fileChooserFunc;
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) {
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   239
            // Special case for load call that looks like "load(" with optional whitespaces.
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   240
            // If we have a fileChooserFunc then call it, so that the user can select a file.
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   241
            if (fileChooserFunc != null && LOAD_CALL.matcher(test).matches()) {
32319
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   242
                String name = readFileName(context.getErr());
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   243
                if (name != null) {
32318
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   244
                    // handle '\' file separator
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   245
                    if (BACKSLASH_FILE_SEPARATOR) {
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   246
                        name = name.replace("\\", "\\\\");
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   247
                    }
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   248
                    result.add("\"" + name + "\")");
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   249
                    return cursor + name.length() + 3;
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
            }
3279b026c98a 8134309: load call argument completion could be done with file chooser
sundar
parents: 32317
diff changeset
   252
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   253
            // did not parse to be a top level expression, no suggestions!
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   254
            return cursor;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   255
        }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   256
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   257
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   258
        // 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
   259
        final Tree rightMostExpr = getRightMostExpression(topExpr);
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   260
        if (rightMostExpr instanceof MemberSelectTree) {
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   261
            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
   262
        } else if (rightMostExpr instanceof IdentifierTree) {
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   263
            return completeIdentifier(exprStr, cursor, result, (IdentifierTree)rightMostExpr);
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   264
        } else {
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   265
            // expression that we cannot handle for completion
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   266
            return cursor;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   267
        }
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   268
    }
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   269
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   270
    // Internals only below this point
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   271
32319
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   272
    // read file name from the user using by showing a swing file chooser diablog
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   273
    private String readFileName(final PrintWriter err) {
32319
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   274
        try {
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   275
            final Object res = ScriptRuntime.apply(fileChooserFunc, null);
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   276
            return res instanceof String? (String)res : null;
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   277
        } catch (final Exception e) {
32319
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   278
            err.println(e);
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   279
            if (Main.DEBUG) {
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   280
                e.printStackTrace();
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   281
            }
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   282
        }
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   283
        return null;
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   284
    }
3f22ddeabc0e 8134381: load completion should not use swing from non UI thread
sundar
parents: 32318
diff changeset
   285
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   286
    // 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
   287
    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
   288
                final MemberSelectTree select, final boolean endsWithDot) {
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   289
        final ExpressionTree objExpr = select.getExpression();
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32243
diff changeset
   290
        final String objExprCode = exprStr.substring((int)objExpr.getStartPosition(), (int)objExpr.getEndPosition());
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   291
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   292
        // try to evaluate the object expression part as a script
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   293
        Object obj = null;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   294
        try {
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   295
            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
   296
        } catch (Exception exp) {
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
   297
            // 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
   298
            if (Main.DEBUG) {
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
   299
                exp.printStackTrace();
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
   300
            }
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   301
        }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   302
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   303
        if (obj != null && obj != ScriptRuntime.UNDEFINED) {
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   304
            if (endsWithDot) {
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   305
                // 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
   306
                result.addAll(propsHelper.getProperties(obj));
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   307
                return cursor;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   308
            } else {
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   309
                // 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
   310
                final String prefix = select.getIdentifier();
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
   311
                result.addAll(propsHelper.getProperties(obj, prefix));
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   312
                return cursor - prefix.length();
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   313
            }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   314
        }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   315
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   316
        return cursor;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   317
    }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   318
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   319
    // fill properties for the given (partial) identifer
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   320
    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
   321
                final IdentifierTree ident) {
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   322
        final String name = ident.getName();
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32245
diff changeset
   323
        result.addAll(propsHelper.getProperties(global, name));
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   324
        return cursor - name.length();
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   325
    }
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   326
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   327
    // returns ExpressionTree if the given code parses to a top level expression.
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   328
    // Or else returns null.
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   329
    private ExpressionTree getTopLevelExpression(final Parser parser, final String code) {
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   330
        try {
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   331
            final CompilationUnitTree cut = parser.parse("<code>", code, null);
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   332
            final List<? extends Tree> stats = cut.getSourceElements();
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   333
            if (stats.size() == 1) {
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   334
                final Tree stat = stats.get(0);
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   335
                if (stat instanceof ExpressionStatementTree) {
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   336
                    return ((ExpressionStatementTree)stat).getExpression();
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   337
                }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   338
            }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   339
        } catch (final NashornException ignored) {
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   340
            // ignore any parser error. This is for completion anyway!
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   341
            // And user will get that error later when the expression is evaluated.
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   342
        }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   343
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   344
        return null;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   345
    }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   346
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   347
    // 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
   348
    private Tree getRightMostExpression(final ExpressionTree expr) {
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   349
        return expr.accept(new SimpleTreeVisitorES5_1<Tree, Void>() {
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   350
            @Override
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   351
            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
   352
                return getRightMostExpression(at.getExpression());
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   353
            }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   354
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   355
            @Override
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   356
            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
   357
                return getRightMostExpression(cat.getExpression());
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   358
            }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   359
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   360
            @Override
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   361
            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
   362
                return getRightMostExpression(cet.getFalseExpression());
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   363
            }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   364
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   365
            @Override
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   366
            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
   367
                return getRightMostExpression(bt.getRightOperand());
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   368
            }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   369
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   370
            @Override
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   371
            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
   372
                return ident;
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   373
            }
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   374
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   375
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   376
            @Override
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   377
            public Tree visitInstanceOf(final InstanceOfTree it, final Void v) {
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   378
                return it.getType();
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   379
            }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   380
32243
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   381
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   382
            @Override
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   383
            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
   384
                return select;
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   385
            }
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   386
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   387
            @Override
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   388
            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
   389
                final ExpressionTree call = nt.getConstructorExpression();
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   390
                if (call instanceof FunctionCallTree) {
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   391
                    final ExpressionTree func = ((FunctionCallTree)call).getFunctionSelect();
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   392
                    // 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
   393
                    // 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
   394
                    if (func.getEndPosition() == nt.getEndPosition()) {
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   395
                        return func;
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   396
                    }
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   397
                }
422ccb5d8437 8133812: identifier and member expression completion handling is not uniform
sundar
parents: 32241
diff changeset
   398
                return null;
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
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   401
            @Override
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   402
            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
   403
                return getRightMostExpression(ut.getExpression());
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   404
            }
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   405
        }, null);
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   406
    }
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   407
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   408
    // 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
   409
    // current ScriptEnvironment being used for REPL.
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   410
    private static Parser createParser(final ScriptEnvironment env) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   411
        final List<String> args = new ArrayList<>();
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   412
        if (env._const_as_var) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   413
            args.add("--const-as-var");
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   414
        }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   415
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   416
        if (env._no_syntax_extensions) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   417
            args.add("-nse");
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   418
        }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   419
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   420
        if (env._scripting) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   421
            args.add("-scripting");
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   422
        }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   423
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   424
        if (env._strict) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   425
            args.add("-strict");
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   426
        }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   427
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 38488
diff changeset
   428
        if (env._es6) {
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 38488
diff changeset
   429
            args.add("--language=es6");
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 38488
diff changeset
   430
        }
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 38488
diff changeset
   431
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   432
        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
   433
    }
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents:
diff changeset
   434
}