Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: pkg/analysis_server/lib/src/services/completion/dart/label_contributor.dart

Issue 2618983003: Remove code duplication (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | pkg/analysis_server/lib/src/services/completion/dart/local_constructor_contributor.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.contributor.dart.label; 5 library services.completion.contributor.dart.label;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:analysis_server/plugin/protocol/protocol.dart' as protocol 9 import 'package:analysis_server/plugin/protocol/protocol.dart' as protocol
10 show Element, ElementKind; 10 show ElementKind;
11 import 'package:analysis_server/src/protocol_server.dart'
12 show CompletionSuggestion, CompletionSuggestionKind;
11 import 'package:analysis_server/src/provisional/completion/dart/completion_dart. dart'; 13 import 'package:analysis_server/src/provisional/completion/dart/completion_dart. dart';
12 import 'package:analysis_server/src/services/completion/dart/completion_manager. dart' 14 import 'package:analysis_server/src/services/completion/dart/completion_manager. dart'
13 show DartCompletionRequestImpl; 15 show DartCompletionRequestImpl;
14 import 'package:analysis_server/src/services/completion/dart/local_declaration_v isitor.dart' 16 import 'package:analysis_server/src/services/completion/dart/local_declaration_v isitor.dart'
15 show LocalDeclarationVisitor; 17 show LocalDeclarationVisitor;
16 import 'package:analysis_server/src/services/completion/dart/optype.dart'; 18 import 'package:analysis_server/src/services/completion/dart/optype.dart';
19 import 'package:analysis_server/src/services/completion/dart/utilities.dart';
17 import 'package:analyzer/dart/ast/ast.dart'; 20 import 'package:analyzer/dart/ast/ast.dart';
18 import 'package:analyzer/dart/ast/standard_ast_factory.dart';
19 import 'package:analyzer/dart/ast/token.dart';
20 import 'package:analyzer/src/dart/ast/token.dart';
21 import 'package:analyzer/src/generated/source.dart';
22
23 import '../../../protocol_server.dart'
24 show CompletionSuggestion, CompletionSuggestionKind, Location;
25
26 const DYNAMIC = 'dynamic';
27
28 final TypeName NO_RETURN_TYPE = astFactory.typeName(
29 astFactory.simpleIdentifier(new StringToken(TokenType.IDENTIFIER, '', 0)),
30 null);
31
32 /**
33 * Create a new protocol Element for inclusion in a completion suggestion.
34 */
35 protocol.Element createLocalElement(
36 Source source, protocol.ElementKind kind, SimpleIdentifier id,
37 {String parameters,
38 TypeName returnType,
39 bool isAbstract: false,
40 bool isDeprecated: false}) {
41 String name;
42 Location location;
43 if (id != null) {
44 name = id.name;
45 // TODO(danrubel) use lineInfo to determine startLine and startColumn
46 location = new Location(source.fullName, id.offset, id.length, 0, 0);
47 } else {
48 name = '';
49 location = new Location(source.fullName, -1, 0, 1, 0);
50 }
51 int flags = protocol.Element.makeFlags(
52 isAbstract: isAbstract,
53 isDeprecated: isDeprecated,
54 isPrivate: Identifier.isPrivateName(name));
55 return new protocol.Element(kind, name, flags,
56 location: location,
57 parameters: parameters,
58 returnType: nameForType(returnType));
59 }
60
61 /**
62 * Create a new suggestion for the given field.
63 * Return the new suggestion or `null` if it could not be created.
64 */
65 CompletionSuggestion createLocalFieldSuggestion(
66 Source source, FieldDeclaration fieldDecl, VariableDeclaration varDecl) {
67 bool deprecated = isDeprecated(fieldDecl) || isDeprecated(varDecl);
68 TypeName type = fieldDecl.fields.type;
69 return createLocalSuggestion(
70 varDecl.name, deprecated, DART_RELEVANCE_LOCAL_FIELD, type,
71 classDecl: fieldDecl.parent,
72 element: createLocalElement(
73 source, protocol.ElementKind.FIELD, varDecl.name,
74 returnType: type, isDeprecated: deprecated));
75 }
76
77 /**
78 * Create a new suggestion based upon the given information.
79 * Return the new suggestion or `null` if it could not be created.
80 */
81 CompletionSuggestion createLocalSuggestion(SimpleIdentifier id,
82 bool isDeprecated, int defaultRelevance, TypeName returnType,
83 {ClassDeclaration classDecl, protocol.Element element}) {
84 if (id == null) {
85 return null;
86 }
87 String completion = id.name;
88 if (completion == null || completion.length <= 0 || completion == '_') {
89 return null;
90 }
91 CompletionSuggestion suggestion = new CompletionSuggestion(
92 CompletionSuggestionKind.INVOCATION,
93 isDeprecated ? DART_RELEVANCE_LOW : defaultRelevance,
94 completion,
95 completion.length,
96 0,
97 isDeprecated,
98 false,
99 returnType: nameForType(returnType),
100 element: element);
101 if (classDecl != null) {
102 SimpleIdentifier classId = classDecl.name;
103 if (classId != null) {
104 String className = classId.name;
105 if (className != null && className.length > 0) {
106 suggestion.declaringType = className;
107 }
108 }
109 }
110 return suggestion;
111 }
112
113 /**
114 * Return `true` if the @deprecated annotation is present
115 */
116 bool isDeprecated(AnnotatedNode node) {
117 if (node != null) {
118 NodeList<Annotation> metadata = node.metadata;
119 if (metadata != null) {
120 return metadata.any((Annotation a) {
121 return a.name is SimpleIdentifier && a.name.name == 'deprecated';
122 });
123 }
124 }
125 return false;
126 }
127
128 /**
129 * Return the name for the given type.
130 */
131 String nameForType(TypeName type) {
132 if (type == NO_RETURN_TYPE) {
133 return null;
134 }
135 if (type == null) {
136 return DYNAMIC;
137 }
138 Identifier id = type.name;
139 if (id == null) {
140 return DYNAMIC;
141 }
142 String name = id.name;
143 if (name == null || name.length <= 0) {
144 return DYNAMIC;
145 }
146 TypeArgumentList typeArgs = type.typeArguments;
147 if (typeArgs != null) {
148 //TODO (danrubel) include type arguments
149 }
150 return name;
151 }
152 21
153 /** 22 /**
154 * A contributor for calculating label suggestions. 23 * A contributor for calculating label suggestions.
155 */ 24 */
156 class LabelContributor extends DartCompletionContributor { 25 class LabelContributor extends DartCompletionContributor {
157 @override 26 @override
158 Future<List<CompletionSuggestion>> computeSuggestions( 27 Future<List<CompletionSuggestion>> computeSuggestions(
159 DartCompletionRequest request) async { 28 DartCompletionRequest request) async {
160 OpType optype = (request as DartCompletionRequestImpl).opType; 29 OpType optype = (request as DartCompletionRequestImpl).opType;
161 30
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 0, 149 0,
281 false, 150 false,
282 false); 151 false);
283 suggestions.add(suggestion); 152 suggestions.add(suggestion);
284 return suggestion; 153 return suggestion;
285 } 154 }
286 } 155 }
287 return null; 156 return null;
288 } 157 }
289 } 158 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analysis_server/lib/src/services/completion/dart/local_constructor_contributor.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698