Skip to content

Commit

Permalink
refactor: selection setting functions
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Dec 4, 2024
1 parent 4bdc2d0 commit febdcb8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
41 changes: 16 additions & 25 deletions lua/various-textobjs/charwise-textobjs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@ local u = require("various-textobjs.utils")
local config = require("various-textobjs.config").config
--------------------------------------------------------------------------------

---@return boolean
---@nodiscard
local function isVisualMode() return vim.fn.mode():find("v") ~= nil end

---Sets the selection for the textobj (characterwise)
---@param startPos { [1]: integer, [2]: integer }
---@param endPos { [1]: integer, [2]: integer }
function M.setSelection(startPos, endPos)
u.normal("m`") -- save last position in jumplist
vim.api.nvim_win_set_cursor(0, startPos)
u.normal(isVisualMode() and "o" or "v")
u.normal(vim.fn.mode() == "v" and "o" or "v")
vim.api.nvim_win_set_cursor(0, endPos)
end

---@param endPos { [1]: integer, [2]: integer }
---@param notFoundMsg string|number
local function selectFromCursorTo(endPos, notFoundMsg)
if #endPos ~= 2 then
u.notFoundMsg(notFoundMsg)
return
end
u.normal("m`") -- save last position in jumplist
u.normal(vim.fn.mode() == "v" and "o" or "v")
vim.api.nvim_win_set_cursor(0, endPos)
end
--------------------------------------------------------------------------------

-- INFO The following function are exposed for creation of custom textobjs, but
Expand Down Expand Up @@ -203,29 +210,16 @@ end
function M.toNextClosingBracket()
local pattern = "().([]}))"
local row, _, endCol = M.getTextobjPos(pattern, "inner", config.forwardLooking.small)
if not (row and endCol) then
u.notFoundMsg(config.forwardLooking.small)
return
end
local cursorPos = vim.api.nvim_win_get_cursor(0)

M.setSelection(cursorPos, { row, endCol })
selectFromCursorTo({ row, endCol }, config.forwardLooking.small)
end

function M.toNextQuotationMark()
-- char before quote must not be escape char. Using `vim.opt.quoteescape` on
-- the off-chance that the user has customized this.
local quoteEscape = vim.opt_local.quoteescape:get() -- default: \
local pattern = ([[()[^%s](["'`])]]):format(quoteEscape)

local row, _, endCol = M.getTextobjPos(pattern, "inner", config.forwardLooking.small)
if not (row and endCol) then
u.notFoundMsg(config.forwardLooking.small)
return
end
local cursorPos = vim.api.nvim_win_get_cursor(0)

M.setSelection(cursorPos, { row, endCol })
selectFromCursorTo({ row, endCol }, config.forwardLooking.small)
end

---@param scope "inner"|"outer"
Expand Down Expand Up @@ -264,10 +258,7 @@ end
function M.nearEoL()
local pattern = "().(%S%s*)$"
local row, _, endCol = M.getTextobjPos(pattern, "inner", 0)
if not (row and endCol) then return end
local cursorPos = vim.api.nvim_win_get_cursor(0)

M.setSelection(cursorPos, { row, endCol })
selectFromCursorTo({ row, endCol }, config.forwardLooking.small)
end

---current line (but characterwise)
Expand All @@ -287,7 +278,7 @@ function M.diagnostic(oldWrapSetting)
-- DEPRECATION (2024-12-03)
if oldWrapSetting ~= nil then
u.warn(
'`.diagnostic()` does not use "wrap" argument anymore. Use the config `textobjs.diagnostic.wrap` instead.'
'`.diagnostic()` does not use a "wrap" argument anymore. Use the config `textobjs.diagnostic.wrap` instead.'
)
end

Expand Down
8 changes: 2 additions & 6 deletions lua/various-textobjs/linewise-textobjs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@ local u = require("various-textobjs.utils")
local config = require("various-textobjs.config").config
--------------------------------------------------------------------------------

---@return boolean
---@nodiscard
local function isVisualLineMode() return vim.fn.mode():find("V") ~= nil end

---sets the selection for the textobj (linewise)
---@param startline integer
---@param endline integer
local function setLinewiseSelection(startline, endline)
-- save last position in jumplist (see #86)
u.normal("m`")
vim.api.nvim_win_set_cursor(0, { startline, 0 })
if not isVisualLineMode() then u.normal("V") end
if vim.fn.mode() ~= "V" then u.normal("V") end
u.normal("o")
vim.api.nvim_win_set_cursor(0, { endline, 0 })
end
Expand Down Expand Up @@ -79,7 +75,7 @@ end

---rest of paragraph (linewise)
function M.restOfParagraph()
if not isVisualLineMode() then u.normal("V") end
if vim.fn.mode() ~= "V" then u.normal("V") end
u.normal("}")

-- one up, except on last line
Expand Down

0 comments on commit febdcb8

Please sign in to comment.