acceptSuggestion method

void acceptSuggestion({
  1. int selectedIndex = 0,
})

Accepts the currently selected suggestion and inserts it at the cursor position.

For mobile devices, uses currentlySelectedSuggestion to determine which suggestion to accept. For desktop/non-mobile, uses the provided selectedIndex.

The method handles different suggestion types:

  • LspCompletion: Uses the label property
  • Map: Uses 'insertText' or 'label' key
  • String: Uses the string directly

After accepting, clears the suggestions and resets the selection index.

Parameters:

  • selectedIndex: The index of the selected suggestion for desktop/non-mobile. Defaults to 0 if not provided.

Implementation

void acceptSuggestion({int selectedIndex = 0}) {
  final suggestions = suggestionsNotifier.value;
  if (suggestions == null || suggestions.isEmpty) return;

  final isMobile =
      defaultTargetPlatform == TargetPlatform.android ||
      defaultTargetPlatform == TargetPlatform.iOS;

  int? index;
  if (isMobile && currentlySelectedSuggestion != null) {
    index = currentlySelectedSuggestion;
  } else {
    index = selectedIndex;
  }

  if (index == null || index < 0 || index >= suggestions.length) return;

  final suggestion = suggestions[index];
  String textToInsert;

  if (suggestion is LspCompletion) {
    textToInsert = suggestion.label;
  } else if (suggestion is Map) {
    textToInsert =
        (suggestion['insertText'] ?? suggestion['label']) as String;
  } else {
    textToInsert = suggestion.toString();
  }

  insertAtCurrentCursor(textToInsert, replaceTypedChar: true);

  suggestionsNotifier.value = null;
  currentlySelectedSuggestion = null;
}