Skip to content

Commit c9cd6cf

Browse files
srawlinsCommit Queue
authored andcommitted
DAS: Move getElementOfNode out of AnalysisServer into utility
This method could have been static the whole time; never referenced instance code from AnalysisServer; and then it just doesn't belong on that class. Change-Id: I81e8a1e65dd379ad89e3f4ab36f10983e96eaeb5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/423169 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 87ccd49 commit c9cd6cf

File tree

9 files changed

+59
-54
lines changed

9 files changed

+59
-54
lines changed

pkg/analysis_server/lib/src/analysis_server.dart

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import 'package:analysis_server/src/server/crash_reporting_attachments.dart';
3131
import 'package:analysis_server/src/server/diagnostic_server.dart';
3232
import 'package:analysis_server/src/services/completion/completion_performance.dart';
3333
import 'package:analysis_server/src/services/correction/fix_performance.dart';
34-
import 'package:analysis_server/src/services/correction/namespace.dart';
3534
import 'package:analysis_server/src/services/correction/refactoring_performance.dart';
3635
import 'package:analysis_server/src/services/dart_tooling_daemon/dtd_services.dart';
3736
import 'package:analysis_server/src/services/pub/pub_api.dart';
@@ -44,6 +43,7 @@ import 'package:analysis_server/src/services/search/search_engine_internal.dart'
4443
import 'package:analysis_server/src/services/user_prompts/dart_fix_prompt_manager.dart';
4544
import 'package:analysis_server/src/services/user_prompts/survey_manager.dart';
4645
import 'package:analysis_server/src/services/user_prompts/user_prompts.dart';
46+
import 'package:analysis_server/src/utilities/extensions/ast.dart';
4747
import 'package:analysis_server/src/utilities/file_string_sink.dart';
4848
import 'package:analysis_server/src/utilities/process.dart';
4949
import 'package:analysis_server/src/utilities/request_statistics.dart';
@@ -53,7 +53,6 @@ import 'package:analysis_server_plugin/src/correction/assist_performance.dart';
5353
import 'package:analysis_server_plugin/src/correction/performance.dart';
5454
import 'package:analyzer/dart/analysis/results.dart';
5555
import 'package:analyzer/dart/analysis/session.dart';
56-
import 'package:analyzer/dart/ast/ast.dart';
5756
import 'package:analyzer/dart/element/element2.dart';
5857
import 'package:analyzer/exception/exception.dart';
5958
import 'package:analyzer/file_system/file_system.dart';
@@ -74,13 +73,11 @@ import 'package:analyzer/src/dart/analysis/results.dart';
7473
import 'package:analyzer/src/dart/analysis/session.dart';
7574
import 'package:analyzer/src/dart/analysis/status.dart' as analysis;
7675
import 'package:analyzer/src/dart/analysis/unlinked_unit_store.dart';
77-
import 'package:analyzer/src/dart/ast/element_locator.dart';
7876
import 'package:analyzer/src/dartdoc/dartdoc_directive_info.dart';
7977
import 'package:analyzer/src/generated/sdk.dart';
8078
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
8179
import 'package:analyzer/src/util/performance/operation_performance.dart';
8280
import 'package:analyzer/src/utilities/extensions/analysis_session.dart';
83-
import 'package:analyzer/src/utilities/extensions/element.dart';
8481
import 'package:analyzer/utilities/extensions/ast.dart';
8582
import 'package:analyzer_plugin/protocol/protocol.dart';
8683
import 'package:analyzer_plugin/src/protocol/protocol_internal.dart'
@@ -661,44 +658,8 @@ abstract class AnalysisServer {
661658
}
662659
}
663660

