Skip to content

Commit

Permalink
Suggest enum constants for imported enums.
Browse files Browse the repository at this point in the history
So, you can complete `Column(crossAxisAlignment: ^)` and get all of
the CrossAxisAlignment right away for selection. Or you can start
typing `center` and by the time when you typed first letters, you get
several completion suggestions. There are several enums with `center`,
but because we give compatible types higher relevance, the correct
one is always the first.

[email protected], [email protected]

Bug: flutter/flutter-intellij#998
Change-Id: I6c2082668c6b25bf23e070915b5df03081ff0d5c
Reviewed-on: https://dart-review.googlesource.com/43485
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
  • Loading branch information
scheglov authored and [email protected] committed Feb 23, 2018
1 parent d2a1673 commit 316af55
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ class LibraryElementSuggestionBuilder extends GeneralizingElementVisitor
_addConstructorSuggestions(element, relevance);
}
}
if (optype.includeReturnValueSuggestions) {
if (element.isEnum) {
String enumName = element.displayName;
int relevance = optype.returnValueSuggestionsFilter(
element.type, DART_RELEVANCE_DEFAULT);
for (var field in element.fields) {
if (field.isEnumConstant) {
addSuggestion(field,
prefix: prefix,
relevance: relevance,
elementCompletion: '$enumName.${field.name}');
}
}
}
}
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,15 @@ abstract class ElementSuggestionBuilder {
* Add a suggestion based upon the given element.
*/
void addSuggestion(Element element,
{String prefix, int relevance: DART_RELEVANCE_DEFAULT}) {
{String prefix,
int relevance: DART_RELEVANCE_DEFAULT,
String elementCompletion}) {
if (element.isPrivate) {
if (element.library != containingLibrary) {
return;
}
}
String completion = element.displayName;
String completion = elementCompletion ?? element.displayName;
if (prefix != null && prefix.length > 0) {
if (completion == null || completion.length <= 0) {
completion = prefix;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,33 @@ int myFunc() {}
assertNotSuggested('two');
}

test_enum_filter() async {
addSource('/a.dart', '''
enum E { one, two }
enum F { three, four }
''');
addTestSource('''
import 'a.dart';
void foo({E e}) {}
main() {
foo(e: ^);
}
''');
await computeSuggestions();

assertSuggestEnum('E');
assertSuggestEnumConst('E.one',
relevance: DART_RELEVANCE_DEFAULT + DART_RELEVANCE_BOOST_TYPE);
assertSuggestEnumConst('E.two',
relevance: DART_RELEVANCE_DEFAULT + DART_RELEVANCE_BOOST_TYPE);

assertSuggestEnum('F');
assertSuggestEnumConst('F.three');
assertSuggestEnumConst('F.four');
}

test_ExpressionStatement_identifier() async {
// SimpleIdentifier ExpressionStatement Block
resolveSource('/testA.dart', '''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2167,10 +2167,13 @@ main() {
''');
await computeSuggestions();

assertSuggestEnum('E');
assertSuggestEnumConst('E.one',
relevance: DART_RELEVANCE_DEFAULT + DART_RELEVANCE_BOOST_TYPE);
assertSuggestEnumConst('E.two',
relevance: DART_RELEVANCE_DEFAULT + DART_RELEVANCE_BOOST_TYPE);

assertSuggestEnum('F');
assertSuggestEnumConst('F.three');
assertSuggestEnumConst('F.four');
}
Expand Down

0 comments on commit 316af55

Please sign in to comment.