diff --git a/lua/snippy/main.lua b/lua/snippy/main.lua index bf3a903..1cfb61c 100644 --- a/lua/snippy/main.lua +++ b/lua/snippy/main.lua @@ -36,18 +36,20 @@ local function cursor_placed() api.nvim_feedkeys(t("lua require('snippy.buf').setup_autocmds()"), '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('', 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'), 'n', true) cursor_placed() end diff --git a/test/functional/selection_spec.lua b/test/functional/selection_spec.lua new file mode 100644 index 0000000..c5efec9 --- /dev/null +++ b/test/functional/selection_spec.lua @@ -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)