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

Unified 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, 11 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 side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer/lib/src/dart/analysis/ast_provider_driver.dart
diff --git a/pkg/analyzer/lib/src/dart/analysis/ast_provider_driver.dart b/pkg/analyzer/lib/src/dart/analysis/ast_provider_driver.dart
new file mode 100644
index 0000000000000000000000000000000000000000..3bd130784ed1b7b1b130d7da42c1e20dfc78f6a6
--- /dev/null
+++ b/pkg/analyzer/lib/src/dart/analysis/ast_provider_driver.dart
@@ -0,0 +1,107 @@
+// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'dart:async';
+
+import 'package:analyzer/dart/ast/ast.dart';
+import 'package:analyzer/dart/element/element.dart';
+import 'package:analyzer/src/dart/analysis/driver.dart';
+import 'package:analyzer/src/dart/ast/utilities.dart';
+import 'package:analyzer/src/dart/element/ast_provider.dart';
+
+abstract class AbstractAstProvider implements AstProvider {
+ /**
+ * Return the [AstNode] for the given [element] if the given [unit], or
+ * `null` if the [element] is not know, or is not defined in the [unit].
+ */
+ @override
+ AstNode findNodeForElement(CompilationUnit unit, Element element) {
+ AstNode nameNode = new NodeLocator(element.nameOffset).searchWithin(unit);
+ if (element is ClassElement) {
+ if (element.isEnum) {
+ return nameNode.getAncestor((node) => node is EnumDeclaration);
+ } else {
+ return nameNode.getAncestor(
+ (node) => node is ClassDeclaration || node is ClassTypeAlias);
+ }
+ }
+ if (element is ConstructorElement) {
+ return nameNode.getAncestor((node) => node is ConstructorDeclaration);
+ }
+ if (element is FieldElement) {
+ if (element.isEnumConstant) {
+ return nameNode.getAncestor((node) => node is EnumConstantDeclaration);
+ } else {
+ return nameNode.getAncestor((node) => node is VariableDeclaration);
+ }
+ }
+ if (element is FunctionElement) {
+ return nameNode.getAncestor((node) => node is FunctionDeclaration);
+ }
+ if (element is FunctionTypeAliasElement) {
+ return nameNode.getAncestor((node) => node is FunctionTypeAlias);
+ }
+ if (element is LocalVariableElement) {
+ return nameNode.getAncestor(
+ (node) => node is DeclaredIdentifier || node is VariableDeclaration);
+ }
+ if (element is MethodElement) {
+ return nameNode.getAncestor((node) => node is MethodDeclaration);
+ }
+ if (element is ParameterElement) {
+ return nameNode.getAncestor((node) => node is FormalParameter);
+ }
+ if (element is PropertyAccessorElement) {
+ if (element.isSynthetic) {
+ return null;
+ }
+ if (element.enclosingElement is ClassElement) {
+ return nameNode.getAncestor((node) => node is MethodDeclaration);
+ } else if (element.enclosingElement is CompilationUnitElement) {
+ return nameNode.getAncestor((node) => node is FunctionDeclaration);
+ }
+ return null;
+ }
+ if (element is TopLevelVariableElement) {
+ return nameNode.getAncestor((node) => node is VariableDeclaration);
+ }
+ return null;
+ }
+
+ @override
+ Future<T> getParsedNodeForElement<T extends AstNode>(Element element) async {
+ CompilationUnit unit = await getParsedUnitForElement(element);
+ return findNodeForElement(unit, element) as T;
+ }
+
+ @override
+ Future<T> getResolvedNodeForElement<T extends AstNode>(
+ Element element) async {
+ CompilationUnit unit = await getResolvedUnitForElement(element);
+ return findNodeForElement(unit, element) as T;
+ }
+}
+
+/**
+ * [AstProvider] implementation for [AnalysisDriver].
+ */
+class AstProviderForDriver extends AbstractAstProvider {
+ final AnalysisDriver driver;
+
+ AstProviderForDriver(this.driver);
+
+ @override
+ Future<CompilationUnit> getParsedUnitForElement(Element element) async {
+ String path = element.source.fullName;
+ ParseResult parseResult = await driver.parseFile(path);
+ return parseResult.unit;
+ }
+
+ @override
+ Future<CompilationUnit> getResolvedUnitForElement(Element element) async {
+ String path = element.source.fullName;
+ AnalysisResult analysisResult = await driver.getResult(path);
+ return analysisResult?.unit;
+ }
+}
« 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