1-based indexing rewrite
Some checks failed
NeoVim tests / plenary-tests (0.11.0) (push) Failing after 9s

This commit is contained in:
2025-04-11 13:20:46 -06:00
parent d9bb01be8b
commit 297e763b15
23 changed files with 449 additions and 572 deletions

View File

@@ -4,7 +4,7 @@ local Range = require 'u.range'
local M = {}
--- @param bracket_range Range
--- @param bracket_range u.Range
--- @param left string
--- @param right string
local function split(bracket_range, left, right)
@@ -52,7 +52,7 @@ local function split(bracket_range, left, right)
bracket_range:replace(code.lines)
end
--- @param bracket_range Range
--- @param bracket_range u.Range
--- @param left string
--- @param right string
local function join(bracket_range, left, right)

View File

@@ -53,7 +53,7 @@ local function prompt_for_bounds()
end
end
--- @param range Range
--- @param range u.Range
--- @param bounds { left: string; right: string }
local function do_surround(range, bounds)
local left = bounds.left
@@ -69,7 +69,7 @@ local function do_surround(range, bounds)
range:replace(left .. range:text() .. right)
elseif range.mode == 'V' then
local buf = Buffer.current()
local cw = CodeWriter.from_line(buf:line0(range.start.lnum):text(), buf.buf)
local cw = CodeWriter.from_line(range.start:line(), buf.buf)
-- write the left bound at the current indent level:
cw:write(left)
@@ -109,10 +109,8 @@ function _G.MySurroundOpFunc(ty)
if not vim_repeat.is_repeating() then hl = range:highlight('IncSearch', { priority = 999 }) end
local bounds = prompt_for_bounds()
if bounds == nil then
if hl then hl.clear() end
return
end
if hl then hl.clear() end
if bounds == nil then return end
do_surround(range, bounds)
end
@@ -166,10 +164,6 @@ function M.setup()
hl_clear()
if to == nil then return end
-- Re-fetch the arange, just in case this action is being repeated:
arange = get_fresh_arange()
if arange == nil then return end
if from_c == 't' then
-- For tags, we want to replace the inner text, not the tag:
local irange = Range.from_text_object('i' .. from_c, { user_defined = true })
@@ -182,7 +176,7 @@ function M.setup()
lrange:replace(to.left)
else
-- replace `from.right` with `to.right`:
local last_line = arange:line0(-1).text() --[[@as string]]
local last_line = arange:line(-1):text()
local from_right_match = last_line:match(vim.pesc(from.right) .. '$')
if from_right_match then
local match_start = arange.stop:clone()
@@ -191,7 +185,7 @@ function M.setup()
end
-- replace `from.left` with `to.left`:
local first_line = arange:line0(0).text() --[[@as string]]
local first_line = arange:line(1):text()
local from_left_match = first_line:match('^' .. vim.pesc(from.left))
if from_left_match then
local match_end = arange.start:clone()
@@ -240,11 +234,11 @@ function M.setup()
)
-- delete last line, if it is empty:
local last = buf:line0(final_range.stop.lnum)
local last = buf:line(final_range.stop.lnum)
if last:text():match '^%s*$' then last:replace(nil) end
-- delete first line, if it is empty:
local first = buf:line0(final_range.start.lnum)
local first = buf:line(final_range.start.lnum)
if first:text():match '^%s*$' then first:replace(nil) end
else
-- trim start:

View File

@@ -11,8 +11,7 @@ function M.setup()
-- Select current line:
utils.define_text_object('a.', function()
local lnum = Pos.from_pos('.').lnum
return Buffer.current():line0(lnum)
return Buffer.current():line(Pos.from_pos('.').lnum)
end)
-- Select the nearest quote: