Skip to content

Commit f4a6509

Browse files
committed
Update suggestion_helper.dart
1 parent d236ff9 commit f4a6509

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

lib/src/code_field/code_controller/helpers/suggestions/suggestion_helper.dart

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,6 @@ class SuggestionHelper {
864864

865865
if (suggestions.isNotEmpty) {
866866
SchedulerBinding.instance.addPostFrameCallback((_) {
867-
print('fallbackToStandardSuggestions');
868867
controller.popupController.show(controller.tableNameBeforeDot, suggestions.toList());
869868
});
870869
} else {
@@ -881,8 +880,21 @@ class SuggestionHelper {
881880
if (cursorPosition <= 0 || text.isEmpty) {
882881
return null;
883882
}
883+
// Prepare customWords and suggestions for isolate
884+
final customWords = controller.autocompleter.customWords.toList();
884885

886+
// Convert suggestion categories to a simple map for isolate
887+
final Map<String, List<String>> suggestionMap = {};
888+
for (final category in controller.popupController.suggestionCategories) {
889+
suggestionMap[category.keys.first] = category.values.first.toList();
890+
}
885891
// Check if we can use isolates
892+
final request = _SuggestionRequest(
893+
text: text.substring(0, cursorPosition),
894+
prefix: '', // Empty prefix indicates we want longest matching prefix
895+
customWords: customWords,
896+
suggestions: suggestionMap,
897+
);
886898
if (_canUseIsolates) {
887899
try {
888900
// Get a send port to communicate with the isolate
@@ -891,23 +903,9 @@ class SuggestionHelper {
891903
// Create a receive port for the response
892904
final responsePort = ReceivePort();
893905

894-
// Prepare customWords and suggestions for isolate
895-
final customWords = controller.autocompleter.customWords.toList();
896-
897-
// Convert suggestion categories to a simple map for isolate
898-
final Map<String, List<String>> suggestionMap = {};
899-
for (final category in controller.popupController.suggestionCategories) {
900-
suggestionMap[category.keys.first] = category.values.first.toList();
901-
}
902-
903906
// Send the request to the isolate
904907
sendPort.send([
905-
_SuggestionRequest(
906-
text: text.substring(0, cursorPosition),
907-
prefix: '', // Empty prefix indicates we want longest matching prefix
908-
customWords: customWords,
909-
suggestions: suggestionMap,
910-
),
908+
request,
911909
responsePort.sendPort // Send the response port so isolate knows where to reply
912910
]);
913911

@@ -919,11 +917,31 @@ class SuggestionHelper {
919917
return result.prefixInfo;
920918
} catch (e) {
921919
// If an error occurs with the isolate, fall back to the main thread implementation
922-
return _getLongestMatchingPrefixOnMainThread(text);
920+
return _getSuggestions(request);
923921
}
924922
} else {
925923
// Web platform or other environment where isolates aren't supported
926-
return _getLongestMatchingPrefixOnMainThread(text);
924+
return _getSuggestions(request);
925+
}
926+
}
927+
928+
Future<Map<String, dynamic>?> _getSuggestions(_SuggestionRequest request) async {
929+
if (request.prefix.isNotEmpty) {
930+
// Handle fetching suggestions for a prefix
931+
final suggestions = await _isolateFetchSuggestions(
932+
request.prefix,
933+
request.customWords,
934+
request.suggestions,
935+
);
936+
return _SuggestionResult(suggestions: suggestions).prefixInfo;
937+
} else {
938+
// Handle finding longest matching prefix
939+
final prefixInfo = await _isolateGetLongestMatchingPrefix(
940+
request.text,
941+
request.customWords,
942+
request.suggestions,
943+
);
944+
return _SuggestionResult(prefixInfo: prefixInfo).prefixInfo;
927945
}
928946
}
929947

0 commit comments

Comments
 (0)