parseFromDartdocToolFile method

SourceElement parseFromDartdocToolFile(
  1. File input, {
  2. int? startLine,
  3. String? element,
  4. required File sourceFile,
  5. String template = '',
  6. String type = '',
  7. bool silent = true,
})

Parses a file containing the output of the dartdoc @tool directive, which contains the dartdoc comment lines (with comment markers stripped) between the tool markers.

This is meant to be run as part of a dartdoc tool that handles snippets.

Implementation

SourceElement parseFromDartdocToolFile(
  File input, {
  int? startLine,
  String? element,
  required File sourceFile,
  String template = '',
  String type = '',
  bool silent = true,
}) {
  final List<SourceLine> lines = <SourceLine>[];
  int lineNumber = startLine ?? 0;
  final List<String> inputStrings = <String>[
    // The parser wants to read the arguments from the input, so we create a new
    // tool line to match the given arguments, so that we can use the same parser for
    // editing and docs generation.
    '/// {@tool $type ${template.isNotEmpty ? ' --template=$template}' : ''}}',
    // Snippet input comes in with the comment markers stripped, so we add them
    // back to make it conform to the source format, so we can use the same
    // parser for editing samples as we do for processing docs.
    ...input
        .readAsLinesSync()
        .map<String>((String line) => '/// $line'.trimRight()),
    '/// {@end-tool}',
  ];
  for (final String line in inputStrings) {
    lines.add(
      SourceLine(line,
          element: element ?? '', line: lineNumber, file: sourceFile),
    );
    lineNumber++;
  }
  // No need to get assumptions: dartdoc won't give that to us.
  final SourceElement newElement = SourceElement(
      SourceElementType.unknownType, element!, -1,
      file: input, comment: lines);
  parseFromComments(<SourceElement>[newElement], silent: silent);
  for (final CodeSample sample in newElement.samples) {
    sample.metadata.addAll(<String, Object?>{
      'id': '${sample.element}.${sample.index}',
      'element': sample.element,
      'sourcePath': sourceFile.path,
      'sourceLine': sample.start.line,
    });
  }
  return newElement;
}