Skip to content

Commit d9b5516

Browse files
committed
fix(shared) better inner parameter selection
The logic of including a point in a range needs `-1` end_col offset only if the point is from the cursor position but not from the end of another range. Fixes nvim-treesitter#700
1 parent aad2c8e commit d9b5516

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

lua/nvim-treesitter-textobjects/shared.lua

+10-11
Original file line numberDiff line numberDiff line change
@@ -225,20 +225,19 @@ end
225225
---@param row integer
226226
---@param col integer
227227
---@return boolean
228-
local function is_in_range(range, row, col)
228+
local function is_in_range(range, row, col, end_col_offset)
229229
local start_row, start_col, end_row, end_col = unpack(range) ---@type integer, integer, integer, integer
230-
end_col = end_col - 1
230+
end_col_offset = end_col_offset or 0
231231

232-
local is_in_rows = start_row <= row and end_row >= row
233-
local is_after_start_col_if_needed = true
234-
if start_row == row then
235-
is_after_start_col_if_needed = col >= start_col
232+
if start_row > row or end_row < row then
233+
return false
236234
end
237-
local is_before_end_col_if_needed = true
238-
if end_row == row then
239-
is_before_end_col_if_needed = col <= end_col
235+
236+
if start_row ~= row or col < start_col then
237+
return false
240238
end
241-
return is_in_rows and is_after_start_col_if_needed and is_before_end_col_if_needed
239+
240+
return end_row == row and col <= end_col + end_col_offset
242241
end
243242

244243
---@param range1 Range4
@@ -276,7 +275,7 @@ local function best_range_at_point(ranges, row, col, opts)
276275
local lookbehind_earliest_start ---@type integer
277276

278277
for _, range in pairs(ranges) do
279-
if range and is_in_range(M.torange4(range), row, col) then
278+
if range and is_in_range(M.torange4(range), row, col, -1) then
280279
local length = range[6] - range[3]
281280
if not range_length or length < range_length then
282281
smallest_range = range

0 commit comments

Comments
 (0)