This commit is contained in:
@@ -16,8 +16,8 @@ vim.api.nvim_create_autocmd('VimResized', {
|
||||
})
|
||||
|
||||
--- @param low number
|
||||
---@param x number
|
||||
---@param high number
|
||||
--- @param x number
|
||||
--- @param high number
|
||||
local function clamp(low, x, high)
|
||||
x = math.max(low, x)
|
||||
x = math.min(x, high)
|
||||
@@ -458,7 +458,7 @@ function M.create_picker(opts)
|
||||
return safe_run(function() return s_items_raw:get() end)
|
||||
end
|
||||
|
||||
---@param items T[]
|
||||
--- @param items T[]
|
||||
function controller.set_items(items)
|
||||
return safe_run(function() s_items_raw:set(items) end)
|
||||
end
|
||||
@@ -478,7 +478,7 @@ function M.create_picker(opts)
|
||||
end
|
||||
|
||||
--- @param indicies number[]
|
||||
---@param ephemeral? boolean
|
||||
--- @param ephemeral? boolean
|
||||
function controller.set_selected_indices(indicies, ephemeral)
|
||||
return safe_run(function()
|
||||
if ephemeral == nil then ephemeral = false end
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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_text_object('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_text_object('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)
|
||||
|
||||
Reference in New Issue
Block a user