Skip to content

Commit becb4b9

Browse files
committed
fix: handle exclusive selection
1 parent 86a0b64 commit becb4b9

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

lua/snippy/main.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,20 @@ local function cursor_placed()
3636
api.nvim_feedkeys(t("<cmd>lua require('snippy.buf').setup_autocmds()<CR>"), 'n', true)
3737
end
3838

39-
local function move_cursor_to(row, col)
39+
local function move_cursor_to(row, col, after)
4040
local line = fn.getline(row)
4141
col = math.max(fn.strchars(line:sub(1, col)) - 1, 0)
42+
col = after and col + 1 or col
4243
api.nvim_feedkeys(t(string.format('%sG0%s', row, string.rep('<Right>', col))), 'n', true)
4344
end
4445

4546
local function select_stop(from, to)
4647
api.nvim_win_set_cursor(0, { from[1] + 1, from[2] + 1 })
4748
ensure_normal_mode()
48-
move_cursor_to(from[1] + 1, from[2] + 1)
49+
move_cursor_to(from[1] + 1, from[2] + 1, false)
4950
api.nvim_feedkeys(t('v'), 'n', true)
50-
move_cursor_to(to[1] + 1, to[2])
51+
local exclusive = vim.o.selection == 'exclusive'
52+
move_cursor_to(to[1] + 1, to[2], exclusive)
5153
api.nvim_feedkeys(t('o<c-g>'), 'n', true)
5254
cursor_placed()
5355
end

test/functional/selection_spec.lua

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
local helpers = require('helpers')
2+
local command = helpers.command
3+
local exec_lua = helpers.exec_lua
4+
5+
describe('Selection', function()
6+
local screen
7+
8+
before_each(function()
9+
helpers.before_each()
10+
screen = helpers.screen
11+
end)
12+
13+
after_each(function()
14+
screen:detach()
15+
end)
16+
17+
it('inclusive', function()
18+
command('set filetype=lua')
19+
command('set selection=inclusive')
20+
exec_lua('snippy.expand_snippet([[local ${1:var} = ${2:val}${0}]])')
21+
screen:expect({
22+
grid = [[
23+
local ^v{3:ar} = val |
24+
{1:~ }|
25+
{1:~ }|
26+
{1:~ }|
27+
{2:-- SELECT --} |
28+
]],
29+
})
30+
exec_lua([[snippy.next()]])
31+
screen:expect({
32+
grid = [[
33+
local var = ^v{3:al} |
34+
{1:~ }|
35+
{1:~ }|
36+
{1:~ }|
37+
{2:-- SELECT --} |
38+
]],
39+
})
40+
end)
41+
42+
it('exclusive', function()
43+
command('set filetype=lua')
44+
command('set selection=exclusive')
45+
exec_lua('snippy.expand_snippet([[local ${1:var} = ${2:val}${0}]])')
46+
screen:expect({
47+
grid = [[
48+
local {3:^var} = val |
49+
{1:~ }|
50+
{1:~ }|
51+
{1:~ }|
52+
{2:-- SELECT --} |
53+
]],
54+
})
55+
exec_lua([[snippy.next()]])
56+
screen:expect({
57+
grid = [[
58+
local var = {3:^val} |
59+
{1:~ }|
60+
{1:~ }|
61+
{1:~ }|
62+
{2:-- SELECT --} |
63+
]],
64+
})
65+
end)
66+
end)

0 commit comments

Comments
 (0)