| 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.suggestion.builder.local; | |
| 6 | |
| 7 import 'package:analysis_server/plugin/protocol/protocol.dart' as protocol | |
| 8 show Element, ElementKind; | |
| 9 import 'package:analysis_server/plugin/protocol/protocol.dart' | |
| 10 hide Element, ElementKind; | |
| 11 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | |
| 12 import 'package:analyzer/src/generated/ast.dart'; | |
| 13 import 'package:analyzer/src/generated/scanner.dart'; | |
| 14 import 'package:analyzer/src/generated/source.dart'; | |
| 15 | |
| 16 const DYNAMIC = 'dynamic'; | |
| 17 | |
| 18 final TypeName NO_RETURN_TYPE = new TypeName( | |
| 19 new SimpleIdentifier(new StringToken(TokenType.IDENTIFIER, '', 0)), null); | |
| 20 | |
| 21 /** | |
| 22 * Create a new protocol Element for inclusion in a completion suggestion. | |
| 23 */ | |
| 24 protocol.Element createElement( | |
| 25 Source source, protocol.ElementKind kind, SimpleIdentifier id, | |
| 26 {String parameters, | |
| 27 TypeName returnType, | |
| 28 bool isAbstract: false, | |
| 29 bool isDeprecated: false}) { | |
| 30 String name; | |
| 31 Location location; | |
| 32 if (id != null) { | |
| 33 name = id.name; | |
| 34 // TODO(danrubel) use lineInfo to determine startLine and startColumn | |
| 35 location = new Location(source.fullName, id.offset, id.length, 0, 0); | |
| 36 } else { | |
| 37 name = ''; | |
| 38 location = new Location(source.fullName, -1, 0, 1, 0); | |
| 39 } | |
| 40 int flags = protocol.Element.makeFlags( | |
| 41 isAbstract: isAbstract, | |
| 42 isDeprecated: isDeprecated, | |
| 43 isPrivate: Identifier.isPrivateName(name)); | |
| 44 return new protocol.Element(kind, name, flags, | |
| 45 location: location, | |
| 46 parameters: parameters, | |
| 47 returnType: nameForType(returnType)); | |
| 48 } | |
| 49 | |
| 50 /** | |
| 51 * Create a new suggestion for the given field. | |
| 52 * Return the new suggestion or `null` if it could not be created. | |
| 53 */ | |
| 54 CompletionSuggestion createFieldSuggestion( | |
| 55 Source source, FieldDeclaration fieldDecl, VariableDeclaration varDecl) { | |
| 56 bool deprecated = isDeprecated(fieldDecl) || isDeprecated(varDecl); | |
| 57 TypeName type = fieldDecl.fields.type; | |
| 58 return createSuggestion( | |
| 59 varDecl.name, deprecated, DART_RELEVANCE_LOCAL_FIELD, type, | |
| 60 classDecl: fieldDecl.parent, | |
| 61 element: createElement(source, protocol.ElementKind.FIELD, varDecl.name, | |
| 62 returnType: type, isDeprecated: deprecated)); | |
| 63 } | |
| 64 | |
| 65 /** | |
| 66 * Create a new suggestion based upon the given information. | |
| 67 * Return the new suggestion or `null` if it could not be created. | |
| 68 */ | |
| 69 CompletionSuggestion createSuggestion(SimpleIdentifier id, bool isDeprecated, | |
| 70 int defaultRelevance, TypeName returnType, | |
| 71 {ClassDeclaration classDecl, protocol.Element element}) { | |
| 72 if (id == null) { | |
| 73 return null; | |
| 74 } | |
| 75 String completion = id.name; | |
| 76 if (completion == null || completion.length <= 0 || completion == '_') { | |
| 77 return null; | |
| 78 } | |
| 79 CompletionSuggestion suggestion = new CompletionSuggestion( | |
| 80 CompletionSuggestionKind.INVOCATION, | |
| 81 isDeprecated ? DART_RELEVANCE_LOW : defaultRelevance, | |
| 82 completion, | |
| 83 completion.length, | |
| 84 0, | |
| 85 isDeprecated, | |
| 86 false, | |
| 87 returnType: nameForType(returnType), | |
| 88 element: element); | |
| 89 if (classDecl != null) { | |
| 90 SimpleIdentifier classId = classDecl.name; | |
| 91 if (classId != null) { | |
| 92 String className = classId.name; | |
| 93 if (className != null && className.length > 0) { | |
| 94 suggestion.declaringType = className; | |
| 95 } | |
| 96 } | |
| 97 } | |
| 98 return suggestion; | |
| 99 } | |
| 100 | |
| 101 /** | |
| 102 * Return `true` if the @deprecated annotation is present | |
| 103 */ | |
| 104 bool isDeprecated(AnnotatedNode node) { | |
| 105 if (node != null) { | |
| 106 NodeList<Annotation> metadata = node.metadata; | |
| 107 if (metadata != null) { | |
| 108 return metadata.any((Annotation a) { | |
| 109 return a.name is SimpleIdentifier && a.name.name == 'deprecated'; | |
| 110 }); | |
| 111 } | |
| 112 } | |
| 113 return false; | |
| 114 } | |
| 115 | |
| 116 /** | |
| 117 * Return the name for the given type. | |
| 118 */ | |
| 119 String nameForType(TypeName type) { | |
| 120 if (type == NO_RETURN_TYPE) { | |
| 121 return null; | |
| 122 } | |
| 123 if (type == null) { | |
| 124 return DYNAMIC; | |
| 125 } | |
| 126 Identifier id = type.name; | |
| 127 if (id == null) { | |
| 128 return DYNAMIC; | |
| 129 } | |
| 130 String name = id.name; | |
| 131 if (name == null || name.length <= 0) { | |
| 132 return DYNAMIC; | |
| 133 } | |
| 134 TypeArgumentList typeArgs = type.typeArguments; | |
| 135 if (typeArgs != null) { | |
| 136 //TODO (danrubel) include type arguments | |
| 137 } | |
| 138 return name; | |
| 139 } | |
| OLD | NEW |