OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, 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.computer.dart.invocation; | |
6 | |
7 import 'package:analysis_server/plugin/edit/utilities/change_builder_dart.dart'; | |
8 import 'package:analysis_server/src/protocol_server.dart' | |
9 show CompletionSuggestion, CompletionSuggestionKind, SourceChange; | |
10 import 'package:analysis_server/src/protocol_server.dart' as protocol | |
11 hide CompletionSuggestion, CompletionSuggestionKind; | |
12 import 'package:analysis_server/src/provisional/completion/completion_dart.dart'
; | |
13 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart' | |
14 show DART_RELEVANCE_HIGH; | |
15 import 'package:analyzer/src/generated/ast.dart'; | |
16 import 'package:analyzer/src/generated/element.dart'; | |
17 import 'package:analyzer/src/generated/engine.dart'; | |
18 import 'package:analyzer/src/generated/resolver.dart'; | |
19 import 'package:analyzer/src/generated/source.dart'; | |
20 | |
21 /** | |
22 * A completion contributor used to suggest replacing partial identifiers inside | |
23 * a class declaration with templates for inherited members. | |
24 */ | |
25 class InheritedContributor extends DartCompletionContributor { | |
26 @override | |
27 List<CompletionSuggestion> internalComputeSuggestions( | |
28 DartCompletionRequest request) { | |
29 if (!request.isResolved) { | |
30 return null; | |
31 } | |
32 AstNode node = new NodeLocator(request.offset).searchWithin(request.unit); | |
33 if (node == null || !_isMemberLevelIdentifier(node)) { | |
34 return null; | |
35 } | |
36 ClassDeclaration classDeclaration = | |
37 node.getAncestor((AstNode node) => node is ClassDeclaration); | |
38 if (classDeclaration != null) { | |
39 ClassElement element = classDeclaration.element; | |
40 if (element == null) { | |
41 return null; | |
42 } | |
43 return _suggestInheritedMembers(request, node, element); | |
44 } | |
45 return null; | |
46 } | |
47 | |
48 /** | |
49 * Return a template for an override of the given [element] in the given | |
50 * [source]. If selected, the template will replace the given [identifier]. | |
51 */ | |
52 String _buildRepacementText( | |
53 Source source, SimpleIdentifier identifier, Element element) { | |
54 AnalysisContext context = element.context; | |
55 DartChangeBuilder builder = new DartChangeBuilder(context); | |
56 builder.addFileEdit(source, context.getModificationStamp(source), | |
57 (DartFileEditBuilder builder) { | |
58 builder.addReplacement(identifier.offset, identifier.length, | |
59 (DartEditBuilder builder) { | |
60 builder.writeOverrideOfInheritedMember(element); | |
61 }); | |
62 }); | |
63 return builder.sourceChange.edits[0].edits[0].replacement.trim(); | |
64 } | |
65 | |
66 /** | |
67 * Build a suggestion to replace the partial [identifier] in the given | |
68 * [source] with an override of the given [element]. | |
69 */ | |
70 CompletionSuggestion _buildSuggestion( | |
71 Source source, SimpleIdentifier identifier, Element element) { | |
72 String completion = _buildRepacementText(source, identifier, element); | |
73 CompletionSuggestion suggestion = new CompletionSuggestion( | |
74 CompletionSuggestionKind.IDENTIFIER, | |
75 DART_RELEVANCE_HIGH, | |
76 completion, | |
77 identifier.offset, | |
78 0, | |
79 element.isDeprecated, | |
80 false); | |
81 suggestion.element = protocol.convertElement(element); | |
82 return suggestion; | |
83 } | |
84 | |
85 /** | |
86 * Return a list containing the names of all of the inherited by not | |
87 * implemented members of the class represented by the given [element] that | |
88 * start with the given [prefix]. The [map] is used to find all of the members | |
89 * that are inherited. | |
90 */ | |
91 List<String> _computeMemberNames( | |
92 MemberMap map, ClassElement element, String prefix) { | |
93 List<String> memberNames = <String>[]; | |
94 int count = map.size; | |
95 for (int i = 0; i < count; i++) { | |
96 String memberName = map.getKey(i); | |
97 if (memberName.startsWith(prefix) && !_hasMember(element, memberName)) { | |
98 memberNames.add(memberName); | |
99 } | |
100 } | |
101 return memberNames; | |
102 } | |
103 | |
104 /** | |
105 * Return `true` if the given [classElement] directly declares a member with | |
106 * the given [memberName]. | |
107 */ | |
108 bool _hasMember(ClassElement classElement, String memberName) { | |
109 return classElement.getField(memberName) != null || | |
110 classElement.getGetter(memberName) != null || | |
111 classElement.getMethod(memberName) != null || | |
112 classElement.getSetter(memberName) != null; | |
113 } | |
114 | |
115 /** | |
116 * Return `true` if the given [node] looks like a partial identifier inside a | |
117 * class declaration. | |
118 */ | |
119 bool _isMemberLevelIdentifier(AstNode node) { | |
120 if (node is SimpleIdentifier) { | |
121 AstNode parent1 = node.parent; | |
122 if (parent1 is TypeName) { | |
123 AstNode parent2 = parent1.parent; | |
124 if (parent2 is VariableDeclarationList) { | |
125 AstNode parent3 = parent2.parent; | |
126 if (parent3 is FieldDeclaration) { | |
127 NodeList<VariableDeclaration> variables = parent2.variables; | |
128 return variables.length == 1 && variables[0].name.name.isEmpty; | |
129 } | |
130 } | |
131 } | |
132 } | |
133 return false; | |
134 } | |
135 | |
136 /** | |
137 * Add any suggestions that are appropriate to the given [request], using the | |
138 * given [element] to find inherited members whose name has the given | |
139 * [identifier] as a prefix. | |
140 */ | |
141 List<CompletionSuggestion> _suggestInheritedMembers( | |
142 DartCompletionRequest request, | |
143 SimpleIdentifier identifier, | |
144 ClassElement element) { | |
145 String name = identifier.name; | |
146 InheritanceManager manager = new InheritanceManager(element.library); | |
147 MemberMap map = manager.getMapOfMembersInheritedFromInterfaces(element); | |
148 List<String> memberNames = _computeMemberNames(map, element, name); | |
149 memberNames.sort(); | |
150 List<CompletionSuggestion> suggestions = <CompletionSuggestion>[]; | |
151 for (String memberName in memberNames) { | |
152 CompletionSuggestion suggestion = | |
153 _buildSuggestion(request.source, identifier, map.get(memberName)); | |
154 if (suggestion != null) { | |
155 suggestions.add(suggestion); | |
156 } | |
157 } | |
158 return suggestions; | |
159 } | |
160 } | |
OLD | NEW |