Skip to content

Commit cc06466

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Fixes completion not showing abstract class with alias declared above it
Fixes: #61244 Change-Id: I0c569653ee9040d8ff0d22135d9be09e7ba4d58d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/444701 Auto-Submit: Felipe Morschel <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent afa127b commit cc06466

File tree

4 files changed

+50
-50
lines changed

4 files changed

+50
-50
lines changed

pkg/analysis_server/lib/src/services/completion/dart/declaration_helper.dart

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,12 @@ class DeclarationHelper {
742742
isNotImported: false,
743743
);
744744
for (var element in namespace.definedNames2.values) {
745+
if (!visibilityTracker.isVisible(
746+
element: element,
747+
importData: importData,
748+
)) {
749+
continue;
750+
}
745751
switch (element) {
746752
case ClassElement():
747753
_suggestConstructors(
@@ -947,9 +953,6 @@ class DeclarationHelper {
947953
/// Adds suggestions for any constructors that are imported into the
948954
/// [library].
949955
void _addImportedConstructors(LibraryElement library) {
950-
// TODO(brianwilkerson): This will create suggestions for elements that
951-
// conflict with different elements imported from a different library. Not
952-
// sure whether that's the desired behavior.
953956
for (var importElement in library.firstFragment.libraryImports) {
954957
var importedLibrary = importElement.importedLibrary;
955958
if (importedLibrary != null) {
@@ -1793,7 +1796,6 @@ class DeclarationHelper {
17931796
element.constructors,
17941797
importData,
17951798
allowNonFactory: !element.isAbstract,
1796-
checkVisibility: false,
17971799
);
17981800
}
17991801
}
@@ -1804,7 +1806,6 @@ class DeclarationHelper {
18041806
required ImportData? importData,
18051807
required bool hasClassName,
18061808
required bool isConstructorRedirect,
1807-
bool checkVisibility = true,
18081809
}) {
18091810
if (mustBeAssignable) {
18101811
return;
@@ -1813,29 +1814,20 @@ class DeclarationHelper {
18131814
if (!element.isVisibleIn(request.libraryElement)) {
18141815
return;
18151816
}
1816-
if (importData?.isNotImported ?? false) {
1817-
if (checkVisibility &&
1818-
!visibilityTracker.isVisible(
1819-
element: element.enclosingElement,
1820-
importData: importData,
1821-
)) {
1822-
// If the constructor is on a class from a not-yet-imported library and
1823-
// the class isn't visible, then we shouldn't suggest it.
1824-
//
1825-
// We could consider computing a prefix and updating the [importData] in
1826-
// order to avoid the collision, but we don't currently do that for any
1827-
// not-yet-imported elements (nor for imported elements that are
1828-
// shadowed by local declarations).
1829-
return;
1830-
}
1831-
} else {
1832-
// Add the class to the visibility tracker so that we will know later that
1833-
// any non-imported elements with the same name are not visible.
1834-
visibilityTracker.isVisible(
1835-
element: element.enclosingElement,
1836-
importData: importData,
1837-
);
1838-
}
1817+
// If the constructor is on a class from a not-yet-imported library and
1818+
// the class isn't visible, then we shouldn't suggest it.
1819+
//
1820+
// We could consider computing a prefix and updating the [importData] in
1821+
// order to avoid the collision, but we don't currently do that for any
1822+
// not-yet-imported elements (nor for imported elements that are
1823+
// shadowed by local declarations).
1824+
1825+
// Add the class to the visibility tracker so that we will know later that
1826+
// any non-imported elements with the same name are not visible.
1827+
visibilityTracker.isVisible(
1828+
element: element.enclosingElement,
1829+
importData: importData,
1830+
);
18391831

18401832
// Use the constructor element's name without the interface type to
18411833
// calculate the matcher score for dot shorthands.
@@ -1868,23 +1860,10 @@ class DeclarationHelper {
18681860
List<ConstructorElement> constructors,
18691861
ImportData? importData, {
18701862
bool allowNonFactory = true,
1871-
bool checkVisibility = true,
18721863
}) {
18731864
if (mustBeAssignable) {
18741865
return;
18751866
}
1876-
if (checkVisibility &&
1877-
constructors.isNotEmpty &&
1878-
!visibilityTracker.isVisible(
1879-
element: constructors.first.enclosingElement,
1880-
importData: importData,
1881-
)) {
1882-
return;
1883-
}
1884-
1885-
if (checkVisibility) {
1886-
checkVisibility = false;
1887-
}
18881867

18891868
for (var constructor in constructors) {
18901869
if (constructor.isVisibleIn(request.libraryElement) &&
@@ -1894,7 +1873,6 @@ class DeclarationHelper {
18941873
hasClassName: false,
18951874
importData: importData,
18961875
isConstructorRedirect: false,
1897-
checkVisibility: checkVisibility,
18981876
);
18991877
}
19001878
}
@@ -1972,11 +1950,7 @@ class DeclarationHelper {
19721950
}
19731951
if (!mustBeType) {
19741952
_suggestStaticFields(element.fields, importData);
1975-
_suggestConstructors(
1976-
element.constructors,
1977-
importData,
1978-
checkVisibility: false,
1979-
);
1953+
_suggestConstructors(element.constructors, importData);
19801954
}
19811955
}
19821956
}

pkg/analysis_server/lib/src/services/completion/dart/visibility_tracker.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ class VisibilityTracker {
2121
/// that has the same name.
2222
///
2323
/// If the [importData] indicated that the element is already imported (either
24-
/// by being `null` or by returning `false` from `isNotImported`) and the name
25-
/// is visible, it will be added to the list of [_declaredNames] so that it
26-
/// will shadow any elements of the same name further up the scope chain.
24+
/// by being `null` or by returning `false` from `isNotImported`), the name
25+
/// is visible.
26+
///
27+
/// If it is already imported, it will be added to the list of
28+
/// [_declaredNames] so that it will shadow any elements of the same name
29+
/// further up the scope chain.
2730
bool isVisible({required Element? element, required ImportData? importData}) {
2831
var name = element?.displayName;
2932
if (name == null) {

pkg/analysis_server/test/lsp/completion_dart_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,25 @@ class CompletionDocumentationResolutionTest extends AbstractCompletionTest {
116116
await initialAnalysis;
117117
}
118118

119+
Future<void> test_abstract_class() async {
120+
newFile(join(projectFolderPath, 'my_class.dart'), '''
121+
typedef MyClass2 = MyClass;
122+
123+
abstract class MyClass {}
124+
''');
125+
126+
content = '''
127+
void f() {
128+
MyClas^
129+
}
130+
''';
131+
132+
await initializeServer();
133+
134+
var completion = await getCompletionItem('MyClass');
135+
expectDocumentation(completion, isNull);
136+
}
137+
119138
Future<void> test_class() async {
120139
newFile(join(projectFolderPath, 'my_class.dart'), '''
121140
/// Class.

pkg/analysis_server/test/services/completion/dart/completion_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6926,6 +6926,8 @@ class File {
69266926
}
69276927
f() => new Fil^
69286928
''');
6929+
// The second `File` constructor that was being hidden previously is from
6930+
// dart:io
69296931
assertResponse(r'''
69306932
replacement
69316933
left: 3
@@ -6940,6 +6942,8 @@ suggestions
69406942
kind: constructorInvocation
69416943
FileMode._internal1
69426944
kind: constructorInvocation
6945+
File
6946+
kind: constructorInvocation
69436947
''');
69446948
}
69456949

0 commit comments

Comments
 (0)