Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Markdown javadoc (JEP-467) #7491

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ TokenSequence<JavaTokenId> nextNonWhitespaceToken(TokenSequence<JavaTokenId> ts)
case LINE_COMMENT:
case BLOCK_COMMENT:
case JAVADOC_COMMENT:
case JAVADOC_COMMENT_LINE_RUN:
break;
default:
return ts;
Expand Down Expand Up @@ -206,6 +207,7 @@ TokenSequence<JavaTokenId> previousNonWhitespaceToken(TokenSequence<JavaTokenId>
case LINE_COMMENT:
case BLOCK_COMMENT:
case JAVADOC_COMMENT:
case JAVADOC_COMMENT_LINE_RUN:
break;
default:
return ts;
Expand Down Expand Up @@ -461,6 +463,7 @@ private Env getEnvImpl(CompilationController controller, TreePath orig, TreePath
case LINE_COMMENT:
case BLOCK_COMMENT:
case JAVADOC_COMMENT:
case JAVADOC_COMMENT_LINE_RUN:
break;
case ARROW:
scope = controller.getTrees().getScope(blockPath);
Expand Down Expand Up @@ -490,6 +493,7 @@ private Env getEnvImpl(CompilationController controller, TreePath orig, TreePath
case LINE_COMMENT:
case BLOCK_COMMENT:
case JAVADOC_COMMENT:
case JAVADOC_COMMENT_LINE_RUN:
break;
case ARROW:
return new Env(offset, prefix, controller, path, sourcePositions, scope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,7 @@ private void insideMemberSelect(Env env) throws IOException {
case LINE_COMMENT:
case BLOCK_COMMENT:
case JAVADOC_COMMENT:
case JAVADOC_COMMENT_LINE_RUN:
break;
default:
lastNonWhitespaceTokenId = ts.token().id();
Expand Down
2 changes: 1 addition & 1 deletion java/java.editor.base/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# under the License.
spec.version.base=2.91.0
is.autoload=true
javac.source=1.8
javac.release=17
javac.compilerargs=-Xlint -Xlint:-serial

test.config.semantic.includes=\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
*/
public final class JavadocCompletionUtils {

static final Pattern JAVADOC_LINE_BREAK = Pattern.compile("\\n[ \\t]*\\**[ \\t]*\\z"); // NOI18N
static final Pattern JAVADOC_LINE_BREAK = Pattern.compile("(\\n[ \\t]*\\**[ \\t]*\\z)|(\\n[ \\t]*///[ \\t]*\\z)"); // NOI18N
static final Pattern JAVADOC_WHITE_SPACE = Pattern.compile("[^ \\t]"); // NOI18N
/**
* javadoc parser considers whatever number of spaces or standalone newline
Expand All @@ -62,7 +62,7 @@ public final class JavadocCompletionUtils {
static final Pattern JAVADOC_EMPTY = Pattern.compile("(\\s*\\**\\s*\n)*\\s*\\**\\s*\\**"); // NOI18N
static final Pattern JAVADOC_FIRST_WHITE_SPACE = Pattern.compile("[ \\t]*\\**[ \\t]*"); // NOI18N
private static Set<JavaTokenId> IGNORE_TOKES = EnumSet.of(
JavaTokenId.WHITESPACE, JavaTokenId.BLOCK_COMMENT, JavaTokenId.LINE_COMMENT);
JavaTokenId.WHITESPACE, JavaTokenId.BLOCK_COMMENT, JavaTokenId.LINE_COMMENT, JavaTokenId.JAVADOC_COMMENT_LINE_RUN);
private static final Logger LOGGER = Logger.getLogger(JavadocCompletionUtils.class.getName());

/**
Expand Down Expand Up @@ -196,6 +196,7 @@ public static TokenSequence<JavadocTokenId> findJavadocTokenSequence(Compilation
break;
}
case JAVADOC_COMMENT:
case JAVADOC_COMMENT_LINE_RUN:
if (token.partType() == PartType.COMPLETE) {
return javac.getElements().getDocComment(e) == null
? null : s.embedded(JavadocTokenId.language());
Expand Down Expand Up @@ -246,36 +247,39 @@ static boolean isInsideIndent(Token<JavadocTokenId> token, int offset) {

/**
* Is javadoc line break?
* @param token token to test
* @param ts a token sequence positioned to the token to test
* @return {@code true} in case the token is something like {@code "\n\t*"}
*/
public static boolean isLineBreak(Token<JavadocTokenId> token) {
return isLineBreak(token, token.length());
public static boolean isLineBreak(TokenSequence<JavadocTokenId> ts) {
return isLineBreak(ts, ts.token().length());
}

/**
* Tests if the token part before {@code pos} is a javadoc line break.
* @param token a token to test
* @param ts a token sequence positioned to the token to test
* @param pos position in the token
* @return {@code true} in case the token is something like {@code "\n\t* |\n\t*"}
*/
public static boolean isLineBreak(Token<JavadocTokenId> token, int pos) {
public static boolean isLineBreak(TokenSequence<JavadocTokenId> ts, int pos) {
Token<JavadocTokenId> token = ts.token();

if (token == null || token.id() != JavadocTokenId.OTHER_TEXT) {
return false;
return ts.isEmpty() || ts.index() == 0;
}
try {
CharSequence text = token.text();
if (pos < token.length())
text = text.subSequence(0, pos);
boolean result = pos > 0
boolean result = (pos > 0
&& JAVADOC_LINE_BREAK.matcher(text).find()
&& (pos == token.length() || !isInsideIndent(token, pos));
&& (pos == token.length() || !isInsideIndent(token, pos))
);
return result;
} catch (IndexOutOfBoundsException e) {
throw (IndexOutOfBoundsException) new IndexOutOfBoundsException("pos: " + pos + ", token.length: " + token.length() + ", token text: " + token.text()).initCause(e);
}
}

public static boolean isWhiteSpace(CharSequence text) {
return text != null && text.length() > 0 && !JAVADOC_WHITE_SPACE.matcher(text).find();
}
Expand Down Expand Up @@ -437,7 +441,8 @@ private static boolean movedToJavadocToken(TokenSequence<JavaTokenId> ts, int of
return false;
}

if (ts.token().id() != JavaTokenId.JAVADOC_COMMENT) {
if (ts.token().id() != JavaTokenId.JAVADOC_COMMENT &&
ts.token().id() != JavaTokenId.JAVADOC_COMMENT_LINE_RUN) {
return false;
}

Expand All @@ -456,6 +461,11 @@ private static boolean isEmptyJavadoc(Token<JavaTokenId> token, int offset) {
// check special case /**|*/
return offset == 3 && "/***/".contentEquals(text); //NOI18N
}
if (token != null && token.id() == JavaTokenId.JAVADOC_COMMENT_LINE_RUN) {
CharSequence text = token.text();
// check special case ///|\n
return offset == 3 && "///\n".contentEquals(text); //NOI18N
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ public static boolean isInsideReference(TokenSequence<JavadocTokenId> jdts, int
}
case OTHER_TEXT:
isBeforeWS |= JavadocCompletionUtils.isWhiteSpace(jdt);
isBeforeWS |= JavadocCompletionUtils.isLineBreak(jdt);
isBeforeWS |= JavadocCompletionUtils.isLineBreak(jdts);
if (isBeforeWS) {
continue;
} else {
Expand Down Expand Up @@ -690,7 +690,9 @@ private static TokenSequence<JavadocTokenId> getJavadocTS(CompilationInfo javac,
TokenSequence<JavadocTokenId> javadoc = null;
TokenSequence<JavaTokenId> ts = SourceUtils.getJavaTokenSequence(javac.getTokenHierarchy(), start);

if (ts.moveNext() && ts.token().id() == JavaTokenId.JAVADOC_COMMENT) {
if (ts.moveNext() &&
(ts.token().id() == JavaTokenId.JAVADOC_COMMENT ||
ts.token().id() == JavaTokenId.JAVADOC_COMMENT_LINE_RUN)) {
javadoc = ts.embedded(JavadocTokenId.language());
}

Expand Down Expand Up @@ -893,14 +895,14 @@ private static void insideTag(DocTreePath tag, JavadocContext jdctx, int caretOf
cs = pos < cs.length() ? cs.subSequence(0, pos) : cs;

if (JavadocCompletionUtils.isWhiteSpace(cs)
|| JavadocCompletionUtils.isLineBreak(jdts.token(), pos)) {
|| JavadocCompletionUtils.isLineBreak(jdts, pos)) {
noPrefix = true;
} else {
// broken syntax
return;
}
} else if (!(JavadocCompletionUtils.isWhiteSpace(jdts.token())
|| JavadocCompletionUtils.isLineBreak(jdts.token()))) {
|| JavadocCompletionUtils.isLineBreak(jdts))) {
// not java reference
return;
} else if (jdts.moveNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,32 +270,32 @@ public void testIsLineBreak() throws Exception {
TokenSequence<JavadocTokenId> jdts = JavadocCompletionUtils.findJavadocTokenSequence(info, offset);
assertTrue(jdts.moveNext());
assertTrue(insertPointer(code, offset),
JavadocCompletionUtils.isLineBreak(jdts.token()));
JavadocCompletionUtils.isLineBreak(jdts));
offset += 1;
jdts = JavadocCompletionUtils.findJavadocTokenSequence(info, offset);
assertTrue(jdts.moveNext());
// token is INDENT
assertFalse(insertPointer(code, offset),
JavadocCompletionUtils.isLineBreak(jdts.token()));
JavadocCompletionUtils.isLineBreak(jdts));

what = " \n";
offset = code.indexOf(what);
jdts = JavadocCompletionUtils.findJavadocTokenSequence(info, offset);
assertTrue(jdts.moveNext());
assertTrue(insertPointer(code, offset),
JavadocCompletionUtils.isLineBreak(jdts.token(), offset - jdts.offset()));
JavadocCompletionUtils.isLineBreak(jdts, offset - jdts.offset()));

what = " * {*i";
offset = code.indexOf(what) + what.length() - 3;
jdts = JavadocCompletionUtils.findJavadocTokenSequence(info, offset);
assertTrue(jdts.moveNext());
assertFalse(insertPointer(code, offset),
JavadocCompletionUtils.isLineBreak(jdts.token()));
JavadocCompletionUtils.isLineBreak(jdts));
assertTrue(insertPointer(code, offset),
JavadocCompletionUtils.isLineBreak(jdts.token(), offset - jdts.offset()));
JavadocCompletionUtils.isLineBreak(jdts, offset - jdts.offset()));
offset = code.indexOf(what);
assertFalse(insertPointer(code, offset),
JavadocCompletionUtils.isLineBreak(jdts.token(), offset - jdts.offset()));
JavadocCompletionUtils.isLineBreak(jdts, offset - jdts.offset()));
}

public void testIsLineBreak2() throws Exception {
Expand All @@ -319,10 +319,10 @@ public void testIsLineBreak2() throws Exception {
assertTrue(jdts.moveNext());
assertTrue(jdts.token().id() == JavadocTokenId.OTHER_TEXT);
assertFalse(insertPointer(code, jdts.offset() + jdts.token().length()),
JavadocCompletionUtils.isLineBreak(jdts.token()));
JavadocCompletionUtils.isLineBreak(jdts));
// test OTHER_TEXT(' * |{')
assertTrue(insertPointer(code, jdts.offset() + jdts.token().length() - 1),
JavadocCompletionUtils.isLineBreak(jdts.token(), jdts.token().length() - 1));
JavadocCompletionUtils.isLineBreak(jdts, jdts.token().length() - 1));
}

public void testIsWhiteSpace() throws Exception {
Expand Down
Loading
Loading