From e93732d75a3c3ac5248264304e7e29154f807880 Mon Sep 17 00:00:00 2001 From: Chris Grieser <73286100+chrisgrieser@users.noreply.github.com> Date: Wed, 4 Dec 2024 19:06:38 +0100 Subject: [PATCH] refactor: selection setting functions --- lua/various-textobjs/charwise-textobjs.lua | 41 +++++++++------------- lua/various-textobjs/linewise-textobjs.lua | 8 ++--- 2 files changed, 18 insertions(+), 31 deletions(-) diff --git a/lua/various-textobjs/charwise-textobjs.lua b/lua/various-textobjs/charwise-textobjs.lua index a2bbee2..6df4701 100644 --- a/lua/various-textobjs/charwise-textobjs.lua +++ b/lua/various-textobjs/charwise-textobjs.lua @@ -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 @@ -203,13 +210,7 @@ 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() @@ -217,15 +218,8 @@ function M.toNextQuotationMark() -- 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" @@ -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) @@ -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 diff --git a/lua/various-textobjs/linewise-textobjs.lua b/lua/various-textobjs/linewise-textobjs.lua index 3406a46..4600f4c 100644 --- a/lua/various-textobjs/linewise-textobjs.lua +++ b/lua/various-textobjs/linewise-textobjs.lua @@ -4,10 +4,6 @@ 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 @@ -15,7 +11,7 @@ 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 @@ -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