664-
var unit = unitResult.unit;
665-
return getElementOfNode(unit.nodeCovering(offset: offset));
666-
}
667-
668-
/// Returns the element associated with the [node].
669-
///
670-
/// If [useMockForImport] is `true` then a [MockLibraryImportElement] will be
671-
/// returned when an import directive or a prefix element is associated with
672-
/// the [node]. The rename-prefix refactoring should be updated to not require
673-
/// this work-around.
674-
///
675-
/// Returns `null` if [node] is `null` or doesn't have an element.
676-
Element2? getElementOfNode(AstNode? node, {bool useMockForImport = false}) {
677-
if (node == null) {
678-
return null;
679-
}
680-
if (node is SimpleIdentifier && node.parent is LibraryIdentifier) {
681-
node = node.parent;
682-
}
683-
if (node is LibraryIdentifier) {
684-
node = node.parent;
685-
}
686-
if (node is StringLiteral && node.parent is UriBasedDirective) {
687-
return null;
688-
}
689-
690-
Element2? element;
691-
if (useMockForImport && node is ImportDirective) {
692-
element = MockLibraryImportElement(node.libraryImport!);
693-
} else {
694-
element = ElementLocator.locate2(node);
695-
}
696-
if (useMockForImport &&
697-
node is SimpleIdentifier &&
698-
element is PrefixElement2) {
699-
element = MockLibraryImportElement(getImportElement(node)!);
700-
}
701-
return element;
661+
var node = unitResult.unit.nodeCovering(offset: offset);
662+
return node?.getElement();
702663
}
703664

704665
/// Return a [LineInfo] for the file with the given [path].

