update lua API to 1-based indices; add renderer
All checks were successful
NeoVim tests / plenary-tests (push) Successful in 11s
All checks were successful
NeoVim tests / plenary-tests (push) Successful in 11s
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
local vim_repeat = require 'u.repeat'
|
||||
local opkeymap = require 'u.opkeymap'
|
||||
local Pos = require 'u.pos'
|
||||
local Range = require 'u.range'
|
||||
local Buffer = require 'u.buffer'
|
||||
local CodeWriter = require 'u.codewriter'
|
||||
|
||||
local M = {}
|
||||
|
||||
local ESC = vim.api.nvim_replace_termcodes('<Esc>', true, false, true)
|
||||
|
||||
local surrounds = {
|
||||
[')'] = { left = '(', right = ')' },
|
||||
['('] = { left = '( ', right = ' )' },
|
||||
@@ -21,8 +21,17 @@ local surrounds = {
|
||||
['`'] = { left = '`', right = '`' },
|
||||
}
|
||||
|
||||
---@return { left: string; right: string }|nil
|
||||
--- @type { left: string; right: string } | nil
|
||||
local CACHED_BOUNDS = nil
|
||||
|
||||
--- @return { left: string; right: string }|nil
|
||||
local function prompt_for_bounds()
|
||||
if vim_repeat.is_repeating() then
|
||||
-- If we are repeating, we don't want to prompt for bounds, because
|
||||
-- we want to reuse the last bounds:
|
||||
return CACHED_BOUNDS
|
||||
end
|
||||
|
||||
local cn = vim.fn.getchar()
|
||||
-- Check for non-printable characters:
|
||||
if type(cn) ~= 'number' or cn < 32 or cn > 126 then return end
|
||||
@@ -36,15 +45,17 @@ local function prompt_for_bounds()
|
||||
vim.keymap.del('c', '>')
|
||||
local endtag = '</' .. tag:sub(2):match '[^ >]*' .. '>'
|
||||
-- selene: allow(global_usage)
|
||||
return { left = tag, right = endtag }
|
||||
CACHED_BOUNDS = { left = tag, right = endtag }
|
||||
return CACHED_BOUNDS
|
||||
else
|
||||
-- Default surround:
|
||||
return (surrounds)[c] or { left = c, right = c }
|
||||
CACHED_BOUNDS = (surrounds)[c] or { left = c, right = c }
|
||||
return CACHED_BOUNDS
|
||||
end
|
||||
end
|
||||
|
||||
---@param range Range
|
||||
---@param bounds { left: string; right: string }
|
||||
--- @param range u.Range
|
||||
--- @param bounds { left: string; right: string }
|
||||
local function do_surround(range, bounds)
|
||||
local left = bounds.left
|
||||
local right = bounds.right
|
||||
@@ -59,7 +70,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.bufnr)
|
||||
|
||||
-- write the left bound at the current indent level:
|
||||
cw:write(left)
|
||||
@@ -86,97 +97,107 @@ local function do_surround(range, bounds)
|
||||
range.start:save_to_pos '.'
|
||||
end
|
||||
|
||||
-- Add surround:
|
||||
--- @param ty 'line' | 'char' | 'block'
|
||||
function _G.MySurroundOpFunc(ty)
|
||||
if ty == 'block' then
|
||||
-- We won't handle block-selection:
|
||||
return
|
||||
end
|
||||
|
||||
local range = Range.from_op_func(ty)
|
||||
local hl
|
||||
if not vim_repeat.is_repeating() then hl = range:highlight('IncSearch', { priority = 999 }) end
|
||||
|
||||
local bounds = prompt_for_bounds()
|
||||
if hl then hl.clear() end
|
||||
if bounds == nil then return end
|
||||
|
||||
do_surround(range, bounds)
|
||||
end
|
||||
|
||||
function M.setup()
|
||||
require('u.repeat').setup()
|
||||
|
||||
-- Visual
|
||||
vim.keymap.set('v', 'S', function()
|
||||
local c = vim.fn.getcharstr()
|
||||
vim.keymap.set('x', 'S', function()
|
||||
local range = Range.from_vtext()
|
||||
local bounds = surrounds[c] or { left = c, right = c }
|
||||
vim_repeat.run(function()
|
||||
do_surround(range, bounds)
|
||||
-- this is a visual mapping: end in normal mode:
|
||||
vim.cmd { cmd = 'normal', args = { '' }, bang = true }
|
||||
end)
|
||||
local bounds = prompt_for_bounds()
|
||||
if bounds == nil then return end
|
||||
|
||||
do_surround(range, bounds)
|
||||
-- this is a visual mapping: end in normal mode:
|
||||
vim.cmd.normal(ESC)
|
||||
end, { noremap = true, silent = true })
|
||||
|
||||
-- Change
|
||||
vim.keymap.set('n', 'cs', function()
|
||||
local from_cn = vim.fn.getchar()
|
||||
local from_cn = vim.fn.getchar() --[[@as number]]
|
||||
-- Check for non-printable characters:
|
||||
if from_cn < 32 or from_cn > 126 then return end
|
||||
local from_c = vim.fn.nr2char(from_cn)
|
||||
local from = surrounds[from_c] or { left = from_c, right = from_c }
|
||||
local function get_fresh_arange()
|
||||
local arange = Range.from_text_object('a' .. from_c, { user_defined = true })
|
||||
if arange == nil then return nil end
|
||||
if from_c == 'q' then
|
||||
from.left = arange.start:char()
|
||||
from.right = arange.stop:char()
|
||||
|
||||
vim_repeat.run_repeatable(function()
|
||||
local from_c = vim.fn.nr2char(from_cn)
|
||||
local from = surrounds[from_c] or { left = from_c, right = from_c }
|
||||
local function get_fresh_arange()
|
||||
local arange = Range.from_motion('a' .. from_c, { user_defined = true })
|
||||
if arange == nil then return end
|
||||
if from_c == 'q' then
|
||||
from.left = arange.start:char()
|
||||
from.right = arange.stop:char()
|
||||
end
|
||||
return arange
|
||||
end
|
||||
return arange
|
||||
end
|
||||
|
||||
local arange = get_fresh_arange()
|
||||
if arange == nil then return nil end
|
||||
local arange = get_fresh_arange()
|
||||
if arange == nil then return end
|
||||
|
||||
local hl_info1 = Range.new(arange.start, arange.start, 'v'):highlight('IncSearch', { priority = 999 })
|
||||
local hl_info2 = Range.new(arange.stop, arange.stop, 'v'):highlight('IncSearch', { priority = 999 })
|
||||
local hl_clear = function()
|
||||
if hl_info1 then hl_info1.clear() end
|
||||
if hl_info2 then hl_info2.clear() end
|
||||
end
|
||||
local hl_info1 = vim_repeat.is_repeating() and nil
|
||||
or Range.new(arange.start, arange.start, 'v'):highlight('IncSearch', { priority = 999 })
|
||||
local hl_info2 = vim_repeat.is_repeating() and nil
|
||||
or Range.new(arange.stop, arange.stop, 'v'):highlight('IncSearch', { priority = 999 })
|
||||
local hl_clear = function()
|
||||
if hl_info1 then hl_info1.clear() end
|
||||
if hl_info2 then hl_info2.clear() end
|
||||
end
|
||||
|
||||
local to = prompt_for_bounds()
|
||||
hl_clear()
|
||||
if to == nil then return end
|
||||
|
||||
vim_repeat.run(function()
|
||||
-- Re-fetch the arange, just in case this action is being repeated:
|
||||
arange = get_fresh_arange()
|
||||
if arange == nil then return nil end
|
||||
local to = prompt_for_bounds()
|
||||
hl_clear()
|
||||
if to == 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 })
|
||||
if arange == nil or irange == nil then return nil end
|
||||
local irange = Range.from_motion('i' .. from_c, { user_defined = true })
|
||||
if arange == nil or irange == nil then return end
|
||||
|
||||
local lrange = Range.new(arange.start, irange.start:must_next(-1))
|
||||
local rrange = Range.new(irange.stop:must_next(1), arange.stop)
|
||||
local lrange, rrange = arange:difference(irange)
|
||||
if not lrange or not rrange then return end
|
||||
|
||||
rrange:replace(to.right)
|
||||
lrange:replace(to.left)
|
||||
else
|
||||
-- replace `from.right` with `to.right`:
|
||||
local last_line = arange:line0(-1).text() --[[@as string]]
|
||||
local from_right_match = last_line:match(vim.pesc(from.right) .. '$')
|
||||
if from_right_match then
|
||||
local match_start = arange.stop:clone()
|
||||
match_start.col = match_start.col - #from_right_match + 1
|
||||
Range.new(match_start, arange.stop):replace(to.right)
|
||||
end
|
||||
local right_text = arange:sub(-1, -#from.right)
|
||||
right_text:replace(to.right)
|
||||
|
||||
-- replace `from.left` with `to.left`:
|
||||
local first_line = arange:line0(0).text() --[[@as string]]
|
||||
local from_left_match = first_line:match('^' .. vim.pesc(from.left))
|
||||
if from_left_match then
|
||||
local match_end = arange.start:clone()
|
||||
match_end.col = match_end.col + #from_left_match - 1
|
||||
Range.new(arange.start, match_end):replace(to.left)
|
||||
end
|
||||
local left_text = arange:sub(1, #from.left)
|
||||
left_text:replace(to.left)
|
||||
end
|
||||
end)
|
||||
end, { noremap = true, silent = true })
|
||||
|
||||
-- Delete
|
||||
local CACHED_DELETE_FROM = nil
|
||||
vim.keymap.set('n', 'ds', function()
|
||||
local txt_obj = vim.fn.getcharstr()
|
||||
vim_repeat.run(function()
|
||||
vim_repeat.run_repeatable(function()
|
||||
local txt_obj = vim_repeat.is_repeating() and CACHED_DELETE_FROM or vim.fn.getcharstr()
|
||||
CACHED_DELETE_FROM = txt_obj
|
||||
|
||||
local buf = Buffer.current()
|
||||
local irange = Range.from_text_object('i' .. txt_obj)
|
||||
local arange = Range.from_text_object('a' .. txt_obj)
|
||||
if arange == nil or irange == nil then return nil end
|
||||
local irange = Range.from_motion('i' .. txt_obj)
|
||||
local arange = Range.from_motion('a' .. txt_obj)
|
||||
if arange == nil or irange == nil then return end
|
||||
local starting_cursor_pos = arange.start:clone()
|
||||
|
||||
-- Now, replace `arange` with the content of `irange`. If `arange` was multiple lines,
|
||||
@@ -187,28 +208,19 @@ function M.setup()
|
||||
-- Dedenting moves the cursor, so we need to set the cursor to a consistent starting spot:
|
||||
arange.start:save_to_pos '.'
|
||||
-- Dedenting also changed the inner text, so re-acquire it:
|
||||
arange = Range.from_text_object('a' .. txt_obj)
|
||||
irange = Range.from_text_object('i' .. txt_obj)
|
||||
arange = Range.from_motion('a' .. txt_obj)
|
||||
irange = Range.from_motion('i' .. txt_obj)
|
||||
if arange == nil or irange == nil then return end -- should never be true
|
||||
arange:replace(irange:lines())
|
||||
|
||||
local final_range = Range.new(
|
||||
arange.start,
|
||||
Pos.new(
|
||||
arange.stop.buf,
|
||||
irange.start.lnum + (arange.stop.lnum + arange.start.lnum),
|
||||
arange.stop.col,
|
||||
arange.stop.off
|
||||
),
|
||||
irange.mode
|
||||
)
|
||||
-- `arange:replace(..)` updates its own `stop` position, so we will use
|
||||
-- `arange` as the final resulting range that holds the modified text
|
||||
|
||||
-- delete last line, if it is empty:
|
||||
local last = buf:line0(final_range.stop.lnum)
|
||||
local last = buf:line(arange.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(arange.start.lnum)
|
||||
if first:text():match '^%s*$' then first:replace(nil) end
|
||||
else
|
||||
-- trim start:
|
||||
@@ -220,35 +232,10 @@ function M.setup()
|
||||
end)
|
||||
end, { noremap = true, silent = true })
|
||||
|
||||
opkeymap('n', 'ys', function(range)
|
||||
local hl_info = range:highlight('IncSearch', { priority = 999 })
|
||||
|
||||
---@type { left: string; right: string }
|
||||
local bounds
|
||||
-- selene: allow(global_usage)
|
||||
if _G.my_surround_bounds ~= nil then
|
||||
-- This command was repeated with `.`, we don't need
|
||||
-- to prompt for the bounds:
|
||||
-- selene: allow(global_usage)
|
||||
bounds = _G.my_surround_bounds
|
||||
else
|
||||
local prompted_bounds = prompt_for_bounds()
|
||||
if prompted_bounds == nil and hl_info then return hl_info.clear() end
|
||||
if prompted_bounds then bounds = prompted_bounds end
|
||||
end
|
||||
|
||||
if hl_info then hl_info.clear() end
|
||||
do_surround(range, bounds)
|
||||
-- selene: allow(global_usage)
|
||||
_G.my_surround_bounds = nil
|
||||
|
||||
-- return repeatable injection
|
||||
return function()
|
||||
-- on_repeat, we "stage" the bounds that we were originally called with:
|
||||
-- selene: allow(global_usage)
|
||||
_G.my_surround_bounds = bounds
|
||||
end
|
||||
end)
|
||||
vim.keymap.set('n', 'ys', function()
|
||||
vim.o.operatorfunc = 'v:lua.MySurroundOpFunc'
|
||||
return 'g@'
|
||||
end, { expr = true })
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user