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

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

Issue 467233003: refactor and cleanup code completion (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: merge Created 6 years, 4 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_services/completion/completion_computer.dart';
10 import 'package:analysis_services/completion/completion_suggestion.dart'; 9 import 'package:analysis_services/completion/completion_suggestion.dart';
10 import 'package:analysis_services/src/completion/dart_completion_manager.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 13
14 /** 14 /**
15 * A computer for calculating invocation / access suggestions 15 * A computer for calculating invocation / access suggestions
16 * `completion.getSuggestions` request results. 16 * `completion.getSuggestions` request results.
17 */ 17 */
18 class InvocationComputer extends CompletionComputer { 18 class InvocationComputer extends DartCompletionComputer {
19 19
20 @override 20 @override
21 bool computeFast(CompilationUnit unit, AstNode node, 21 bool computeFast(DartCompletionRequest request) {
22 List<CompletionSuggestion> suggestions) {
23 // TODO: implement computeFast 22 // TODO: implement computeFast
24 return false; 23 return false;
25 } 24 }
26 25
27 @override 26 @override
28 Future<bool> computeFull(CompilationUnit unit, AstNode node, 27 Future<bool> computeFull(DartCompletionRequest request) {
29 List<CompletionSuggestion> suggestions) { 28 return request.node.accept(new _InvocationAstVisitor(request));
30 return node.accept(new _InvocationAstVisitor(unit, offset, suggestions));
31 } 29 }
32 } 30 }
33 31
34 /** 32 /**
35 * An [AstNode] vistor for determining the appropriate invocation/access 33 * An [AstNode] vistor for determining the appropriate invocation/access
36 * suggestions based upon the node in which the completion is requested. 34 * suggestions based upon the node in which the completion is requested.
37 */ 35 */
38 class _InvocationAstVisitor extends GeneralizingAstVisitor<Future<bool>> { 36 class _InvocationAstVisitor extends GeneralizingAstVisitor<Future<bool>> {
39 final CompilationUnit unit; 37 final DartCompletionRequest request;
40 final int offset;
41 final List<CompletionSuggestion> suggestions;
42 AstNode completionNode; 38 AstNode completionNode;
43 39
44 _InvocationAstVisitor(this.unit, this.offset, this.suggestions); 40 _InvocationAstVisitor(this.request);
45 41
42 @override
46 Future<bool> visitNode(AstNode node) { 43 Future<bool> visitNode(AstNode node) {
47 return new Future.value(false); 44 return new Future.value(false);
48 } 45 }
49 46
47 @override
50 Future<bool> visitPrefixedIdentifier(PrefixedIdentifier node) { 48 Future<bool> visitPrefixedIdentifier(PrefixedIdentifier node) {
51 if (node.identifier == completionNode) { 49 if (node.identifier == completionNode) {
52 return _addSuggestions(node.prefix.bestElement); 50 return _addSuggestions(node.prefix.bestElement);
53 } 51 }
54 return super.visitPrefixedIdentifier(node); 52 return super.visitPrefixedIdentifier(node);
55 } 53 }
56 54
55 @override
57 Future<bool> visitSimpleIdentifier(SimpleIdentifier node) { 56 Future<bool> visitSimpleIdentifier(SimpleIdentifier node) {
58 completionNode = node; 57 completionNode = node;
59 return node.parent.accept(this); 58 return node.parent.accept(this);
60 } 59 }
61 60
62 /** 61 /**
63 * Add invocation / access suggestions for the given element. 62 * Add invocation / access suggestions for the given element.
64 */ 63 */
65 Future<bool> _addSuggestions(Element element) { 64 Future<bool> _addSuggestions(Element element) {
66 if (element != null) { 65 if (element != null) {
67 return element.accept(new _InvocationElementVisitor(suggestions)); 66 return element.accept(new _InvocationElementVisitor(request));
68 } 67 }
69 return new Future.value(false); 68 return new Future.value(false);
70 } 69 }
71 } 70 }
72 71
73 /** 72 /**
74 * An [Element] visitor for determining the appropriate invocation/access 73 * An [Element] visitor for determining the appropriate invocation/access
75 * suggestions based upon the element for which the completion is requested. 74 * suggestions based upon the element for which the completion is requested.
76 */ 75 */
77 class _InvocationElementVisitor extends GeneralizingElementVisitor<Future<bool>> 76 class _InvocationElementVisitor extends GeneralizingElementVisitor<Future<bool>>
78 { 77 {
79 final List<CompletionSuggestion> suggestions; 78 final DartCompletionRequest request;
80 79
81 _InvocationElementVisitor(this.suggestions); 80 _InvocationElementVisitor(this.request);
82 81
82 @override
83 Future<bool> visitElement(Element element) { 83 Future<bool> visitElement(Element element) {
84 return new Future.value(false); 84 return new Future.value(false);
85 } 85 }
86 86
87 @override
87 Future<bool> visitVariableElement(VariableElement element) { 88 Future<bool> visitVariableElement(VariableElement element) {
88 return _addSuggestions(element.type); 89 return _addSuggestions(element.type);
89 } 90 }
90 91
91 Future<bool> _addSuggestions(DartType type) { 92 Future<bool> _addSuggestions(DartType type) {
92 if (type != null && type.element != null) { 93 if (type != null && type.element != null) {
93 type.element.accept(new _SuggestionBuilderVisitor(suggestions)); 94 type.element.accept(new _SuggestionBuilderVisitor(request));
94 return new Future.value(true); 95 return new Future.value(true);
95 } 96 }
96 return new Future.value(false); 97 return new Future.value(false);
97 } 98 }
98 } 99 }
99 100
100 /** 101 /**
101 * An [Element] visitor that builds suggestions by recursively visiting 102 * An [Element] visitor that builds suggestions by recursively visiting
102 * elements in a type hierarchy. 103 * elements in a type hierarchy.
103 */ 104 */
104 class _SuggestionBuilderVisitor extends GeneralizingElementVisitor { 105 class _SuggestionBuilderVisitor extends GeneralizingElementVisitor {
105 final List<CompletionSuggestion> suggestions; 106 final DartCompletionRequest request;
106 107
107 _SuggestionBuilderVisitor(this.suggestions); 108 _SuggestionBuilderVisitor(this.request);
108 109
110 @override
109 visitClassElement(ClassElement element) { 111 visitClassElement(ClassElement element) {
110 //TODO (danrubel): filter private members if not in the same library 112 //TODO (danrubel): filter private members if not in the same library
111 element.visitChildren(this); 113 element.visitChildren(this);
112 } 114 }
113 115
116 @override
114 visitElement(Element element) { 117 visitElement(Element element) {
115 // ignored 118 // ignored
116 } 119 }
117 120
121 @override
118 visitFieldElement(FieldElement element) { 122 visitFieldElement(FieldElement element) {
119 if (!element.isSynthetic) { 123 if (!element.isSynthetic) {
120 _addSuggestion(element, CompletionSuggestionKind.FIELD); 124 _addSuggestion(element, CompletionSuggestionKind.FIELD);
121 } 125 }
122 } 126 }
123 127
128 @override
124 visitMethodElement(MethodElement element) { 129 visitMethodElement(MethodElement element) {
125 if (!element.isSynthetic) { 130 if (!element.isSynthetic) {
126 _addSuggestion(element, CompletionSuggestionKind.METHOD); 131 _addSuggestion(element, CompletionSuggestionKind.METHOD);
127 } 132 }
128 } 133 }
129 134
135 @override
130 visitPropertyAccessorElement(PropertyAccessorElement element) { 136 visitPropertyAccessorElement(PropertyAccessorElement element) {
131 if (!element.isSynthetic) { 137 if (!element.isSynthetic) {
132 if (element.isGetter) { 138 if (element.isGetter) {
133 _addSuggestion(element, CompletionSuggestionKind.GETTER); 139 _addSuggestion(element, CompletionSuggestionKind.GETTER);
134 } else if (element.isSetter) { 140 } else if (element.isSetter) {
135 _addSuggestion( 141 _addSuggestion(
136 element, 142 element,
137 CompletionSuggestionKind.SETTER, 143 CompletionSuggestionKind.SETTER,
138 element.displayName); 144 element.displayName);
139 } 145 }
140 } 146 }
141 } 147 }
142 148
143 void _addSuggestion(Element element, CompletionSuggestionKind kind, 149 void _addSuggestion(Element element, CompletionSuggestionKind kind,
144 [String completion = null]) { 150 [String completion = null]) {
145 if (completion == null) { 151 if (completion == null) {
146 completion = element.name; 152 completion = element.name;
147 } 153 }
148 if (completion != null && completion.length > 0) { 154 if (completion != null && completion.length > 0) {
149 suggestions.add( 155 request.suggestions.add(
150 new CompletionSuggestion( 156 new CompletionSuggestion(
151 kind, 157 kind,
152 CompletionRelevance.DEFAULT, 158 CompletionRelevance.DEFAULT,
153 completion, 159 completion,
154 completion.length, 160 completion.length,
155 0, 161 0,
156 element.isDeprecated, 162 element.isDeprecated,
157 false /* isPotential */)); 163 false));
158 } 164 }
159 } 165 }
160 } 166 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698