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

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

Issue 634653002: add named constructor, cascade selector suggestions and more tests (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 2 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.computer.dart.invocation; 5 library services.completion.computer.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/suggestion_builder.dart' ; 10 import 'package:analysis_server/src/services/completion/suggestion_builder.dart' ;
11 import 'package:analyzer/src/generated/ast.dart'; 11 import 'package:analyzer/src/generated/ast.dart';
12 import 'package:analyzer/src/generated/element.dart'; 12 import 'package:analyzer/src/generated/element.dart';
13 import 'package:analyzer/src/generated/scanner.dart';
13 14
14 /** 15 /**
15 * A computer for calculating invocation / access suggestions 16 * A computer for calculating invocation / access suggestions
16 * `completion.getSuggestions` request results. 17 * `completion.getSuggestions` request results.
17 */ 18 */
18 class InvocationComputer extends DartCompletionComputer { 19 class InvocationComputer extends DartCompletionComputer {
19 20
20 @override 21 @override
21 bool computeFast(DartCompletionRequest request) { 22 bool computeFast(DartCompletionRequest request) {
22 // TODO: implement computeFast 23 // TODO: implement computeFast
23 return false; 24 return false;
24 } 25 }
25 26
26 @override 27 @override
27 Future<bool> computeFull(DartCompletionRequest request) { 28 Future<bool> computeFull(DartCompletionRequest request) {
28 return request.node.accept(new _InvocationAstVisitor(request)); 29 return request.node.accept(new _InvocationAstVisitor(request));
29 } 30 }
30 } 31 }
31 32
32 /** 33 /**
33 * An [AstNode] vistor for determining the appropriate invocation/access 34 * An [AstNode] vistor for determining the appropriate invocation/access
34 * suggestions based upon the node in which the completion is requested. 35 * suggestions based upon the node in which the completion is requested.
35 */ 36 */
36 class _InvocationAstVisitor extends GeneralizingAstVisitor<Future<bool>> { 37 class _InvocationAstVisitor extends GeneralizingAstVisitor<Future<bool>> {
37 final DartCompletionRequest request; 38 final DartCompletionRequest request;
38 AstNode completionNode;
39 39
40 _InvocationAstVisitor(this.request); 40 _InvocationAstVisitor(this.request);
41 41
42 @override 42 @override
43 Future<bool> visitConstructorName(ConstructorName node) {
44 // SimpleIdentifier PrefixedIdentifier TypeName ConstructorName
45 Token period = node.period;
46 if (period != null && period.end <= request.offset) {
47 return _addNamedConstructorSuggestions(node);
48 }
49 return super.visitConstructorName(node);
50 }
51
52 @override
43 Future<bool> visitNode(AstNode node) { 53 Future<bool> visitNode(AstNode node) {
44 return new Future.value(false); 54 return new Future.value(false);
45 } 55 }
46 56
47 @override 57 @override
48 Future<bool> visitPrefixedIdentifier(PrefixedIdentifier node) { 58 Future<bool> visitPrefixedIdentifier(PrefixedIdentifier node) {
49 if (node.identifier == completionNode) { 59 if (request.offset > node.period.offset) {
50 return _addSuggestions(node.prefix.bestElement); 60 SimpleIdentifier prefix = node.prefix;
61 if (prefix != null) {
62 return _addElementSuggestions(prefix.bestElement);
63 }
51 } 64 }
52 return super.visitPrefixedIdentifier(node); 65 return super.visitPrefixedIdentifier(node);
53 } 66 }
54 67
55 @override 68 @override
69 Future<bool> visitPropertyAccess(PropertyAccess node) {
70 if (request.offset > node.offset) {
71 return _addExpressionSuggestions(node.realTarget);
72 }
73 return super.visitPropertyAccess(node);
74 }
75
76 @override
56 Future<bool> visitSimpleIdentifier(SimpleIdentifier node) { 77 Future<bool> visitSimpleIdentifier(SimpleIdentifier node) {
57 completionNode = node;
58 return node.parent.accept(this); 78 return node.parent.accept(this);
59 } 79 }
60 80
61 /** 81 /**
62 * Add invocation / access suggestions for the given element. 82 * Add invocation / access suggestions for the given element.
63 */ 83 */
64 Future<bool> _addSuggestions(Element element) { 84 Future<bool> _addElementSuggestions(Element element) {
65 if (element != null) { 85 if (element != null) {
66 return element.accept(new _InvocationElementVisitor(request)); 86 return element.accept(new _InvocationElementVisitor(request));
67 } 87 }
68 return new Future.value(false); 88 return new Future.value(false);
69 } 89 }
90
91 /**
92 * Add invocation / access suggestions for the given expression.
93 */
94 Future<bool> _addExpressionSuggestions(Expression target) {
95 if (target != null) {
96 DartType type = target.bestType;
97 if (type != null) {
98 ClassElementSuggestionBuilder.suggestionsFor(request, type.element);
99 return new Future.value(true);
100 }
101 }
102 return new Future.value(false);
103 }
104
105 Future<bool> _addNamedConstructorSuggestions(ConstructorName node) {
106 TypeName typeName = node.type;
107 if (typeName != null) {
108 DartType type = typeName.type;
109 if (type != null) {
110 NamedConstructorSuggestionBuilder.suggestionsFor(request, type.element);
111 return new Future.value(true);
112 }
113 }
114 return new Future.value(false);
115 }
70 } 116 }
71 117
72 /** 118 /**
73 * An [Element] visitor for determining the appropriate invocation/access 119 * An [Element] visitor for determining the appropriate invocation/access
74 * suggestions based upon the element for which the completion is requested. 120 * suggestions based upon the element for which the completion is requested.
75 */ 121 */
76 class _InvocationElementVisitor extends GeneralizingElementVisitor<Future<bool>> 122 class _InvocationElementVisitor extends GeneralizingElementVisitor<Future<bool>>
77 { 123 {
78 final DartCompletionRequest request; 124 final DartCompletionRequest request;
79 125
(...skipping 27 matching lines...) Expand all
107 153
108 @override 154 @override
109 Future<bool> visitVariableElement(VariableElement element) { 155 Future<bool> visitVariableElement(VariableElement element) {
110 DartType type = element.type; 156 DartType type = element.type;
111 if (type != null) { 157 if (type != null) {
112 ClassElementSuggestionBuilder.suggestionsFor(request, type.element); 158 ClassElementSuggestionBuilder.suggestionsFor(request, type.element);
113 } 159 }
114 return new Future.value(true); 160 return new Future.value(true);
115 } 161 }
116 } 162 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698