OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
8 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; | 8 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; |
9 import 'package:analyzer/dart/element/element.dart'; | 9 import 'package:analyzer/dart/element/element.dart'; |
10 import 'package:analyzer/dart/element/type.dart'; | 10 import 'package:analyzer/dart/element/type.dart'; |
11 import 'package:analyzer/file_system/file_system.dart'; | 11 import 'package:analyzer/file_system/file_system.dart'; |
12 import 'package:analyzer_plugin/protocol/protocol_common.dart' hide Element; | 12 import 'package:analyzer_plugin/protocol/protocol_common.dart' hide Element; |
13 import 'package:analyzer_plugin/src/utilities/completion/completion_target.dart'
; | 13 import 'package:analyzer_plugin/src/utilities/completion/completion_target.dart'
; |
14 import 'package:analyzer_plugin/src/utilities/completion/element_suggestion_buil
der.dart'; | 14 import 'package:analyzer_plugin/src/utilities/completion/element_suggestion_buil
der.dart'; |
15 import 'package:analyzer_plugin/src/utilities/completion/optype.dart'; | 15 import 'package:analyzer_plugin/src/utilities/completion/optype.dart'; |
16 import 'package:analyzer_plugin/utilities/completion/completion_core.dart'; | 16 import 'package:analyzer_plugin/utilities/completion/completion_core.dart'; |
17 | 17 |
18 /** | 18 /** |
19 * A contributor for calculating suggestions for inherited references. | 19 * A contributor for calculating suggestions for inherited references. |
| 20 * |
| 21 * Plugin developers should extend this function and primarily |
| 22 * overload `computeSuggestions` (if needed). |
20 */ | 23 */ |
21 class InheritedReferenceContributor extends Object | 24 class InheritedReferenceContributor extends Object |
22 with ElementSuggestionBuilder | 25 with ElementSuggestionBuilder |
23 implements CompletionContributor { | 26 implements CompletionContributor { |
24 @override | 27 @override |
25 LibraryElement containingLibrary; | 28 LibraryElement containingLibrary; |
26 | 29 |
27 @override | 30 @override |
28 CompletionSuggestionKind kind; | 31 CompletionSuggestionKind kind; |
29 | 32 |
30 @override | 33 @override |
31 ResourceProvider resourceProvider; | 34 ResourceProvider resourceProvider; |
32 | 35 |
| 36 /** |
| 37 * Plugin contributors should primarily overload this function. |
| 38 * Should more parameters be needed for autocompletion needs, the |
| 39 * overloaded function should define those parameters and |
| 40 * call on `computeSuggestionsForClass`. |
| 41 */ |
33 @override | 42 @override |
34 Future<Null> computeSuggestions( | 43 Future<Null> computeSuggestions( |
35 CompletionRequest request, CompletionCollector collector) async { | 44 CompletionRequest request, CompletionCollector collector) async { |
36 CompletionTarget target = | 45 CompletionTarget target = |
37 new CompletionTarget.forOffset(request.result.unit, request.offset); | 46 new CompletionTarget.forOffset(request.result.unit, request.offset); |
38 OpType optype = new OpType.forCompletion(target, request.offset); | 47 OpType optype = new OpType.forCompletion(target, request.offset); |
39 if (!optype.includeIdentifiers) { | 48 if (!optype.includeIdentifiers) { |
40 return; | 49 return; |
41 } | 50 } |
42 ClassDeclaration classDecl = _enclosingClass(target); | 51 ClassDeclaration classDecl = _enclosingClass(target); |
43 if (classDecl == null || classDecl.element == null) { | 52 if (classDecl == null || classDecl.element == null) { |
44 return; | 53 return; |
45 } | 54 } |
46 containingLibrary = request.result.libraryElement; | 55 containingLibrary = request.result.libraryElement; |
47 _computeSuggestionsForClass2( | 56 _computeSuggestionsForClass2(collector, target, |
48 collector, | 57 resolutionMap.elementDeclaredByClassDeclaration(classDecl), optype); |
49 target, | 58 } |
50 resolutionMap.elementDeclaredByClassDeclaration(classDecl), | 59 |
51 request, | 60 /** |
52 optype); | 61 * Clients should not overload this function. |
| 62 */ |
| 63 Future<Null> computeSuggestionsForClass( |
| 64 CompletionRequest request, |
| 65 CompletionCollector collector, |
| 66 ClassElement classElement, { |
| 67 AstNode entryPoint, |
| 68 bool skipChildClass, |
| 69 CompletionTarget target, |
| 70 OpType optype, |
| 71 }) async { |
| 72 target ??= new CompletionTarget.forOffset( |
| 73 request.result.unit, request.offset, |
| 74 entryPoint: entryPoint); |
| 75 optype ??= new OpType.forCompletion(target, request.offset); |
| 76 if (!optype.includeIdentifiers) { |
| 77 return; |
| 78 } |
| 79 if (classElement == null) { |
| 80 ClassDeclaration classDecl = _enclosingClass(target); |
| 81 if (classDecl == null || classDecl.element == null) { |
| 82 return; |
| 83 } |
| 84 classElement = resolutionMap.elementDeclaredByClassDeclaration(classDecl); |
| 85 } |
| 86 containingLibrary = request.result.libraryElement; |
| 87 _computeSuggestionsForClass2(collector, target, classElement, optype, |
| 88 skipChildClass: skipChildClass); |
53 } | 89 } |
54 | 90 |
55 _addSuggestionsForType(InterfaceType type, OpType optype, | 91 _addSuggestionsForType(InterfaceType type, OpType optype, |
56 {bool isFunctionalArgument: false}) { | 92 {bool isFunctionalArgument: false}) { |
57 if (!isFunctionalArgument) { | 93 if (!isFunctionalArgument) { |
58 for (PropertyAccessorElement elem in type.accessors) { | 94 for (PropertyAccessorElement elem in type.accessors) { |
59 if (elem.isGetter) { | 95 if (elem.isGetter) { |
60 if (optype.includeReturnValueSuggestions) { | 96 if (optype.includeReturnValueSuggestions) { |
61 addSuggestion(elem); | 97 addSuggestion(elem); |
62 } | 98 } |
(...skipping 12 matching lines...) Expand all Loading... |
75 addSuggestion(elem); | 111 addSuggestion(elem); |
76 } | 112 } |
77 } else { | 113 } else { |
78 if (optype.includeVoidReturnSuggestions) { | 114 if (optype.includeVoidReturnSuggestions) { |
79 addSuggestion(elem); | 115 addSuggestion(elem); |
80 } | 116 } |
81 } | 117 } |
82 } | 118 } |
83 } | 119 } |
84 | 120 |
85 void _computeSuggestionsForClass2( | 121 void _computeSuggestionsForClass2(CompletionCollector collector, |
86 CompletionCollector collector, | 122 CompletionTarget target, ClassElement classElement, OpType optype, |
87 CompletionTarget target, | |
88 ClassElement classElement, | |
89 CompletionRequest request, | |
90 OpType optype, | |
91 {bool skipChildClass: true}) { | 123 {bool skipChildClass: true}) { |
92 bool isFunctionalArgument = target.isFunctionalArgument(); | 124 bool isFunctionalArgument = target.isFunctionalArgument(); |
93 kind = isFunctionalArgument | 125 kind = isFunctionalArgument |
94 ? CompletionSuggestionKind.IDENTIFIER | 126 ? CompletionSuggestionKind.IDENTIFIER |
95 : CompletionSuggestionKind.INVOCATION; | 127 : CompletionSuggestionKind.INVOCATION; |
96 | 128 |
97 if (!skipChildClass) { | 129 if (!skipChildClass) { |
98 _addSuggestionsForType(classElement.type, optype, | 130 _addSuggestionsForType(classElement.type, optype, |
99 isFunctionalArgument: isFunctionalArgument); | 131 isFunctionalArgument: isFunctionalArgument); |
100 } | 132 } |
(...skipping 26 matching lines...) Expand all Loading... |
127 if (node is FieldDeclaration) { | 159 if (node is FieldDeclaration) { |
128 if (node.isStatic) { | 160 if (node.isStatic) { |
129 return null; | 161 return null; |
130 } | 162 } |
131 } | 163 } |
132 node = node.parent; | 164 node = node.parent; |
133 } | 165 } |
134 return null; | 166 return null; |
135 } | 167 } |
136 } | 168 } |
OLD | NEW |