Skip to content

Commit

Permalink
refactor: move text object mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
JRasmusBm committed Apr 6, 2024
1 parent 11e77aa commit debad75
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
35 changes: 11 additions & 24 deletions vim/ftplugin/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,18 @@ vim.cmd [[
command! PR norm gg/Ticketf[yi]/#f[v2f]p0WysaW]Wdw
]]

local function with_textobject(action)
return function()
local old_func = vim.go.operatorfunc
_G.op_func_helper = function()
_G.op_func_helper = nil
vim.go.operatorfunc = old_func
local start = vim.api.nvim_buf_get_mark(0, "[")
local end_ = vim.api.nvim_buf_get_mark(0, "]")
action(start, end_)
end
vim.go.operatorfunc = "v:lua.op_func_helper"
vim.api.nvim_feedkeys("g@", "n", false)
end
end

local replace_in_textobject = function(regex_)
return with_textobject(function(start_position, end_position)
vim.cmd(
"silent keeppatterns"
.. start_position[1]
.. ","
.. end_position[1]
.. regex_
)
end)
return require("jrasmusbm.utils.mappings").with_textobject(
function(start_position, end_position)
vim.cmd(
"silent keeppatterns"
.. start_position[1]
.. ","
.. end_position[1]
.. regex_
)
end
)
end

vim.keymap.set(
Expand Down
15 changes: 15 additions & 0 deletions vim/lua/jrasmusbm/utils/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,19 @@ M.feedkeys = function(keys)
)
end

M.with_textobject = function(action)
return function()
local old_func = vim.go.operatorfunc
_G.op_func_helper = function()
_G.op_func_helper = nil
vim.go.operatorfunc = old_func
local start = vim.api.nvim_buf_get_mark(0, "[")
local end_ = vim.api.nvim_buf_get_mark(0, "]")
action(start, end_)
end
vim.go.operatorfunc = "v:lua.op_func_helper"
vim.api.nvim_feedkeys("g@", "n", false)
end
end

return M

0 comments on commit debad75

Please sign in to comment.