Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(shared) better inner parameter selection #702

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions lua/nvim-treesitter-textobjects/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -224,21 +224,24 @@ end
---@param range Range4
---@param row integer
---@param col integer
---@param end_col_offset integer?
---@return boolean
local function is_in_range(range, row, col)
local function is_in_range(range, row, col, end_col_offset)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a brief comment on why the end_col_offset is needed (in what situation,) and a link to the issue.

local start_row, start_col, end_row, end_col = unpack(range) ---@type integer, integer, integer, integer
end_col = end_col - 1

local is_in_rows = start_row <= row and end_row >= row
local is_after_start_col_if_needed = true
if start_row == row then
is_after_start_col_if_needed = col >= start_col
if start_row > row or end_row < row then
return false
end
local is_before_end_col_if_needed = true
if end_row == row then
is_before_end_col_if_needed = col <= end_col

if start_row == row and col < start_col then
return false
end
return is_in_rows and is_after_start_col_if_needed and is_before_end_col_if_needed

if end_row ~= row then
return true
end

return col <= end_col + (end_col_offset or 0)
end

---@param range1 Range4
Expand Down Expand Up @@ -276,7 +279,7 @@ local function best_range_at_point(ranges, row, col, opts)
local lookbehind_earliest_start ---@type integer

for _, range in pairs(ranges) do
if range and is_in_range(M.torange4(range), row, col) then
if range and is_in_range(M.torange4(range), row, col, -1) then
local length = range[6] - range[3]
if not range_length or length < range_length then
smallest_range = range
Expand Down
4 changes: 4 additions & 0 deletions tests/select/python/selection_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ def __init__(self, *arg):
my_list.append(arg_)

self.my_list = my_list

print(1, type('1'), ['1'])
print(['1', '2'], 3)
print(['1', '2'])
6 changes: 5 additions & 1 deletion tests/select/python_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ describe("command equality Python:", function()
run:compare_cmds("aligned_indent.py", { row = 1, col = 0, cmds = { "dfn", "d;" } })
-- select using move
run:compare_cmds("aligned_indent.py", { row = 1, col = 0, cmds = { "d]a", "v]ad", "c]a" } })
run:compare_cmds("selection_mode.py", { row = 2, col = 4, cmds = { "dam", "dVam", "vamd", "Vamd" } })
run:compare_cmds("selection_mode.py", { row = 2, col = 4, cmds = { "dam", "dVam", "vamd", "Vamd", "dG" } })
run:compare_cmds("selection_mode.py", { row = 3, col = 8, cmds = { "kdG", "dam", "dVam", "vamd", "Vamd" } })
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dG, kdG motions break when the reference code changes slightly. Maybe better if you do something like d]M which is more meaningful

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My intention was comparing the plugin action with something with something independent, but still reliable. IIRC the test compares results and expect only equation or only difference. In the former case I got passed test when all keystrokes gave the same bad result. While the test is defined as now, let allow to compare with something reliable (line 25 alraedy contains d;).

run:compare_cmds("selection_mode.py", { row = 5, col = 8, cmds = { "dVao", "dao" } }, nil, false)
run:compare_cmds("selection_mode.py", { row = 10, col = 17, cmds = { "dia", "dt," } })
run:compare_cmds("selection_mode.py", { row = 11, col = 23, cmds = { "dia", "d%" } })
run:compare_cmds("selection_mode.py", { row = 12, col = 23, cmds = { "dia", "d%" } })
end)
Loading