Compare commits

..

2 Commits

Author SHA1 Message Date
362d9e905d (stylua) lower max line length to 100
Some checks failed
NeoVim tests / plenary-tests (push) Failing after 8s
2025-04-12 08:57:11 -06:00
3fe84197c1 more cleanup 2025-04-12 08:56:10 -06:00
9 changed files with 122 additions and 63 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/.lux/ /.lux/
lux.lock
*.src.rock *.src.rock

View File

@ -24,7 +24,9 @@ function Buffer.current() return Buffer.from_nr(0) end
--- @param listed boolean --- @param listed boolean
--- @param scratch boolean --- @param scratch boolean
--- @return u.Buffer --- @return u.Buffer
function Buffer.create(listed, scratch) return Buffer.from_nr(vim.api.nvim_create_buf(listed, scratch)) end function Buffer.create(listed, scratch)
return Buffer.from_nr(vim.api.nvim_create_buf(listed, scratch))
end
function Buffer:set_tmp_options() function Buffer:set_tmp_options()
self:set_option('bufhidden', 'delete') self:set_option('bufhidden', 'delete')
@ -36,7 +38,9 @@ end
function Buffer:get_option(nm) return vim.api.nvim_get_option_value(nm, { buf = self.bufnr }) end function Buffer:get_option(nm) return vim.api.nvim_get_option_value(nm, { buf = self.bufnr }) end
--- @param nm string --- @param nm string
function Buffer:set_option(nm, val) return vim.api.nvim_set_option_value(nm, val, { buf = self.bufnr }) end function Buffer:set_option(nm, val)
return vim.api.nvim_set_option_value(nm, val, { buf = self.bufnr })
end
--- @param nm string --- @param nm string
function Buffer:get_var(nm) return vim.api.nvim_buf_get_var(self.bufnr, nm) end function Buffer:get_var(nm) return vim.api.nvim_buf_get_var(self.bufnr, nm) end
@ -67,11 +71,11 @@ end
--- @param stop number 1-based line index --- @param stop number 1-based line index
function Buffer:lines(start, stop) return Range.from_lines(self.bufnr, start, stop) end function Buffer:lines(start, stop) return Range.from_lines(self.bufnr, start, stop) end
--- @param txt_obj string --- @param motion string
--- @param opts? { contains_cursor?: boolean; pos?: u.Pos } --- @param opts? { contains_cursor?: boolean; pos?: u.Pos }
function Buffer:txtobj(txt_obj, opts) function Buffer:motion(motion, opts)
opts = vim.tbl_extend('force', opts or {}, { bufnr = self.bufnr }) opts = vim.tbl_extend('force', opts or {}, { bufnr = self.bufnr })
return Range.from_motion(txt_obj, opts) return Range.from_motion(motion, opts)
end end
--- @param event string|string[] --- @param event string|string[]

View File

@ -1,7 +1,9 @@
local M = {} local M = {}
--- @params name string --- @params name string
function M.file_for_name(name) return vim.fs.joinpath(vim.fn.stdpath 'cache', 'u.log', name .. '.log.jsonl') end function M.file_for_name(name)
return vim.fs.joinpath(vim.fn.stdpath 'cache', 'u.log', name .. '.log.jsonl')
end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Logger class -- Logger class
@ -30,7 +32,10 @@ end
function Logger:write(level, ...) function Logger:write(level, ...)
local data = { ... } local data = { ... }
if #data == 1 then data = data[1] end if #data == 1 then data = data[1] end
(vim.uv or vim.loop).fs_write(self.fd, vim.json.encode { ts = os.date(), level = level, data = data } .. '\n') (vim.uv or vim.loop).fs_write(
self.fd,
vim.json.encode { ts = os.date(), level = level, data = data } .. '\n'
)
end end
function Logger:trace(...) self:write('INFO', ...) end function Logger:trace(...) self:write('INFO', ...) end

View File

