Skip to content

Commit d8db810

Browse files
srawlinsCommit Queue
authored and
Commit Queue
committed
[analyzer] Parse fenced code blocks in doc comments
This simplifies comment reference-parsing in this refactor. Basically when a comment reference is discovered, we immediately parse it into a CommentReferenceImpl, instead of passing around the offset from place to place. Additionally we store fenced code block data on each Comment, for highlighting purposes. More data will be parsed in this code in the future. In particular, doc imports. Work towards #50702 Change-Id: Idb2dcae3fb54af567b1a7f43896c3226644bf6cc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/317446 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 42db97d commit d8db810

File tree

8 files changed

+847
-543
lines changed

8 files changed

+847
-543
lines changed

Diff for: pkg/analyzer/lib/src/dart/ast/ast.dart

+47
Original file line numberDiff line numberDiff line change
@@ -3225,6 +3225,10 @@ sealed class CombinatorImpl extends AstNodeImpl implements Combinator {
32253225
/// '/ **' (CHARACTER | [CommentReference])* '&#42;/'
32263226
/// | ('///' (CHARACTER - EOL)* EOL)+
32273227
abstract final class Comment implements AstNode {
3228+
/// The fenced code blocks parsed in this comment.
3229+
@experimental
3230+
List<MdFencedCodeBlock> get fencedCodeBlocks;
3231+
32283232
/// Return `true` if this is a block comment.
32293233
bool get isBlock;
32303234

@@ -3270,6 +3274,9 @@ final class CommentImpl extends AstNodeImpl implements Comment {
32703274
/// within it.
32713275
final NodeListImpl<CommentReferenceImpl> _references = NodeListImpl._();
32723276

3277+
@override
3278+
final List<MdFencedCodeBlock> fencedCodeBlocks;
3279+
32733280
/// Initialize a newly created comment. The list of [tokens] must contain at
32743281
/// least one token. The [_type] is the type of the comment. The list of
32753282
/// [references] can be empty if the comment does not contain any embedded
@@ -3278,6 +3285,7 @@ final class CommentImpl extends AstNodeImpl implements Comment {
32783285
required this.tokens,
32793286
required CommentType type,
32803287
required List<CommentReferenceImpl> references,
3288+
required this.fencedCodeBlocks,
32813289
}) : _type = type {
32823290
_references._initialize(this, references);
32833291
}
@@ -11942,6 +11950,45 @@ final class MapPatternImpl extends DartPatternImpl implements MapPattern {
1194211950
}
1194311951
}
1194411952

11953+
/// A Markdown fenced code block found in a documentation comment.
11954+
@experimental
11955+
final class MdFencedCodeBlock {
11956+
/// The 'info string'.
11957+
///
11958+
/// This includes any text following the opening backticks. For example, in
11959+
/// a fenced code block starting with "```dart", the info string is "dart".
11960+
///
11961+
/// If no text follows the opening backticks, the info string is `null`.
11962+
///
11963+
/// See CommonMark specification at
11964+
/// <https://spec.commonmark.org/0.30/#fenced-code-blocks>.
11965+
final String? infoString;
11966+
11967+
/// Information about the comment lines that make up this code block.
11968+
final List<MdFencedCodeBlockLine> lines;
11969+
11970+
MdFencedCodeBlock({
11971+
required this.infoString,
11972+
required List<MdFencedCodeBlockLine> lines,
11973+
}) : lines = List.of(lines, growable: false);
11974+
}
11975+
11976+
/// A Markdown fenced code block line found in a documentation comment.
11977+
@experimental
11978+
final class MdFencedCodeBlockLine {
11979+
/// The offset of the start of the fenced code block, from the beginning of
11980+
/// compilation unit.
11981+
final int offset;
11982+
11983+
/// The length of the fenced code block.
11984+
final int length;
11985+
11986+
MdFencedCodeBlockLine({
11987+
required this.offset,
11988+
required this.length,
11989+
});
11990+
}
11991+
1194511992
/// A method declaration.
1194611993
///
1194711994
/// methodDeclaration ::=

0 commit comments

Comments
 (0)