Skip to content

Commit 8515d74

Browse files
committed
Add ImportElement.namespace and use it in completion.
R=brianwilkerson@google.com Change-Id: I58cadf0e3239ad4ae8584d79485f4d9ebda10eba Reviewed-on: https://dart-review.googlesource.com/54064 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
1 parent 410db09 commit 8515d74

File tree

12 files changed

+30
-37
lines changed

12 files changed

+30
-37
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class LibraryMemberContributor extends DartCompletionContributor {
5757
LibraryElementSuggestionBuilder builder =
5858
new LibraryElementSuggestionBuilder(containingLibrary,
5959
CompletionSuggestionKind.INVOCATION, typesOnly, instCreation);
60-
for (var element in library.exportNamespace.definedNames.values) {
60+
for (var element in importElem.namespace.definedNames.values) {
6161
element.accept(builder);
6262
}
6363
suggestions.addAll(builder.suggestions);

pkg/analysis_server/lib/src/services/correction/namespace.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ ImportElement internal_getImportElement(
114114
if (importElementsMap.containsKey(importElement)) {
115115
continue;
116116
}
117-
Namespace namespace =
118-
new NamespaceBuilder().createImportNamespaceForDirective(importElement);
117+
Namespace namespace = importElement.namespace;
119118
Set<Element> elements = new Set.from(namespace.definedNames.values);
120119
importElementsMap[importElement] = elements;
121120
}

pkg/analysis_server/lib/src/services/correction/util.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import 'package:analyzer/src/dart/ast/utilities.dart';
1616
import 'package:analyzer/src/dart/scanner/reader.dart';
1717
import 'package:analyzer/src/dart/scanner/scanner.dart';
1818
import 'package:analyzer/src/generated/engine.dart';
19-
import 'package:analyzer/src/generated/resolver.dart';
2019
import 'package:analyzer/src/generated/source.dart';
2120
import 'package:analyzer_plugin/protocol/protocol_common.dart'
2221
show SourceChange, SourceEdit;
@@ -342,9 +341,7 @@ int getExpressionPrecedence(AstNode node) {
342341
* Returns the namespace of the given [ImportElement].
343342
*/
344343
Map<String, Element> getImportNamespace(ImportElement imp) {
345-
NamespaceBuilder builder = new NamespaceBuilder();
346-
Namespace namespace = builder.createImportNamespaceForDirective(imp);
347-
return namespace.definedNames;
344+
return imp.namespace.definedNames;
348345
}
349346

350347
/**

pkg/analysis_server/test/completion_test.dart

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,25 +1606,17 @@ main() {
16061606
extraFiles: sources,
16071607
failingTests: '1');
16081608

1609-
buildTests(
1610-
'test_importPrefix_hideCombinator',
1611-
'''
1609+
buildTests('test_importPrefix_hideCombinator', '''
16121610
import 'dart:math' as math hide PI;
16131611
main() {
16141612
math.!1
1615-
}''',
1616-
<String>["1-PI", "1+LN10"],
1617-
failingTests: '1');
1613+
}''', <String>["1-PI", "1+LN10"]);
16181614

1619-
buildTests(
1620-
'test_importPrefix_showCombinator',
1621-
'''
1615+
buildTests('test_importPrefix_showCombinator', '''
16221616
import 'dart:math' as math show PI;
16231617
main() {
16241618
math.!1
1625-
}''',
1626-
<String>["1+PI", "1-LN10"],
1627-
failingTests: '1');
1619+
}''', <String>["1+PI", "1-LN10"]);
16281620

16291621
sources.clear();
16301622
sources["/lib.dart"] = '''

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,7 @@ main() {
161161
assertNotSuggested('B');
162162
}
163163

164-
@failingTest
165164
test_PrefixedIdentifier_library_import_withShow() async {
166-
// As it is designed now, I think we should not suggest B.
167-
// However an alternative design could be suggesting it, but updating show.
168-
// This could be a part of project-wide suggesting and auto-importing.
169165
addSource('/a.dart', r'''
170166
class A {}
171167
class B {}

pkg/analyzer/lib/dart/element/element.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,11 @@ abstract class ImportElement implements Element, UriReferencedElement {
14571457
*/
14581458
bool get isDeferred;
14591459

1460+
/**
1461+
* The [Namespace] that this directive contributes to the containing library.
1462+
*/
1463+
Namespace get namespace;
1464+
14601465
/**
14611466
* Return the prefix that was specified as part of the import directive, or
14621467
* `null` if there was no prefix specified.

pkg/analyzer/lib/src/dart/analysis/search.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import 'package:analyzer/src/dart/analysis/index.dart';
1414
import 'package:analyzer/src/dart/ast/utilities.dart';
1515
import 'package:analyzer/src/dart/element/element.dart';
1616
import 'package:analyzer/src/dart/element/member.dart';
17-
import 'package:analyzer/src/dart/resolver/scope.dart' show NamespaceBuilder;
1817
import 'package:analyzer/src/summary/idl.dart';
1918
import 'package:collection/collection.dart';
2019

@@ -823,11 +822,7 @@ class _ImportElementReferencesVisitor extends RecursiveAstVisitor {
823822
_ImportElementReferencesVisitor(
824823
ImportElement element, this.enclosingUnitElement)
825824
: importElement = element {
826-
importedElements = new NamespaceBuilder()
827-
.createImportNamespaceForDirective(element)
828-
.definedNames
829-
.values
830-
.toSet();
825+
importedElements = element.namespace.definedNames.values.toSet();
831826
}
832827

833828
@override

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5790,6 +5790,11 @@ class ImportElementImpl extends UriReferencedElementImpl
57905790
*/
57915791
String _selectedUri;
57925792

5793+
/**
5794+
* The cached value of [namespace].
5795+
*/
5796+
Namespace _namespace;
5797+
57935798
/**
57945799
* Initialize a newly created import element at the given [offset].
57955800
* The offset may be `-1` if the import is synthetic.
@@ -5939,6 +5944,12 @@ class ImportElementImpl extends UriReferencedElementImpl
59395944
return offset;
59405945
}
59415946

5947+
@override
5948+
Namespace get namespace {
5949+
return _namespace ??=
5950+
new NamespaceBuilder().createImportNamespaceForDirective(this);
5951+
}
5952+
59425953
PrefixElement get prefix {
59435954
if (_prefix == null) {
59445955
if (_kernel != null && _kernel.name != null) {

pkg/analyzer/lib/src/dart/element/handle.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,9 @@ class ImportElementHandle extends ElementHandle implements ImportElement {
762762
@override
763763
ElementKind get kind => ElementKind.IMPORT;
764764

765+
@override
766+
Namespace get namespace => actualElement.namespace;
767+
765768
@override
766769
PrefixElement get prefix => actualElement.prefix;
767770

pkg/analyzer/lib/src/dart/resolver/scope.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,13 +488,11 @@ class LibraryImportScope extends Scope {
488488
* later reference.
489489
*/
490490
void _createImportedNamespaces() {
491-
NamespaceBuilder builder = new NamespaceBuilder();
492491
List<ImportElement> imports = _definingLibrary.imports;
493492
int count = imports.length;
494493
_importedNamespaces = new List<Namespace>(count);
495494
for (int i = 0; i < count; i++) {
496-
_importedNamespaces[i] =
497-
builder.createImportNamespaceForDirective(imports[i]);
495+
_importedNamespaces[i] = imports[i].namespace;
498496
}
499497
}
500498

0 commit comments

Comments
 (0)