blob: c120d04ceabd1311af0e18f54e22cf51753ffe07 [file] [log] [blame]
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analysis_server/src/provisional/completion/dart/completion_dart.dart';
import 'package:analysis_server/src/services/completion/dart/suggestion_builder.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
/// A contributor that produces suggestions based on the named constructors
/// defined on a given class. More concretely, this class produces suggestions
/// for expressions of the form `C.^` or `C<E>.^`, where `C` is the name of a
/// class.
class NamedConstructorContributor extends DartCompletionContributor {
NamedConstructorContributor(
DartCompletionRequest request,
SuggestionBuilder builder,
) : super(request, builder);
@override
Future<void> computeSuggestions() async {
var node = request.target.containingNode;
if (node is ConstructorName) {
var libraryElement = request.libraryElement;
var namedType = node.type2;
var type = namedType.type;
if (type != null) {
var element = type.element;
if (element is ClassElement) {
_buildSuggestions(request, builder, libraryElement, element);
}
}
}
}
void _buildSuggestions(
DartCompletionRequest request,
SuggestionBuilder builder,
LibraryElement libElem,
ClassElement classElem) {
var isLocalClassDecl = classElem.library == libElem;
for (var constructor in classElem.constructors) {
if (isLocalClassDecl || !constructor.isPrivate) {
if (!classElem.isAbstract || constructor.isFactory) {
builder.suggestConstructor(constructor, hasClassName: true);
}
}
}
}
}