Skip to content

Commit 995770d

Browse files
markdown link optimisation
1 parent 2b18db6 commit 995770d

File tree

2 files changed

+61
-33
lines changed

2 files changed

+61
-33
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,35 +1417,54 @@ private boolean isFollowedByURLPattern() {
14171417
}
14181418

14191419
// Parses a complete URL reference starting from current position
1420-
private Object parseURLReference() throws InvalidInputException {
1420+
private Object parseURLReference(boolean httpFlag) throws InvalidInputException {
1421+
// httpFlag is used to check wheather URL contains http or not
14211422
StringBuilder urlBuilder = new StringBuilder();
14221423
int firstTokenStartPos = this.scanner.startPosition;
14231424
//start with the current identifier
14241425
if (this.currentTokenType == TerminalToken.TokenNameIdentifier) {
14251426
urlBuilder.append(this.scanner.getCurrentTokenSource());
14261427
consumeToken();
14271428
}
1429+
char[] fullURL;
1430+
int pos;
1431+
if (httpFlag) {
1432+
// Add :
1433+
TerminalToken token1 = readTokenSafely();
1434+
if (token1 == TerminalToken.TokenNameCOLON) {
1435+
urlBuilder.append(this.scanner.getCurrentTokenSource());
1436+
consumeToken();
1437+
}
14281438

1429-
// Add :
1430-
TerminalToken token1 = readTokenSafely();
1431-
if (token1 == TerminalToken.TokenNameCOLON) {
1432-
urlBuilder.append(this.scanner.getCurrentTokenSource());
1433-
consumeToken();
1434-
}
1439+
pos = this.scanner.getCurrentTokenEndPosition() + 1;
1440+
char c;
1441+
while (pos < this.source.length) {
1442+
c = (this.source[pos] == ')') ? this.source[pos] : readChar();
1443+
1444+
if (c == ':') continue;
1445+
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '*' || c == ')' || c == '(' || c == '[' || c == ']' ) {
1446+
break;
1447+
}
1448+
urlBuilder.append(c);
1449+
pos++;
1450+
}
1451+
fullURL = urlBuilder.toString().toCharArray();
1452+
} else {
14351453

1436-
int pos = this.scanner.getCurrentTokenEndPosition() + 1;
1437-
char c;
1438-
while (pos < this.source.length) {
1439-
c = (this.source[pos] == ')') ? this.source[pos] : readChar();
1454+
//process the second token
1455+
TerminalToken token1 = readTokenSafely();
1456+
if (token1 == TerminalToken.TokenNameDOT) {
1457+
urlBuilder.append(this.scanner.getCurrentTokenSource());
1458+
consumeToken();
1459+
}
14401460

1441-
if (c == ':') continue;
1442-
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '*' || c == ')' || c == '(' || c == '[' || c == ']' ) {
1443-
break;
1444-
}
1445-
urlBuilder.append(c);
1446-
pos++;
1461+
TerminalToken token2 = readTokenSafely();
1462+
if (token2 == TerminalToken.TokenNameIdentifier) {
1463+
urlBuilder.append(this.scanner.getCurrentTokenSource());
1464+
}
1465+
pos = this.scanner.getCurrentTokenEndPosition() + 1;
1466+
fullURL = urlBuilder.toString().toCharArray();
14471467
}
1448-
char[] fullURL = urlBuilder.toString().toCharArray();
14491468
this.identifierPtr = 0;
14501469
this.identifierStack[this.identifierPtr] = fullURL;
14511470
this.identifierPositionStack[this.identifierPtr] = (((long) firstTokenStartPos) << 32) + (pos - 1);
@@ -1454,7 +1473,8 @@ private Object parseURLReference() throws InvalidInputException {
14541473
}
14551474

14561475
// Ensure the markdown URL bracket syntax follows []() or [][]
1457-
private void checkMarkdownLinkSyntaxValid(int currStartPos) {
1476+
private void checkMarkdownLinkSyntaxValid(int currStartPos, boolean httpFlag) {
1477+
// httpFlag is used to check wheather URL contains http or not
14581478
char secondBracket = this.source[currStartPos - 2];
14591479
char thirdBracket = this.source[currStartPos - 1];
14601480

@@ -1573,15 +1593,23 @@ else if (this.tagValue == TAG_VALUE_VALUE) {
15731593
case TokenNameUNDERSCORE:
15741594
case TokenNameIdentifier :
15751595
char[] identifier = this.scanner.getCurrentIdentifierSource();
1576-
if (isURLScheme(identifier)) {
1577-
if (isFollowedByURLPattern()) {
1578-
if (typeRef == null) {
1579-
int currStartPos = this.scanner.startPosition;
1580-
typeRefStartPosition = this.scanner.getCurrentTokenStartPosition();
1581-
typeRef = parseURLReference();
1582-
checkMarkdownLinkSyntaxValid(currStartPos);
1583-
if (this.abort) return false;
1596+
if (this.source[this.scanner.getCurrentTokenStartPosition() - 1] == '(') {
1597+
if (isURLScheme(identifier)) {
1598+
if (isFollowedByURLPattern()) {
1599+
if (typeRef == null) {
1600+
int currStartPos = this.scanner.startPosition;
1601+
typeRefStartPosition = this.scanner.getCurrentTokenStartPosition();
1602+
typeRef = parseURLReference(true);
1603+
checkMarkdownLinkSyntaxValid(currStartPos, true);
1604+
if (this.abort) return false;
1605+
}
15841606
}
1607+
} else if (this.source[this.scanner.getCurrentTokenEndPosition() +1 ] == '.') { //$NON-NLS-1$
1608+
int currStartPos = this.scanner.startPosition;
1609+
typeRefStartPosition = this.scanner.getCurrentTokenStartPosition();
1610+
typeRef = parseURLReference(false);
1611+
checkMarkdownLinkSyntaxValid(currStartPos, true);
1612+
if (this.abort) return false;
15851613
}
15861614
}
15871615
if (typeRef == null) {

org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterMarkdownTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,7 +1931,7 @@ public class Markdown() {}
19311931
TagElement tagElement = (TagElement) tags.fragments().get(1);
19321932
List<?> tagFragments = tagElement.fragments();
19331933
assertTrue(tagFragments.get(0) instanceof TextElement);
1934-
assertTrue(tagFragments.get(1) instanceof QualifiedName);
1934+
assertTrue(tagFragments.get(1) instanceof SimpleName);
19351935
}
19361936
}
19371937

@@ -1995,6 +1995,7 @@ public class Markdown() {}
19951995
}
19961996
}
19971997

1998+
// invalid syntax
19981999
public void testMarkdownURLs4531_05() throws JavaModelException {
19992000
String source = """
20002001
/// @see [Ex Si][http://ex.com]
@@ -2007,11 +2008,10 @@ public class Markdown() {}
20072008
TypeDeclaration typedeclaration = (TypeDeclaration) compilUnit.types().get(0);
20082009
Javadoc javadoc = typedeclaration.getJavadoc();
20092010
TagElement tags = (TagElement) javadoc.tags().get(0);
2010-
assertEquals("fragments count does not match", 2, tags.fragments().size());
2011-
TagElement tagElement = (TagElement) tags.fragments().get(1);
2012-
List<?> tagFragments = tagElement.fragments();
2013-
assertTrue(tagFragments.get(0) instanceof TextElement);
2014-
assertTrue(tagFragments.get(1) instanceof SimpleName);
2011+
assertEquals("fragments count does not match", 3, tags.fragments().size());
2012+
assertTrue(tags.fragments().get(0) instanceof TextElement);
2013+
assertTrue(tags.fragments().get(1) instanceof TextElement);
2014+
assertTrue(tags.fragments().get(2) instanceof TextElement);
20152015
}
20162016
}
20172017

0 commit comments

Comments
 (0)