How to get the start and end number of an Element?

43 views Asked by At

I need to know the line number and column of start and end of an Element from the Analyzer package.

For example:

// Some class
class Foo {
  String bar = '';
}

The Foo class starts at line 2 column 0 and ends at line 4 column 0.
How can I know that from ClassElement?

1

There are 1 answers

0
Jonathan On BEST ANSWER

Found how to get the AstNode from an Element:

AstNode getAstNodeFromElement(Element element) {
  final session = element.session;
  final parsedLibResult = session!.getParsedLibraryByElement(element.library!)
      as ParsedLibraryResult;
  final elDeclarationResult = parsedLibResult.getElementDeclaration(element);
  return elDeclarationResult!.node;
}

The AstNode has offset and end integers that mark the location in the dart file.