Skip to content

Commit 7dac618

Browse files
committed
fix(shared): better inner parameter selection
1 parent 63c4dce commit 7dac618

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

lua/nvim-treesitter-textobjects/shared.lua

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,21 +214,24 @@ end
214214
---@param range Range4
215215
---@param row integer
216216
---@param col integer
217+
---@param end_col_offset integer? nil (implies 0) or -1 - see tests/select/python/selection_mode.py
217218
---@return boolean
218-
local function is_in_range(range, row, col)
219+
local function is_in_range(range, row, col, end_col_offset)
219220
local start_row, start_col, end_row, end_col = unpack(range) ---@type integer, integer, integer, integer
220-
end_col = end_col - 1
221221

222-
local is_in_rows = start_row <= row and end_row >= row
223-
local is_after_start_col_if_needed = true
224-
if start_row == row then
225-
is_after_start_col_if_needed = col >= start_col
222+
if start_row > row or end_row < row then
223+
return false
226224
end
227-
local is_before_end_col_if_needed = true
228-
if end_row == row then
229-
is_before_end_col_if_needed = col <= end_col
225+
226+
if start_row == row and col < start_col then
227+
return false
230228
end
231-
return is_in_rows and is_after_start_col_if_needed and is_before_end_col_if_needed
229+
230+
if end_row ~= row then
231+
return true
232+
end
233+
234+
return col <= end_col + (end_col_offset or 0)
232235
end
233236

234237
---@param range1 Range4
@@ -266,7 +269,7 @@ local function best_range_at_point(ranges, row, col, opts)
266269
local lookbehind_earliest_start ---@type integer
267270

268271
for _, range in pairs(ranges) do
269-
if range and is_in_range(M.torange4(range), row, col) then
272+
if range and is_in_range(M.torange4(range), row, col, -1) then
270273
local length = range[6] - range[3]
271274
if not range_length or length < range_length then
272275
smallest_range = range

tests/select/python/selection_mode.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ def __init__(self, *arg):
66
my_list.append(arg_)
77

88
self.my_list = my_list
9+
10+
# see https://github.com/nvim-treesitter/nvim-treesitter-textobjects/issues/700
11+
print(1, type('1'), ['1'])
12+
print(['1', '2'], 3)
13+
print(['1', '2'])

tests/select/python_spec.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ describe('command equality Python:', function()
2727
run:compare_cmds('aligned_indent.py', { row = 1, col = 0, cmds = { 'd]a', 'v]ad', 'c]a' } })
2828
run:compare_cmds(
2929
'selection_mode.py',
30-
{ row = 2, col = 4, cmds = { 'dam', 'dVam', 'vamd', 'Vamd' } }
30+
{ row = 2, col = 4, cmds = { 'dam', 'dVam', 'vamd', 'Vamd', 'dG' } }
3131
)
3232
run:compare_cmds('selection_mode.py', { row = 5, col = 8, cmds = { 'dVao', 'dao' } }, nil, false)
33+
run:compare_cmds('selection_mode.py', { row = 10, col = 17, cmds = { 'dia', 'dt,' } })
34+
run:compare_cmds('selection_mode.py', { row = 11, col = 23, cmds = { 'dia', 'd%' } })
35+
run:compare_cmds('selection_mode.py', { row = 12, col = 23, cmds = { 'dia', 'd%' } })
3336
end)

0 commit comments

Comments
 (0)