Skip to content

Commit

Permalink
fix: Modifications via pattern-based selections.
Browse files Browse the repository at this point in the history
  • Loading branch information
kylechui committed Aug 2, 2022
1 parent af61634 commit 3d8c197
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
6 changes: 3 additions & 3 deletions lua/nvim-surround/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ M.default_opts = {
end,
},
},
["i"] = {
["i"] = { -- TODO: Add find/delete/change functions
add = function()
local left_delimiter = M.get_input("Enter the left delimiter: ")
if left_delimiter then
Expand Down Expand Up @@ -354,7 +354,7 @@ end

-- Returns the add key for the surround associated with a given character, if one exists.
---@param char string? The input character.
---@return function? @The function to get the delimiters to be added.
---@return fun(): string[][]? @The function to get the delimiters to be added.
M.get_add = function(char)
char = require("nvim-surround.utils").get_alias(char)
local key = M.get_opts().delimiters[char] or M.get_opts().delimiters.invalid_key_behavior
Expand All @@ -363,7 +363,7 @@ end

-- Returns the delete key for the surround associated with a given character, if one exists.
---@param char string? The input character.
---@return function? @The function to get the selections to be deleted.
---@return fun(string?): selections? @The function to get the selections to be deleted.
M.get_delete = function(char)
char = require("nvim-surround.utils").get_alias(char)
local key = M.get_opts().delimiters[char] or M.get_opts().delimiters.invalid_key_behavior
Expand Down
8 changes: 5 additions & 3 deletions lua/nvim-surround/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ M.delete_surround = function(args)
end

-- Get the selections to delete
local selections = utils.get_nearest_selections(args.del_char, "delete")
local selections = config.get_delete(args.del_char)(args.del_char)

if selections then
-- Delete the right selection first to ensure selection positions are correct
Expand All @@ -154,6 +154,7 @@ M.delete_surround = function(args)
selections.left.first_pos[1],
selections.left.first_pos[1] + selections.right.first_pos[1] - selections.left.last_pos[1]
)
buffer.set_curpos(selections.left.first_pos)
end

buffer.reset_curpos(args.curpos)
Expand All @@ -173,12 +174,13 @@ M.change_surround = function(args)
end

-- Get the selections to change
local selections = utils.get_nearest_selections(args.del_char, "change")
local selections = config.get_change(args.del_char).target(args.del_char)
if selections then
local delimiters = args.add_delimiters()
-- Change the right selection first to ensure selection positions are correct
buffer.change_selection(selections.right, delimiters[2])
buffer.change_selection(selections.left, delimiters[1])
buffer.set_curpos(selections.left.first_pos)
end

buffer.reset_curpos(args.curpos)
Expand Down Expand Up @@ -265,7 +267,7 @@ M.change_callback = function()
local del_char = utils.get_alias(utils.get_char())
local change = config.get_change(del_char)
-- Get the selections to change
local selections = utils.get_nearest_selections(del_char, "change")
local selections = config.get_change(del_char).target(del_char)
if not selections then
return
end
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-surround/textobjects.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ M.get_selection = function(char)
-- Smart quotes feature; jump to the next quote if it is on the same line
local curpos = buffer.get_curpos()
if M.is_quote(char) and vim.fn.searchpos(char, "cnW")[1] == curpos[1] then
vim.fn.cursor(vim.fn.searchpos(char, "cnW"))
vim.fn.searchpos(char, "cW")
end

buffer.set_operator_marks(char)
Expand Down

0 comments on commit 3d8c197

Please sign in to comment.