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

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

Issue 480413003: more invocation code completion improvements (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
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library services.completion.suggestion.builder;
6
7 import 'package:analysis_server/src/services/completion/completion_suggestion.da rt';
8 import 'package:analysis_server/src/services/completion/dart_completion_manager. dart';
9 import 'package:analyzer/src/generated/element.dart';
10
11 /**
12 * This class visits elements in a class and provides suggestions based upon
13 * the visible members in that class. Clients should call
14 * [ClassElementSuggestionBuilder.suggestionsFor].
15 */
16 class ClassElementSuggestionBuilder extends GeneralizingElementVisitor {
17 final DartCompletionRequest request;
18
19 ClassElementSuggestionBuilder(this.request);
20
21 @override
22 visitClassElement(ClassElement element) {
23 element.visitChildren(this);
24 }
25
26 @override
27 visitElement(Element element) {
28 // ignored
29 }
30
31 @override
32 visitFieldElement(FieldElement element) {
33 _addSuggestion(element, CompletionSuggestionKind.FIELD);
34 }
35
36 @override
37 visitMethodElement(MethodElement element) {
38 _addSuggestion(element, CompletionSuggestionKind.METHOD);
39 }
40
41 @override
42 visitPropertyAccessorElement(PropertyAccessorElement element) {
43 if (element.isGetter) {
44 _addSuggestion(element, CompletionSuggestionKind.GETTER);
45 } else if (element.isSetter) {
46 _addSuggestion(element, CompletionSuggestionKind.SETTER);
47 }
48 }
49
50 void _addSuggestion(Element element, CompletionSuggestionKind kind) {
51 if (element.isSynthetic) {
52 return;
53 }
54 if (element.isPrivate) {
55 LibraryElement elementLibrary =
56 element.getAncestor((parent) => parent is LibraryElement);
57 LibraryElement unitLibrary =
58 request.unit.element.getAncestor((parent) => parent is LibraryElement) ;
59 if (elementLibrary != unitLibrary) {
60 return;
61 }
62 }
63 String completion = element.displayName;
64 if (completion == null || completion.length <= 0) {
65 return;
66 }
67 request.suggestions.add(
68 new CompletionSuggestion(
69 kind,
70 CompletionRelevance.DEFAULT,
71 completion,
72 completion.length,
73 0,
74 element.isDeprecated,
75 false));
76 }
77
78 /**
79 * Add suggestions for the visible members in the given class
80 */
81 static void suggestionsFor(DartCompletionRequest request, Element element) {
82 if (element is ClassElement) {
83 element.accept(new ClassElementSuggestionBuilder(request));
84 }
85 }
86 }
87
88 /**
89 * This class visits elements in a library and provides suggestions based upon
90 * the visible members in that library. Clients should call
91 * [LibraryElementSuggestionBuilder.suggestionsFor].
92 */
93 class LibraryElementSuggestionBuilder extends GeneralizingElementVisitor {
94
95 final DartCompletionRequest request;
96
97 LibraryElementSuggestionBuilder(this.request);
98
99 @override
100 visitClassElement(ClassElement element) {
101 _addSuggestion(element);
102 }
103
104 @override
105 visitCompilationUnitElement(CompilationUnitElement element) {
106 element.visitChildren(this);
107 }
108
109 @override
110 visitElement(Element element) {
111 // ignored
112 }
113
114 @override
115 visitFunctionTypeAliasElement(FunctionTypeAliasElement element) {
116 _addSuggestion(element);
117 }
118
119 @override
120 visitTopLevelVariableElement(TopLevelVariableElement element) {
121 _addSuggestion(element);
122 }
123
124 void _addSuggestion(Element element) {
125 if (element != null) {
126 String completion = element.name;
127 if (completion != null && completion.length > 0) {
128 request.suggestions.add(
129 new CompletionSuggestion(
130 CompletionSuggestionKind.fromElementKind(element.kind),
131 CompletionRelevance.DEFAULT,
132 completion,
133 completion.length,
134 0,
135 element.isDeprecated,
136 false));
137 }
138 }
139 }
140
141 /**
142 * Add suggestions for the visible members in the given library
143 */
144 static void suggestionsFor(DartCompletionRequest request,
145 LibraryElement library) {
146 if (library != null) {
147 library.visitChildren(new LibraryElementSuggestionBuilder(request));
148 }
149 }
150 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698