--- a/langtools/src/jdk.jshell/share/classes/jdk/jshell/SourceCodeAnalysisImpl.java Wed Jul 05 22:05:29 2017 +0200
+++ b/langtools/src/jdk.jshell/share/classes/jdk/jshell/SourceCodeAnalysisImpl.java Mon Aug 15 11:39:53 2016 -0700
@@ -171,13 +171,13 @@
MaskCommentsAndModifiers mcm = new MaskCommentsAndModifiers(srcInput, false);
if (mcm.endsWithOpenComment()) {
proc.debug(DBG_COMPA, "Incomplete (open comment): %s\n", srcInput);
- return new CompletionInfo(DEFINITELY_INCOMPLETE, srcInput.length(), null, srcInput + '\n');
+ return new CompletionInfoImpl(DEFINITELY_INCOMPLETE, null, srcInput + '\n');
}
String cleared = mcm.cleared();
String trimmedInput = Util.trimEnd(cleared);
if (trimmedInput.isEmpty()) {
// Just comment or empty
- return new CompletionInfo(Completeness.EMPTY, srcInput.length(), srcInput, "");
+ return new CompletionInfoImpl(Completeness.EMPTY, srcInput, "");
}
CaInfo info = ca.scan(trimmedInput);
Completeness status = info.status;
@@ -195,12 +195,12 @@
+ mcm.mask().substring(nonCommentNonWhiteLength);
proc.debug(DBG_COMPA, "Complete: %s\n", compileSource);
proc.debug(DBG_COMPA, " nothing remains.\n");
- return new CompletionInfo(status, unitEndPos, compileSource, "");
+ return new CompletionInfoImpl(status, compileSource, "");
} else {
String remain = srcInput.substring(unitEndPos);
proc.debug(DBG_COMPA, "Complete: %s\n", src);
proc.debug(DBG_COMPA, " remaining: %s\n", remain);
- return new CompletionInfo(status, unitEndPos, src, remain);
+ return new CompletionInfoImpl(status, src, remain);
}
case COMPLETE_WITH_SEMI:
// The unit is the whole non-coment/white input plus semicolon
@@ -209,19 +209,19 @@
+ mcm.mask().substring(nonCommentNonWhiteLength);
proc.debug(DBG_COMPA, "Complete with semi: %s\n", compileSource);
proc.debug(DBG_COMPA, " nothing remains.\n");
- return new CompletionInfo(status, unitEndPos, compileSource, "");
+ return new CompletionInfoImpl(status, compileSource, "");
case DEFINITELY_INCOMPLETE:
proc.debug(DBG_COMPA, "Incomplete: %s\n", srcInput);
- return new CompletionInfo(status, unitEndPos, null, srcInput + '\n');
+ return new CompletionInfoImpl(status, null, srcInput + '\n');
case CONSIDERED_INCOMPLETE:
proc.debug(DBG_COMPA, "Considered incomplete: %s\n", srcInput);
- return new CompletionInfo(status, unitEndPos, null, srcInput + '\n');
+ return new CompletionInfoImpl(status, null, srcInput + '\n');
case EMPTY:
proc.debug(DBG_COMPA, "Detected empty: %s\n", srcInput);
- return new CompletionInfo(status, unitEndPos, srcInput, "");
+ return new CompletionInfoImpl(status, srcInput, "");
case UNKNOWN:
proc.debug(DBG_COMPA, "Detected error: %s\n", srcInput);
- return new CompletionInfo(status, unitEndPos, srcInput, "");
+ return new CompletionInfoImpl(status, srcInput, "");
}
throw new InternalError();
}
@@ -665,7 +665,7 @@
if (c.getKind() == ElementKind.CONSTRUCTOR || c.getKind() == ElementKind.METHOD) {
simpleName += paren.apply(hasParams.contains(simpleName));
}
- result.add(new Suggestion(simpleName, smart.test(c)));
+ result.add(new SuggestionImpl(simpleName, smart.test(c)));
}
}
@@ -1700,4 +1700,98 @@
}
}
}
+
+ /**
+ * A candidate for continuation of the given user's input.
+ */
+ private static class SuggestionImpl implements Suggestion {
+
+ private final String continuation;
+ private final boolean matchesType;
+
+ /**
+ * Create a {@code Suggestion} instance.
+ *
+ * @param continuation a candidate continuation of the user's input
+ * @param matchesType does the candidate match the target type
+ */
+ public SuggestionImpl(String continuation, boolean matchesType) {
+ this.continuation = continuation;
+ this.matchesType = matchesType;
+ }
+
+ /**
+ * The candidate continuation of the given user's input.
+ *
+ * @return the continuation string
+ */
+ @Override
+ public String continuation() {
+ return continuation;
+ }
+
+ /**
+ * Indicates whether input continuation matches the target type and is thus
+ * more likely to be the desired continuation. A matching continuation is
+ * preferred.
+ *
+ * @return {@code true} if this suggested continuation matches the
+ * target type; otherwise {@code false}
+ */
+ @Override
+ public boolean matchesType() {
+ return matchesType;
+ }
+ }
+
+ /**
+ * The result of {@code analyzeCompletion(String input)}.
+ * Describes the completeness and position of the first snippet in the given input.
+ */
+ private static class CompletionInfoImpl implements CompletionInfo {
+
+ private final Completeness completeness;
+ private final String source;
+ private final String remaining;
+
+ CompletionInfoImpl(Completeness completeness, String source, String remaining) {
+ this.completeness = completeness;
+ this.source = source;
+ this.remaining = remaining;
+ }
+
+ /**
+ * The analyzed completeness of the input.
+ *
+ * @return an enum describing the completeness of the input string.
+ */
+ @Override
+ public Completeness completeness() {
+ return completeness;
+ }
+
+ /**
+ * Input remaining after the complete part of the source.
+ *
+ * @return the portion of the input string that remains after the
+ * complete Snippet
+ */
+ @Override
+ public String remaining() {
+ return remaining;
+ }
+
+ /**
+ * Source code for the first Snippet of code input. For example, first
+ * statement, or first method declaration. Trailing semicolons will be
+ * added, as needed.
+ *
+ * @return the source of the first encountered Snippet
+ */
+ @Override
+ public String source() {
+ return source;
+ }
+ }
+
}