author | sundar |
Fri, 03 Nov 2017 19:53:09 +0530 | |
changeset 47492 | 560fab171dc7 |
parent 47216 | 71c04702a3d5 |
child 52938 | 5ff7480c9e28 |
permissions | -rw-r--r-- |
32241 | 1 |
/* |
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.nashorn.tools.jjs; |
|
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 | 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 | 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 | 36 |
import jdk.nashorn.api.tree.AssignmentTree; |
37 |
import jdk.nashorn.api.tree.BinaryTree; |
|
38 |
import jdk.nashorn.api.tree.CompilationUnitTree; |
|
39 |
import jdk.nashorn.api.tree.CompoundAssignmentTree; |
|
40 |
import jdk.nashorn.api.tree.ConditionalExpressionTree; |
|
41 |
import jdk.nashorn.api.tree.ExpressionTree; |
|
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 | 45 |
import jdk.nashorn.api.tree.InstanceOfTree; |
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 | 48 |
import jdk.nashorn.api.tree.SimpleTreeVisitorES5_1; |
49 |
import jdk.nashorn.api.tree.Tree; |
|
50 |
import jdk.nashorn.api.tree.UnaryTree; |
|
51 |
import jdk.nashorn.api.tree.Parser; |
|
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 | 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 | 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 | 60 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
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 | 66 |
final class NashornCompleter implements Completer { |
67 |
private final Context context; |
|
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 | 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 | 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 | 79 |
this.context = context; |
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 | 186 |
} |
187 |
||
38488 | 188 |
public boolean isComplete(String input) { |
189 |
try { |
|
190 |
parser.parse("<shell>", input, null); |
|
191 |
} catch (final Exception pexp) { |
|
192 |
// Do we have a parse error at the end of current line? |
|
193 |
// If so, read more lines from the console. |
|
194 |
int line = input.split("\n").length; |
|
195 |
int lastLineLen = input.length() - (input.lastIndexOf("\n") + 1); |
|
196 |
||
197 |
if (isParseErrorAt(pexp, line, lastLineLen)) { |
|
198 |
return false; |
|
199 |
} |
|
200 |
} |
|
201 |
return true; |
|
202 |
} |
|
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 | 211 |
@Override |
212 |
public int complete(final String test, final int cursor, final List<CharSequence> result) { |
|
213 |
// check that cursor is at the end of test string. Do not complete in the middle! |
|
214 |
if (cursor != test.length()) { |
|
215 |
return cursor; |
|
216 |
} |
|
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 | 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 | 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 | 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 | 253 |
// did not parse to be a top level expression, no suggestions! |
254 |
return cursor; |
|
255 |
} |
|
256 |
||
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 | 266 |
return cursor; |
267 |
} |
|
32243
422ccb5d8437
8133812: identifier and member expression completion handling is not uniform
sundar
parents:
32241
diff
changeset
|
268 |
} |
32241 | 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 | 291 |
|
292 |
// try to evaluate the object expression part as a script |
|
293 |
Object obj = null; |
|
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 | 301 |
} |
302 |
||
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 | 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 | 307 |
return cursor; |
308 |
} else { |
|
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 | 313 |
} |
314 |
} |
|
315 |
||
316 |
return cursor; |
|
317 |
} |
|
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 | 327 |
// returns ExpressionTree if the given code parses to a top level expression. |
328 |
// Or else returns null. |
|
329 |
private ExpressionTree getTopLevelExpression(final Parser parser, final String code) { |
|
330 |
try { |
|
331 |
final CompilationUnitTree cut = parser.parse("<code>", code, null); |
|
332 |
final List<? extends Tree> stats = cut.getSourceElements(); |
|
333 |
if (stats.size() == 1) { |
|
334 |
final Tree stat = stats.get(0); |
|
335 |
if (stat instanceof ExpressionStatementTree) { |
|
336 |
return ((ExpressionStatementTree)stat).getExpression(); |
|
337 |
} |
|
338 |
} |
|
339 |
} catch (final NashornException ignored) { |
|
340 |
// ignore any parser error. This is for completion anyway! |
|
341 |
// And user will get that error later when the expression is evaluated. |
|
342 |
} |
|
343 |
||
344 |
return null; |
|
345 |
} |
|
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 | 350 |
@Override |
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 | 353 |
} |
354 |
||
355 |
@Override |
|
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 | 358 |
} |
359 |
||
360 |
@Override |
|
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 | 363 |
} |
364 |
||
365 |
@Override |
|
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 | 368 |
} |
369 |
||
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 | 377 |
public Tree visitInstanceOf(final InstanceOfTree it, final Void v) { |
378 |
return it.getType(); |
|
379 |
} |
|
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 | 401 |
@Override |
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 | 404 |
} |
405 |
}, null); |
|
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 | 434 |
} |