addSuggestion method

void addSuggestion(
  1. Element element, {
  2. String? prefix,
  3. int relevance = DART_RELEVANCE_DEFAULT,
})
inherited

Add a suggestion based upon the given element.

Implementation

void addSuggestion(
  Element element, {
  String? prefix,
  int relevance = DART_RELEVANCE_DEFAULT,
}) {
  if (element.isPrivate) {
    if (element.library != containingLibrary) {
      return;
    }
  }
  var completion = element.displayName;
  if (prefix != null && prefix.isNotEmpty) {
    if (completion.isEmpty) {
      completion = prefix;
    } else {
      completion = '$prefix.$completion';
    }
  }
  if (completion.isEmpty) {
    return;
  }
  var builder = SuggestionBuilderImpl(resourceProvider);
  var suggestion = builder.forElement(
    element,
    completion: completion,
    kind: kind,
    relevance: relevance,
  );
  if (suggestion != null) {
    if (element.isSynthetic && element is PropertyAccessorElement) {
      var cacheKey = element.name;
      if (cacheKey != null) {
        var existingSuggestion = _syntheticMap[cacheKey];

        // Pair getter/setter by updating the existing suggestion
        if (existingSuggestion != null) {
          var getter = element is GetterElement
              ? suggestion
              : existingSuggestion;
          var elemKind = element.enclosingElement is ClassElement
              ? protocol.ElementKind.FIELD
              : protocol.ElementKind.TOP_LEVEL_VARIABLE;
          existingSuggestion.element = protocol.Element(
            elemKind,
            existingSuggestion.element!.name,
            existingSuggestion.element!.flags,
            location: getter.element?.location,
            typeParameters: getter.element?.typeParameters,
            parameters: null,
            returnType: getter.returnType,
          );
          return;
        }

        // Cache lone getter/setter so that it can be paired
        _syntheticMap[cacheKey] = suggestion;
      }
    }
    if (_completions.add(suggestion.completion)) {
      suggestions.add(suggestion);
    }
  }
}