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

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

Issue 1113063005: improve import suggestion (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: merge Created 5 years, 7 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.keyword; 5 library services.completion.contributor.dart.keyword;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:analysis_server/src/protocol.dart'; 9 import 'package:analysis_server/src/protocol.dart';
10 import 'package:analysis_server/src/services/completion/dart_completion_manager. dart'; 10 import 'package:analysis_server/src/services/completion/dart_completion_manager. dart';
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 // then the user is probably finishing that 100 // then the user is probably finishing that
101 _addImportDirectiveKeywords(previousMember); 101 _addImportDirectiveKeywords(previousMember);
102 return; 102 return;
103 } 103 }
104 } 104 }
105 if (previousMember == null || previousMember is Directive) { 105 if (previousMember == null || previousMember is Directive) {
106 if (previousMember == null && 106 if (previousMember == null &&
107 !node.directives.any((d) => d is LibraryDirective)) { 107 !node.directives.any((d) => d is LibraryDirective)) {
108 _addSuggestions([Keyword.LIBRARY], DART_RELEVANCE_HIGH); 108 _addSuggestions([Keyword.LIBRARY], DART_RELEVANCE_HIGH);
109 } 109 }
110 _addSuggestions( 110 _addSuggestions([Keyword.EXPORT, Keyword.PART], DART_RELEVANCE_HIGH);
111 [Keyword.EXPORT, Keyword.IMPORT, Keyword.PART], DART_RELEVANCE_HIGH); 111 _addSuggestion2("import '';",
112 offset: 8, relevance: DART_RELEVANCE_HIGH + 1);
113 _addSuggestion2("import '' as ;",
114 offset: 8, relevance: DART_RELEVANCE_HIGH);
115 _addSuggestion2("import '' hide ;",
116 offset: 8, relevance: DART_RELEVANCE_HIGH);
117 _addSuggestion2("import '' show ;",
118 offset: 8, relevance: DART_RELEVANCE_HIGH);
112 } 119 }
113 if (entity == null || entity is Declaration) { 120 if (entity == null || entity is Declaration) {
114 if (previousMember is FunctionDeclaration && 121 if (previousMember is FunctionDeclaration &&
115 previousMember.functionExpression is FunctionExpression && 122 previousMember.functionExpression is FunctionExpression &&
116 previousMember.functionExpression.body is EmptyFunctionBody) { 123 previousMember.functionExpression.body is EmptyFunctionBody) {
117 _addSuggestion2(ASYNC, DART_RELEVANCE_HIGH); 124 _addSuggestion2(ASYNC, relevance: DART_RELEVANCE_HIGH);
118 } 125 }
119 _addCompilationUnitKeywords(); 126 _addCompilationUnitKeywords();
120 } 127 }
121 } 128 }
122 129
123 @override 130 @override
124 visitExpressionFunctionBody(ExpressionFunctionBody node) { 131 visitExpressionFunctionBody(ExpressionFunctionBody node) {
125 if (entity == node.expression) { 132 if (entity == node.expression) {
126 _addExpressionKeywords(node); 133 _addExpressionKeywords(node);
127 } 134 }
128 } 135 }
129 136
130 @override 137 @override
131 visitFormalParameterList(FormalParameterList node) { 138 visitFormalParameterList(FormalParameterList node) {
132 AstNode constructorDecl = 139 AstNode constructorDecl =
133 node.getAncestor((p) => p is ConstructorDeclaration); 140 node.getAncestor((p) => p is ConstructorDeclaration);
134 if (constructorDecl != null) { 141 if (constructorDecl != null) {
135 _addSuggestions([Keyword.THIS]); 142 _addSuggestions([Keyword.THIS]);
136 } 143 }
137 } 144 }
138 145
139 @override 146 @override
140 visitFunctionExpression(FunctionExpression node) { 147 visitFunctionExpression(FunctionExpression node) {
141 if (entity == node.body) { 148 if (entity == node.body) {
142 _addSuggestion2(ASYNC, DART_RELEVANCE_HIGH); 149 _addSuggestion2(ASYNC, relevance: DART_RELEVANCE_HIGH);
143 if (node.body is EmptyFunctionBody && 150 if (node.body is EmptyFunctionBody &&
144 node.parent is FunctionDeclaration && 151 node.parent is FunctionDeclaration &&
145 node.parent.parent is CompilationUnit) { 152 node.parent.parent is CompilationUnit) {
146 _addCompilationUnitKeywords(); 153 _addCompilationUnitKeywords();
147 } 154 }
148 } 155 }
149 } 156 }
150 157
151 @override 158 @override
152 visitIfStatement(IfStatement node) { 159 visitIfStatement(IfStatement node) {
(...skipping 14 matching lines...) Expand all
167 } 174 }
168 } 175 }
169 176
170 @override 177 @override
171 visitMethodDeclaration(MethodDeclaration node) { 178 visitMethodDeclaration(MethodDeclaration node) {
172 if (entity == node.body) { 179 if (entity == node.body) {
173 if (node.body is EmptyFunctionBody) { 180 if (node.body is EmptyFunctionBody) {
174 _addClassBodyKeywords(); 181 _addClassBodyKeywords();
175 _addSuggestion2(ASYNC); 182 _addSuggestion2(ASYNC);
176 } else { 183 } else {
177 _addSuggestion2(ASYNC, DART_RELEVANCE_HIGH); 184 _addSuggestion2(ASYNC, relevance: DART_RELEVANCE_HIGH);
178 } 185 }
179 } 186 }
180 } 187 }
181 188
182 @override 189 @override
183 visitNamedExpression(NamedExpression node) { 190 visitNamedExpression(NamedExpression node) {
184 if (entity is SimpleIdentifier && entity == node.expression) { 191 if (entity is SimpleIdentifier && entity == node.expression) {
185 _addExpressionKeywords(node); 192 _addExpressionKeywords(node);
186 } 193 }
187 } 194 }
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 Keyword.THROW, 308 Keyword.THROW,
302 Keyword.TRY, 309 Keyword.TRY,
303 Keyword.VAR, 310 Keyword.VAR,
304 Keyword.VOID, 311 Keyword.VOID,
305 Keyword.WHILE 312 Keyword.WHILE
306 ]); 313 ]);
307 } 314 }
308 315
309 void _addSuggestion(Keyword keyword, 316 void _addSuggestion(Keyword keyword,
310 [int relevance = DART_RELEVANCE_KEYWORD]) { 317 [int relevance = DART_RELEVANCE_KEYWORD]) {
311 _addSuggestion2(keyword.syntax, relevance); 318 _addSuggestion2(keyword.syntax, relevance: relevance);
312 } 319 }
313 320
314 void _addSuggestion2(String completion, 321 void _addSuggestion2(String completion,
315 [int relevance = DART_RELEVANCE_KEYWORD]) { 322 {int offset, int relevance: DART_RELEVANCE_KEYWORD}) {
323 if (offset == null) {
324 offset = completion.length;
325 }
316 request.addSuggestion(new CompletionSuggestion( 326 request.addSuggestion(new CompletionSuggestion(
317 CompletionSuggestionKind.KEYWORD, relevance, completion, 327 CompletionSuggestionKind.KEYWORD, relevance, completion, offset, 0,
318 completion.length, 0, false, false)); 328 false, false));
319 } 329 }
320 330
321 void _addSuggestions(List<Keyword> keywords, 331 void _addSuggestions(List<Keyword> keywords,
322 [int relevance = DART_RELEVANCE_KEYWORD]) { 332 [int relevance = DART_RELEVANCE_KEYWORD]) {
323 keywords.forEach((Keyword keyword) { 333 keywords.forEach((Keyword keyword) {
324 _addSuggestion(keyword, relevance); 334 _addSuggestion(keyword, relevance);
325 }); 335 });
326 } 336 }
327 337
328 bool _inAsyncMethodOrFunction(AstNode node) { 338 bool _inAsyncMethodOrFunction(AstNode node) {
329 FunctionBody body = node.getAncestor((n) => n is FunctionBody); 339 FunctionBody body = node.getAncestor((n) => n is FunctionBody);
330 return body != null && body.isAsynchronous; 340 return body != null && body.isAsynchronous;
331 } 341 }
332 342
333 bool _inClassMemberBody(AstNode node) { 343 bool _inClassMemberBody(AstNode node) {
334 while (true) { 344 while (true) {
335 AstNode body = node.getAncestor((n) => n is FunctionBody); 345 AstNode body = node.getAncestor((n) => n is FunctionBody);
336 if (body == null) { 346 if (body == null) {
337 return false; 347 return false;
338 } 348 }
339 AstNode parent = body.parent; 349 AstNode parent = body.parent;
340 if (parent is ConstructorDeclaration || parent is MethodDeclaration) { 350 if (parent is ConstructorDeclaration || parent is MethodDeclaration) {
341 return true; 351 return true;
342 } 352 }
343 node = parent; 353 node = parent;
344 } 354 }
345 } 355 }
346 } 356 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698