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