Skip to content

Commit

Permalink
Add codeOffset/codeLength properties to ElementImpl.
Browse files Browse the repository at this point in the history
  • Loading branch information
scheglov committed Mar 2, 2016
1 parent 02f8887 commit 089e1ba
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 4 deletions.
40 changes: 38 additions & 2 deletions pkg/analyzer/lib/src/dart/element/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
if (stackTraceParameter != null) {
LocalVariableElementImpl stackTrace =
new LocalVariableElementImpl.forNode(stackTraceParameter);
_setCodeRange(stackTrace, stackTraceParameter);
_currentHolder.addLocalVariable(stackTrace);
stackTraceParameter.staticElement = stackTrace;
}
Expand Down Expand Up @@ -413,6 +414,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
}
SimpleIdentifier className = node.name;
ClassElementImpl element = new ClassElementImpl.forNode(className);
_setCodeRange(element, node);
element.metadata = _createElementAnnotations(node.metadata);
List<TypeParameterElement> typeParameters = holder.typeParameters;
List<DartType> typeArguments = _createTypeParameterTypes(typeParameters);
Expand Down Expand Up @@ -462,6 +464,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
_visitChildren(holder, node);
SimpleIdentifier className = node.name;
ClassElementImpl element = new ClassElementImpl.forNode(className);
_setCodeRange(element, node);
element.metadata = _createElementAnnotations(node.metadata);
element.abstract = node.abstractKeyword != null;
element.mixinApplication = true;
Expand All @@ -478,6 +481,14 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
return null;
}

@override
Object visitCompilationUnit(CompilationUnit node) {
if (compilationUnitElement is ElementImpl) {
_setCodeRange(compilationUnitElement as ElementImpl, node);
}
return super.visitCompilationUnit(node);
}

