Skip to content

Commit

Permalink
refactor: re-distribute some utils
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Jan 1, 2024
1 parent d203404 commit 5ceb1c8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
8 changes: 6 additions & 2 deletions lua/various-textobjs/blockwise-textobjs.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
local u = require("various-textobjs.utils")

local M = {}
local fn = vim.fn
local bo = vim.bo
local u = require("various-textobjs.utils")
local getCursor = vim.api.nvim_win_get_cursor

--------------------------------------------------------------------------------

---Column Textobj (blockwise down until indent or shorter line)
function M.column()
local lastLnum = fn.line("$")
local startRow = u.getCursor(0)[1]
local startRow = getCursor(0)[1]
local trueCursorCol = fn.virtcol(".") -- virtcol accurately accounts for tabs as indentation
local extraColumns = vim.v.count1 - 1 -- has to be done before running the other :normal commands, since they change v:count
local nextLnum = startRow
Expand All @@ -22,6 +25,7 @@ function M.column()
local linesDown = nextLnum - 1 - startRow

-- start visual block mode
-- INFO requires special character `^V`
if not (fn.mode() == "") then vim.cmd.execute([["normal! \<C-v>"]]) end

-- set position
Expand Down
17 changes: 9 additions & 8 deletions lua/various-textobjs/charwise-textobjs.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local M = {}
local u = require("various-textobjs.utils")
local getCursor = vim.api.nvim_win_get_cursor
--------------------------------------------------------------------------------

---@return boolean
Expand All @@ -14,13 +15,13 @@ end
---@param startPos pos
---@param endPos pos
local function setSelection(startPos, endPos)
u.setCursor(0, startPos)
vim.api.nvim_win_set_cursor(0, startPos)
if isVisualMode() then
u.normal("o")
else
u.normal("v")
end
u.setCursor(0, endPos)
vim.api.nvim_win_set_cursor(0, endPos)
end

--------------------------------------------------------------------------------
Expand All @@ -37,7 +38,7 @@ end
---@return pos? endPos
---@nodiscard
local function searchTextobj(pattern, scope, lookForwL)
local cursorRow, cursorCol = unpack(u.getCursor(0))
local cursorRow, cursorCol = unpack(getCursor(0))
local lineContent = u.getline(cursorRow)
local lastLine = vim.api.nvim_buf_line_count(0)
local beginCol = 0 ---@type number|nil
Expand Down Expand Up @@ -92,7 +93,7 @@ local function selectTextobj(patterns, scope, lookForwL)
elseif type(patterns) == "table" then
local closestRow = math.huge
local shortestDist = math.huge
local cursorCol = u.getCursor(0)[2]
local cursorCol = getCursor(0)[2]

for _, pattern in ipairs(patterns) do
local startPos, endPos = searchTextobj(pattern, scope, lookForwL)
Expand Down Expand Up @@ -149,7 +150,7 @@ function M.toNextClosingBracket(lookForwL)
u.notFoundMsg(lookForwL)
return
end
local startPos = u.getCursor(0)
local startPos = getCursor(0)

setSelection(startPos, endPos)
end
Expand All @@ -166,7 +167,7 @@ function M.toNextQuotationMark(lookForwL)
u.notFoundMsg(lookForwL)
return
end
local startPos = u.getCursor(0)
local startPos = getCursor(0)

setSelection(startPos, endPos)
end
Expand Down Expand Up @@ -208,7 +209,7 @@ function M.nearEoL()

local _, endPos = searchTextobj(pattern, "inner", 0)
if not endPos then return end
local startPos = u.getCursor(0)
local startPos = getCursor(0)

setSelection(startPos, endPos)
end
Expand Down Expand Up @@ -237,7 +238,7 @@ function M.diagnostic(lookForwL)

local nextD = vim.diagnostic.get_next { wrap = false }
local curStandingOnPrevD = false -- however, if prev diag is covered by or before the cursor has yet to be determined
local curRow, curCol = unpack(u.getCursor(0))
local curRow, curCol = unpack(getCursor(0))

if prevD then
local curAfterPrevDstart = (curRow == prevD.lnum + 1 and curCol >= prevD.col)
Expand Down
26 changes: 15 additions & 11 deletions lua/various-textobjs/linewise-textobjs.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
local u = require("various-textobjs.utils")