@ -2,7 +2,9 @@ local MAX_COL = vim.v.maxcol
--- @param bufnr number --- @param bufnr number
--- @param lnum number 1-based --- @param lnum number 1-based
local function line_text(bufnr, lnum) return vim.api.nvim_buf_get_lines(bufnr, lnum - 1, lnum, false)[1] end local function line_text(bufnr, lnum)
return vim.api.nvim_buf_get_lines(bufnr, lnum - 1, lnum, false)[1]
end
--- @class u.Pos --- @class u.Pos
--- @field bufnr number buffer number --- @field bufnr number buffer number
@ -54,7 +56,9 @@ end
function Pos.__lt(a, b) return a.lnum < b.lnum or (a.lnum == b.lnum and a.col < b.col) end function Pos.__lt(a, b) return a.lnum < b.lnum or (a.lnum == b.lnum and a.col < b.col) end
function Pos.__le(a, b) return a < b or a == b end function Pos.__le(a, b) return a < b or a == b end
function Pos.__eq(a, b) return Pos.is(a) and Pos.is(b) and a.bufnr == b.bufnr and a.lnum == b.lnum and a.col == b.col end function Pos.__eq(a, b)
return Pos.is(a) and Pos.is(b) and a.bufnr == b.bufnr and a.lnum == b.lnum and a.col == b.col
end
function Pos.__add(x, y) function Pos.__add(x, y)
if type(x) == 'number' then if type(x) == 'number' then
x, y = y, x x, y = y, x
@ -77,7 +81,7 @@ function Pos.from_pos(name)
return Pos.new(p[1], p[2], p[3], p[4]) return Pos.new(p[1], p[2], p[3], p[4])
end end
function Pos:is_invalid() return self.bufnr == 0 and self.lnum == 0 and self.col == 0 and self.off == 0 end function Pos:is_invalid() return self.lnum == 0 and self.col == 0 and self.off == 0 end
function Pos:clone() return Pos.new(self.bufnr, self.lnum, self.col, self.off) end function Pos:clone() return Pos.new(self.bufnr, self.lnum, self.col, self.off) end
@ -202,7 +206,9 @@ end
--- @return u.Pos|nil --- @return u.Pos|nil
function Pos:find_match(max_chars, invocations) function Pos:find_match(max_chars, invocations)
if invocations == nil then invocations = {} end if invocations == nil then invocations = {} end
if vim.tbl_contains(invocations, function(p) return self == p end, { predicate = true }) then return nil end if vim.tbl_contains(invocations, function(p) return self == p end, { predicate = true }) then
return nil
end
table.insert(invocations, self) table.insert(invocations, self)
local openers = { '{', '[', '(', '<' } local openers = { '{', '[', '(', '<' }
@ -212,7 +218,10 @@ function Pos:find_match(max_chars, invocations)
local is_closer = vim.tbl_contains(closers, c) local is_closer = vim.tbl_contains(closers, c)
if not is_opener and not is_closer then return nil end if not is_opener and not is_closer then return nil end
local i, _ = vim.iter(is_opener and openers or closers):enumerate():find(function(_, c2) return c == c2 end) local i, _ = vim
.iter(is_opener and openers or closers)
:enumerate()
:find(function(_, c2) return c == c2 end)
-- Store the character we will be looking for: -- Store the character we will be looking for:
local c_match = (is_opener and closers or openers)[i] local c_match = (is_opener and closers or openers)[i]
@ -255,7 +264,14 @@ end
--- @param lines string|string[] --- @param lines string|string[]
function Pos:insert_before(lines) function Pos:insert_before(lines)
if type(lines) == 'string' then lines = vim.split(lines, '\n') end if type(lines) == 'string' then lines = vim.split(lines, '\n') end
vim.api.nvim_buf_set_text(self.bufnr, self.lnum - 1, self.col - 1, self.lnum - 1, self.col - 1, lines) vim.api.nvim_buf_set_text(
self.bufnr,
self.lnum - 1,
self.col - 1,
self.lnum - 1,
self.col - 1,
lines
)
end end
return Pos return Pos

View File

