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 = Platform.isAndroid || Platform.isIOS;
  final safeSelectedIndex = isMobile
      ? (currentlySelectedSuggestion ?? 0).clamp(0, suggestions.length - 1)
      : selectedIndex.clamp(0, suggestions.length - 1);
  final selected = suggestions[safeSelectedIndex];
  final insertText = _extractSuggestionText(selected);

  if (insertText.isNotEmpty) {
    insertAtCurrentCursor(insertText, replaceTypedChar: true);
  }

  suggestionsNotifier.value = null;
  currentlySelectedSuggestion = 0;
}