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

Unified Diff: pkg/analysis_server/lib/src/services/completion/dart/local_reference_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 side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_server/lib/src/services/completion/dart/local_reference_contributor.dart
diff --git a/pkg/analysis_server/lib/src/services/completion/dart/local_reference_contributor.dart b/pkg/analysis_server/lib/src/services/completion/dart/local_reference_contributor.dart
index 2d3bdd1fcfce69223dcbc2c192770ea70fad638d..41e50c53e5631d0c8b1f84348e202dd243b26bf4 100644
--- a/pkg/analysis_server/lib/src/services/completion/dart/local_reference_contributor.dart
+++ b/pkg/analysis_server/lib/src/services/completion/dart/local_reference_contributor.dart
@@ -8,143 +8,24 @@ import 'dart:async';
import 'package:analysis_server/plugin/protocol/protocol.dart' as protocol
show Element, ElementKind;
+import 'package:analysis_server/src/protocol_server.dart'
+ show CompletionSuggestion, CompletionSuggestionKind, Location;
import 'package:analysis_server/src/provisional/completion/dart/completion_dart.dart';
import 'package:analysis_server/src/services/completion/dart/completion_manager.dart'
show DartCompletionRequestImpl;
import 'package:analysis_server/src/services/completion/dart/local_declaration_visitor.dart'
show LocalDeclarationVisitor;
import 'package:analysis_server/src/services/completion/dart/optype.dart';
+import 'package:analysis_server/src/services/completion/dart/utilities.dart';
import 'package:analysis_server/src/services/correction/strings.dart';
import 'package:analysis_server/src/utilities/documentation.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/standard_resolution_map.dart';
-import 'package:analyzer/dart/ast/standard_ast_factory.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
-import 'package:analyzer/src/dart/ast/token.dart';
-import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/generated/utilities_dart.dart' show ParameterKind;
-import '../../../protocol_server.dart'
- show CompletionSuggestion, CompletionSuggestionKind, Location;
-
-const DYNAMIC = 'dynamic';
-
-final TypeName NO_RETURN_TYPE = astFactory.typeName(
- astFactory.simpleIdentifier(new StringToken(TokenType.IDENTIFIER, '', 0)),
- null);
-
-/**
-* Create a new protocol Element for inclusion in a completion suggestion.
-*/
-protocol.Element _createLocalElement(
- Source source, protocol.ElementKind kind, SimpleIdentifier id,
- {String parameters,
- TypeName returnType,
- bool isAbstract: false,
- bool isDeprecated: false}) {
- String name;
- Location location;
- if (id != null) {
- name = id.name;
- // TODO(danrubel) use lineInfo to determine startLine and startColumn
- location = new Location(source.fullName, id.offset, id.length, 0, 0);
- } else {
- name = '';
- location = new Location(source.fullName, -1, 0, 1, 0);
- }
- int flags = protocol.Element.makeFlags(
- isAbstract: isAbstract,
- isDeprecated: isDeprecated,
- isPrivate: Identifier.isPrivateName(name));
- return new protocol.Element(kind, name, flags,
- location: location,
- parameters: parameters,
- returnType: _nameForType(returnType));
-}
-
-/**
-* Create a new suggestion based upon the given information.
-* Return the new suggestion or `null` if it could not be created.
-*/
-CompletionSuggestion _createLocalSuggestion(
- SimpleIdentifier id,
- CompletionSuggestionKind kind,
- bool isDeprecated,
- int defaultRelevance,
- TypeName returnType,
- {ClassDeclaration classDecl,
- protocol.Element element}) {
- if (id == null) {
- return null;
- }
- String completion = id.name;
- if (completion == null || completion.length <= 0 || completion == '_') {
- return null;
- }
- CompletionSuggestion suggestion = new CompletionSuggestion(
- kind,
- isDeprecated ? DART_RELEVANCE_LOW : defaultRelevance,
- completion,
- completion.length,
- 0,
- isDeprecated,
- false,
- returnType: _nameForType(returnType),
- element: element);
- if (classDecl != null) {
- SimpleIdentifier classId = classDecl.name;
- if (classId != null) {
- String className = classId.name;
- if (className != null && className.length > 0) {
- suggestion.declaringType = className;
- }
- }
- }
- return suggestion;
-}
-
-/**
-* Return `true` if the @deprecated annotation is present
-*/
-bool _isDeprecated(AnnotatedNode node) {
- if (node != null) {
- NodeList<Annotation> metadata = node.metadata;
- if (metadata != null) {
- return metadata.any((Annotation a) {
- return a.name is SimpleIdentifier && a.name.name == 'deprecated';
- });
- }
- }
- return false;
-}
-
-/**
-* Return the name for the given type.
-*/
-String _nameForType(TypeName type) {
- if (type == NO_RETURN_TYPE) {
- return null;
- }
- if (type == null) {
- return DYNAMIC;
- }
- Identifier id = type.name;
- if (id == null) {
- return DYNAMIC;
- }
- String name = id.name;
- if (name == null || name.length <= 0) {
- return DYNAMIC;
- }
- TypeArgumentList typeArgs = type.typeArguments;
- if (typeArgs != null) {
- //TODO (danrubel) include type arguments
- }
- return name;
-}
-
/**
* A contributor for calculating suggestions for declarations in the local
* file and containing library.
@@ -244,7 +125,7 @@ class _LocalVisitor extends LocalDeclarationVisitor {
NO_RETURN_TYPE,
protocol.ElementKind.CLASS,
isAbstract: declaration.isAbstract,
- isDeprecated: _isDeprecated(declaration));
+ isDeprecated: isDeprecated(declaration));
}
}
@@ -257,7 +138,7 @@ class _LocalVisitor extends LocalDeclarationVisitor {
NO_RETURN_TYPE,
protocol.ElementKind.CLASS_TYPE_ALIAS,
isAbstract: true,
- isDeprecated: _isDeprecated(declaration));
+ isDeprecated: isDeprecated(declaration));
}
}
@@ -269,12 +150,12 @@ class _LocalVisitor extends LocalDeclarationVisitor {
declaration.name,
NO_RETURN_TYPE,
protocol.ElementKind.ENUM,
- isDeprecated: _isDeprecated(declaration));
+ isDeprecated: isDeprecated(declaration));
for (EnumConstantDeclaration enumConstant in declaration.constants) {
if (!enumConstant.isSynthetic) {
_addLocalSuggestion_includeReturnValueSuggestions_enumConstant(
enumConstant, declaration,
- isDeprecated: _isDeprecated(declaration));
+ isDeprecated: isDeprecated(declaration));
}
}
}
@@ -284,7 +165,7 @@ class _LocalVisitor extends LocalDeclarationVisitor {
void declaredField(FieldDeclaration fieldDecl, VariableDeclaration varDecl) {
if (optype.includeReturnValueSuggestions &&
(!optype.inStaticMethodBody || fieldDecl.isStatic)) {
- bool deprecated = _isDeprecated(fieldDecl) || _isDeprecated(varDecl);
+ bool deprecated = isDeprecated(fieldDecl) || isDeprecated(varDecl);
TypeName typeName = fieldDecl.fields.type;
_addLocalSuggestion_includeReturnValueSuggestions(
fieldDecl.documentationComment,
@@ -326,7 +207,7 @@ class _LocalVisitor extends LocalDeclarationVisitor {
declaration.name,
typeName,
elemKind,
- isDeprecated: _isDeprecated(declaration),
+ isDeprecated: isDeprecated(declaration),
param: declaration.functionExpression.parameters,
relevance: relevance);
}
@@ -342,7 +223,7 @@ class _LocalVisitor extends LocalDeclarationVisitor {
declaration.returnType,
protocol.ElementKind.FUNCTION_TYPE_ALIAS,
isAbstract: true,
- isDeprecated: _isDeprecated(declaration));
+ isDeprecated: isDeprecated(declaration));
}
}
@@ -394,7 +275,7 @@ class _LocalVisitor extends LocalDeclarationVisitor {
typeName,
elemKind,
isAbstract: declaration.isAbstract,
- isDeprecated: _isDeprecated(declaration),
+ isDeprecated: isDeprecated(declaration),
classDecl: declaration.parent,
param: param,
relevance: relevance);
@@ -419,7 +300,7 @@ class _LocalVisitor extends LocalDeclarationVisitor {
varDecl.name,
varList.type,
protocol.ElementKind.TOP_LEVEL_VARIABLE,
- isDeprecated: _isDeprecated(varList) || _isDeprecated(varDecl),
+ isDeprecated: isDeprecated(varList) || isDeprecated(varDecl),
relevance: DART_RELEVANCE_LOCAL_TOP_LEVEL_VARIABLE);
}
}
@@ -434,9 +315,9 @@ class _LocalVisitor extends LocalDeclarationVisitor {
CompletionSuggestionKind kind = targetIsFunctionalArgument
? CompletionSuggestionKind.IDENTIFIER
: optype.suggestKind;
- CompletionSuggestion suggestion = _createLocalSuggestion(
- id, kind, isDeprecated, relevance, typeName,
- classDecl: classDecl);
+ CompletionSuggestion suggestion = createLocalSuggestion(
+ id, isDeprecated, relevance, typeName,
+ classDecl: classDecl, kind: kind);
if (suggestion != null) {
_setDocumentation(suggestion, documentationComment);
if (privateMemberRelevance != null &&
@@ -444,7 +325,7 @@ class _LocalVisitor extends LocalDeclarationVisitor {
suggestion.relevance = privateMemberRelevance;
}
suggestionMap.putIfAbsent(suggestion.completion, () => suggestion);
- suggestion.element = _createLocalElement(request.source, elemKind, id,
+ suggestion.element = createLocalElement(request.source, elemKind, id,
isAbstract: isAbstract,
isDeprecated: isDeprecated,
parameters: param?.toSource(),

Powered by Google App Engine
This is Rietveld 408576698