Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: pkg/analysis_server/lib/src/services/completion/prefixed_element_contributor.dart

Issue 1082233003: suggest "this." local fields in constructor param (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: merge Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library services.completion.contributor.dart.invocation; 5 library services.completion.contributor.dart.invocation;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:analysis_server/src/services/completion/dart_completion_manager. dart'; 9 import 'package:analysis_server/src/services/completion/dart_completion_manager. dart';
10 import 'package:analysis_server/src/services/completion/local_declaration_visito r.dart'; 10 import 'package:analysis_server/src/services/completion/local_declaration_visito r.dart';
11 import 'package:analysis_server/src/services/completion/local_suggestion_builder .dart';
11 import 'package:analysis_server/src/services/completion/optype.dart'; 12 import 'package:analysis_server/src/services/completion/optype.dart';
12 import 'package:analysis_server/src/services/completion/suggestion_builder.dart' ; 13 import 'package:analysis_server/src/services/completion/suggestion_builder.dart' ;
13 import 'package:analyzer/src/generated/ast.dart'; 14 import 'package:analyzer/src/generated/ast.dart';
14 import 'package:analyzer/src/generated/element.dart'; 15 import 'package:analyzer/src/generated/element.dart';
15 16
16 import '../../protocol_server.dart' as protocol;
17 import '../../protocol_server.dart' 17 import '../../protocol_server.dart'
18 show CompletionSuggestion, CompletionSuggestionKind; 18 show CompletionSuggestion, CompletionSuggestionKind;
19 import '../../protocol_server.dart' as protocol;
19 20
20 /** 21 /**
21 * A contributor for calculating invocation / access suggestions 22 * A contributor for calculating invocation / access suggestions
22 * `completion.getSuggestions` request results. 23 * `completion.getSuggestions` request results.
23 */ 24 */
24 class PrefixedElementContributor extends DartCompletionContributor { 25 class PrefixedElementContributor extends DartCompletionContributor {
25 SuggestionBuilder builder; 26 SuggestionBuilder builder;
26 27
27 @override 28 @override
28 bool computeFast(DartCompletionRequest request) { 29 bool computeFast(DartCompletionRequest request) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 } 74 }
74 if (node is Expression) { 75 if (node is Expression) {
75 InterfaceTypeSuggestionBuilder.suggestionsFor(request, node.bestType); 76 InterfaceTypeSuggestionBuilder.suggestionsFor(request, node.bestType);
76 return new Future.value(true); 77 return new Future.value(true);
77 } 78 }
78 return new Future.value(false); 79 return new Future.value(false);
79 } 80 }
80 } 81 }
81 82
82 /** 83 /**
84 * A suggestion builder for 'this.' constructor arguments.
85 */
86 class _FieldFormalSuggestionBuilder implements SuggestionBuilder {
87 final DartCompletionRequest request;
88
89 _FieldFormalSuggestionBuilder(this.request);
90
91 @override
92 bool computeFast(AstNode node) {
93 if (node is FieldFormalParameter) {
94
95 // Compute fields already referenced
96 ConstructorDeclaration constructorDecl =
97 node.getAncestor((p) => p is ConstructorDeclaration);
98 List<String> referencedFields = new List<String>();
99 for (FormalParameter param in constructorDecl.parameters.parameters) {
100 if (param is FieldFormalParameter) {
101 SimpleIdentifier fieldId = param.identifier;
102 if (fieldId != null && fieldId != request.target.entity) {
103 String fieldName = fieldId.name;
104 if (fieldName != null && fieldName.length > 0) {
105 referencedFields.add(fieldName);
106 }
107 }
108 }
109 }
110
111 // Add suggestions for fields that are not already referenced
112 ClassDeclaration classDecl =
113 constructorDecl.getAncestor((p) => p is ClassDeclaration);
114 for (ClassMember member in classDecl.members) {
115 if (member is FieldDeclaration) {
116 for (VariableDeclaration varDecl in member.fields.variables) {
117 SimpleIdentifier fieldId = varDecl.name;
118 if (fieldId != null) {
119 String fieldName = fieldId.name;
120 if (fieldName != null && fieldName.length > 0) {
121 if (!referencedFields.contains(fieldName)) {
122 CompletionSuggestion suggestion =
123 createFieldSuggestion(member, varDecl);
124 if (suggestion != null) {
125 request.addSuggestion(suggestion);
126 }
127 }
128 }
129 }
130 }
131 }
132 }
133 } else {
134 // This should never be called with a case not handled above.
135 assert(false);
136 }
137 return true;
138 }
139
140 @override
141 Future<bool> computeFull(AstNode node) {
142 // This should never be called; we should always be able to compute
143 // suggestions and return true in computeFast method.
144 assert(false);
145 return null;
146 }
147 }
148
149 /**
83 * An [AstNode] vistor for determining which suggestion builder 150 * An [AstNode] vistor for determining which suggestion builder
84 * should be used to build invocation/access suggestions. 151 * should be used to build invocation/access suggestions.
85 */ 152 */
86 class _InvocationAstVisitor extends GeneralizingAstVisitor<SuggestionBuilder> { 153 class _InvocationAstVisitor extends GeneralizingAstVisitor<SuggestionBuilder> {
87 final DartCompletionRequest request; 154 final DartCompletionRequest request;
88 155
89 _InvocationAstVisitor(this.request); 156 _InvocationAstVisitor(this.request);
90 157
91 @override 158 @override
92 SuggestionBuilder visitConstructorName(ConstructorName node) { 159 SuggestionBuilder visitConstructorName(ConstructorName node) {
93 // some PrefixedIdentifier nodes are transformed into 160 // some PrefixedIdentifier nodes are transformed into
94 // ConstructorName nodes during the resolution process. 161 // ConstructorName nodes during the resolution process.
95 return new _PrefixedIdentifierSuggestionBuilder(request); 162 return new _PrefixedIdentifierSuggestionBuilder(request);
96 } 163 }
97 164
98 @override 165 @override
166 SuggestionBuilder visitFieldFormalParameter(FieldFormalParameter node) {
167 return new _FieldFormalSuggestionBuilder(request);
168 }
169
170 @override
99 SuggestionBuilder visitMethodInvocation(MethodInvocation node) { 171 SuggestionBuilder visitMethodInvocation(MethodInvocation node) {
100 return new _ExpressionSuggestionBuilder(request); 172 return new _ExpressionSuggestionBuilder(request);
101 } 173 }
102 174
103 @override 175 @override
104 SuggestionBuilder visitNode(AstNode node) { 176 SuggestionBuilder visitNode(AstNode node) {
105 return null; 177 return null;
106 } 178 }
107 179
108 @override 180 @override
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 } 423 }
352 return new Future.value(false); 424 return new Future.value(false);
353 } 425 }
354 426
355 @override 427 @override
356 Future<bool> visitVariableElement(VariableElement element) { 428 Future<bool> visitVariableElement(VariableElement element) {
357 InterfaceTypeSuggestionBuilder.suggestionsFor(request, element.type); 429 InterfaceTypeSuggestionBuilder.suggestionsFor(request, element.type);
358 return new Future.value(true); 430 return new Future.value(true);
359 } 431 }
360 } 432 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698