| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:async'; |
| 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/element/element.dart'; |
| 9 import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 10 import 'package:analyzer/src/dart/ast/utilities.dart'; |
| 11 import 'package:analyzer/src/dart/element/ast_provider.dart'; |
| 12 |
| 13 abstract class AbstractAstProvider implements AstProvider { |
| 14 /** |
| 15 * Return the [AstNode] for the given [element] if the given [unit], or |
| 16 * `null` if the [element] is not know, or is not defined in the [unit]. |
| 17 */ |
| 18 @override |
| 19 AstNode findNodeForElement(CompilationUnit unit, Element element) { |
| 20 AstNode nameNode = new NodeLocator(element.nameOffset).searchWithin(unit); |
| 21 if (element is ClassElement) { |
| 22 if (element.isEnum) { |
| 23 return nameNode.getAncestor((node) => node is EnumDeclaration); |
| 24 } else { |
| 25 return nameNode.getAncestor( |
| 26 (node) => node is ClassDeclaration || node is ClassTypeAlias); |
| 27 } |
| 28 } |
| 29 if (element is ConstructorElement) { |
| 30 return nameNode.getAncestor((node) => node is ConstructorDeclaration); |
| 31 } |
| 32 if (element is FieldElement) { |
| 33 if (element.isEnumConstant) { |
| 34 return nameNode.getAncestor((node) => node is EnumConstantDeclaration); |
| 35 } else { |
| 36 return nameNode.getAncestor((node) => node is VariableDeclaration); |
| 37 } |
| 38 } |
| 39 if (element is FunctionElement) { |
| 40 return nameNode.getAncestor((node) => node is FunctionDeclaration); |
| 41 } |
| 42 if (element is FunctionTypeAliasElement) { |
| 43 return nameNode.getAncestor((node) => node is FunctionTypeAlias); |
| 44 } |
| 45 if (element is LocalVariableElement) { |
| 46 return nameNode.getAncestor( |
| 47 (node) => node is DeclaredIdentifier || node is VariableDeclaration); |
| 48 } |
| 49 if (element is MethodElement) { |
| 50 return nameNode.getAncestor((node) => node is MethodDeclaration); |
| 51 } |
| 52 if (element is ParameterElement) { |
| 53 return nameNode.getAncestor((node) => node is FormalParameter); |
| 54 } |
| 55 if (element is PropertyAccessorElement) { |
| 56 if (element.isSynthetic) { |
| 57 return null; |
| 58 } |
| 59 if (element.enclosingElement is ClassElement) { |
| 60 return nameNode.getAncestor((node) => node is MethodDeclaration); |
| 61 } else if (element.enclosingElement is CompilationUnitElement) { |
| 62 return nameNode.getAncestor((node) => node is FunctionDeclaration); |
| 63 } |
| 64 return null; |
| 65 } |
| 66 if (element is TopLevelVariableElement) { |
| 67 return nameNode.getAncestor((node) => node is VariableDeclaration); |
| 68 } |
| 69 return null; |
| 70 } |
| 71 |
| 72 @override |
| 73 Future<T> getParsedNodeForElement<T extends AstNode>(Element element) async { |
| 74 CompilationUnit unit = await getParsedUnitForElement(element); |
| 75 return findNodeForElement(unit, element) as T; |
| 76 } |
| 77 |
| 78 @override |
| 79 Future<T> getResolvedNodeForElement<T extends AstNode>( |
| 80 Element element) async { |
| 81 CompilationUnit unit = await getResolvedUnitForElement(element); |
| 82 return findNodeForElement(unit, element) as T; |
| 83 } |
| 84 } |
| 85 |
| 86 class AstProviderForDriver extends AbstractAstProvider { |
| 87 final AnalysisDriver driver; |
| 88 |
| 89 AstProviderForDriver(this.driver); |
| 90 |
| 91 @override |
| 92 Future<CompilationUnit> getParsedUnitForElement(Element element) async { |
| 93 String path = element.source.fullName; |
| 94 ParseResult parseResult = await driver.parseFile(path); |
| 95 return parseResult.unit; |
| 96 } |
| 97 |
| 98 @override |
| 99 Future<CompilationUnit> getResolvedUnitForElement(Element element) async { |
| 100 String path = element.source.fullName; |
| 101 AnalysisResult analysisResult = await driver.getResult(path); |
| 102 return analysisResult?.unit; |
| 103 } |
| 104 } |
| OLD | NEW |