local M = {}
local fn = vim.fn
local a = vim.api
local getCursor = vim.api.nvim_win_get_cursor

local u = require("various-textobjs.utils")
--------------------------------------------------------------------------------

---@return boolean
Expand All @@ -15,10 +17,10 @@ end
---@param startline integer
---@param endline integer
local function setLinewiseSelection(startline, endline)
u.setCursor(0, { startline, 0 })
a.nvim_win_set_cursor(0, { startline, 0 })
if not isVisualLineMode() then u.normal("V") end
u.normal("o")
u.setCursor(0, { endline, 0 })
a.nvim_win_set_cursor(0, { endline, 0 })
end

---@param lineNr number
Expand All @@ -34,7 +36,7 @@ end
---@param scope "inner"|"outer" outer adds one line after the fold
---@param lookForwL integer number of lines to look forward for the textobj
function M.closedFold(scope, lookForwL)
local startLnum = a.nvim_win_get_cursor(0)[1]
local startLnum = getCursor(0)[1]
local lastLine = a.nvim_buf_line_count(0)
local startedOnFold = fn.foldclosed(startLnum) > 0
local foldStart, foldEnd
Expand Down Expand Up @@ -77,7 +79,7 @@ function M.restOfParagraph()
u.normal("}")

-- one up, except on last line
local curLnum = a.nvim_win_get_cursor(0)[1]
local curLnum = getCursor(0)[1]
local lastLine = a.nvim_buf_line_count(0)
if curLnum ~= lastLine then u.normal("k") end
end
Expand All @@ -86,7 +88,7 @@ end
---@param scope "inner"|"outer" inner excludes the backticks
---@param lookForwL integer number of lines to look forward for the textobj
function M.mdFencedCodeBlock(scope, lookForwL)
local cursorLnum = a.nvim_win_get_cursor(0)[1]
local cursorLnum = getCursor(0)[1]
local codeBlockPattern = "^```%w*$"

-- scan buffer for all code blocks, add beginnings & endings to a table each
Expand Down Expand Up @@ -153,7 +155,7 @@ end
function M.indentation(startBorder, endBorder, blankLines)
if not blankLines then blankLines = "withBlanks" end

local curLnum = a.nvim_win_get_cursor(0)[1]
local curLnum = getCursor(0)[1]
local lastLine = a.nvim_buf_line_count(0)
while isBlankLine(curLnum) do -- when on blank line, use next line
if lastLine == curLnum then
Expand Down Expand Up @@ -205,7 +207,7 @@ end
---from cursor position down all lines with same or higher indentation;
---essentially `ii` downwards
function M.restOfIndentation()
local startLnum = a.nvim_win_get_cursor(0)[1]
local startLnum = getCursor(0)[1]
local lastLine = a.nvim_buf_line_count(0)
local curLnum = startLnum
while isBlankLine(curLnum) do -- when on blank line, use next line
Expand Down Expand Up @@ -245,7 +247,9 @@ end
---@param lnum number
---@return boolean
local function isCommentedLine(lnum)
local commentStringRegex = "^%s*" .. vim.pesc(vim.bo.commentstring):gsub(" ?%%%%s ?", ".*") .. "%s*$"
local commentStringRegex = "^%s*"
.. vim.pesc(vim.bo.commentstring):gsub(" ?%%%%s ?", ".*")
.. "%s*$"
return u.getline(lnum):find(commentStringRegex) ~= nil
end

Expand All @@ -256,7 +260,7 @@ function M.multiCommentedLines(lookForwL)
return
end

local curLnum = a.nvim_win_get_cursor(0)[1]
local curLnum = getCursor(0)[1]
local startLnum = curLnum
local lastLine = a.nvim_buf_line_count(0)

Expand Down Expand Up @@ -298,7 +302,7 @@ function M.notebookCell(scope)
return
end

local curLnum = a.nvim_win_get_cursor(0)[1]
local curLnum = getCursor(0)[1]
local lastLine = a.nvim_buf_line_count(0)
local prevLnum = curLnum
local nextLnum = isCellBorder(curLnum) and curLnum + 1 or curLnum
Expand Down

0 comments on commit 5ceb1c8

Please sign in to comment.