Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: pkg/analyzer/lib/src/dart/analysis/ast_provider_driver.dart

Issue 2669353002: Stop using Element.computeNode() in refactorings and fixes. (Closed)
Patch Set: documentation comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 /**
87 * [AstProvider] implementation for [AnalysisDriver].
88 */
89 class AstProviderForDriver extends AbstractAstProvider {
90 final AnalysisDriver driver;
91
92 AstProviderForDriver(this.driver);
93
94 @override
95 Future<CompilationUnit> getParsedUnitForElement(Element element) async {
96 String path = element.source.fullName;
97 ParseResult parseResult = await driver.parseFile(path);
98 return parseResult.unit;
99 }
100
101 @override
102 Future<CompilationUnit> getResolvedUnitForElement(Element element) async {
103 String path = element.source.fullName;
104 AnalysisResult analysisResult = await driver.getResult(path);
105 return analysisResult?.unit;
106 }
107 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/dart/analysis/ast_provider_context.dart ('k') | pkg/analyzer/lib/src/dart/element/ast_provider.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698