Skip to content

Commit

Permalink
fix: Visual selection highlight disappearing. (#312)
Browse files Browse the repository at this point in the history
* fix: Visual selection highlight disappearing.

* fix: Unit tests, `.gitignore`.

* fix: Properly get cursor position when triggering visual surround.
  • Loading branch information
kylechui authored Feb 27, 2024
1 parent 8f2af76 commit a72a97c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
.tests
doc/tags
20 changes: 18 additions & 2 deletions lua/nvim-surround/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -677,21 +677,37 @@ M.set_keymaps = function(args)
M.set_keymap({
mode = "x",
lhs = "<Plug>(nvim-surround-visual)",
rhs = "<Esc><Cmd>lua require'nvim-surround'.visual_surround({ line_mode = false })<CR>",
rhs = function()
local curpos = require("nvim-surround.buffer").get_curpos()
return string.format(
":lua require'nvim-surround'.visual_surround({ line_mode = false, curpos = { %d, %d } })<CR>",
curpos[1],
curpos[2]
)
end,
opts = {
buffer = args.buffer,
desc = "Add a surrounding pair around a visual selection",
silent = true,
expr = true,
},
})
M.set_keymap({
mode = "x",
lhs = "<Plug>(nvim-surround-visual-line)",
rhs = "<Esc><Cmd>lua require'nvim-surround'.visual_surround({ line_mode = true })<CR>",
rhs = function()
local curpos = require("nvim-surround.buffer").get_curpos()
return string.format(
":lua require'nvim-surround'.visual_surround({ line_mode = true, curpos = { %d, %d } })<CR>",
curpos[1],
curpos[2]
)
end,
opts = {
buffer = args.buffer,
desc = "Add a surrounding pair around a visual selection, on new lines",
silent = true,
expr = true,
},
})
M.set_keymap({
Expand Down
6 changes: 2 additions & 4 deletions lua/nvim-surround/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,8 @@ M.normal_surround = function(args)
end

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

Expand Down Expand Up @@ -142,7 +140,7 @@ M.visual_surround = function(args)
config.get_opts().indent_lines(first_pos[1], last_pos[1] + #delimiters[1] + #delimiters[2] - 2)
buffer.restore_curpos({
first_pos = first_pos,
old_pos = curpos,
old_pos = args.curpos,
})
end

Expand Down
40 changes: 38 additions & 2 deletions tests/minimal_init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
vim.opt.expandtab = true
local M = {}

require("nvim-surround").setup()
function M.root(root)
local f = debug.getinfo(1, "S").source:sub(2)
return vim.fn.fnamemodify(f, ":p:h:h") .. "/" .. (root or "")
end

---@param plugin string
function M.load(plugin)
local name = plugin:match(".*/(.*)")
local package_root = M.root(".tests/site/pack/deps/start/")
if not vim.loop.fs_stat(package_root .. name) then
print("Installing " .. plugin)
vim.fn.mkdir(package_root, "p")
vim.fn.system({
"git",
"clone",
"--depth=1",
"https://github.com/" .. plugin .. ".git",
package_root .. "/" .. name,
})
end
end

function M.setup()
vim.cmd([[set runtimepath=$VIMRUNTIME]])
vim.opt.runtimepath:append(M.root())
vim.opt.packpath = { M.root(".tests/site") }
M.load("nvim-lua/plenary.nvim")
vim.env.XDG_CONFIG_HOME = M.root(".tests/config")
vim.env.XDG_DATA_HOME = M.root(".tests/data")
vim.env.XDG_STATE_HOME = M.root(".tests/state")
vim.env.XDG_CACHE_HOME = M.root(".tests/cache")

vim.opt.expandtab = true
require("nvim-surround").setup()
end

M.setup()

0 comments on commit a72a97c

Please sign in to comment.