-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[23] markdown javadoc should detect code regions (#2821)
finetune parsing start of line by new companion IMarkdownCommentHelper + detect excess slashes after '///' + determine minimum amount of whitespace per markdown comment + preserve additional whitespace at beginning of line + detect code block from 4+ leading spaces + ensuree DocCommentParser and JavadocParser are in sync Also: + avoid showing closing ']' of links + resolve one warning (redundant null check) + fix and extend compliance settings in RunCompletionParserTests + fix & extend test source in ../Converter_23/src/markdown/testBug228648 + fix 1-off-bug in ASTConverterMarkdownTest.verifyPositions() + locally improve code structure (constant part outside the loop) fixes #2808
- Loading branch information
1 parent
677afd9
commit 3747271
Showing
8 changed files
with
448 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
...e.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/IMarkdownCommentHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 GK Software SE and others. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* This is an implementation of an early-draft specification developed under the Java | ||
* Community Process (JCP) and is made available for testing and evaluation purposes | ||
* only. The code is not compatible with any specification of the JCP. | ||
* | ||
* Contributors: | ||
* Stephan Herrmann - Initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.jdt.internal.compiler.parser; | ||
|
||
/** | ||
* Companion class for AbstractCommentParser to decide significance of whitespace | ||
* within a markdown comment, | ||
* and to detect code blocks (either by indentation or fenced with {@code ```}). | ||
*/ | ||
public interface IMarkdownCommentHelper { | ||
|
||
void recordSlash(int nextIndex); | ||
|
||
void recordBackTick(boolean lineStarted); | ||
|
||
/** | ||
* When at the beginning of a comment line, record that a whitespace was seen. | ||
* @return {@code true} if this whitespace is significant, | ||
* i.e., beyond the common indent of all lines of this commonmark comment. | ||
*/ | ||
boolean recordSignificantLeadingSpace(); | ||
|
||
boolean isInCodeBlock(); | ||
|
||
/** Retrieve the start of the current text, possibly including significant leading whitespace. */ | ||
int getTextStart(int textStart); | ||
|
||
/** Call me when we are past the first element of a line. */ | ||
void resetLineStart(); | ||
|
||
void resetAtLineEnd(); | ||
|
||
|
||
static IMarkdownCommentHelper create(AbstractCommentParser parser) { | ||
boolean markdown = parser.source[parser.javadocStart + 1] == '/'; | ||
if (markdown) { | ||
int lineStart = parser.javadocStart + 3; | ||
int commonIndent = parser.peekMarkdownCommonIndent(lineStart); | ||
return new MarkdownCommentHelper(lineStart, commonIndent); | ||
} else { | ||
return new NullMarkdownHelper(); | ||
} | ||
} | ||
|
||
} | ||
|
||
class NullMarkdownHelper implements IMarkdownCommentHelper { | ||
public NullMarkdownHelper() { | ||
} | ||
@Override | ||
public void recordSlash(int nextIndex) { | ||
// nop | ||
} | ||
@Override | ||
public void recordBackTick(boolean lineStarted) { | ||
// nop | ||
} | ||
@Override | ||
public boolean recordSignificantLeadingSpace() { | ||
return false; | ||
} | ||
@Override | ||
public boolean isInCodeBlock() { | ||
return false; | ||
} | ||
@Override | ||
public int getTextStart(int textStart) { | ||
return textStart; | ||
} | ||
@Override | ||
public void resetLineStart() { | ||
// nop | ||
} | ||
@Override | ||
public void resetAtLineEnd() { | ||
// nop | ||
} | ||
} | ||
class MarkdownCommentHelper implements IMarkdownCommentHelper { | ||
|
||
int commonIndent; | ||
int slashCount = 0; | ||
int leadingSpaces = 0; | ||
int markdownLineStart = -1; | ||
int backTickCount = 0; | ||
boolean insideFence = false; | ||
|
||
public MarkdownCommentHelper(int lineStart, int commonIndent) { | ||
this.markdownLineStart = lineStart; | ||
this.commonIndent = commonIndent; | ||
} | ||
|
||
@Override | ||
public void recordSlash(int nextIndex) { | ||
if (this.slashCount < 3) { | ||
if (++this.slashCount == 3) { | ||
this.markdownLineStart = nextIndex; | ||
this.leadingSpaces = 0; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void recordBackTick(boolean lineStarted) { | ||
if (this.backTickCount < 3) { | ||
if (this.backTickCount == 0 && lineStarted) { | ||
return; | ||
} | ||
if (++this.backTickCount == 3) { | ||
this.insideFence^=true; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public boolean recordSignificantLeadingSpace() { | ||
if (this.markdownLineStart != -1) { | ||
if (++this.leadingSpaces > this.commonIndent) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isInCodeBlock() { | ||
return this.insideFence || (this.leadingSpaces - this.commonIndent >= 4); | ||
} | ||
|
||
@Override | ||
public int getTextStart(int textStart) { | ||
if (this.markdownLineStart > -1) { | ||
return this.markdownLineStart + this.commonIndent; | ||
} | ||
return textStart; | ||
} | ||
|
||
@Override | ||
public void resetLineStart() { | ||
this.markdownLineStart = -1; | ||
} | ||
|
||
@Override | ||
public void resetAtLineEnd() { | ||
this.slashCount = 0; | ||
this.leadingSpaces = 0; | ||
this.markdownLineStart = -1; | ||
this.backTickCount = 0; | ||
// do not reset `insideFence` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.