parseFromDartdocToolFile method
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;
}