Skip to content

Commit

Permalink
[23] Parsing details for markdown comments eclipse-jdt#2824
Browse files Browse the repository at this point in the history
+ code in different fences

fixes eclipse-jdt#2824
  • Loading branch information
stephan-herrmann committed Aug 18, 2024
1 parent b219429 commit fcaceb5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,8 @@ protected boolean commentParse() {
this.textStart = this.index;
break;
}
} else if (nextCharacter == '`') {
this.markdownHelper.recordBackTick(this.lineStarted);
} else if (nextCharacter == '`' || nextCharacter == '~') {
this.markdownHelper.recordFenceChar(previousChar, nextCharacter, this.lineStarted);
}
}
if (isFormatterParser && nextCharacter == '<') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface IMarkdownCommentHelper {

void recordSlash(int nextIndex);

void recordBackTick(boolean lineStarted);
void recordFenceChar(char previous, char next, boolean lineStarted);

/**
* When at the beginning of a comment line, record that a whitespace was seen.
Expand Down Expand Up @@ -68,7 +68,7 @@ public void recordSlash(int nextIndex) {
// nop
}
@Override
public void recordBackTick(boolean lineStarted) {
public void recordFenceChar(char previous, char next, boolean lineStarted) {
// nop
}
@Override
Expand Down Expand Up @@ -102,9 +102,11 @@ class MarkdownCommentHelper implements IMarkdownCommentHelper {
int slashCount = 0;
int leadingSpaces = 0;
int markdownLineStart = -1;
int backTickCount = 0;
boolean insideIndentedCodeBlock = false;
boolean insideFencedCodeBlock = false;
char fenceChar;
int fenceCharCount;
int fenceLength;
boolean isBlankLine = true;
boolean previousIsBlankLine = true;

Expand All @@ -124,18 +126,24 @@ public void recordSlash(int nextIndex) {
}

@Override
public void recordBackTick(boolean lineStarted) {
public void recordFenceChar(char previous, char next, boolean lineStarted) {
if (this.insideIndentedCodeBlock) {
return;
}
if (this.backTickCount < 3) {
if (this.backTickCount == 0 && lineStarted) {
if (this.fenceCharCount == 0) {
if (lineStarted)
return;
}
if (++this.backTickCount == 3) {
this.insideFencedCodeBlock^=true;
}
this.fenceChar = next;
this.fenceCharCount = 1;
return;
}
if (next != this.fenceChar || previous != next)
return;
int required = this.insideFencedCodeBlock ? this.fenceLength : 3;
if (++this.fenceCharCount == required) {
this.insideFencedCodeBlock^=true;
}
this.fenceLength = this.fenceCharCount;
}

@Override
Expand Down Expand Up @@ -182,7 +190,7 @@ public void resetAtLineEnd() {
this.slashCount = 0;
this.leadingSpaces = 0;
this.markdownLineStart = -1;
this.backTickCount = 0;
this.fenceCharCount = 0;
// do not reset `insideFence`
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3519,6 +3519,57 @@ void paraAfterCode() { }
}
}

public void testGH2808_fencedCode() throws JavaModelException {
// fence can only be terminated by same number of same fence chars
this.workingCopies = new ICompilationUnit[1];
this.workingCopies[0] = getWorkingCopy("/Converter_23/src/markdown/gh2808/FencedCode.java",
"""
package markdown.gh2808;
public class FencedCode {
/// ``~~mix is not a fence
/// Plain Text
///
/// ~~~~script
/// @Override // not a tag even after ...
/// ~~~
/// or
/// ````
/// @Override
/// ~~~~
void indentedWithFence() { }
}
"""
);
CompilationUnit compilUnit = (CompilationUnit) runConversion(this.workingCopies[0], true);
if (this.docCommentSupport.equals(JavaCore.ENABLED)) {
List unitComments = compilUnit.getCommentList();
assertEquals("Wrong number of comments", 1, unitComments.size());

Comment comment = (Comment) unitComments.get(0);
assertEquals("Comment should be javadoc", comment.getNodeType(), ASTNode.JAVADOC);
List tagList = ((Javadoc) comment).tags();

String[] tags = {
null
};
String[][] lines = {
{ // one TagElement with many TextElements
"``~~mix is not a fence",
"Plain Text",
"~~~~script",
"@Override // not a tag even after ...",
"~~~",
"or",
"````",
"@Override",
"~~~~"
}
};
assertTagsAndTexts(tagList, tags, lines);
}
}

public void testGH2808_linkWithArrayReference() throws JavaModelException {
// for a negative variant see org.eclipse.jdt.core.tests.compiler.regression.MarkdownCommentsTest.test021()
this.workingCopies = new ICompilationUnit[1];
Expand Down

0 comments on commit fcaceb5

Please sign in to comment.