@@ -149,41 +149,43 @@ end
149
149
--- @param range ' prefix' | ' full'
150
150
--- @param regex string
151
151
--- @param exclude_from_prefix_regex string
152
- --- @return number[]
152
+ --- @return { start_col : number , length : number }
153
153
--- TODO: switch to return start_col, length to simplify downstream logic
154
154
function utils .get_regex_around_cursor (range , regex , exclude_from_prefix_regex )
155
155
local current_col = vim .api .nvim_win_get_cursor (0 )[2 ] + 1
156
156
local line = vim .api .nvim_get_current_line ()
157
157
158
158
-- Search backward for the start of the word
159
159
local start_col = current_col
160
+ local length = 0
160
161
while start_col > 0 do
161
162
local char = line :sub (start_col - 1 , start_col - 1 )
162
163
if char :match (regex ) == nil then break end
163
164
start_col = start_col - 1
165
+ length = length + 1
164
166
end
165
167
166
- local end_col = current_col - 1
167
-
168
168
-- Search forward for the end of the word if configured
169
169
if range == ' full' then
170
- while end_col < # line do
171
- local char = line :sub (end_col , end_col )
170
+ while start_col + length < # line do
171
+ local col = start_col + length
172
+ local char = line :sub (col , col )
172
173
if char :match (regex ) == nil then break end
173
- end_col = end_col + 1
174
+ length = length + 1
174
175
end
175
176
end
176
177
177
178
-- exclude characters matching exclude_prefix_regex from the beginning of the bounds
178
179
if exclude_from_prefix_regex ~= nil then
179
- while start_col <= end_col do
180
- local char = line :sub (start_col + 1 , start_col + 1 )
180
+ while length > 0 do
181
+ local char = line :sub (start_col , start_col )
181
182
if char :match (exclude_from_prefix_regex ) == nil then break end
182
183
start_col = start_col + 1
184
+ length = length - 1
183
185
end
184
186
end
185
187
186
- return { start_col , end_col }
188
+ return { start_col = start_col , length = length }
187
189
end
188
190
189
191
return utils
0 commit comments