9292
9393--- @param params { lookup_tables : LookupTable[] , step : number , fallback : function }
9494local function make_step_fn (params )
95- return function ()
95+ local handler_fn = function ()
9696 local cursor = vim .api .nvim_win_get_cursor (0 )
9797 local cursor_row , cursor_col = cursor [1 ] - 1 , cursor [2 ]
9898 local word_col = get_cursor_word () or cursor_col
99- local line = vim .api .nvim_buf_get_lines ( 0 , cursor_row , cursor_row + 1 , true )[ 1 ]
99+ local line = vim .api .nvim_get_current_line ()
100100
101101 local range = find_range_at_cursor ({ cursor_row , cursor_col })
102102
103- if not range then return params . fallback () end
103+ if not range then return false end
104104
105105 local _ , _ , range_end_row , range_end_col = unpack (range )
106106 local class_end_col = cursor_row == range_end_row and range_end_col or - 1
107107 local subline = line :sub (word_col + 1 , class_end_col )
108108
109109 local handler = find_best_handler (subline , params .lookup_tables )
110110
111- if not handler then return params . fallback () end
111+ if not handler then return false end
112112
113113 local lookup_table = handler .lookup_table
114114 local match = handler .term
115115
116116 if not match then
117117 for _ , term in pairs (lookup_table .sorted ) do
118118 local col = subline :find (term )
119- if col and (not match or col < match .col ) then match = { col = col - 1 , term = term } end
119+
120+ if col then
121+ match = { col = col - 1 , term = term }
122+ break
123+ end
120124 end
121125 end
122126
123- if not match then return params . fallback () end
127+ if not match then return false end
124128
125129 local index = lookup_table .reverse [match .term ]
126130
127131 if params .step == 1 and index == # lookup_table .normal then return end
128132 if params .step == - 1 and index == 1 then return end
129133
130- local start_col = cursor_col + (word_col - cursor_col ) + match .col
131- local end_col = cursor_col + (word_col - cursor_col ) + match .col + # match .term
134+ local offset = word_col - cursor_col
135+ local start_col = cursor_col + offset + match .col
136+ local end_col = start_col + # match .term
132137 local next_value = lookup_table .normal [index + params .step ]
133138
134139 -- move the cursor to the beginning of the match
@@ -142,6 +147,14 @@ local function make_step_fn(params)
142147 if params .step == - 1 and cursor_col > new_end_col then
143148 vim .api .nvim_win_set_cursor (0 , { cursor_row + 1 , new_end_col })
144149 end
150+
151+ return true
152+ end
153+
154+ return function ()
155+ for _ = 1 , vim .v .count1 do
156+ if not handler_fn () then params .fallback () end
157+ end
145158 end
146159end
147160
0 commit comments