Skip to content

Commit

Permalink
fix(select): patch #539 regression (#546)
Browse files Browse the repository at this point in the history
* fix(select): patch #539 regression

* remove useless check

* tests(select): add tests for selection mode

* fix(select): handle possible "n" as selection_mode

* fix(tests): adjust according to config keymaps

* fix(tests): adjust params

* fix(tests): do not compare the first command with itself
  • Loading branch information
phgz authored Jan 1, 2024
1 parent 0e2d5bd commit 85b9d0c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 20 deletions.
14 changes: 11 additions & 3 deletions lua/nvim-treesitter/textobjects/select.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,19 @@ function M.detect_selection_mode(query_string, keymap_mode)
selection_mode = selection_modes or "v"
end

if selection_mode == "n" then
selection_mode = "v"
local ret_value = selection_mode
local mode = vim.fn.mode(1)
local is_normal_or_charwise_v = mode == "n" or mode == "v"

if not is_normal_or_charwise_v then
-- According to "mode()" mapping, if we are in operator pending mode or visual mode,
-- then last char is {v,V,<C-v>}, exept for "no", which is "o", in which case we honor
-- last set `selection_mode`
local visual_mode = mode:sub(#mode)
ret_value = visual_mode == "o" and selection_mode or visual_mode
end

return selection_mode
return ret_value == "n" and "v" or ret_value
end

M.keymaps_per_buf = {}
Expand Down
3 changes: 3 additions & 0 deletions scripts/minimal_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ require("nvim-treesitter.configs").setup {
["as"] = "@statement.outer",
["is"] = "@statement.outer",
},
selection_modes = {
["@function.outer"] = "V", -- linewise
},
-- You can choose the select mode (default is charwise 'v')
--
-- Can also be a function which gets passed a table with the keys
Expand Down
27 changes: 15 additions & 12 deletions tests/select/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,36 @@ local M = {}
local assert = require "luassert"
local Path = require "plenary.path"

function M.run_equal_cmds_test(file, spec)
function M.run_compare_cmds_test(file, spec, equal)
assert.are.same(1, vim.fn.filereadable(file), string.format('File "%s" not readable', file))

-- load reference file
vim.cmd(string.format("edit %s", file))

local first_cmd_lines = nil
local to_compare_to = nil
for _, cmd in pairs(spec.cmds) do
-- set cursor pos
vim.api.nvim_win_set_cursor(0, { spec.row, spec.col })
-- run command
vim.cmd([[normal ]] .. vim.api.nvim_replace_termcodes(cmd, true, true, true))

local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
if first_cmd_lines == nil then
first_cmd_lines = lines
end

-- clear any changes (avoid no write since last change)
-- call before assert
vim.cmd "edit!"

assert.are.same(
first_cmd_lines,
lines,
string.format("Commands %s and %s produces different results", spec.cmds[1], cmd)
)
if to_compare_to == nil then
to_compare_to = lines
else
local assert_statement = equal and assert.are.same or assert.are.Not.same
local message = equal and "different" or "same"
assert_statement(
to_compare_to,
lines,
string.format("Commands %s and %s produces %s results", spec.cmds[1], cmd, message)
)
end
end
end

Expand All @@ -48,11 +51,11 @@ function Runner:new(it, base_dir, buf_opts)
return setmetatable(runner, self)
end

function Runner:equal_cmds(file, spec, title)
function Runner:compare_cmds(file, spec, title, equal)
title = title and title or string.format("%s,%s", spec.row, spec.col)
self.it(string.format("%s[%s]", file, title), function()
local path = self.base_dir / file
M.run_equal_cmds_test(path.filename, spec)
M.run_compare_cmds_test(path.filename, spec, equal == nil and true or equal)
end)
end

Expand Down
8 changes: 8 additions & 0 deletions tests/select/python/selection_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Test:
def __init__(self, *arg):
my_list = []

for arg_ in arg:
my_list.append(arg_)

self.my_list = my_list
12 changes: 7 additions & 5 deletions tests/select/python_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ local run = Runner:new(it, "tests/select/python", {
})

describe("command equality Python:", function()
run:equal_cmds("aligned_indent.py", { row = 1, col = 0, cmds = { "daa", "vaad", "caa" } })
run:equal_cmds("aligned_indent.py", { row = 1, col = 10, cmds = { "dia", "viad", "cia" } })
run:equal_cmds("aligned_indent.py", {
run:compare_cmds("aligned_indent.py", { row = 1, col = 0, cmds = { "daa", "vaad", "caa" } })
run:compare_cmds("aligned_indent.py", { row = 1, col = 10, cmds = { "dia", "viad", "cia" } })
run:compare_cmds("aligned_indent.py", {
row = 1,
col = 0,
cmds = {
Expand All @@ -20,7 +20,9 @@ describe("command equality Python:", function()
},
})
-- select using built-in finds (f, F, t, T)
run:equal_cmds("aligned_indent.py", { row = 1, col = 0, cmds = { "dfi", "vfid", "cfi" } })
run:compare_cmds("aligned_indent.py", { row = 1, col = 0, cmds = { "dfi", "vfid", "cfi" } })
-- select using move
run:equal_cmds("aligned_indent.py", { row = 1, col = 0, cmds = { "d]a", "v]ad", "c]a" } })
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 = 5, col = 8, cmds = { "dVao", "dao" } }, nil, false)
end)

0 comments on commit 85b9d0c

Please sign in to comment.