Skip to content

Commit 5721b68

Browse files
code re-factor
1 parent 9ac2927 commit 5721b68

File tree

2 files changed

+55
-45
lines changed

2 files changed

+55
-45
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,6 @@ public class Markdown() {}
20312031
TagElement tags = (TagElement) javadoc.tags().get(0);
20322032
assertTrue(tags.fragments().get(0) instanceof TextElement);
20332033
assertTrue(tags.fragments().get(1) instanceof TextElement);
2034-
assertTrue(tags.fragments().get(2) instanceof TextElement);
20352034
}
20362035
}
20372036

@@ -2139,7 +2138,7 @@ public class Markdown() {}
21392138

21402139
public void testMarkdownURLs4531_12() throws JavaModelException {
21412140
String source = """
2142-
/// @see [Ex Si)(http://ex.com ) [Ex Si]( https://www.ex.com)
2141+
/// @see [Ex Si](http://ex.com ) [Ex Si]( https://www.ex.com)
21432142
public class Markdown() {}
21442143
""";
21452144

org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/DocCommentParser.java

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -734,56 +734,19 @@ protected boolean parseMarkdownLinks(int previousPosition) throws InvalidInputEx
734734
}
735735
break;
736736
case ')':
737+
if (peekChar() == '\n' || peekChar() == ' ') {
738+
valid = parseMarkdownLinkTags(true, start, previousPosition, tStart, tEnd);
739+
break loop;
740+
}
741+
break;
737742
case ']':
738743
if ((peekChar() == '[' ) || peekChar() == '(') {
739744
tStart = start;
740745
tEnd = this.index - 1;
741746
currentChar = readChar();
742747
start = this.index;
743748
} else if (peekChar() != ']') {
744-
int eofBkup = this.scanner.eofPosition;
745-
this.scanner.eofPosition = this.index - 1;
746-
this.scanner.resetTo(start, this.javadocEnd);
747-
this.inlineTagStarted = true;
748-
this.inlineTagStart = previousPosition;
749-
this.tagValue = TAG_LINK_VALUE;
750-
int indexBkup = this.index;
751-
if(currentChar != ']')
752-
valid = parseURLReference();
753-
else
754-
valid = parseReference(true);
755-
this.index = indexBkup;
756-
// This creates a two level structure. The @link tag is added to
757-
// another tag element, which gets added to the astStack
758-
// Both tag elements must get the same source range.
759-
TagElement previousTag = (TagElement) this.astStack[this.astPtr];
760-
int parentStart = previousTag.getStartPosition();
761-
previousTag.setSourceRange(parentStart, this.index - parentStart);
762-
List fragments = previousTag.fragments();
763-
int size = fragments.size();
764-
if (size == 0) {
765-
// no existing fragment => just add the element
766-
TagElement inlineTag = this.ast.newTagElement();
767-
fragments.add(inlineTag);
768-
previousTag = inlineTag;
769-
} else {
770-
// If last fragment is a tag, then use it as previous tag
771-
ASTNode lastFragment = (ASTNode) fragments.get(size-1);
772-
if (lastFragment.getNodeType() == ASTNode.TAG_ELEMENT) {
773-
lastFragment.setSourceRange(lastFragment.getStartPosition(), this.index - previousPosition);
774-
previousTag = (TagElement) lastFragment;
775-
}
776-
}
777-
if (tEnd != -1) {
778-
TextElement text = this.ast.newTextElement();
779-
text.setText(new String( this.source, tStart, tEnd-tStart));
780-
text.setSourceRange(tStart, tEnd-tStart);
781-
previousTag.fragments().add(0, text);
782-
}
783-
this.tagValue = NO_TAG_VALUE;
784-
this.inlineTagStarted = false;
785-
this.inlineTagStart = -1;
786-
this.scanner.eofPosition = eofBkup;
749+
valid = parseMarkdownLinkTags(false, start, previousPosition, tStart, tEnd);
787750
break loop;
788751
}
789752
break;
@@ -799,6 +762,54 @@ protected boolean parseMarkdownLinks(int previousPosition) throws InvalidInputEx
799762
return valid;
800763
}
801764

765+
private boolean parseMarkdownLinkTags(boolean refFlag, int start, int previousPosition, int tStart, int tEnd ) throws InvalidInputException {
766+
boolean valid = false;
767+
int eofBkup = this.scanner.eofPosition;
768+
this.scanner.eofPosition = this.index - 1;
769+
this.scanner.resetTo(start, this.javadocEnd);
770+
this.inlineTagStarted = true;
771+
this.inlineTagStart = previousPosition;
772+
this.tagValue = TAG_LINK_VALUE;
773+
int indexBkup = this.index;
774+
if (refFlag)
775+
valid = parseURLReference();
776+
else
777+
valid = parseReference(true);
778+
this.index = indexBkup;
779+
// This creates a two level structure. The @link tag is added to
780+
// another tag element, which gets added to the astStack
781+
// Both tag elements must get the same source range.
782+
TagElement previousTag = (TagElement) this.astStack[this.astPtr];
783+
int parentStart = previousTag.getStartPosition();
784+
previousTag.setSourceRange(parentStart, this.index - parentStart);
785+
List fragments = previousTag.fragments();
786+
int size = fragments.size();
787+
if (size == 0) {
788+
// no existing fragment => just add the element
789+
TagElement inlineTag = this.ast.newTagElement();
790+
fragments.add(inlineTag);
791+
previousTag = inlineTag;
792+
} else {
793+
// If last fragment is a tag, then use it as previous tag
794+
ASTNode lastFragment = (ASTNode) fragments.get(size-1);
795+
if (lastFragment.getNodeType() == ASTNode.TAG_ELEMENT) {
796+
lastFragment.setSourceRange(lastFragment.getStartPosition(), this.index - previousPosition);
797+
previousTag = (TagElement) lastFragment;
798+
}
799+
}
800+
if (tEnd != -1) {
801+
TextElement text = this.ast.newTextElement();
802+
text.setText(new String( this.source, tStart, tEnd-tStart));
803+
text.setSourceRange(tStart, tEnd-tStart);
804+
previousTag.fragments().add(0, text);
805+
}
806+
this.tagValue = NO_TAG_VALUE;
807+
this.inlineTagStarted = false;
808+
this.inlineTagStart = -1;
809+
this.scanner.eofPosition = eofBkup;
810+
return valid;
811+
}
812+
802813
@Override
803814
protected boolean parseTag(int previousPosition) throws InvalidInputException {
804815
this.markdownHelper.resetLineStart();

0 commit comments

Comments
 (0)