sourceFileContents property

String sourceFileContents

Implementation

String get sourceFileContents {
  if (sourceFile != null && _sourceFileContents == null) {
    // Strip lines until the first non-comment line. This gets rid of the
    // copyright and comment directing the reader to the original source file.
    final List<String> stripped = <String>[];
    bool doneStrippingHeaders = false;
    try {
      for (final String line in sourceFile!.readAsLinesSync()) {
        if (!doneStrippingHeaders &&
            RegExp(r'^\s*(\/\/.*)?$').hasMatch(line)) {
          continue;
        }
        // Stop skipping lines after the first line that isn't stripped.
        doneStrippingHeaders = true;
        stripped.add(line);
      }
    } on FileSystemException catch (e) {
      throw SnippetException(
        'Unable to read linked source file ${sourceFile!}: $e',
        file: _lineProto.file?.absolute.path,
      );
    }
    // Remove any section markers
    final RegExp sectionMarkerRegExp = RegExp(
      r'(\/\/\*\*+\n)?\/\/\* [▼▲]+.*$(\n\/\/\*\*+)?\n\n?',
      multiLine: true,
    );
    _sourceFileContents =
        stripped.join('\n').replaceAll(sectionMarkerRegExp, '');
  }
  return _sourceFileContents ?? '';
}