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