update lua API to 1-based indices; add renderer
All checks were successful
NeoVim tests / plenary-tests (push) Successful in 11s

This commit is contained in:
2025-05-04 15:22:19 -06:00
parent 44a97b5baa
commit c760c495b7
32 changed files with 4309 additions and 1071 deletions

View File

@@ -1,4 +1,4 @@
local utils = require 'u.utils'
local txtobj = require 'u.txtobj'
local Pos = require 'u.pos'
local Range = require 'u.range'
local Buffer = require 'u.buffer'
@@ -7,52 +7,33 @@ local M = {}
function M.setup()
-- Select whole file:
utils.define_text_object('ag', function() return Buffer.current():all() end)
txtobj.define('ag', function() return Buffer.current():all() end)
-- Select current line:
utils.define_text_object('a.', function()
local lnum = Pos.from_pos('.').lnum
return Buffer.current():line0(lnum)
end)
txtobj.define('a.', function() return Buffer.current():line(Pos.from_pos('.').lnum) end)
-- Select the nearest quote:
utils.define_text_object('aq', function() return Range.find_nearest_quotes() end)
utils.define_text_object('iq', function()
txtobj.define('aq', function() return Range.find_nearest_quotes() end)
txtobj.define('iq', function()
local range = Range.find_nearest_quotes()
if range == nil then return end
return range:shrink(1)
end)
---Selects the next quote object (searches forward)
---@param q string
--- @param q string
local function define_quote_obj(q)
local function select_around()
-- Operator mappings are effectively running in visual mode, the way
-- `define_text_object` is implemented, so feed the keys `a${q}` to vim
-- to select the appropriate text-object
vim.cmd { cmd = 'normal', args = { 'a' .. q }, bang = true }
local function select_around() return Range.from_motion('a' .. q) end
-- Now check on the visually selected text:
local range = Range.from_vtext()
if range:is_empty() then return range.start end
range.start = range.start:find_next(1, q) or range.start
range.stop = range.stop:find_next(-1, q) or range.stop
return range
end
txtobj.define('a' .. q, function() return select_around() end)
txtobj.define('i' .. q, function()
local range = select_around()
if range == nil or range:is_empty() then return range end
utils.define_text_object('a' .. q, function() return select_around() end)
utils.define_text_object('i' .. q, function()
local range_or_pos = select_around()
if Range.is(range_or_pos) then
local start_next = range_or_pos.start:next(1)
local stop_prev = range_or_pos.stop:next(-1)
if start_next > stop_prev then return start_next end
local range = range_or_pos:shrink(1)
return range
else
return range_or_pos
end
local start_next = range.start:next(1) or range.start
local stop_prev = range.stop:next(-1)
if start_next > stop_prev then return Range.new(start_next) end
return range:shrink(1) or range
end)
end
define_quote_obj [["]]
@@ -60,36 +41,26 @@ function M.setup()
define_quote_obj [[`]]
---Selects the "last" quote object (searches backward)
---@param q string
--- @param q string
local function define_last_quote_obj(q)
local function select_around()
local curr = Pos.from_pos('.'):find_next(-1, q)
if not curr then return end
-- Reset visual selection to current context:
Range.new(curr, curr):set_visual_selection()
vim.cmd.normal('a' .. q)
local range = Range.from_vtext()
if range:is_empty() then return range.start end
range.start = range.start:find_next(1, q) or range.start
range.stop = range.stop:find_next(-1, q) or range.stop
return range
curr:save_to_pos '.'
return Range.from_motion('a' .. q)
end
utils.define_text_object('al' .. q, function() return select_around() end)
utils.define_text_object('il' .. q, function()
local range_or_pos = select_around()
if range_or_pos == nil then return end
txtobj.define('al' .. q, function() return select_around() end)
txtobj.define('il' .. q, function()
local range = select_around()
if range == nil or range:is_empty() then return range end
if Range.is(range_or_pos) then
local start_next = range_or_pos.start:next(1)
local stop_prev = range_or_pos.stop:next(-1)
if start_next > stop_prev then return start_next end
local start_next = range.start:next(1) or range.start
local stop_prev = range.stop:next(-1)
if start_next > stop_prev then return Range.new(start_next) end
local range = range_or_pos:shrink(1)
return range
else
return range_or_pos
end
return range:shrink(1) or range
end)
end
define_last_quote_obj [["]]
@@ -111,8 +82,8 @@ function M.setup()
local keybinds = { ... }
table.insert(keybinds, b)
for _, k in ipairs(keybinds) do
utils.define_text_object('al' .. k, function() return select_around() end)
utils.define_text_object('il' .. k, function()
txtobj.define('al' .. k, function() return select_around() end)
txtobj.define('il' .. k, function()
local range = select_around()
return range and range:shrink(1)
end)