visitComment method

  1. @override
void visitComment(
  1. Comment node
)
override

Implementation

@override
void visitComment(Comment node) {
  super.visitComment(node);

  var content = unit.content;

  var toolDirectives = node.docDirectives
      .where((directive) => directive.type == DocDirectiveType.tool)
      .whereType<BlockDocDirective>();
  for (var toolDirective in toolDirectives) {
    var contentsStart = toolDirective.openingTag.end;
    var contentsEnd = toolDirective.closingTag?.offset;

    // Skip unclosed tags.
    if (contentsEnd == null) {
      continue;
    }

    var strValue = content.substring(contentsStart, contentsEnd);
    if (strValue.isEmpty) {
      continue;
    }

    var seeCodeIn = '** See code in ';
    var startIndex = strValue.indexOf('${seeCodeIn}examples/api/');
    if (startIndex != -1) {
      final folderWithExamplesApi = this.folderWithExamplesApi;
      if (folderWithExamplesApi == null) {
        // Examples directory doesn't exist.
        return;
      }
      startIndex += seeCodeIn.length;
      var endIndex = strValue.indexOf('.dart') + 5;
      var pathSnippet = strValue.substring(startIndex, endIndex);
      // Split on '/' because that's what the comment syntax uses, but
      // re-join it using the resource provider to get the right separator
      // for the platform.
      var examplePath = resourceProvider.pathContext.joinAll([
        folderWithExamplesApi.path,
        ...pathSnippet.split('/'),
      ]);
      var offset = contentsStart + startIndex;
      var length = endIndex - startIndex;
      _documentLinks.add(DartDocumentLink(offset, length, examplePath));
    }
  }
}