calculateInlineSuggestion method

void calculateInlineSuggestion(
  1. ChipSuggestions<T>? _chipSuggest, {
  2. bool notify = false,
})

Implementation

void calculateInlineSuggestion(ChipSuggestions<T>? _chipSuggest,
    {bool notify = false}) {
  if (_isDisposed) return;
  // Looks for the first suggestion that actually matches what the user is typing so we
  // can add an inline suggestion
  if (query.isEmpty) {
    suggestion = Suggestion.empty();
  } else {
    /// Provides us indexed access
    final suggestions = [...?_chipSuggest?.suggestions];
    final List<Map<String, String>> allTokens = suggestions.map((chip) {
      final itemTokens = tokenizer(chip).keyed((_) => _.toLowerCase());
      return itemTokens;
    }).toList();
    _log.info("allTokens: ${allTokens.length}");

    final matchingItem = allTokens.whereIndexed(
      (entry) {
        return entry.keys.any((s) => s.startsWith(query.toLowerCase()));
      },
    ).firstOrNull;

    if (matchingItem != null) {
      suggestion = Suggestion.highlighted(
          item: suggestions[matchingItem.index],

          /// Probably the longest suggestion token would be best... this gives the most recognizability (remember that
          /// all these tokens come from the same item anyway)
          highlightText: matchingItem.value
              .whereKeys((key) => key.startsWith(query.toLowerCase()))
              .entries
              .sortedBy(((a, b) => a.key.length.compareTo(b.key.length))
                  as int Function(
                      MapEntry<String, String>?, MapEntry<String, String>?)?)
              .last
              .value);

      _log.info("Found suggestion: $_suggestion");
    } else {
      suggestion = Suggestion.empty();
    }
  }
  if (notify) {
    notifyListeners();
  }
}