pkg/analysis_server/lib/src/handler/legacy/edit_get_available_refactorings.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:async';
77
import 'package:analysis_server/protocol/protocol_generated.dart';
88
import 'package:analysis_server/src/handler/legacy/legacy_handler.dart';
99
import 'package:analysis_server/src/services/refactoring/legacy/refactoring.dart';
10+
import 'package:analysis_server/src/utilities/extensions/ast.dart';
1011
import 'package:analyzer/dart/element/element2.dart';
1112
import 'package:analyzer/utilities/extensions/ast.dart';
1213
import 'package:analyzer_plugin/protocol/protocol_common.dart';
@@ -75,7 +76,7 @@ class EditGetAvailableRefactoringsHandler extends LegacyHandler {
7576
var resolvedUnit = await server.getResolvedUnit(file);
7677
if (resolvedUnit != null) {
7778
var node = resolvedUnit.unit.nodeCovering(offset: offset);
78-
var element = server.getElementOfNode(node);
79+
var element = node?.getElement();
7980
if (element != null) {
8081
var refactoringWorkspace = server.refactoringWorkspace;
8182
// try CONVERT_METHOD_TO_GETTER

pkg/analysis_server/lib/src/lsp/handlers/code_actions/dart.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'package:analysis_server/src/services/correction/refactoring_performance.
1515
import 'package:analysis_server/src/services/refactoring/framework/refactoring_context.dart';
1616
import 'package:analysis_server/src/services/refactoring/framework/refactoring_processor.dart';
1717
import 'package:analysis_server/src/services/refactoring/legacy/refactoring.dart';
18+
import 'package:analysis_server/src/utilities/extensions/ast.dart';
1819
import 'package:analysis_server_plugin/edit/assist/assist.dart';
1920
import 'package:analysis_server_plugin/edit/assist/dart_assist_context.dart';
2021
import 'package:analysis_server_plugin/edit/fix/dart_fix_context.dart';
@@ -401,7 +402,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
401402
timer.restart();
402403

403404
var node = unitResult.unit.nodeCovering(offset: offset);
404-
var element = server.getElementOfNode(node);
405+
var element = node?.getElement();
405406

406407
// Getter to Method
407408
if (element is GetterElement &&

pkg/analysis_server/lib/src/lsp/handlers/commands/abstract_refactor.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'package:analysis_server/src/lsp/handlers/handlers.dart';
1313
import 'package:analysis_server/src/lsp/progress.dart';
1414
import 'package:analysis_server/src/protocol_server.dart';
1515
import 'package:analysis_server/src/services/refactoring/legacy/refactoring.dart';
16+
import 'package:analysis_server/src/utilities/extensions/ast.dart';
1617
import 'package:analyzer/dart/analysis/results.dart';
1718
import 'package:analyzer/dart/element/element2.dart';
1819
import 'package:analyzer/utilities/extensions/ast.dart';
@@ -126,7 +127,7 @@ abstract class AbstractRefactorCommandHandler extends SimpleEditCommandHandler
126127

127128
case RefactoringKind.CONVERT_GETTER_TO_METHOD:
128129
var node = result.unit.nodeCovering(offset: offset);
129-
var element = server.getElementOfNode(node);
130+
var element = node?.getElement();
130131
if (element is GetterElement) {
131132
var refactor = ConvertGetterToMethodRefactoring(
132133
server.refactoringWorkspace,
@@ -142,7 +143,7 @@ abstract class AbstractRefactorCommandHandler extends SimpleEditCommandHandler
142143

143144
case RefactoringKind.CONVERT_METHOD_TO_GETTER:
144145
var node = result.unit.nodeCovering(offset: offset);
145-
var element = server.getElementOfNode(node);
146+
var element = node?.getElement();
146147
if (element is ExecutableElement2) {
147148
var refactor = ConvertMethodToGetterRefactoring(
148149
server.refactoringWorkspace,

pkg/analysis_server/lib/src/lsp/handlers/handler_implementation.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:analysis_server/src/lsp/handlers/handlers.dart';
99
import 'package:analysis_server/src/lsp/mapping.dart';
1010
import 'package:analysis_server/src/lsp/registration/feature_registration.dart';
1111
import 'package:analysis_server/src/search/type_hierarchy.dart';
12+
import 'package:analysis_server/src/utilities/extensions/ast.dart';
1213
import 'package:analyzer/dart/analysis/results.dart';
1314
import 'package:analyzer/dart/element/element2.dart';
1415
import 'package:analyzer/src/util/performance/operation_performance.dart';
@@ -63,7 +64,7 @@ class ImplementationHandler
6364
OperationPerformanceImpl performance,
6465
) async {
6566
var node = result.unit.nodeCovering(offset: offset);
66-
var element = server.getElementOfNode(node);
67+
var element = node?.getElement();
6768
if (element == null) {
6869
return success([]);
6970
}

pkg/analysis_server/lib/src/lsp/handlers/handler_references.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:analysis_server/src/lsp/registration/feature_registration.dart';
1010
import 'package:analysis_server/src/search/element_references.dart';
1111
import 'package:analysis_server/src/services/search/search_engine.dart'
1212
show SearchMatch;
13+
import 'package:analysis_server/src/utilities/extensions/ast.dart';
1314
import 'package:analyzer/dart/analysis/results.dart';
1415
import 'package:analyzer/dart/ast/ast.dart';
1516
import 'package:analyzer/dart/element/element2.dart';
@@ -66,10 +67,10 @@ class ReferencesHandler
6667
var node = result.unit.nodeCovering(offset: offset);
6768
node = _getReferenceTargetNode(node);
6869

69-
var element = switch (server.getElementOfNode(node)) {
70+
var element = switch (node?.getElement()) {
7071
FieldFormalParameterElement2(:var field2?) => field2,
7172
PropertyAccessorElement2(:var variable3?) => variable3,
72-
(var element) => element,
73+
var element => element,
7374
};
7475

7576
if (element == null) {

pkg/analysis_server/lib/src/lsp/handlers/handler_rename.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'package:analysis_server/src/lsp/mapping.dart';
1313
import 'package:analysis_server/src/lsp/registration/feature_registration.dart';
1414
import 'package:analysis_server/src/services/refactoring/legacy/refactoring.dart';
1515
import 'package:analysis_server/src/services/refactoring/legacy/rename_unit_member.dart';
16+
import 'package:analysis_server/src/utilities/extensions/ast.dart';
1617
import 'package:analysis_server/src/utilities/extensions/string.dart';
1718
import 'package:analyzer/dart/ast/ast.dart';
1819
import 'package:analyzer/dart/element/element2.dart';
@@ -64,7 +65,7 @@ class PrepareRenameHandler
6465
return (unit, offset).mapResults((result, offset) async {
6566
var node = result.unit.nodeCovering(offset: offset);
6667
node = _tweakLocatedNode(node, offset);
67-
var element = server.getElementOfNode(node, useMockForImport: true);
68+
var element = node?.getElement(useMockForImport: true);
6869

6970
if (node == null || element == null) {
7071
return success(null);
@@ -160,7 +161,7 @@ class RenameHandler extends LspMessageHandler<RenameParams, WorkspaceEdit?> {
160161
) async {
161162
var node = result.unit.nodeCovering(offset: offset);
162163
node = _tweakLocatedNode(node, offset);
163-
var element = server.getElementOfNode(node, useMockForImport: true);
164+
var element = node?.getElement(useMockForImport: true);
164165
if (node == null || element == null) {
165166
return success(null);
166167
}

pkg/analysis_server/lib/src/services/refactoring/legacy/refactoring_manager.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'package:analysis_server/src/protocol_server.dart'
1515
import 'package:analysis_server/src/services/correction/status.dart';
1616
import 'package:analysis_server/src/services/refactoring/legacy/refactoring.dart';
1717
import 'package:analysis_server/src/services/search/search_engine.dart';
18+
import 'package:analysis_server/src/utilities/extensions/ast.dart';
1819
import 'package:analyzer/dart/analysis/session.dart';
1920
import 'package:analyzer/dart/ast/ast.dart';
2021
import 'package:analyzer/dart/element/element2.dart';
@@ -223,7 +224,7 @@ class RefactoringManager {
223224
var resolvedUnit = await server.getResolvedUnit(file);
224225
if (resolvedUnit != null) {
225226
var node = resolvedUnit.unit.nodeCovering(offset: offset);
226-
var element = server.getElementOfNode(node);
227+
var element = node?.getElement();
227228
if (element is GetterElement) {
228229
refactoring = ConvertGetterToMethodRefactoring(
229230
refactoringWorkspace,
@@ -236,7 +237,7 @@ class RefactoringManager {
236237
var resolvedUnit = await server.getResolvedUnit(file);
237238
if (resolvedUnit != null) {
238239
var node = resolvedUnit.unit.nodeCovering(offset: offset);
239-
var element = server.getElementOfNode(node);
240+
var element = node?.getElement();
240241
if (element is ExecutableElement2) {
241242
refactoring = ConvertMethodToGetterRefactoring(
242243
refactoringWorkspace,
@@ -316,7 +317,7 @@ class RefactoringManager {
316317
var resolvedUnit = await server.getResolvedUnit(file);
317318
if (resolvedUnit != null) {
318319
var node = resolvedUnit.unit.nodeCovering(offset: offset);
319-
var element = server.getElementOfNode(node, useMockForImport: true);
320+
var element = node?.getElement(useMockForImport: true);
320321
if (node is RepresentationDeclaration) {
321322
var extensionType = node.parent;
322323
if (extensionType is ExtensionTypeDeclaration &&

pkg/analysis_server/lib/src/utilities/extensions/ast.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analysis_server/src/services/correction/namespace.dart';
56
import 'package:analysis_server/src/utilities/extensions/element.dart';
67
import 'package:analyzer/dart/ast/ast.dart';
78
import 'package:analyzer/dart/ast/token.dart';
89
import 'package:analyzer/dart/ast/visitor.dart';
910
import 'package:analyzer/dart/element/element2.dart';
1011
import 'package:analyzer/source/source.dart';
12+
import 'package:analyzer/src/dart/ast/element_locator.dart';
1113
import 'package:analyzer/src/dart/ast/token.dart';
1214
import 'package:analyzer/src/dart/ast/utilities.dart';
1315
import 'package:analyzer/src/utilities/extensions/ast.dart';
1416
import 'package:analyzer/src/utilities/extensions/collection.dart';
17+
import 'package:analyzer/src/utilities/extensions/element.dart';
1518

1619
class ThrowStatement {
1720
final ExpressionStatement statement;
@@ -188,6 +191,40 @@ extension AstNodeExtension on AstNode {
188191
}
189192
return null;
190193
}
194+
195+
/// Returns the element associated with `this`.
196+
///
197+
/// If [useMockForImport] is `true` then a [MockLibraryImportElement] will be
198+
/// returned when an import directive or a prefix element is associated with
199+
/// `this`. The rename-prefix refactoring should be updated to not require
200+
/// this work-around.
201+
///
202+
/// Returns `null` if `this` is `null` or doesn't have an element.
203+
Element2? getElement({bool useMockForImport = false}) {
204+
AstNode? node = this;
205+
if (node is SimpleIdentifier && node.parent is LibraryIdentifier) {
206+
node = node.parent;
207+
}
208+
if (node is LibraryIdentifier) {
209+
node = node.parent;
210+
}
211+
if (node is StringLiteral && node.parent is UriBasedDirective) {
212+
return null;
213+
}
214+
215+
Element2? element;
216+
if (useMockForImport && node is ImportDirective) {
217+
element = MockLibraryImportElement(node.libraryImport!);
218+
} else {
219+
element = ElementLocator.locate2(node);
220+
}
221+
if (useMockForImport &&
222+
node is SimpleIdentifier &&
223+
element is PrefixElement2) {
224+
element = MockLibraryImportElement(getImportElement(node)!);
225+
}
226+
return element;
227+
}
191228
}
192229

193230
extension BinaryExpressionExtension on BinaryExpression {

0 commit comments

Comments
 (0)