author | jlahoda |
Mon, 29 Feb 2016 11:54:06 +0100 | |
changeset 36160 | f42d362d0d17 |
parent 33362 | 65ec6de1d6b4 |
child 38908 | f0c186d76c8a |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
2 |
* Copyright (c) 2014, 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.jshell; |
|
27 |
||
28 |
import java.util.List; |
|
29 |
||
30 |
/** |
|
31 |
* Provides analysis utilities for source code input. |
|
32 |
* Optional functionality that provides for a richer interactive experience. |
|
33 |
* Includes completion analysis: |
|
34 |
* Is the input a complete snippet of code? |
|
35 |
* Do I need to prompt for more input? |
|
36 |
* Would adding a semicolon make it complete? |
|
37 |
* Is there more than one snippet? |
|
38 |
* etc. |
|
39 |
* Also includes completion suggestions, as might be used in tab-completion. |
|
40 |
* |
|
41 |
*/ |
|
42 |
public abstract class SourceCodeAnalysis { |
|
43 |
||
44 |
/** |
|
45 |
* Given an input string, find the first snippet of code (one statement, |
|
46 |
* definition, import, or expression) and evaluate if it is complete. |
|
47 |
* @param input the input source string |
|
48 |
* @return a CompletionInfo instance with location and completeness info |
|
49 |
*/ |
|
50 |
public abstract CompletionInfo analyzeCompletion(String input); |
|
51 |
||
52 |
/** |
|
53 |
* Compute possible follow-ups for the given input. |
|
54 |
* Uses information from the current <code>JShell</code> state, including |
|
55 |
* type information, to filter the suggestions. |
|
56 |
* @param input the user input, so far |
|
57 |
* @param cursor the current position of the cursors in the given {@code input} text |
|
58 |
* @param anchor outgoing parameter - when an option will be completed, the text between |
|
59 |
* the anchor and cursor will be deleted and replaced with the given option |
|
60 |
* @return list of candidate continuations of the given input. |
|
61 |
*/ |
|
62 |
public abstract List<Suggestion> completionSuggestions(String input, int cursor, int[] anchor); |
|
63 |
||
64 |
/** |
|
65 |
* Compute a description/help string for the given user's input. |
|
66 |
* @param input the snippet the user wrote so far |
|
67 |
* @param cursor the current position of the cursors in the given {@code input} text |
|
68 |
* @return description/help string for the given user's input |
|
69 |
*/ |
|
70 |
public abstract String documentation(String input, int cursor); |
|
71 |
||
72 |
/** |
|
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
73 |
* Infer the type of the given expression. The expression spans from the beginning of {@code code} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
74 |
* to the given {@code cursor} position. Returns null if the type of the expression cannot |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
75 |
* be inferred. |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
76 |
* |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
77 |
* @param code the expression for which the type should be inferred |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
78 |
* @param cursor current cursor position in the given code |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
79 |
* @return the inferred type, or null if it cannot be inferred |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
80 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
81 |
public abstract String analyzeType(String code, int cursor); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
82 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
83 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
84 |
* List qualified names known for the simple name in the given code immediately |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
85 |
* to the left of the given cursor position. The qualified names are gathered by inspecting the |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
86 |
* classpath used by eval (see {@link JShell#addToClasspath(java.lang.String)}). |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
87 |
* |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
88 |
* @param code the expression for which the candidate qualified names should be computed |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
89 |
* @param cursor current cursor position in the given code |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
90 |
* @return the known qualified names |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
91 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
92 |
public abstract QualifiedNames listQualifiedNames(String code, int cursor); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
93 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
94 |
/** |
33362 | 95 |
* Internal only constructor |
96 |
*/ |
|
97 |
SourceCodeAnalysis() {} |
|
98 |
||
99 |
/** |
|
100 |
* The result of <code>analyzeCompletion(String input)</code>. |
|
101 |
* Describes the completeness and position of the first snippet in the given input. |
|
102 |
*/ |
|
103 |
public static class CompletionInfo { |
|
104 |
||
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
105 |
CompletionInfo(Completeness completeness, int unitEndPos, String source, String remaining) { |
33362 | 106 |
this.completeness = completeness; |
107 |
this.unitEndPos = unitEndPos; |
|
108 |
this.source = source; |
|
109 |
this.remaining = remaining; |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* The analyzed completeness of the input. |
|
114 |
*/ |
|
115 |
public final Completeness completeness; |
|
116 |
||
117 |
/** |
|
118 |
* The end of the first unit of source. |
|
119 |
*/ |
|
120 |
public final int unitEndPos; |
|
121 |
||
122 |
/** |
|
123 |
* Source code for the first unit of code input. For example, first |
|
124 |
* statement, or first method declaration. Trailing semicolons will |
|
125 |
* be added, as needed |
|
126 |
*/ |
|
127 |
public final String source; |
|
128 |
||
129 |
/** |
|
130 |
* Input remaining after the source |
|
131 |
*/ |
|
132 |
public final String remaining; |
|
133 |
} |
|
134 |
||
135 |
/** |
|
136 |
* Describes the completeness of the given input. |
|
137 |
*/ |
|
138 |
public enum Completeness { |
|
139 |
/** |
|
140 |
* The input is a complete source snippet (declaration or statement) as is. |
|
141 |
*/ |
|
142 |
COMPLETE(true), |
|
143 |
||
144 |
/** |
|
145 |
* With this addition of a semicolon the input is a complete source snippet. |
|
146 |
* This will only be returned when the end of input is encountered. |
|
147 |
*/ |
|
148 |
COMPLETE_WITH_SEMI(true), |
|
149 |
||
150 |
/** |
|
151 |
* There must be further source beyond the given input in order for it |
|
152 |
* to be complete. A semicolon would not complete it. |
|
153 |
* This will only be returned when the end of input is encountered. |
|
154 |
*/ |
|
155 |
DEFINITELY_INCOMPLETE(false), |
|
156 |
||
157 |
/** |
|
158 |
* A statement with a trailing (non-terminated) empty statement. |
|
159 |
* Though technically it would be a complete statement |
|
160 |
* with the addition of a semicolon, it is rare |
|
161 |
* that that assumption is the desired behavior. |
|
162 |
* The input is considered incomplete. Comments and white-space are |
|
163 |
* still considered empty. |
|
164 |
*/ |
|
165 |
CONSIDERED_INCOMPLETE(false), |
|
166 |
||
167 |
||
168 |
/** |
|
169 |
* An empty input. |
|
170 |
* The input is considered incomplete. Comments and white-space are |
|
171 |
* still considered empty. |
|
172 |
*/ |
|
173 |
EMPTY(false), |
|
174 |
||
175 |
/** |
|
176 |
* The completeness of the input could not be determined because it |
|
177 |
* contains errors. Error detection is not a goal of completeness |
|
178 |
* analysis, however errors interfered with determining its completeness. |
|
179 |
* The input is considered complete because evaluating is the best |
|
180 |
* mechanism to get error information. |
|
181 |
*/ |
|
182 |
UNKNOWN(true); |
|
183 |
||
184 |
/** |
|
185 |
* Is the first snippet of source complete. For example, "x=" is not |
|
186 |
* complete, but "x=2" is complete, even though a subsequent line could |
|
187 |
* make it "x=2+2". Already erroneous code is marked complete. |
|
188 |
*/ |
|
189 |
public final boolean isComplete; |
|
190 |
||
191 |
Completeness(boolean isComplete) { |
|
192 |
this.isComplete = isComplete; |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
/** |
|
197 |
* A candidate for continuation of the given user's input. |
|
198 |
*/ |
|
199 |
public static class Suggestion { |
|
200 |
||
201 |
/** |
|
202 |
* Create a {@code Suggestion} instance. |
|
203 |
* @param continuation a candidate continuation of the user's input |
|
204 |
* @param isSmart is the candidate "smart" |
|
205 |
*/ |
|
206 |
public Suggestion(String continuation, boolean isSmart) { |
|
207 |
this.continuation = continuation; |
|
208 |
this.isSmart = isSmart; |
|
209 |
} |
|
210 |
||
211 |
/** |
|
212 |
* The candidate continuation of the given user's input. |
|
213 |
*/ |
|
214 |
public final String continuation; |
|
215 |
||
216 |
/** |
|
217 |
* Is it an input continuation that matches the target type and is thus more |
|
218 |
* likely to be the desired continuation. A smart continuation |
|
219 |
* is preferred. |
|
220 |
*/ |
|
221 |
public final boolean isSmart; |
|
222 |
} |
|
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
223 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
224 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
225 |
* List of possible qualified names. |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
226 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
227 |
public static final class QualifiedNames { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
228 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
229 |
private final List<String> names; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
230 |
private final int simpleNameLength; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
231 |
private final boolean upToDate; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
232 |
private final boolean resolvable; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
233 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
234 |
QualifiedNames(List<String> names, int simpleNameLength, boolean upToDate, boolean resolvable) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
235 |
this.names = names; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
236 |
this.simpleNameLength = simpleNameLength; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
237 |
this.upToDate = upToDate; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
238 |
this.resolvable = resolvable; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
239 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
240 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
241 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
242 |
* Known qualified names for the given simple name in the original code. |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
243 |
* |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
244 |
* @return known qualified names |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
245 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
246 |
public List<String> getNames() { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
247 |
return names; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
248 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
249 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
250 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
251 |
* The length of the simple name in the original code for which the |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
252 |
* qualified names where gathered. |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
253 |
* |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
254 |
* @return the length of the simple name; -1 if there is no name immediately left to the cursor for |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
255 |
* which the candidates could be computed |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
256 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
257 |
public int getSimpleNameLength() { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
258 |
return simpleNameLength; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
259 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
260 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
261 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
262 |
* Whether the result is based on up to date data. The |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
263 |
* {@link SourceCodeAnalysis#listQualifiedNames(java.lang.String, int) listQualifiedNames} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
264 |
* method may return before the classpath is fully inspected, in which case this method will |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
265 |
* return {@code false}. If the result is based on a fully inspected classpath, this method |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
266 |
* will return {@code true}. |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
267 |
* |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
268 |
* @return true iff the results is based on up-to-date data |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
269 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
270 |
public boolean isUpToDate() { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
271 |
return upToDate; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
272 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
273 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
274 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
275 |
* Whether the given simple name in the original code refers to a resolvable element. |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
276 |
* |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
277 |
* @return true iff the given simple name in the original code refers to a resolvable element |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
278 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
279 |
public boolean isResolvable() { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
280 |
return resolvable; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
281 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
282 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
283 |
} |
33362 | 284 |
} |