@@ -35,48 +35,44 @@ pub var autocomplete_suggestions: std.ArrayList([]const u8) = undefined;
3535pub var autocomplete_ui_manager : suggestion_ui.AutocompleteUI = undefined ;
3636
3737/// Last word processed for suggestions
38- pub var last_word_processed : []const u8 = "" ;
39-
40- pub const SuggestionPool = struct {
41- // Pre-allocated buffers for suggestions
42- buffers : [config .TEXT .MAX_SUGGESTIONS ][config .TEXT .MAX_SUGGESTION_LEN ]u8 ,
43- // Track which buffers are in use
44- in_use : [config .TEXT .MAX_SUGGESTIONS ]bool ,
45-
46- pub fn init () SuggestionPool {
47- return SuggestionPool {
48- .buffers = undefined ,
49- .in_use = [_ ]bool {false } ** config .TEXT .MAX_SUGGESTIONS ,
50- };
51- }
38+ pub var last_word : [256 ]u8 = undefined ;
39+ pub var last_word_len : usize = 0 ;
5240
53- pub fn allocSuggestion (self : * SuggestionPool , allocator : std.mem.Allocator , text : []const u8 ) ! []u8 {
54- // First try to find an available buffer
55- for (self .in_use , 0.. ) | in_use , i | {
56- if (! in_use and text .len < self .buffers [i ].len ) {
57- self .in_use [i ] = true ;
58- @memcpy (self .buffers [i ][0.. text .len ], text );
59- return self .buffers [i ][0.. text .len ];
60- }
61- }
41+ // Replace the clearSuggestions function with this:
42+ pub fn clearSuggestions () void {
43+ // Destroy the entire list and create a new one
44+ autocomplete_suggestions .deinit ();
45+ autocomplete_suggestions = std .ArrayList ([]const u8 ).init (gpa_allocator );
46+ }
6247
63- // Fall back to regular allocation if pool is full
64- return allocator .dupe (u8 , text );
65- }
48+ // Update getAutocompleteSuggestions
49+ pub fn getAutocompleteSuggestions () ! void {
50+ // Get current word
51+ const current_word = autocomplete_engine .current_word ;
6652
67- pub fn freeSuggestion (self : * SuggestionPool , allocator : std.mem.Allocator , text : []const u8 ) void {
68- // Check if this is one of our pooled buffers
69- for (0.. self .buffers .len ) | i | {
70- if (@intFromPtr (text .ptr ) == @intFromPtr (& self .buffers [i ][0 ])) {
71- self .in_use [i ] = false ;
72- return ;
73- }
74- }
53+ // Skip processing if the word hasn't changed
54+ if (current_word .len > 0 and current_word .len == last_word_len and
55+ std .mem .eql (u8 , current_word , last_word [0.. last_word_len ]) and
56+ autocomplete_suggestions .items .len > 0 )
57+ {
58+ return error .NoSuggestionsNeeded ;
59+ }
7560
76- // Not from our pool, so free normally
77- allocator .free (text );
61+ // Store current word for next comparison
62+ if (current_word .len > 0 and current_word .len < last_word .len ) {
63+ @memcpy (last_word [0.. current_word .len ], current_word );
64+ last_word_len = current_word .len ;
65+ } else {
66+ // Too long or empty
67+ last_word_len = 0 ;
7868 }
79- };
69+
70+ // Clear suggestions by recreating the list
71+ clearSuggestions ();
72+
73+ // Get new suggestions
74+ try autocomplete_engine .getSuggestions (& autocomplete_suggestions );
75+ }
8076
8177/// Initialize suggestion handling components
8278pub fn init (allocator : std.mem.Allocator , module_instance : anytype ) ! void {
@@ -112,40 +108,6 @@ pub fn setCurrentWord(word: []const u8) void {
112108 autocomplete_engine .setCurrentWord (word );
113109}
114110
115- /// Get autocompletion suggestions
116- pub fn getAutocompleteSuggestions () ! void {
117- // Skip processing if the word hasn't changed
118- if (std .mem .eql (u8 , autocomplete_engine .current_word , last_word_processed ) and
119- autocomplete_suggestions .items .len > 0 )
120- {
121- return error .NoSuggestionsNeeded ;
122- }
123-
124- // Store the current word as the last processed
125- const current_word = autocomplete_engine .current_word ;
126- if (last_word_processed .len > 0 ) {
127- gpa_allocator .free (last_word_processed );
128- }
129- if (! std .mem .eql (u8 , last_word_processed , current_word )) {
130- const owned_word = try gpa_allocator .dupe (u8 , current_word );
131- if (last_word_processed .len > 0 ) {
132- gpa_allocator .free (last_word_processed );
133- }
134- last_word_processed = owned_word ;
135- } else {
136- debug .debugPrint ("Reusing existing word - avoided allocation\n " , .{});
137- }
138-
139- // Clear existing suggestions
140- for (autocomplete_suggestions .items ) | item | {
141- gpa_allocator .free (item );
142- }
143- autocomplete_suggestions .clearRetainingCapacity ();
144-
145- // Get new suggestions
146- try autocomplete_engine .getSuggestions (& autocomplete_suggestions );
147- }
148-
149111/// Check if a word is spelled correctly
150112pub fn isWordCorrect (word : []const u8 ) bool {
151113 return spell_checker .isCorrect (word );
@@ -600,20 +562,9 @@ pub fn addWordToVocabulary(word: []const u8) !void {
600562/// Deinitialize components
601563pub fn deinit () void {
602564 // Free resources
603- for (suggestions .items ) | item | {
604- gpa_allocator .free (item );
605- }
606565 suggestions .deinit ();
607-
608- for (autocomplete_suggestions .items ) | item | {
609- gpa_allocator .free (item );
610- }
611566 autocomplete_suggestions .deinit ();
612567
613- if (last_word_processed .len > 0 ) {
614- gpa_allocator .free (last_word_processed );
615- }
616-
617568 spell_checker .deinit ();
618569 autocomplete_engine .deinit ();
619570 autocomplete_ui_manager .deinit ();
0 commit comments