spanWithName property

FileSpan get spanWithName

Returns span expanded to include an identifier immediately before the declaration, if possible.

Implementation

FileSpan get spanWithName {
  var text = span.file.getText(0);

  // Move backwards through any whitespace between the name and the arguments.
  var i = span.start.offset - 1;
  while (i > 0 && text.codeUnitAt(i).isWhitespace) {
    i--;
  }

  // Then move backwards through the name itself.
  if (!text.codeUnitAt(i).isName) return span;
  i--;
  while (i >= 0 && text.codeUnitAt(i).isName) {
    i--;
  }

  // If the name didn't start with [isNameStart], it's not a valid identifier.
  if (!text.codeUnitAt(i + 1).isNameStart) return span;

  // Trim because it's possible that this span is empty (for example, a mixin
  // may be declared without an argument list).
  return span.file.span(i + 1, span.end.offset).trim();
}