@override
Object visitConstructorDeclaration(ConstructorDeclaration node) {
ElementHolder holder = new ElementHolder();
Expand All @@ -492,6 +503,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
SimpleIdentifier constructorName = node.name;
ConstructorElementImpl element =
new ConstructorElementImpl.forNode(constructorName);
_setCodeRange(element, node);
element.metadata = _createElementAnnotations(node.metadata);
setElementDocumentationComment(element, node);
if (node.externalKeyword != null) {
Expand Down Expand Up @@ -533,6 +545,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
SimpleIdentifier variableName = node.identifier;
LocalVariableElementImpl element =
new LocalVariableElementImpl.forNode(variableName);
_setCodeRange(element, node);
element.metadata = _createElementAnnotations(node.metadata);
ForEachStatement statement = node.parent as ForEachStatement;
int declarationEnd = node.offset + node.length;
Expand Down Expand Up @@ -565,6 +578,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
} else {
parameter = new DefaultParameterElementImpl.forNode(parameterName);
}
_setCodeRange(parameter, node);
parameter.const3 = node.isConst;
parameter.final2 = node.isFinal;
parameter.parameterKind = node.kind;
Expand Down Expand Up @@ -601,6 +615,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
Object visitEnumDeclaration(EnumDeclaration node) {
SimpleIdentifier enumName = node.name;
ClassElementImpl enumElement = new ClassElementImpl.forNode(enumName);
_setCodeRange(enumElement, node);
enumElement.metadata = _createElementAnnotations(node.metadata);
enumElement.enum2 = true;
setElementDocumentationComment(enumElement, node);
Expand Down Expand Up @@ -630,6 +645,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
_fieldMap == null ? null : _fieldMap[parameterName.name];
FieldFormalParameterElementImpl parameter =
new FieldFormalParameterElementImpl.forNode(parameterName);
_setCodeRange(parameter, node);
parameter.const3 = node.isConst;
parameter.final2 = node.isFinal;
parameter.parameterKind = node.kind;
Expand Down Expand Up @@ -671,6 +687,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
SimpleIdentifier functionName = node.name;
FunctionElementImpl element =
new FunctionElementImpl.forNode(functionName);
_setCodeRange(element, node);
element.metadata = _createElementAnnotations(node.metadata);
setElementDocumentationComment(element, node);
if (node.externalKeyword != null) {
Expand Down Expand Up @@ -718,6 +735,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
if (node.isGetter) {
PropertyAccessorElementImpl getter =
new PropertyAccessorElementImpl.forNode(propertyNameNode);
_setCodeRange(getter, node);
getter.metadata = _createElementAnnotations(node.metadata);
setElementDocumentationComment(getter, node);
if (node.externalKeyword != null) {
Expand Down Expand Up @@ -745,6 +763,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
} else {
PropertyAccessorElementImpl setter =
new PropertyAccessorElementImpl.forNode(propertyNameNode);
_setCodeRange(setter, node);
setter.metadata = _createElementAnnotations(node.metadata);
setElementDocumentationComment(setter, node);
if (node.externalKeyword != null) {
Expand Down Expand Up @@ -796,6 +815,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
FunctionBody body = node.body;
FunctionElementImpl element =
new FunctionElementImpl.forOffset(node.beginToken.offset);
_setCodeRange(element, node);
element.functions = holder.functions;
element.labels = holder.labels;
element.localVariables = holder.localVariables;
Expand Down Expand Up @@ -834,6 +854,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
List<TypeParameterElement> typeParameters = holder.typeParameters;
FunctionTypeAliasElementImpl element =
new FunctionTypeAliasElementImpl.forNode(aliasName);
_setCodeRange(element, node);
element.metadata = _createElementAnnotations(node.metadata);
setElementDocumentationComment(element, node);
element.parameters = parameters;
Expand All @@ -852,6 +873,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
SimpleIdentifier parameterName = node.identifier;
ParameterElementImpl parameter =
new ParameterElementImpl.forNode(parameterName);
_setCodeRange(parameter, node);
parameter.parameterKind = node.kind;
_setParameterVisibleRange(node, parameter);
_currentHolder.addParameter(parameter);
Expand Down Expand Up @@ -884,6 +906,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
SimpleIdentifier labelName = label.label;
LabelElementImpl element =
new LabelElementImpl.forNode(labelName, onSwitchStatement, false);
_setCodeRange(element, node);
_currentHolder.addLabel(element);
labelName.staticElement = element;
}
Expand Down Expand Up @@ -919,6 +942,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
}
MethodElementImpl element =
new MethodElementImpl(nameOfMethod, methodName.offset);
_setCodeRange(element, node);
element.metadata = _createElementAnnotations(node.metadata);
setElementDocumentationComment(element, node);
element.abstract = node.isAbstract;
Expand Down Expand Up @@ -957,6 +981,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
if (node.isGetter) {
PropertyAccessorElementImpl getter =
new PropertyAccessorElementImpl.forNode(propertyNameNode);
_setCodeRange(getter, node);
getter.metadata = _createElementAnnotations(node.metadata);
setElementDocumentationComment(getter, node);
if (node.externalKeyword != null) {
Expand Down Expand Up @@ -984,6 +1009,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
} else {
PropertyAccessorElementImpl setter =
new PropertyAccessorElementImpl.forNode(propertyNameNode);
_setCodeRange(setter, node);
setter.metadata = _createElementAnnotations(node.metadata);
setElementDocumentationComment(setter, node);
if (node.externalKeyword != null) {
Expand Down Expand Up @@ -1062,6 +1088,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
SimpleIdentifier parameterName = node.identifier;
ParameterElementImpl parameter =
new ParameterElementImpl.forNode(parameterName);
_setCodeRange(parameter, node);
parameter.const3 = node.isConst;
parameter.final2 = node.isFinal;
parameter.parameterKind = node.kind;
Expand Down Expand Up @@ -1107,6 +1134,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
SimpleIdentifier parameterName = node.name;
TypeParameterElementImpl typeParameter =
new TypeParameterElementImpl.forNode(parameterName);
_setCodeRange(typeParameter, node);
typeParameter.metadata = _createElementAnnotations(node.metadata);
TypeParameterTypeImpl typeParameterType =
new TypeParameterTypeImpl(typeParameter);
Expand Down Expand Up @@ -1135,6 +1163,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
}
element = field;
field.static = fieldNode.isStatic;
_setCodeRange(element, node);
setElementDocumentationComment(element, fieldNode);
field.hasImplicitType = varList.type == null;
_currentHolder.addField(field);
Expand All @@ -1148,6 +1177,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
variable = new LocalVariableElementImpl.forNode(variableName);
}
element = variable;
_setCodeRange(element, node);
Block enclosingBlock = node.getAncestor((node) => node is Block);
// TODO(brianwilkerson) This isn't right for variables declared in a for
// loop.
Expand All @@ -1164,6 +1194,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
variable = new TopLevelVariableElementImpl.forNode(variableName);
}
element = variable;
_setCodeRange(element, node);
if (varList.parent is TopLevelVariableDeclaration) {
setElementDocumentationComment(element, varList.parent);
}
Expand Down Expand Up @@ -1226,8 +1257,9 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
elementAnnotations = _createElementAnnotations(node.metadata);
}
for (VariableDeclaration variableDeclaration in node.variables) {
(variableDeclaration.element as ElementImpl).metadata =
elementAnnotations;
ElementImpl element = variableDeclaration.element as ElementImpl;
_setCodeRange(element, node.parent);
element.metadata = elementAnnotations;
}
return null;
}
Expand Down Expand Up @@ -1319,6 +1351,10 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
return null;
}

void _setCodeRange(ElementImpl element, AstNode node) {
element.setCodeRange(node.offset, node.length);
}

/**
* Sets the visible source range for formal parameter.
*/
Expand Down
48 changes: 48 additions & 0 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,17 @@ abstract class ElementImpl implements Element {
*/
int _docRangeLength;

/**
* The offset of the beginning of the element's code in the file that contains
* the element, or `null` if the element is synthetic.
*/
int _codeOffset;

/**
* The length of the element's code, or `null` if the element is synthetic.
*/
int _codeLength;

/**
* Initialize a newly created element to have the given [name] at the given
* [_nameOffset].
Expand All @@ -1748,6 +1759,17 @@ abstract class ElementImpl implements Element {
ElementImpl.forNode(Identifier name)
: this(name == null ? "" : name.name, name == null ? -1 : name.offset);

/**
* The length of the element's code, or `null` if the element is synthetic.
*/
int get codeLength => _codeLength;

/**
* The offset of the beginning of the element's code in the file that contains
* the element, or `null` if the element is synthetic.
*/
int get codeOffset => _codeOffset;

@override
AnalysisContext get context {
if (_enclosingElement == null) {
Expand Down Expand Up @@ -2027,6 +2049,14 @@ abstract class ElementImpl implements Element {
}
}

/**
* Set the code range for this element.
*/
void setCodeRange(int offset, int length) {
_codeOffset = offset;
_codeLength = length;
}

/**
* Set the documentation comment source range for this element.
*/
Expand Down Expand Up @@ -3106,6 +3136,24 @@ class LibraryElementImpl extends ElementImpl implements LibraryElement {
: super.forNode(name),
nameLength = name != null ? name.length : 0;

@override
int get codeLength {
if (_definingCompilationUnit is CompilationUnitElementImpl) {
return (_definingCompilationUnit as CompilationUnitElementImpl)
.codeLength;
}
return null;
}

@override
int get codeOffset {
if (_definingCompilationUnit is CompilationUnitElementImpl) {
return (_definingCompilationUnit as CompilationUnitElementImpl)
.codeOffset;
}
return null;
}

@override
CompilationUnitElement get definingCompilationUnit =>
_definingCompilationUnit;
Expand Down
Loading

0 comments on commit 089e1ba

Please sign in to comment.