Skip to content

Commit 9860866

Browse files
bwilkersonCommit Queue
authored and
Commit Queue
committed
Move more of KeywordContributor to the new framework
Change-Id: I2b257545a3af7fa2f803e8f954d14cd3679c35c1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/319781 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent fc35778 commit 9860866

File tree

4 files changed

+238
-190
lines changed

4 files changed

+238
-190
lines changed

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

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
102102
_forExpression(node);
103103
}
104104

105+
@override
106+
void visitBooleanLiteral(BooleanLiteral node) {
107+
_forExpression(node);
108+
}
109+
105110
@override
106111
void visitClassDeclaration(ClassDeclaration node) {
107112
if (offset < node.classKeyword.offset) {
@@ -141,11 +146,99 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
141146
}
142147
}
143148

149+
@override
150+
void visitConstructorDeclaration(ConstructorDeclaration node) {
151+
var separator = node.separator;
152+
if (separator != null) {
153+
if (offset >= separator.end && offset <= node.body.offset) {
154+
collector.completionLocation = 'ConstructorDeclaration_initializer';
155+
keywordHelper.addConstructorInitializerKeywords(node);
156+
}
157+
}
158+
}
159+
144160
@override
145161
void visitDoubleLiteral(DoubleLiteral node) {
146162
_visitParentIfAtOrBeforeNode(node);
147163
}
148164

165+
@override
166+
void visitEnumDeclaration(EnumDeclaration node) {
167+
if (!featureSet.isEnabled(Feature.enhanced_enums)) {
168+
return;
169+
}
170+
if (offset < node.enumKeyword.offset) {
171+
// There are no modifiers for enums.
172+
return;
173+
}
174+
if (offset <= node.enumKeyword.end) {
175+
keywordHelper.addKeyword(Keyword.ENUM);
176+
return;
177+
}
178+
if (offset <= node.name.end) {
179+
// TODO(brianwilkerson) Suggest a name for the mixin.
180+
return;
181+
}
182+
if (offset <= node.leftBracket.offset) {
183+
keywordHelper.addEnumDeclarationKeywords(node);
184+
return;
185+
}
186+
var rightBracket = node.rightBracket;
187+
if (!rightBracket.isSynthetic && offset >= rightBracket.end) {
188+
return;
189+
}
190+
var semicolon = node.semicolon;
191+
if (semicolon != null && offset >= semicolon.end) {
192+
collector.completionLocation = 'EnumDeclaration_member';
193+
_forEnumMember();
194+
}
195+
}
196+
197+
@override
198+
void visitExtensionDeclaration(ExtensionDeclaration node) {
199+
if (offset < node.extensionKeyword.offset) {
200+
// There are no modifiers for extensions.
201+
return;
202+
}
203+
if (offset <= node.extensionKeyword.end) {
204+
keywordHelper.addKeyword(Keyword.EXTENSION);
205+
return;
206+
}
207+
var name = node.name;
208+
if (name != null && offset <= name.end) {
209+
// TODO(brianwilkerson) We probably need to suggest `on`.
210+
// TODO(brianwilkerson) We probably need to suggest `type` when extension
211+
// types are supported.
212+
// Don't suggest a name for the extension.
213+
return;
214+
}
215+
if (offset <= node.leftBracket.offset) {
216+
if (node.onKeyword.isSynthetic) {
217+
keywordHelper.addExtensionDeclarationKeywords(node);
218+
} else {
219+
collector.completionLocation = 'ExtensionDeclaration_extendedType';
220+
}
221+
return;
222+
}
223+
if (offset >= node.leftBracket.end && offset <= node.rightBracket.offset) {
224+
collector.completionLocation = 'ExtensionDeclaration_member';
225+
_forExtensionMember();
226+
}
227+
}
228+
229+
@override
230+
void visitFunctionDeclaration(FunctionDeclaration node) {
231+
// If the cursor is at the beginning of the declaration, include the
232+
// compilation unit keywords. See dartbug.com/41039.
233+
var returnType = node.returnType;
234+
if ((returnType == null || returnType.beginToken == returnType.endToken) &&
235+
offset <= node.name.offset) {
236+
collector.completionLocation = 'FunctionDeclaration_returnType';
237+
keywordHelper.addKeyword(Keyword.DYNAMIC);
238+
keywordHelper.addKeyword(Keyword.VOID);
239+
}
240+
}
241+
149242
@override
150243
void visitIntegerLiteral(IntegerLiteral node) {
151244
_visitParentIfAtOrBeforeNode(node);
@@ -204,6 +297,42 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
204297
_forPattern();
205298
}
206299

