Skip to content

Commit

Permalink
fix: handle exclusive selection
Browse files Browse the repository at this point in the history
  • Loading branch information
dcampos committed Dec 20, 2024
1 parent 86a0b64 commit becb4b9
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lua/snippy/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,20 @@ local function cursor_placed()
api.nvim_feedkeys(t("<cmd>lua require('snippy.buf').setup_autocmds()<CR>"), 'n', true)
end

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

local function select_stop(from, to)
api.nvim_win_set_cursor(0, { from[1] + 1, from[2] + 1 })
ensure_normal_mode()
move_cursor_to(from[1] + 1, from[2] + 1)
move_cursor_to(from[1] + 1, from[2] + 1, false)
api.nvim_feedkeys(t('v'), 'n', true)
move_cursor_to(to[1] + 1, to[2])
local exclusive = vim.o.selection == 'exclusive'
move_cursor_to(to[1] + 1, to[2], exclusive)
api.nvim_feedkeys(t('o<c-g>'), 'n', true)
cursor_placed()
end
Expand Down
66 changes: 66 additions & 0 deletions test/functional/selection_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
local helpers = require('helpers')
local command = helpers.command
local exec_lua = helpers.exec_lua

describe('Selection', function()
local screen

before_each(function()
helpers.before_each()
screen = helpers.screen
end)

after_each(function()
screen:detach()
end)

it('inclusive', function()
command('set filetype=lua')
command('set selection=inclusive')
exec_lua('snippy.expand_snippet([[local ${1:var} = ${2:val}${0}]])')
screen:expect({
grid = [[
local ^v{3:ar} = val |
{1:~ }|
{1:~ }|
{1:~ }|
{2:-- SELECT --} |
]],
})
exec_lua([[snippy.next()]])
screen:expect({
grid = [[
local var = ^v{3:al} |
{1:~ }|
{1:~ }|
{1:~ }|
{2:-- SELECT --} |
]],
})
end)

it('exclusive', function()
command('set filetype=lua')
command('set selection=exclusive')
exec_lua('snippy.expand_snippet([[local ${1:var} = ${2:val}${0}]])')
screen:expect({
grid = [[
local {3:^var} = val |
{1:~ }|
{1:~ }|
{1:~ }|
{2:-- SELECT --} |
]],
})
exec_lua([[snippy.next()]])
screen:expect({
grid = [[
local var = {3:^val} |
{1:~ }|
{1:~ }|
{1:~ }|
{2:-- SELECT --} |
]],
})
end)
end)

0 comments on commit becb4b9

Please sign in to comment.