Skip to content

Commit e3af3c8

Browse files
committed
Code completion improvement. Recommend all 'E.x' where E is an enum, also, filter out only the correct enum types in named arguments.
BUG=#24759 Review URL: https://codereview.chromium.org/1989393002 .
1 parent 6c778ca commit e3af3c8

File tree

5 files changed

+106
-8
lines changed

5 files changed

+106
-8
lines changed

pkg/analysis_server/lib/src/provisional/completion/dart/completion_dart.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export 'package:analysis_server/src/provisional/completion/completion_core.dart'
2020
const int DART_RELEVANCE_COMMON_USAGE = 1200;
2121
const int DART_RELEVANCE_DEFAULT = 1000;
2222
const int DART_RELEVANCE_HIGH = 2000;
23-
const int DART_RELEVANCE_INCREMENT = 20;
23+
const int DART_RELEVANCE_INCREMENT = 100;
2424
const int DART_RELEVANCE_INHERITED_ACCESSOR = 1057;
2525
const int DART_RELEVANCE_INHERITED_FIELD = 1058;
2626
const int DART_RELEVANCE_INHERITED_METHOD = 1057;

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,13 @@ class _LocalVisitor extends LocalDeclarationVisitor {
256256
_addLocalSuggestion_includeTypeNameSuggestions(
257257
declaration.name, NO_RETURN_TYPE, protocol.ElementKind.ENUM,
258258
isDeprecated: _isDeprecated(declaration));
259+
for (EnumConstantDeclaration enumConstant in declaration.constants) {
260+
if (!enumConstant.isSynthetic) {
261+
_addLocalSuggestion_includeReturnValueSuggestions_enumConstant(
262+
enumConstant, declaration,
263+
isDeprecated: _isDeprecated(declaration));
264+
}
265+
}
259266
}
260267
}
261268

@@ -419,6 +426,42 @@ class _LocalVisitor extends LocalDeclarationVisitor {
419426
}
420427
}
421428

