Skip to content

Commit a72a97c

Browse files
authored
fix: Visual selection highlight disappearing. (#312)
* fix: Visual selection highlight disappearing. * fix: Unit tests, `.gitignore`. * fix: Properly get cursor position when triggering visual surround.
1 parent 8f2af76 commit a72a97c

File tree

4 files changed

+66
-8
lines changed

4 files changed

+66
-8
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# git ls-files --others --exclude-from=.git/info/exclude
2+
# Lines that start with '#' are comments.
3+
# For a project mostly in C, the following would be a good set of
4+
# exclude patterns (uncomment them if you want to use them):
5+
# *.[oa]
6+
# *~
7+
.tests
8+
doc/tags

lua/nvim-surround/config.lua

+18-2
Original file line numberDiff line numberDiff line change
@@ -677,21 +677,37 @@ M.set_keymaps = function(args)
677677
M.set_keymap({
678678
mode = "x",
679679
lhs = "<Plug>(nvim-surround-visual)",
680-
rhs = "<Esc><Cmd>lua require'nvim-surround'.visual_surround({ line_mode = false })<CR>",
680+
rhs = function()
681+
local curpos = require("nvim-surround.buffer").get_curpos()
682+
return string.format(
683+
":lua require'nvim-surround'.visual_surround({ line_mode = false, curpos = { %d, %d } })<CR>",
684+
curpos[1],
685+
curpos[2]
686+
)
687+
end,
681688
opts = {
682689
buffer = args.buffer,
683690
desc = "Add a surrounding pair around a visual selection",
684691
silent = true,
692+
expr = true,
685693
},
686694
})
687695
M.set_keymap({
688696
mode = "x",
689697
lhs = "<Plug>(nvim-surround-visual-line)",
690-
rhs = "<Esc><Cmd>lua require'nvim-surround'.visual_surround({ line_mode = true })<CR>",
698+
rhs = function()
699+
local curpos = require("nvim-surround.buffer").get_curpos()
700+
return string.format(
701+
":lua require'nvim-surround'.visual_surround({ line_mode = true, curpos = { %d, %d } })<CR>",
702+
curpos[1],
703+
curpos[2]
704+
)
705+
end,
691706
opts = {
692707
buffer = args.buffer,
693708
desc = "Add a surrounding pair around a visual selection, on new lines",
694709
silent = true,
710+
expr = true,
695711
},
696712
})
697713
M.set_keymap({

lua/nvim-surround/init.lua

+2-4
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,8 @@ M.normal_surround = function(args)
7878
end
7979

8080
-- Add delimiters around a visual selection.
81-
---@param args { line_mode: boolean } Whether or not the delimiters should get put on new lines.
81+
---@param args { line_mode: boolean, curpos: position }
8282
M.visual_surround = function(args)
83-
-- Save the current position of the cursor
84-
local curpos = buffer.get_curpos()
8583
-- Get a character and selection from the user
8684
local ins_char = input.get_char()
8785

@@ -142,7 +140,7 @@ M.visual_surround = function(args)
142140
config.get_opts().indent_lines(first_pos[1], last_pos[1] + #delimiters[1] + #delimiters[2] - 2)
143141
buffer.restore_curpos({
144142
first_pos = first_pos,
145-
old_pos = curpos,
143+
old_pos = args.curpos,
146144
})
147145
end
148146

tests/minimal_init.lua

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1-
vim.opt.expandtab = true
1+
local M = {}
22

3-
require("nvim-surround").setup()
3+
function M.root(root)
4+
local f = debug.getinfo(1, "S").source:sub(2)
5+
return vim.fn.fnamemodify(f, ":p:h:h") .. "/" .. (root or "")
6+
end
7+
8+
---@param plugin string
9+
function M.load(plugin)
10+
local name = plugin:match(".*/(.*)")
11+
local package_root = M.root(".tests/site/pack/deps/start/")
12+
if not vim.loop.fs_stat(package_root .. name) then
13+
print("Installing " .. plugin)
14+
vim.fn.mkdir(package_root, "p")
15+
vim.fn.system({
16+
"git",
17+
"clone",
18+
"--depth=1",
19+
"https://github.com/" .. plugin .. ".git",
20+
package_root .. "/" .. name,
21+
})
22+
end
23+
end
24+
25+
function M.setup()
26+
vim.cmd([[set runtimepath=$VIMRUNTIME]])
27+
vim.opt.runtimepath:append(M.root())
28+
vim.opt.packpath = { M.root(".tests/site") }
29+
M.load("nvim-lua/plenary.nvim")
30+
vim.env.XDG_CONFIG_HOME = M.root(".tests/config")
31+
vim.env.XDG_DATA_HOME = M.root(".tests/data")
32+
vim.env.XDG_STATE_HOME = M.root(".tests/state")
33+
vim.env.XDG_CACHE_HOME = M.root(".tests/cache")
34+
35+
vim.opt.expandtab = true
36+
require("nvim-surround").setup()
37+
end
38+
39+
M.setup()

0 commit comments

Comments
 (0)