Skip to content

Commit 85b9d0c

Browse files
authored
fix(select): patch #539 regression (#546)
* 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
1 parent 0e2d5bd commit 85b9d0c

File tree

5 files changed

+44
-20
lines changed

5 files changed

+44
-20
lines changed

lua/nvim-treesitter/textobjects/select.lua

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,19 @@ function M.detect_selection_mode(query_string, keymap_mode)
134134
selection_mode = selection_modes or "v"
135135
end
136136

137-
if selection_mode == "n" then
138-
selection_mode = "v"
137+
local ret_value = selection_mode
138+
local mode = vim.fn.mode(1)
139+
local is_normal_or_charwise_v = mode == "n" or mode == "v"
140+
141+
if not is_normal_or_charwise_v then
142+
-- According to "mode()" mapping, if we are in operator pending mode or visual mode,
143+
-- then last char is {v,V,<C-v>}, exept for "no", which is "o", in which case we honor
144+
-- last set `selection_mode`
145+
local visual_mode = mode:sub(#mode)
146+
ret_value = visual_mode == "o" and selection_mode or visual_mode
139147
end
140148

141-
return selection_mode
149+
return ret_value == "n" and "v" or ret_value
142150
end
143151

144152
M.keymaps_per_buf = {}

scripts/minimal_init.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ require("nvim-treesitter.configs").setup {
6666
["as"] = "@statement.outer",
6767
["is"] = "@statement.outer",
6868
},
69+
selection_modes = {
70+
["@function.outer"] = "V", -- linewise
71+
},
6972
-- You can choose the select mode (default is charwise 'v')
7073
--
7174
-- Can also be a function which gets passed a table with the keys

tests/select/common.lua

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,36 @@ local M = {}
33
local assert = require "luassert"
44
local Path = require "plenary.path"
55

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

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

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

1919
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
20-
if first_cmd_lines == nil then
21-
first_cmd_lines = lines
22-
end
2320

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

28-
assert.are.same(
29-
first_cmd_lines,
30-
lines,
31-
string.format("Commands %s and %s produces different results", spec.cmds[1], cmd)
32-
)
25+
if to_compare_to == nil then
26+
to_compare_to = lines
27+
else
28+
local assert_statement = equal and assert.are.same or assert.are.Not.same
29+
local message = equal and "different" or "same"
30+
assert_statement(
31+
to_compare_to,
32+
lines,
33+
string.format("Commands %s and %s produces %s results", spec.cmds[1], cmd, message)
34+
)
35+
end
3336
end
3437
end
3538

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

51-
function Runner:equal_cmds(file, spec, title)
54+
function Runner:compare_cmds(file, spec, title, equal)
5255
title = title and title or string.format("%s,%s", spec.row, spec.col)
5356
self.it(string.format("%s[%s]", file, title), function()
5457
local path = self.base_dir / file
55-
M.run_equal_cmds_test(path.filename, spec)
58+
M.run_compare_cmds_test(path.filename, spec, equal == nil and true or equal)
5659
end)
5760
end
5861

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Test:
2+
def __init__(self, *arg):
3+
my_list = []
4+
5+
for arg_ in arg:
6+
my_list.append(arg_)
7+
8+
self.my_list = my_list

tests/select/python_spec.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ local run = Runner:new(it, "tests/select/python", {
88
})
99

1010
describe("command equality Python:", function()
11-
run:equal_cmds("aligned_indent.py", { row = 1, col = 0, cmds = { "daa", "vaad", "caa" } })
12-
run:equal_cmds("aligned_indent.py", { row = 1, col = 10, cmds = { "dia", "viad", "cia" } })
13-
run:equal_cmds("aligned_indent.py", {
11+
run:compare_cmds("aligned_indent.py", { row = 1, col = 0, cmds = { "daa", "vaad", "caa" } })
12+
run:compare_cmds("aligned_indent.py", { row = 1, col = 10, cmds = { "dia", "viad", "cia" } })
13+
run:compare_cmds("aligned_indent.py", {
1414
row = 1,
1515
col = 0,
1616
cmds = {
@@ -20,7 +20,9 @@ describe("command equality Python:", function()
2020
},
2121
})
2222
-- select using built-in finds (f, F, t, T)
23-
run:equal_cmds("aligned_indent.py", { row = 1, col = 0, cmds = { "dfi", "vfid", "cfi" } })
23+
run:compare_cmds("aligned_indent.py", { row = 1, col = 0, cmds = { "dfi", "vfid", "cfi" } })
2424
-- select using move
25-
run:equal_cmds("aligned_indent.py", { row = 1, col = 0, cmds = { "d]a", "v]ad", "c]a" } })
25+
run:compare_cmds("aligned_indent.py", { row = 1, col = 0, cmds = { "d]a", "v]ad", "c]a" } })
26+
run:compare_cmds("selection_mode.py", { row = 2, col = 4, cmds = { "dam", "dVam", "vamd", "Vamd" } })
27+
run:compare_cmds("selection_mode.py", { row = 5, col = 8, cmds = { "dVao", "dao" } }, nil, false)
2628
end)

0 commit comments

Comments
 (0)