docFor method

String? docFor(
  1. int declStart
)

Returns the doc comment ending immediately above the declaration that starts at declStart, or null if there is none.

Implementation

String? docFor(int declStart) {
  final declLine = _lineOf(declStart);
  if (declLine <= 0) return null;

  // Collect contiguous line-doc comments (`///` or leading `#`) upward.
  final lineDocs = <String>[];
  var line = declLine - 1;
  while (line >= 0) {
    final content = _lineText(line).trim();
    if (content.startsWith('///')) {
      lineDocs.add(content.substring(3).trimLeft());
    } else if (content.startsWith('#')) {
      lineDocs.add(content.substring(1).trimLeft());
    } else {
      break;
    }
    line--;
  }
  if (lineDocs.isNotEmpty) {
    return lineDocs.reversed.join('\n').trim();
  }

  // Otherwise look for a block comment `/** ... */` ending just above.
  final block = _blockDocAbove(declLine);
  if (block != null) return block;

  return null;
}