429+
void _addLocalSuggestion_enumConstant(
430+
EnumConstantDeclaration constantDeclaration,
431+
EnumDeclaration enumDeclaration,
432+
{bool isAbstract: false,
433+
bool isDeprecated: false,
434+
ClassDeclaration classDecl,
435+
int relevance: DART_RELEVANCE_DEFAULT}) {
436+
String completion =
437+
'${enumDeclaration.name.name}.${constantDeclaration.name.name}';
438+
CompletionSuggestion suggestion = new CompletionSuggestion(
439+
CompletionSuggestionKind.INVOCATION,
440+
isDeprecated ? DART_RELEVANCE_LOW : relevance,
441+
completion,
442+
completion.length,
443+
0,
444+
isDeprecated,
445+
false,
446+
returnType: enumDeclaration.name.name);
447+
448+
suggestionMap.putIfAbsent(suggestion.completion, () => suggestion);
449+
int flags = protocol.Element.makeFlags(
450+
isAbstract: isAbstract,
451+
isDeprecated: isDeprecated,
452+
isPrivate: Identifier.isPrivateName(constantDeclaration.name.name));
453+
suggestion.element = new protocol.Element(
454+
protocol.ElementKind.ENUM_CONSTANT,
455+
constantDeclaration.name.name,
456+
flags,
457+
location: new Location(
458+
request.source.fullName,
459+
constantDeclaration.name.offset,
460+
constantDeclaration.name.length,
461+
0,
462+
0));
463+
}
464+
422465
void _addLocalSuggestion_includeReturnValueSuggestions(
423466
SimpleIdentifier id, TypeName typeName, protocol.ElementKind elemKind,
424467
{bool isAbstract: false,
@@ -438,6 +481,22 @@ class _LocalVisitor extends LocalDeclarationVisitor {
438481
}
439482
}
440483

484+
void _addLocalSuggestion_includeReturnValueSuggestions_enumConstant(
485+
EnumConstantDeclaration constantDeclaration,
486+
EnumDeclaration enumDeclaration,
487+
{bool isAbstract: false,
488+
bool isDeprecated: false,
489+
int relevance: DART_RELEVANCE_DEFAULT}) {
490+
relevance = optype.returnValueSuggestionsFilter(
491+
enumDeclaration.element.type, relevance);
492+
if (relevance != null) {
493+
_addLocalSuggestion_enumConstant(constantDeclaration, enumDeclaration,
494+
isAbstract: isAbstract,
495+
isDeprecated: isDeprecated,
496+
relevance: relevance);
497+
}
498+
}
499+
441500
void _addLocalSuggestion_includeTypeNameSuggestions(
442501
SimpleIdentifier id, TypeName typeName, protocol.ElementKind elemKind,
443502
{bool isAbstract: false,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,16 @@ class _OpTypeAstVisitor extends GeneralizingAstVisitor {
641641
optype.includeReturnValueSuggestions = true;
642642
optype.returnValueSuggestionsFilter = (DartType dartType, int relevance) {
643643
DartType type = node.element?.type;
644+
bool isEnum = type != null &&
645+
type.element is ClassElement &&
646+
(type.element as ClassElement).isEnum;
647+
if (isEnum) {
648+
if (type == dartType) {
649+
return relevance + DART_RELEVANCE_INCREMENT;
650+
} else {
651+
return null;
652+
}
653+
}
644654
if (type != null &&
645655
dartType != null &&
646656
!type.isDynamic &&

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,10 @@ abstract class DartCompletionContributorTest extends AbstractContextTest {
216216
}
217217

218218
CompletionSuggestion assertSuggestEnumConst(String completion,
219-
{bool isDeprecated: false}) {
219+
{int relevance: DART_RELEVANCE_DEFAULT, bool isDeprecated: false}) {
220220
CompletionSuggestion suggestion =
221-
assertSuggest(completion, isDeprecated: isDeprecated);
221+
assertSuggest(completion, relevance: relevance, isDeprecated: isDeprecated);
222+
expect(suggestion.completion, completion);
222223
expect(suggestion.isDeprecated, isDeprecated);
223224
expect(suggestion.element.kind, protocol.ElementKind.ENUM_CONSTANT);
224225
return suggestion;

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

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -355,21 +355,19 @@ void main() {expect(foo: ^)}''');
355355
expect(replacementLength, 0);
356356
assertSuggestTopLevelVar('a', 'A',
357357
relevance:
358-
DART_RELEVANCE_LOCAL_TOP_LEVEL_VARIABLE + DART_RELEVANCE_INCREMENT);
358+
DART_RELEVANCE_LOCAL_TOP_LEVEL_VARIABLE + DART_RELEVANCE_INCREMENT);
359359
assertSuggestTopLevelVar('b', 'B',
360360
relevance:
361-
DART_RELEVANCE_LOCAL_TOP_LEVEL_VARIABLE + DART_RELEVANCE_INCREMENT);
361+
DART_RELEVANCE_LOCAL_TOP_LEVEL_VARIABLE + DART_RELEVANCE_INCREMENT);
362362
assertSuggestTopLevelVar('c', 'C',
363363
relevance:
364-
DART_RELEVANCE_LOCAL_TOP_LEVEL_VARIABLE + DART_RELEVANCE_INCREMENT);
364+
DART_RELEVANCE_LOCAL_TOP_LEVEL_VARIABLE + DART_RELEVANCE_INCREMENT);
365365
assertSuggestTopLevelVar('d', 'D',
366366
relevance: DART_RELEVANCE_LOCAL_TOP_LEVEL_VARIABLE);
367367
assertSuggestTopLevelVar('e', 'E',
368368
relevance: DART_RELEVANCE_LOCAL_TOP_LEVEL_VARIABLE);
369369
}
370370

371-
372-
373371
test_AsExpression_type() async {
374372
// SimpleIdentifier TypeName AsExpression
375373
addTestSource('''
@@ -1929,6 +1927,8 @@ class A {a(blat: ^) { }}''');
19291927
addTestSource('enum E { one, two } main() {^}');
19301928
await computeSuggestions();
19311929
assertSuggestEnum('E');
1930+
assertSuggestEnumConst('E.one');
1931+
assertSuggestEnumConst('E.two');
19321932
assertNotSuggested('one');
19331933
assertNotSuggested('two');
19341934
}
@@ -1937,10 +1937,38 @@ class A {a(blat: ^) { }}''');
19371937
addTestSource('@deprecated enum E { one, two } main() {^}');
19381938
await computeSuggestions();
19391939
assertSuggestEnum('E', isDeprecated: true);
1940+
assertSuggestEnumConst('E.one', isDeprecated: true);
1941+
assertSuggestEnumConst('E.two', isDeprecated: true);
19401942
assertNotSuggested('one');
19411943
assertNotSuggested('two');
19421944
}
19431945

1946+
test_enum_filter() async {
1947+
// SimpleIdentifier NamedExpression ArgumentList
1948+
// InstanceCreationExpression
1949+
addTestSource('''
1950+
enum E { one, two }
1951+
enum F { three, four }
1952+
class A {}
1953+
class B {
1954+
B({E someE});
1955+
}
1956+
A a = new A();
1957+
B b = new B(someE: ^);
1958+
''');
1959+
await computeSuggestions();
1960+
1961+
expect(replacementOffset, completionOffset);
1962+
expect(replacementLength, 0);
1963+
assertSuggestEnumConst('E.one',
1964+
relevance: DART_RELEVANCE_DEFAULT + DART_RELEVANCE_INCREMENT);
1965+
assertSuggestEnumConst('E.two',
1966+
relevance: DART_RELEVANCE_DEFAULT + DART_RELEVANCE_INCREMENT);
1967+
assertNotSuggested('a');
1968+
assertNotSuggested('F.three');
1969+
assertNotSuggested('F.four');
1970+
}
1971+
19441972
test_ExpressionStatement_identifier() async {
19451973
// SimpleIdentifier ExpressionStatement Block
19461974
addSource(

0 commit comments

Comments
 (0)