Skip to content

Commit d18af76

Browse files
bwilkersonCommit Queue
authored andcommitted
[migration] analysis_server.dart
Change-Id: I111faf848d4243f72fb5e9bf20393784d6ed1fcf Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/410180 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent c600842 commit d18af76

File tree

4 files changed

+28
-59
lines changed

4 files changed

+28
-59
lines changed

pkg/analysis_server/lib/src/analysis_server.dart

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
// ignore_for_file: analyzer_use_new_elements
6-
75
import 'dart:async';
86
import 'dart:io' as io;
97
import 'dart:io';
@@ -56,7 +54,6 @@ import 'package:analysis_server_plugin/src/correction/fix_generators.dart';
5654
import 'package:analyzer/dart/analysis/results.dart';
5755
import 'package:analyzer/dart/analysis/session.dart';
5856
import 'package:analyzer/dart/ast/ast.dart';
59-
import 'package:analyzer/dart/element/element.dart';
6057
import 'package:analyzer/dart/element/element2.dart';
6158
import 'package:analyzer/error/error.dart';
6259
import 'package:analyzer/exception/exception.dart';
@@ -674,9 +671,15 @@ abstract class AnalysisServer {
674671
return getElementOfNode2(node);
675672
}
676673

677-
/// Return the [Element] of the given [node], or `null` if [node] is `null` or
678-
/// does not have an element.
679-
Element? getElementOfNode(AstNode? node) {
674+
/// Returns the element associated with the [node].
675+
///
676+
/// If [useMockForImport] is `true` then a [MockLibraryImportElement] will be
677+
/// returned when an import directive or a prefix element is associated with
678+
/// the [node]. The rename-prefix refactoring should be updated to not require
679+
/// this work-around.
680+
///
681+
/// Returns `null` if [node] is `null` or doesn't have an element.
682+
Element2? getElementOfNode2(AstNode? node, {bool useMockForImport = false}) {
680683
if (node == null) {
681684
return null;
682685
}
@@ -690,42 +693,20 @@ abstract class AnalysisServer {
690693
return null;
691694
}
692695

693-
Element? element;
694-
switch (node) {
695-
case ExportDirective():
696-
element = node.element;
697-
case ImportDirective():
698-
element = node.element;
699-
case PartOfDirective():
700-
element = node.element;
701-
default:
702-
element = ElementLocator.locate2(node).asElement;
696+
Element2? element;
697+
if (useMockForImport && node is ImportDirective) {
698+
element = MockLibraryImportElement(node.libraryImport!);
699+
} else {
700+
element = ElementLocator.locate2(node);
703701
}
704-
705-
if (node is SimpleIdentifier && element is PrefixElement) {
706-
element = getImportElement(node);
702+
if (useMockForImport &&
703+
node is SimpleIdentifier &&
704+
element is PrefixElement2) {
705+
element = MockLibraryImportElement(getImportElement2(node)!);
707706
}
708707
return element;
709708
}
710709

711-
/// Return the [Element] of the given [node], or `null` if [node] is `null` or
712-
/// does not have an element.
713-
Element2? getElementOfNode2(AstNode? node) {
714-
if (node == null) {
715-
return null;
716-
}
717-
if (node is SimpleIdentifier && node.parent is LibraryIdentifier) {
718-
node = node.parent;
719-
}
720-
if (node is LibraryIdentifier) {
721-
node = node.parent;
722-
}
723-
if (node is StringLiteral && node.parent is UriBasedDirective) {
724-
return null;
725-
}
726-
return ElementLocator.locate2(node);
727-
}
728-
729710
/// Return a [LineInfo] for the file with the given [path].
730711
///
731712
/// If the file does not exist or cannot be read, returns `null`.

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import 'package:analysis_server/src/utilities/extensions/string.dart';
1919
import 'package:analyzer/dart/ast/ast.dart';
2020
import 'package:analyzer/dart/element/element.dart';
2121
import 'package:analyzer/src/dart/ast/utilities.dart';
22+
import 'package:analyzer/src/utilities/extensions/element.dart';
2223

2324
AstNode? _tweakLocatedNode(AstNode? node, int offset) {
2425
if (node is RepresentationDeclaration) {
@@ -66,7 +67,9 @@ class PrepareRenameHandler
6667
return (unit, offset).mapResults((unit, offset) async {
6768
var node = NodeLocator(offset).searchWithin(unit.unit);
6869
node = _tweakLocatedNode(node, offset);
69-
var element = server.getElementOfNode(node);
70+
var element =
71+
server.getElementOfNode2(node, useMockForImport: true).asElement;
72+
7073
if (node == null || element == null) {
7174
return success(null);
7275
}
@@ -161,7 +164,8 @@ class RenameHandler extends LspMessageHandler<RenameParams, WorkspaceEdit?> {
161164
) async {
162165
var node = NodeLocator(offset).searchWithin(unit.unit);
163166
node = _tweakLocatedNode(node, offset);
164-
var element = server.getElementOfNode(node);
167+
var element =
168+
server.getElementOfNode2(node, useMockForImport: true).asElement;
165169
if (node == null || element == null) {
166170
return success(null);
167171
}
@@ -223,7 +227,7 @@ class RenameHandler extends LspMessageHandler<RenameParams, WorkspaceEdit?> {
223227

224228
// Set the completer to complete to show that request is paused, and
225229
// that processing of incoming messages can continue while we wait
226-
// for the user's response.
230+
// for the user's response.
227231
message.completer?.complete();
228232

229233
// Otherwise, ask the user whether to proceed with the rename.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import 'package:analyzer/dart/ast/ast.dart';
1818
import 'package:analyzer/dart/element/element2.dart';
1919
import 'package:analyzer/src/dart/ast/utilities.dart';
2020
import 'package:analyzer/src/utilities/cancellation.dart';
21+
import 'package:analyzer/src/utilities/extensions/element.dart';
2122

2223
int test_resetCount = 0;
2324

@@ -314,7 +315,8 @@ class RefactoringManager {
314315
var resolvedUnit = await server.getResolvedUnit(file);
315316
if (resolvedUnit != null) {
316317
var node = NodeLocator(offset).searchWithin(resolvedUnit.unit);
317-
var element = server.getElementOfNode(node);
318+
var element =
319+
server.getElementOfNode2(node, useMockForImport: true)?.asElement;
318320
if (node is RepresentationDeclaration) {
319321
var extensionType = node.parent;
320322
if (extensionType is ExtensionTypeDeclaration &&

pkg/analysis_server/test/edit/refactoring_test.dart

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2721,24 +2721,6 @@ library my.new_name;
27212721
);
27222722
}
27232723

2724-
Future<void> test_library_partOfDirective() {
2725-
newFile('$testPackageLibPath/my_lib.dart', '''
2726-
library aaa.bbb.ccc;
2727-
part 'test.dart';
2728-
''');
2729-
addTestFile('''
2730-
part of aaa.bbb.ccc;
2731-
''');
2732-
return assertSuccessfulRefactoring(
2733-
() {
2734-
return sendRenameRequest('aaa.bb', 'my.new_name');
2735-
},
2736-
'''
2737-
part of my.new_name;
2738-
''',
2739-
);
2740-
}
2741-
27422724
Future<void> test_localVariable() {
27432725
setPriorityFiles([testFile]);
27442726
addTestFile('''

0 commit comments

Comments
 (0)