300+
@override
301+
void visitMixinDeclaration(MixinDeclaration node) {
302+
if (offset < node.mixinKeyword.offset) {
303+
keywordHelper.addMixinModifiers(node);
304+
return;
305+
}
306+
if (offset <= node.mixinKeyword.end) {
307+
keywordHelper.addKeyword(Keyword.MIXIN);
308+
return;
309+
}
310+
if (offset <= node.name.end) {
311+
// TODO(brianwilkerson) Suggest a name for the mixin.
312+
return;
313+
}
314+
if (offset <= node.leftBracket.offset) {
315+
keywordHelper.addMixinDeclarationKeywords(node);
316+
return;
317+
}
318+
if (offset >= node.leftBracket.end && offset <= node.rightBracket.offset) {
319+
collector.completionLocation = 'MixinDeclaration_member';
320+
_forMixinMember();
321+
var element = node.members.elementBefore(offset);
322+
if (element is MethodDeclaration) {
323+
var body = element.body;
324+
if (body.isEmpty) {
325+
keywordHelper.addFunctionBodyModifiers(body);
326+
}
327+
}
328+
}
329+
}
330+
331+
@override
332+
void visitNullLiteral(NullLiteral node) {
333+
_forExpression(node);
334+
}
335+
207336
@override
208337
void visitParenthesizedPattern(ParenthesizedPattern node) {
209338
collector.completionLocation = 'ParenthesizedPattern_expression';
@@ -274,6 +403,53 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
274403
_visitParentIfAtOrBeforeNode(node);
275404
}
276405

406+
@override
407+
void visitSymbolLiteral(SymbolLiteral node) {
408+
_forExpression(node);
409+
}
410+
411+
@override
412+
void visitThisExpression(ThisExpression node) {
413+
_forExpression(node);
414+
}
415+
416+
@override
417+
void visitThrowExpression(ThrowExpression node) {
418+
collector.completionLocation = 'ThrowExpression_expression';
419+
_forExpression(node);
420+
}
421+
422+
@override
423+
void visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) {
424+
var variableDeclarationList = node.variables;
425+
var variables = variableDeclarationList.variables;
426+
if (variables.isEmpty || offset > variables.first.beginToken.end) {
427+
return;
428+
}
429+
if (node.externalKeyword == null) {
430+
keywordHelper.addKeyword(Keyword.EXTERNAL);
431+
}
432+
if (variableDeclarationList.lateKeyword == null &&
433+
featureSet.isEnabled(Feature.non_nullable)) {
434+
keywordHelper.addKeyword(Keyword.LATE);
435+
}
436+
if (!variables.first.isConst) {
437+
keywordHelper.addKeyword(Keyword.CONST);
438+
}
439+
if (!variables.first.isFinal) {
440+
keywordHelper.addKeyword(Keyword.FINAL);
441+
}
442+
}
443+
444+
@override
445+
void visitVariableDeclaration(VariableDeclaration node) {
446+
var equals = node.equals;
447+
if (equals != null && offset >= equals.end) {
448+
collector.completionLocation = 'VariableDeclaration_initializer';
449+
_forExpression(node);
450+
}
451+
}
452+
277453
/// Add the suggestions that are appropriate when the selection is at the
278454
/// beginning of a class member.
279455
void _forClassMember() {
@@ -304,6 +480,12 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
304480
// _addVariablesInScope(node, mustBeConstant: true);
305481
}
306482

483+
/// Add the suggestions that are appropriate when the selection is at the
484+
/// beginning of an enum member.
485+
void _forEnumMember() {
486+
keywordHelper.addEnumMemberKeywords();
487+
}
488+
307489
/// Add the suggestions that are appropriate when the selection is at the
308490
/// beginning of an expression. The [node] provides context to determine which
309491
/// keywords to include.
@@ -314,6 +496,18 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
314496
// _addVariablesInScope(node);
315497
}
316498

499+
/// Add the suggestions that are appropriate when the selection is at the
500+
/// beginning of an extension member.
501+
void _forExtensionMember() {
502+
keywordHelper.addExtensionMemberKeywords();
503+
}
504+
505+
/// Add the suggestions that are appropriate when the selection is at the
506+
/// beginning of a mixin member.
507+
void _forMixinMember() {
508+
keywordHelper.addMixinMemberKeywords();
509+
}
510+
317511
/// Add the suggestions that are appropriate when the selection is at the
318512
/// beginning of a pattern.
319513
void _forPattern() {

0 commit comments

Comments
 (0)