Skip to content

Commit e7cdd28

Browse files
committed
Review comments - Remove tag format option and extract handleMarkdown()
1 parent 7adfb84 commit e7cdd28

File tree

1 file changed

+76
-75
lines changed

1 file changed

+76
-75
lines changed

org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CommentsPreparator.java

Lines changed: 76 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,8 @@ public boolean visit(TagElement node) {
650650
if (startIndex > 1) {
651651
this.ctm.get(startIndex).breakBefore();
652652
}
653-
654-
handleHtmlAndMarkdown(node);
653+
handleMarkdown(node);
654+
handleHtml(node);
655655
this.ctm.get(tokenStartingAt(node.getStartPosition())).setToEscape(false);
656656

657657
} else if (node.isNested() && (IMMUTABLE_TAGS.contains(tagName) || TagElement.TAG_SNIPPET.equals(tagName))) {
@@ -672,7 +672,8 @@ public boolean visit(TagElement node) {
672672
public void endVisit(TagElement node) {
673673
String tagName = node.getTagName();
674674
if (tagName == null || tagName.length() <= 1) {
675-
handleHtmlAndMarkdown(node);
675+
handleMarkdown(node);
676+
handleHtml(node);
676677
} else if (TagElement.TAG_SEE.equals(tagName)) {
677678
handleStringLiterals(this.tm.toString(node), node.getStartPosition());
678679
}
@@ -799,78 +800,7 @@ private void alignJavadocTag(List<Token> tagTokens, int paramNameAlign, int desc
799800
}
800801
}
801802

802-
private void handleHtmlAndMarkdown(TagElement node) {
803-
804-
if (node.getParent() instanceof Javadoc javaDoc && javaDoc.isMarkdown()
805-
&& this.options.comment_format_markdown_comment) {
806-
807-
String text = this.tm.toString(node);
808-
Matcher matcher = MARKDOWN_LIST_PATTERN.matcher(text); // Check for MarkDown lists [Ordered & Unordered]
809-
int previousLevel = 0;
810-
Map<Integer, Token> tokenPositions = new HashMap<>();
811-
Token parent = null;
812-
while (matcher.find()) {
813-
int startPos = matcher.start() + node.getStartPosition();
814-
int tokenIndex = tokenStartingAt(startPos);
815-
Token listToken = this.ctm.get(tokenIndex);
816-
int currentIndent = 0;
817-
int i = matcher.start();
818-
while (text.charAt(i) != '/') {
819-
if (text.charAt(i) == '\t') {
820-
currentIndent += 2;
821-
} else {
822-
currentIndent++;
823-
}
824-
i--;
825-
if (i == -1) {
826-
break;
827-
}
828-
}
829-
int currentSize = tokenPositions.size();
830-
if (tokenIndex != 1) {
831-
listToken.breakBefore();
832-
}
833-
if (currentSize > 0 && tokenPositions.get(currentIndent) != null) {
834-
listToken.setIndent(tokenPositions.get(currentIndent).getIndent());
835-
} else if (currentSize > 0 && previousLevel > currentIndent) {
836-
listToken.spaceBefore();
837-
} else if (currentSize > 0 && currentIndent > 2 && previousLevel < currentIndent) {
838-
listToken.setIndent(parent.getIndent() + 2);
839-
} else {
840-
if (parent != null && parent.getIndent() > 0) {
841-
listToken.setIndent(parent.getIndent());
842-
} else {
843-
listToken.spaceBefore();
844-
}
845-
}
846-
listToken.spaceAfter();
847-
parent = listToken;
848-
previousLevel = currentIndent;
849-
tokenPositions.put(currentIndent, listToken);
850-
}
851-
matcher = MARKDOWN_HEADINGS_PATTERN_1.matcher(text); // Check for MarkDown headings #h1 - #h6
852-
while (matcher.find()) {
853-
int startPos = matcher.start() + node.getStartPosition();
854-
int tokenIndex = tokenStartingAt(startPos);
855-
Token listToken = this.ctm.get(tokenIndex);
856-
if (tokenIndex != 1) {
857-
listToken.breakBefore();
858-
}
859-
listToken.spaceBefore();
860-
listToken.spaceAfter();
861-
}
862-
863-
matcher = MARKDOWN_HEADINGS_PATTERN_2.matcher(text); // Check for MarkDown headings with styles '-- & ==='
864-
while (matcher.find()) {
865-
int startPos = matcher.start() + node.getStartPosition();
866-
int tokenIndex = tokenStartingAt(startPos);
867-
Token listToken = this.ctm.get(tokenIndex);
868-
if (tokenIndex != 1) {
869-
listToken.breakBefore();
870-
}
871-
}
872-
873-
}
803+
private void handleHtml(TagElement node) {
874804

875805
if (!this.options.comment_format_html && !this.options.comment_format_source)
876806
return;
@@ -1638,4 +1568,75 @@ public void finishUp() {
16381568
if (this.lastFormatOffComment != null)
16391569
this.tm.addDisableFormatTokenPair(this.lastFormatOffComment, this.tm.get(this.tm.size() - 1));
16401570
}
1571+
1572+
private void handleMarkdown(TagElement node) {
1573+
if (node.getParent() instanceof Javadoc javaDoc && javaDoc.isMarkdown()
1574+
&& this.options.comment_format_markdown_comment) {
1575+
String text = this.tm.toString(node);
1576+
Matcher matcher = MARKDOWN_LIST_PATTERN.matcher(text); // Check for MarkDown lists [Ordered & Unordered]
1577+
int previousLevel = 0;
1578+
Map<Integer, Token> tokenPositions = new HashMap<>();
1579+
Token parent = null;
1580+
while (matcher.find()) {
1581+
int startPos = matcher.start() + node.getStartPosition();
1582+
int tokenIndex = tokenStartingAt(startPos);
1583+
Token listToken = this.ctm.get(tokenIndex);
1584+
int currentIndent = 0;
1585+
int i = matcher.start();
1586+
while (text.charAt(i) != '/') {
1587+
if (text.charAt(i) == '\t') {
1588+
currentIndent += 2;
1589+
} else {
1590+
currentIndent++;
1591+
}
1592+
i--;
1593+
if (i == -1) {
1594+
break;
1595+
}
1596+
}
1597+
int currentSize = tokenPositions.size();
1598+
if (tokenIndex != 1) {
1599+
listToken.breakBefore();
1600+
}
1601+
if (currentSize > 0 && tokenPositions.get(currentIndent) != null) {
1602+
listToken.setIndent(tokenPositions.get(currentIndent).getIndent());
1603+
} else if (currentSize > 0 && previousLevel > currentIndent) {
1604+
listToken.spaceBefore();
1605+
} else if (currentSize > 0 && currentIndent > 2 && previousLevel < currentIndent) {
1606+
listToken.setIndent(parent.getIndent() + 2);
1607+
} else {
1608+
if (parent != null && parent.getIndent() > 0) {
1609+
listToken.setIndent(parent.getIndent());
1610+
} else {
1611+
listToken.spaceBefore();
1612+
}
1613+
}
1614+
listToken.spaceAfter();
1615+
parent = listToken;
1616+
previousLevel = currentIndent;
1617+
tokenPositions.put(currentIndent, listToken);
1618+
}
1619+
matcher = MARKDOWN_HEADINGS_PATTERN_1.matcher(text); // Check for MarkDown headings #h1 - #h6
1620+
while (matcher.find()) {
1621+
int startPos = matcher.start() + node.getStartPosition();
1622+
int tokenIndex = tokenStartingAt(startPos);
1623+
Token listToken = this.ctm.get(tokenIndex);
1624+
if (tokenIndex != 1) {
1625+
listToken.breakBefore();
1626+
}
1627+
listToken.spaceBefore();
1628+
listToken.spaceAfter();
1629+
}
1630+
1631+
matcher = MARKDOWN_HEADINGS_PATTERN_2.matcher(text); // Check for MarkDown headings with styles '-- & ==='
1632+
while (matcher.find()) {
1633+
int startPos = matcher.start() + node.getStartPosition();
1634+
int tokenIndex = tokenStartingAt(startPos);
1635+
Token listToken = this.ctm.get(tokenIndex);
1636+
if (tokenIndex != 1) {
1637+
listToken.breakBefore();
1638+
}
1639+
}
1640+
}
1641+
}
16411642
}

0 commit comments

Comments
 (0)