deletionRange method

SourceRange deletionRange(
  1. AstNode node
)

Return the deletion range of the node, considering the spaces and comments before and after it.

Implementation

SourceRange deletionRange(AstNode node) {
  var begin = node.beginToken;
  begin = begin.precedingComments ?? begin;

  var end = node.endToken.next!;
  end = end.precedingComments ?? end;

  int startOffset;
  int endOffset;
  if (end.isEof) {
    var type = begin.type;
    if (node is AnnotatedNode &&
        (type == TokenType.SINGLE_LINE_COMMENT ||
            type == TokenType.MULTI_LINE_COMMENT)) {
      var firstToken = node.firstTokenAfterCommentAndMetadata;
      startOffset = firstToken.previous!.end;
    } else if (begin.previous!.isEof) {
      startOffset = begin.offset;
    } else {
      startOffset = begin.previous!.end;
    }
    endOffset = node.endToken.end;
  } else {
    startOffset = begin.offset;
    endOffset = end.offset;
  }
  return startOffsetEndOffset(startOffset, endOffset);
}