1-based indexing rewrite
All checks were successful
NeoVim tests / plenary-tests (push) Successful in 14s

This commit is contained in:
2025-04-11 13:20:46 -06:00
parent d9bb01be8b
commit 0ee6caa7ba
26 changed files with 730 additions and 874 deletions

View File

@@ -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)
@@ -63,7 +63,7 @@ local function shallow_copy_arr(arr) return vim.iter(arr):totable() end
--- @generic T
--- @param opts SelectOpts<T>
function M.create_picker(opts)
function M.create_picker(opts) -- {{{
local is_in_insert_mode = vim.api.nvim_get_mode().mode:sub(1, 1) == 'i'
local stopinsert = not is_in_insert_mode
@@ -145,17 +145,19 @@ function M.create_picker(opts)
col = s_w_input_coords:get().col,
relative = 'editor',
focusable = true,
border = 'rounded',
border = vim.o.winborder or 'rounded',
}
local w_input_buf = Buffer.create(false, true)
local w_input = vim.api.nvim_open_win(w_input_buf.buf, false, w_input_cfg)
vim.wo[w_input].number = false
vim.wo[w_input].relativenumber = false
local w_input = vim.api.nvim_open_win(w_input_buf.bufnr, false, w_input_cfg)
vim.wo[w_input][0].cursorline = false
vim.wo[w_input][0].list = false
vim.wo[w_input][0].number = false
vim.wo[w_input][0].relativenumber = false
-- The following option is a signal to other plugins like 'cmp' to not mess
-- with this buffer:
vim.bo[w_input_buf.buf].buftype = 'prompt'
vim.fn.prompt_setprompt(w_input_buf.buf, '')
vim.bo[w_input_buf.bufnr].buftype = 'prompt'
vim.fn.prompt_setprompt(w_input_buf.bufnr, '')
vim.api.nvim_set_current_win(w_input)
tracker.create_effect(safe_wrap(function()
@@ -174,10 +176,10 @@ function M.create_picker(opts)
border = 'rounded',
}
local w_list_buf = Buffer.create(false, true)
local w_list = vim.api.nvim_open_win(w_list_buf.buf, false, w_list_cfg)
vim.wo[w_list].number = false
vim.wo[w_list].relativenumber = false
vim.wo[w_list].scrolloff = 0
local w_list = vim.api.nvim_open_win(w_list_buf.bufnr, false, w_list_cfg)
vim.wo[w_list][0].number = false
vim.wo[w_list][0].relativenumber = false
vim.wo[w_list][0].scrolloff = 0
tracker.create_effect(safe_wrap(function()
-- update window position/size every time the editor is resized:
w_list_cfg = vim.tbl_deep_extend('force', w_list_cfg, s_w_list_coords:get())
@@ -222,12 +224,18 @@ function M.create_picker(opts)
local filter_text = vim.trim(s_filter_text:get()):lower()
local filter_pattern = ''
local use_plain_pattern = false
if #formatted_items > 250 and #filter_text <= 3 then
filter_pattern = filter_text:gsub('%.', '%%.')
filter_pattern = filter_text
use_plain_pattern = true
elseif #formatted_items > 1000 then
filter_pattern = filter_text
use_plain_pattern = true
else
filter_pattern = '('
.. vim.iter(vim.split(filter_text, '')):map(function(c) return c .. '.*' end):join ''
.. ')'
use_plain_pattern = false
end
filter_pattern = filter_pattern:lower()
@@ -245,7 +253,14 @@ function M.create_picker(opts)
local formatted_as_string = Renderer.markup_to_string({ tree = inf.formatted }):lower()
formatted_strings[inf.orig_idx] = formatted_as_string
matches[inf.orig_idx] = string.match(formatted_as_string, filter_pattern)
if use_plain_pattern then
local x, y = formatted_as_string:find(filter_pattern, 1, true)
if x ~= nil and y ~= nil then
matches[inf.orig_idx] = formatted_as_string:sub(x, y)
end
else
matches[inf.orig_idx] = string.match(formatted_as_string, filter_pattern)
end
return matches[inf.orig_idx] ~= nil
end)
@@ -329,7 +344,7 @@ function M.create_picker(opts)
-- "invalid window ID" errors):
H.unsubscribe_render_effect()
-- buftype=prompt buffers are not "temporary", so delete the buffer manually:
vim.api.nvim_buf_delete(w_input_buf.buf, { force = true })
vim.api.nvim_buf_delete(w_input_buf.bufnr, { force = true })
-- The following is not needed, since the buffer is deleted above:
-- vim.api.nvim_win_close(w_input, false)
vim.api.nvim_win_close(w_list, false)
@@ -370,9 +385,9 @@ function M.create_picker(opts)
--
-- Events
--
vim.keymap.set('i', '<Esc>', function() H.finish(true) end, { buffer = w_input_buf.buf })
vim.keymap.set('i', '<Esc>', function() H.finish(true) end, { buffer = w_input_buf.bufnr })
vim.keymap.set('i', '<CR>', function() H.finish() end, { buffer = w_input_buf.buf })
vim.keymap.set('i', '<CR>', function() H.finish() end, { buffer = w_input_buf.bufnr })
local function action_next_line()
local max_line = #s_filtered_items:get()
@@ -382,8 +397,8 @@ function M.create_picker(opts)
end
s_cursor_index:set(next_cursor_index)
end
vim.keymap.set('i', '<C-n>', safe_wrap(action_next_line), { buffer = w_input_buf.buf, desc = 'Picker: next' })
vim.keymap.set('i', '<Down>', safe_wrap(action_next_line), { buffer = w_input_buf.buf, desc = 'Picker: next' })
vim.keymap.set('i', '<C-n>', safe_wrap(action_next_line), { buffer = w_input_buf.bufnr, desc = 'Picker: next' })
vim.keymap.set('i', '<Down>', safe_wrap(action_next_line), { buffer = w_input_buf.bufnr, desc = 'Picker: next' })
local function action_prev_line()
local max_line = #s_filtered_items:get()
@@ -391,8 +406,8 @@ function M.create_picker(opts)
if next_cursor_index - s_top_offset:get() < 1 then s_top_offset:set(s_top_offset:get() - 1) end
s_cursor_index:set(next_cursor_index)
end
vim.keymap.set('i', '<C-p>', safe_wrap(action_prev_line), { buffer = w_input_buf.buf, desc = 'Picker: previous' })
vim.keymap.set('i', '<Up>', safe_wrap(action_prev_line), { buffer = w_input_buf.buf, desc = 'Picker: previous' })
vim.keymap.set('i', '<C-p>', safe_wrap(action_prev_line), { buffer = w_input_buf.bufnr, desc = 'Picker: previous' })
vim.keymap.set('i', '<Up>', safe_wrap(action_prev_line), { buffer = w_input_buf.bufnr, desc = 'Picker: previous' })
vim.keymap.set(
'i',
@@ -410,11 +425,11 @@ function M.create_picker(opts)
end
action_next_line()
end),
{ buffer = w_input_buf.buf }
{ buffer = w_input_buf.bufnr }
)
for key, fn in pairs(opts.mappings or {}) do
vim.keymap.set('i', key, safe_wrap(function() return fn(controller) end), { buffer = w_input_buf.buf })
vim.keymap.set('i', key, safe_wrap(function() return fn(controller) end), { buffer = w_input_buf.bufnr })
end
-- Render:
@@ -458,7 +473,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 +493,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
@@ -505,7 +520,7 @@ function M.create_picker(opts)
end
return controller --[[@as SelectController]]
end
end -- }}}
--------------------------------------------------------------------------------
-- END create_picker
@@ -522,6 +537,7 @@ function M.register_ui_select()
--- @param items `T`[]
--- @param opts { prompt?: string, kind?: any, format_item?: fun(item: T):string }
--- @param cb fun(item: T|nil):any
--- @diagnostic disable-next-line: duplicate-set-field
function vim.ui.select(items, opts, cb)
M.create_picker {
items = items,
@@ -552,7 +568,7 @@ end
--------------------------------------------------------------------------------
--- @param opts? { limit?: number }
function M.files(opts)
function M.files(opts) -- {{{
opts = opts or {}
opts.limit = opts.limit or 10000
@@ -706,6 +722,7 @@ function M.files(opts)
if #lines >= opts.limit then
if not job_inf.notified_over_limit then
vim.notify('Picker list is too large (truncating list to ' .. opts.limit .. ' items)', vim.log.levels.WARN)
pcall(vim.fn.jobstop, job_inf.id)
job_inf.notified_over_limit = true
end
return
@@ -726,10 +743,10 @@ function M.files(opts)
set_lines_as_items_state()
end),
})
end
end -- }}}
function M.buffers()
local cwd = vim.fn.getcwd(0, 0)
function M.buffers() -- {{{
local cwd = vim.fn.getcwd()
-- ensure that `cwd` ends with a trailing slash:
if cwd[#cwd] ~= '/' then cwd = cwd .. '/' end
@@ -831,10 +848,10 @@ function M.buffers()
end,
},
}
end
end -- }}}
local IS_CODE_SYMBOL_RUNNING = false
function M.lsp_code_symbols()
function M.lsp_code_symbols() -- {{{
if IS_CODE_SYMBOL_RUNNING then return end
IS_CODE_SYMBOL_RUNNING = true
@@ -881,13 +898,12 @@ function M.lsp_code_symbols()
-- Kick off the async operation:
vim.lsp.buf.document_symbol { on_list = STEPS._1_on_symbols }
end
end -- }}}
function M.setup()
utils.ucmd('Files', M.files)
utils.ucmd('Buffers', M.buffers)
utils.ucmd('Lspcodesymbols', M.lsp_code_symbols)
M.register_ui_select()
end
return M

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.bufnr)
-- 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
@@ -141,7 +139,7 @@ function M.setup()
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 })
local arange = Range.from_txtobj('a' .. from_c, { user_defined = true })
if arange == nil then return end
if from_c == 'q' then
from.left = arange.start:char()
@@ -166,13 +164,9 @@ 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 })
local irange = Range.from_txtobj('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))
@@ -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()
@@ -210,8 +204,8 @@ function M.setup()
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)
local irange = Range.from_txtobj('i' .. txt_obj)
local arange = Range.from_txtobj('a' .. txt_obj)
if arange == nil or irange == nil then return end
local starting_cursor_pos = arange.start:clone()
@@ -223,28 +217,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_txtobj('a' .. txt_obj)
irange = Range.from_txtobj('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:

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_txtobj('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_txtobj('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)