@ -37,7 +37,13 @@ function Range.new(start, stop, mode)
local _1 = posstr(r.start) local _1 = posstr(r.start)
local _2 = posstr(r.stop) local _2 = posstr(r.stop)
return string.format('Range{bufnr=%d, mode=%s, start=%s, stop=%s}', r.start.bufnr, r.mode, _1, _2) return string.format(
'Range{bufnr=%d, mode=%s, start=%s, stop=%s}',
r.start.bufnr,
r.mode,
_1,
_2
)
end end
setmetatable(r, { __index = Range, __tostring = str }) setmetatable(r, { __index = Range, __tostring = str })
return r return r
@ -92,35 +98,35 @@ function Range.from_lines(bufnr, start_line, stop_line)
return Range.new(Pos.new(bufnr, start_line, 1), Pos.new(bufnr, stop_line, Pos.MAX_COL), 'V') return Range.new(Pos.new(bufnr, start_line, 1), Pos.new(bufnr, stop_line, Pos.MAX_COL), 'V')
end end
--- @param text_obj string --- @param motion string
--- @param opts? { bufnr?: number; contains_cursor?: boolean; pos?: u.Pos, user_defined?: boolean } --- @param opts? { bufnr?: number; contains_cursor?: boolean; pos?: u.Pos, user_defined?: boolean }
--- @return u.Range|nil --- @return u.Range|nil
function Range.from_motion(text_obj, opts) function Range.from_motion(motion, opts)
-- Options handling:
opts = opts or {} opts = opts or {}
if opts.bufnr == nil then opts.bufnr = vim.api.nvim_get_current_buf() end if opts.bufnr == nil then opts.bufnr = vim.api.nvim_get_current_buf() end
if opts.contains_cursor == nil then opts.contains_cursor = false end if opts.contains_cursor == nil then opts.contains_cursor = false end
if opts.user_defined == nil then opts.user_defined = false end if opts.user_defined == nil then opts.user_defined = false end
--- @type "a" | "i" -- Extract some information from the motion:
local selection_type = text_obj:sub(1, 1) --- @type 'a'|'i', string
local obj_type = text_obj:sub(#text_obj, #text_obj) local scope, motion_rest = motion:sub(1, 1), motion:sub(2)
local is_quote = vim.tbl_contains({ "'", '"', '`' }, obj_type) local is_txtobj = scope == 'a' or scope == 'i'
local cursor = Pos.from_pos '.' local is_quote_txtobj = is_txtobj and vim.tbl_contains({ "'", '"', '`' }, motion_rest)
--- @type u.Pos --- @type u.Pos
local start local start
--- @type u.Pos --- @type u.Pos
local stop local stop
-- Capture the original state of the buffer for restoration later.
vim.api.nvim_buf_call(opts.bufnr, function()
local original_state = { local original_state = {
winview = vim.fn.winsaveview(), winview = vim.fn.winsaveview(),
regquote = vim.fn.getreg '"', regquote = vim.fn.getreg '"',
posdot = vim.fn.getpos '.', cursor = vim.fn.getpos '.',
poslb = vim.fn.getpos "'[", pos_lbrack = vim.fn.getpos "'[",
posrb = vim.fn.getpos "']", pos_rbrack = vim.fn.getpos "']",
} }
vim.api.nvim_buf_call(opts.bufnr, function()
if opts.pos ~= nil then opts.pos:save_to_pos '.' end if opts.pos ~= nil then opts.pos:save_to_pos '.' end
Pos.invalid():save_to_pos "'[" Pos.invalid():save_to_pos "'["
@ -131,39 +137,44 @@ function Range.from_motion(text_obj, opts)
vim.cmd { vim.cmd {
cmd = 'normal', cmd = 'normal',
bang = not opts.user_defined, bang = not opts.user_defined,
args = { '""y' .. text_obj }, args = { '""y' .. motion },
mods = { silent = true }, mods = { silent = true },
} }
on_yank_enabled = prev_on_yank_enabled on_yank_enabled = prev_on_yank_enabled
start = Pos.from_pos "'[" start = Pos.from_pos "'["
stop = Pos.from_pos "']" stop = Pos.from_pos "']"
end)
-- Restore original state: -- Restore original state:
vim.fn.winrestview(original_state.winview) vim.fn.winrestview(original_state.winview)
vim.fn.setreg('"', original_state.regquote) vim.fn.setreg('"', original_state.regquote)
vim.fn.setpos('.', original_state.posdot) vim.fn.setpos('.', original_state.cursor)
vim.fn.setpos("'[", original_state.poslb) vim.fn.setpos("'[", original_state.pos_lbrack)
vim.fn.setpos("']", original_state.posrb) vim.fn.setpos("']", original_state.pos_rbrack)
if start == stop and start:is_invalid() then return nil end
-- Fixup the bounds:
if if
-- I have no idea why, but when yanking `i"`, the stop-mark is -- I have no idea why, but when yanking `i"`, the stop-mark is
-- placed on the ending quote. For other text-objects, the stop- -- placed on the ending quote. For other text-objects, the stop-
-- mark is placed before the closing character. -- mark is placed before the closing character.
(is_quote and selection_type == 'i' and stop:char() == obj_type) (is_quote_txtobj and scope == 'i' and stop:char() == motion_rest)
-- *Sigh*, this also sometimes happens for `it` as well. -- *Sigh*, this also sometimes happens for `it` as well.
or (text_obj == 'it' and stop:char() == '<') or (motion == 'it' and stop:char() == '<')
then then
stop = stop:next(-1) or stop stop = stop:next(-1) or stop
end end
end) if is_quote_txtobj and scope == 'a' then
start = start:find_next(1, motion_rest) or start
stop = stop:find_next(-1, motion_rest) or stop
end
if start == stop and start:is_invalid() then return nil end if
if opts.contains_cursor and not Range.new(start, stop):contains(cursor) then return nil end opts.contains_cursor
and not Range.new(start, stop):contains(Pos.new(unpack(original_state.cursor)))
if is_quote and selection_type == 'a' then then
start = start:find_next(1, obj_type) or start return nil
stop = stop:find_next(-1, obj_type) or stop
end end
return Range.new(start, stop) return Range.new(start, stop)
@ -244,7 +255,9 @@ function Range.smallest(ranges)
return smallest return smallest
end end
function Range:clone() return Range.new(self.start:clone(), self.stop ~= nil and self.stop:clone() or nil, self.mode) end function Range:clone()
return Range.new(self.start:clone(), self.stop ~= nil and self.stop:clone() or nil, self.mode)
end
function Range:line_count() function Range:line_count()
if self:is_empty() then return 0 end if self:is_empty() then return 0 end
return self.stop.lnum - self.start.lnum + 1 return self.stop.lnum - self.start.lnum + 1
@ -308,7 +321,8 @@ function Range:line(l)
if l < 0 then l = self:line_count() + l + 1 end if l < 0 then l = self:line_count() + l + 1 end
if l > self:line_count() then return end if l > self:line_count() then return end
local line_indices = vim.fn.getregionpos(self.start:as_vim(), self.stop:as_vim(), { type = self.mode }) local line_indices =
vim.fn.getregionpos(self.start:as_vim(), self.stop:as_vim(), { type = self.mode })
local line_bounds = line_indices[l] local line_bounds = line_indices[l]
local start = Pos.new(unpack(line_bounds[1])) local start = Pos.new(unpack(line_bounds[1]))
@ -327,7 +341,9 @@ function Range:replace(replacement)
local function update_stop_non_linewise() local function update_stop_non_linewise()
local new_last_line_num = self.start.lnum + #replacement - 1 local new_last_line_num = self.start.lnum + #replacement - 1
local new_last_col = #(replacement[#replacement] or '') local new_last_col = #(replacement[#replacement] or '')
if new_last_line_num == self.start.lnum then new_last_col = new_last_col + self.start.col - 1 end if new_last_line_num == self.start.lnum then
new_last_col = new_last_col + self.start.col - 1
end
self.stop = Pos.new(bufnr, new_last_line_num, new_last_col) self.stop = Pos.new(bufnr, new_last_line_num, new_last_col)
end end
local function update_stop_linewise() local function update_stop_linewise()
@ -397,7 +413,9 @@ end
--- @param amount number --- @param amount number
function Range:must_shrink(amount) function Range:must_shrink(amount)
local shrunk = self:shrink(amount) local shrunk = self:shrink(amount)
if shrunk == nil or shrunk:is_empty() then error 'error in Range:must_shrink: Range:shrink() returned nil' end if shrunk == nil or shrunk:is_empty() then
error 'error in Range:must_shrink: Range:shrink() returned nil'
end
return shrunk return shrunk
end end

View File

@ -218,7 +218,8 @@ function Renderer:_reconcile() -- {{{
self:_set_lines(lnum0, lnum0, true, { line_change.item }) self:_set_lines(lnum0, lnum0, true, { line_change.item })
elseif line_change.kind == 'change' then elseif line_change.kind == 'change' then
-- Compute inter-line diff, and apply: -- Compute inter-line diff, and apply:
local col_changes = H.levenshtein(vim.split(line_change.from, ''), vim.split(line_change.to, '')) local col_changes =
H.levenshtein(vim.split(line_change.from, ''), vim.split(line_change.to, ''))
for _, col_change in ipairs(col_changes) do for _, col_change in ipairs(col_changes) do
local cnum0 = col_change.index - 1 local cnum0 = col_change.index - 1
@ -310,7 +311,15 @@ function Renderer:get_pos_infos(pos0) -- {{{
-- because the NeoVim API is over-inclusive in what it returns: -- because the NeoVim API is over-inclusive in what it returns:
--- @type RendererExtmark[] --- @type RendererExtmark[]
local intersecting_extmarks = vim local intersecting_extmarks = vim
.iter(vim.api.nvim_buf_get_extmarks(self.bufnr, self.ns, pos0, pos0, { details = true, overlap = true })) .iter(
vim.api.nvim_buf_get_extmarks(
self.bufnr,
self.ns,
pos0,
pos0,
{ details = true, overlap = true }
)
)
--- @return RendererExtmark --- @return RendererExtmark
:map(function(ext) :map(function(ext)
--- @type number, number, number, { end_row?: number; end_col?: number }|nil --- @type number, number, number, { end_row?: number; end_col?: number }|nil

View File

@ -37,7 +37,10 @@ function Signal:set(value)
-- We don't handle cyclic updates: -- We don't handle cyclic updates:
if self.changing then if self.changing then
if M.debug then if M.debug then
vim.notify('circular dependency detected' .. (self.name and (' in ' .. self.name) or ''), vim.log.levels.WARN) vim.notify(
'circular dependency detected' .. (self.name and (' in ' .. self.name) or ''),
vim.log.levels.WARN
)
end end
return return
end end
@ -143,7 +146,10 @@ function Signal:debounce(ms)
local now_ms = (vim.uv or vim.loop).hrtime() / 1e6 local now_ms = (vim.uv or vim.loop).hrtime() / 1e6
-- If there is anything older than `ms` in our queue, emit it: -- If there is anything older than `ms` in our queue, emit it:
local older_than_ms = vim.iter(state.queued):filter(function(item) return now_ms - item.ts > ms end):totable() local older_than_ms = vim
.iter(state.queued)
:filter(function(item) return now_ms - item.ts > ms end)
:totable()
local last_older_than_ms = older_than_ms[#older_than_ms] local last_older_than_ms = older_than_ms[#older_than_ms]
if last_older_than_ms then if last_older_than_ms then
filtered:set(last_older_than_ms.value) filtered:set(last_older_than_ms.value)

View File

@ -1,5 +1,5 @@
package = "u.nvim" package = "u.nvim"
version = "0.1.0" version = "0.2.0"
lua = ">=5.1" lua = ">=5.1"
[description] [description]

View File

@ -1,6 +1,6 @@
call_parentheses = "None" call_parentheses = "None"
collapse_simple_statement = "Always" collapse_simple_statement = "Always"
column_width = 120 column_width = 100
indent_type = "Spaces" indent_type = "Spaces"
indent_width = 2 indent_width = 2
quote_style = "AutoPreferSingle" quote_style = "AutoPreferSingle"