Summary
The JShell API is enhanced to allow limited highlighting of snippets
Problem
The JShell tool could show certain elements (like keywords or deprecated elements) of the code snippets highlighted.
Solution
A new API method, and supporting classes, is added to the JShell API, to provide highlighting attributes for given code snippets. This method is then used by the JShell Tool to show highlighted snippets to the user.
Specification
The specdiff of the proposed change is attached, and is also available for convenience here: http://cr.openjdk.java.net/~jlahoda/8274148/specdiff.00/overview-summary.html
The API patch is:
diff --git a/src/jdk.jshell/share/classes/jdk/jshell/SourceCodeAnalysis.java b/src/jdk.jshell/share/classes/jdk/jshell/SourceCodeAnalysis.java
index e37517df1e60..7a78c04f5a86 100644
--- a/src/jdk.jshell/share/classes/jdk/jshell/SourceCodeAnalysis.java
+++ b/src/jdk.jshell/share/classes/jdk/jshell/SourceCodeAnalysis.java
@@ -27,6 +27,7 @@
import java.util.Collection;
import java.util.List;
+import java.util.Set;
/**
* Provides analysis utilities for source code input.
@@ -170,6 +171,19 @@
*/
public abstract Collection<Snippet> dependents(Snippet snippet);
+ /**
+ * Returns a collection of {@code Highlight}s which can be used to color
+ * the given snippet.
+ * <p>
+ * The returned {@code Highlight}s do not overlap, and are sorted by their
+ * start position.
+ *
+ * @param snippet the snippet for which the {@code Highlight}s should be computed
+ * @return the computed {@code Highlight}s.
+ * @since 19
+ */
+ public abstract List<Highlight> highlights(String snippet);
+
/**
* Internal only constructor
*/
@@ -460,4 +474,32 @@ public boolean isResolvable() {
*/
int wrappedToSourcePosition(int pos);
}
+
+ /**Assigns attributes usable for coloring to spans inside a snippet.
+ *
+ * @param start the starting position of the span
+ * @param end the ending position of the span
+ * @param attributes the attributes assigned to the span
+ * @since 19
+ */
+ public record Highlight(int start, int end, Set<Attribute> attributes) {}
+
+ /**
+ * A span attribute which can be used to derive a coloring.
+ * @since 19
+ */
+ public enum Attribute {
+ /**
+ * The span refers to a declaration of an element.
+ */
+ DECLARATION,
+ /**
+ * The span refers to a deprecated element.
+ */
+ DEPRECATED,
+ /**
+ * The span is a keyword.
+ */
+ KEYWORD;
+ }
- csr of
-
JDK-8274148 can jshell show deprecated classes, methods and fields as strikethrough text?
-
- Resolved
-