Skip to content

Commit 79c1cc1

Browse files
committed
Review - Handle Heading / body issue
1 parent 8bc4323 commit 79c1cc1

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16647,6 +16647,23 @@ class Mark {
1664716647
formatSource(input, expected);
1664816648
}
1664916649

16650+
public void testMarkdownHeadingWithBody() throws JavaModelException {
16651+
setComplianceLevel(CompilerOptions.VERSION_23);
16652+
String input = """
16653+
/// # heading
16654+
/// body content
16655+
class Mark {
16656+
}
16657+
""";
16658+
String expected = """
16659+
/// # heading
16660+
/// body content
16661+
class Mark {
16662+
}
16663+
""";
16664+
formatSource(input, expected);
16665+
}
16666+
1665016667
public void testMarkdownSnippetComments() throws JavaModelException {
1665116668
setComplianceLevel(CompilerOptions.VERSION_23);
1665216669
String input = """

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ public class CommentsPreparator extends ASTVisitor {
100100
}
101101

102102
private final static Pattern MARKDOWN_LIST_PATTERN = Pattern.compile("(?<!\\S)(?:[-+*]|\\d+\\.)([ \\t]+)"); //$NON-NLS-1$
103-
private final static Pattern MARKDOWN_HEADINGS_PATTERN_1 = Pattern.compile("(?:(?<=^)|(?<=///[ \\t]*))(#{1,6})([ \\t]+)([^#\\n]+)", Pattern.MULTILINE); //$NON-NLS-1$
104-
private final static Pattern MARKDOWN_HEADINGS_PATTERN_2 = Pattern.compile("(?:^|(?<=///[ \\t]+))[ \\t]*([=-])\\1*[ \\t]*(?=\\n|$)", Pattern.MULTILINE); //$NON-NLS-1$
103+
private final static Pattern MARKDOWN_HEADINGS_PATTERN_1 = Pattern.compile("(?:(?<=^)|(?<=///[ \\t]*))(#{1,6})([ \\t]+)([^\\n]*)"); //$NON-NLS-1$
104+
private final static Pattern MARKDOWN_HEADINGS_PATTERN_2 = Pattern.compile("(?:^|(?<=///[ \\t]+))[ \\t]*([=-])\\1*[ \\t]*(?=\\n|$)"); //$NON-NLS-1$
105105
private final static Pattern MARKDOWN_FENCES_PATTERN = Pattern.compile("[ \\t]*(?:///[ \\t]*)?(?:`+|~+)[ \\t]*(?:\\R)?"); //$NON-NLS-1$
106106
private final static Pattern MARKDOWN_TABLE_PATTERN = Pattern.compile("(?m)(?s)(?:\\s*///\\s*)?\\|[^\\r\\n]*?\\|[ \\t]*(?:\\r?\\n|$)(?:\\s*///\\s*)?\\|(?:\\s*[:-]+\\s*\\|)+[ \\t]*(?:\\r?\\n|$)(?:(?:\\s*///\\s*)?\\|[^\\r\\n]*?\\|[ \\t]*(?:\\r?\\n|$))+"); //$NON-NLS-1$
107107

@@ -852,8 +852,11 @@ private void handleMarkdown(TagElement node) {
852852
if (tokenIndex != 1) {
853853
headingToken.breakBefore();
854854
}
855+
int endPos = matcher.end() + node.getStartPosition();
856+
int endIndex = tokenEndingAt(endPos-1);
857+
Token endingToken = this.ctm.get(endIndex);
858+
endingToken.breakAfter();
855859
headingToken.spaceBefore();
856-
headingToken.spaceAfter();
857860
}
858861

859862
matcher = MARKDOWN_HEADINGS_PATTERN_2.matcher(text); // Check for MarkDown headings with styles '-- & ==='

0 commit comments

Comments
 (0)