@@ -813,18 +813,11 @@ class CodeForgeController implements DeltaTextInputClient {
813813 if (suggestions == null || suggestions.isEmpty) return ;
814814
815815 final isMobile = Platform .isAndroid || Platform .isIOS;
816- final selected = isMobile
817- ? suggestions[currentlySelectedSuggestion! ]
818- : suggestions[selectedIndex];
819- String insertText = '' ;
820-
821- if (selected is LspCompletion ) {
822- insertText = selected.label;
823- } else if (selected is Map ) {
824- insertText = selected['insertText' ] ?? selected['label' ] ?? '' ;
825- } else if (selected is String ) {
826- insertText = selected;
827- }
816+ final safeSelectedIndex = isMobile
817+ ? (currentlySelectedSuggestion ?? 0 ).clamp (0 , suggestions.length - 1 )
818+ : selectedIndex.clamp (0 , suggestions.length - 1 );
819+ final selected = suggestions[safeSelectedIndex];
820+ final insertText = _extractSuggestionText (selected);
828821
829822 if (insertText.isNotEmpty) {
830823 insertAtCurrentCursor (insertText, replaceTypedChar: true );
@@ -834,6 +827,37 @@ class CodeForgeController implements DeltaTextInputClient {
834827 currentlySelectedSuggestion = 0 ;
835828 }
836829
830+ String _extractSuggestionText (dynamic suggestion) {
831+ if (suggestion is LspCompletion ) {
832+ return suggestion.label;
833+ }
834+ if (suggestion is Map ) {
835+ final dynamic insertText =
836+ suggestion['insertText' ] ??
837+ suggestion['value' ] ??
838+ suggestion['label' ];
839+ return insertText is String ? insertText : '' ;
840+ }
841+ if (suggestion is String ) {
842+ return suggestion;
843+ }
844+
845+ final dynamic dynamicSuggestion = suggestion;
846+ try {
847+ final dynamic insertText = dynamicSuggestion.insertText;
848+ if (insertText is String && insertText.isNotEmpty) return insertText;
849+ } catch (_) {}
850+ try {
851+ final dynamic value = dynamicSuggestion.value;
852+ if (value is String && value.isNotEmpty) return value;
853+ } catch (_) {}
854+ try {
855+ final dynamic label = dynamicSuggestion.label;
856+ if (label is String ) return label;
857+ } catch (_) {}
858+ return '' ;
859+ }
860+
837861 /// Adds a line decoration to the editor.
838862 ///
839863 /// Line decorations can highlight code ranges with background colors,
@@ -2075,7 +2099,7 @@ class CodeForgeController implements DeltaTextInputClient {
20752099 _isMobile &&
20762100 currentlySelectedSuggestion != null ) {
20772101 final sugg = suggestionsNotifier.value! [currentlySelectedSuggestion! ];
2078- final text = sugg is LspCompletion ? sugg.label : sugg as String ;
2102+ final text = _extractSuggestionText ( sugg) ;
20792103 insertAtCurrentCursor (text, replaceTypedChar: true );
20802104 suggestionsNotifier.value = null ;
20812105 currentlySelectedSuggestion = null ;
0 commit comments