19 Commits

Author SHA1 Message Date
44a97b5baa (README.md) point to v2
All checks were successful
NeoVim tests / plenary-tests (push) Successful in 10s
2025-05-04 15:42:46 -06:00
7fb60add94 move away from vim.opt_global to vim.go
All checks were successful
NeoVim tests / plenary-tests (push) Successful in 9s
2025-03-19 22:29:33 -06:00
79499e898c cleanup some vim.cmd calls
All checks were successful
NeoVim tests / plenary-tests (push) Successful in 9s
2025-03-12 23:12:28 -06:00
de01a95cdc update example surround plugin 2025-01-05 14:17:20 -07:00
c87cc7c387 add rockspec 2024-11-12 10:04:27 -07:00
5c244cfc0a support repeated Range:replace calls/empty ranges
- Range:replace now updates its bounds to reflect the replacement
- Support the notion of an empty range
2024-11-12 07:48:23 -07:00
58c1eca4da add examples 2024-11-10 07:07:34 -07:00
d1dfc31dc6 move to GitHub 2024-11-08 22:08:53 -07:00
db6e8567c3 README.md: fix typos
All checks were successful
NeoVim tests / plenary-tests (push) Successful in 30s
2024-11-04 22:41:45 -07:00
121e0c0f7a rebrand to u.nvim
All checks were successful
NeoVim tests / plenary-tests (push) Successful in 8s
2024-10-23 17:01:12 -06:00
f3e352ceda (repeat) refactor slightly 2024-10-23 16:50:58 -06:00
6d49d1a0e4 fix undo tracking 2024-10-22 08:08:29 -06:00
3c948ac985 (bug) indicies past EOL should not error 2024-10-16 08:05:13 -06:00
8bbb2ba9c8 better Range:highlight(..) handling 2024-10-14 17:48:14 -06:00
03c5500966 (State) add track_winview() 2024-10-12 14:08:39 -06:00
e45d51cdf5 remove print 2024-10-12 00:00:06 -06:00
c2ba91893f add Range:highlight() 2024-10-11 23:50:23 -06:00
3bf3836bd7 remove use of feedkeys 2024-10-11 22:56:10 -06:00
bdd1d949de switch more APIs away from feedkeys 2024-10-11 18:22:35 -06:00
21 changed files with 881 additions and 176 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.src.rock

View File

@@ -9,7 +9,7 @@ fmt:
stylua .
test: $(PLENARY_DIR)
nvim -u NORC --headless -c 'set packpath+=~/.local/share/nvim/site' -c 'packadd plenary.nvim' -c "PlenaryBustedDirectory spec/"
NVIM_APPNAME=noplugstest nvim -u NORC --headless -c 'set packpath+=~/.local/share/nvim/site' -c 'packadd plenary.nvim' -c "PlenaryBustedDirectory spec/"
$(PLENARY_DIR):
git clone https://github.com/nvim-lua/plenary.nvim/ $(PLENARY_DIR)

View File

@@ -1,8 +1,12 @@
# Text Tools (TT)
# u.nvim
Welcome to **Text Tools (TT)** a powerful Lua library designed to enhance your text manipulation experience in NeoVim, focusing primarily on a context-aware "Range" utility. This utility allows you to work efficiently with text selections based on various conditions, in a variety of contexts, making coding and editing more intuitive and productive.
🚨🚨 **BRANCH NOTICE: further development is happening on the `v2` branch. In the future, `v2` will be merged into `master`. If you want to pin to an older version of this library, please refer to a specific commit, or the `v1` branch.** 🚨🚨
This is meant to be used as a **library**, not a plugin. On its own, `text-tools.nvim` does nothing on its own. It is meant to be used by plugin authors, to make their lives easier based on the variety of utilities I found I needed while growing my NeoVim config.
🚨🚨[CLICK HERE FOR v2](https://github.com/jrop/u.nvim/tree/v2)🚨🚨
Welcome to **u.nvim** a powerful Lua library designed to enhance your text manipulation experience in NeoVim, focusing primarily on a context-aware "Range" utility. This utility allows you to work efficiently with text selections based on various conditions, in a variety of contexts, making coding and editing more intuitive and productive.
This is meant to be used as a **library**, not a plugin. On its own, `u.nvim` does nothing. It is meant to be used by plugin authors, to make their lives easier based on the variety of utilities I found I needed while growing my NeoVim config.
## Features
@@ -13,11 +17,11 @@ This is meant to be used as a **library**, not a plugin. On its own, `text-tools
### Installation
Lazy:
lazy.nvim:
```lua
-- Setting `lazy = true` ensures that the library is only loaded
-- when `require 'tt.<utility>' is called.
{ 'jrop/text-tools.nvim', lazy = true }
-- when `require 'u.<utility>' is called.
{ 'jrop/u.nvim', lazy = true }
```
## Usage
@@ -31,7 +35,7 @@ I love NeoVim. I am coming to love Lua. I don't like 1-based indices; perhaps I
The `Range` utility is the main feature upon which most other things in this library are built, aside from a few standalone utilities. Ranges can be constructed manually, or preferably, obtained based on a variety of contexts.
```lua
local Range = require 'tt.range'
local Range = require 'u.range'
local start = Pos.new(0, 0, 0) -- Line 1, first column
local stop = Pos.new(0, 2, 0) -- Line 3, first column
@@ -109,7 +113,7 @@ range:replace(nil)
Define custom (dot-repeatable) key mappings for text objects:
```lua
local opkeymap = require 'tt.opkeymap'
local opkeymap = require 'u.opkeymap'
-- invoke this function by typing, for example, `<leader>riw`:
-- `range` will contain the bounds of the motion `iw`.
@@ -123,7 +127,7 @@ end)
To write code with indentation, use the `CodeWriter` class:
```lua
local CodeWriter = require 'tt.codewriter'
local CodeWriter = require 'u.codewriter'
local cw = CodeWriter.new()
cw:write('{')
cw:indent(function(innerCW)
@@ -139,8 +143,8 @@ cw:write('}')
Simply by returning a `Range` or a `Pos`, you can easily and quickly define your own text objects:
```lua
local utils = require 'tt.utils'
local Range = require 'tt.range'
local utils = require 'u.utils'
local Range = require 'u.range'
-- Select whole file:
utils.define_text_object('ag', function()
@@ -153,7 +157,7 @@ end)
Access and manipulate buffers easily:
```lua
local Buffer = require 'tt.buffer'
local Buffer = require 'u.buffer'
local buf = Buffer.current()
buf:line_count() -- the number of lines in the current buffer
buf:get_option '...'

86
examples/splitjoin.lua Normal file
View File

@@ -0,0 +1,86 @@
local vim_repeat = require 'u.repeat'
local CodeWriter = require 'u.codewriter'
local Range = require 'u.range'
local M = {}
---@param bracket_range Range
---@param left string
---@param right string
local function split(bracket_range, left, right)
local code = CodeWriter.from_pos(bracket_range.start)
code:write_raw(left)
local curr = bracket_range.start:next()
if curr == nil then return end
local last_start = curr
-- Sanity check: if we "skipped" past the start/stop of the overall range, then something is wrong:
-- This can happen with greater-/less- than signs that are mathematical, and not brackets:
while curr > bracket_range.start and curr < bracket_range.stop do
if vim.tbl_contains({ '{', '[', '(', '<' }, curr:char()) then
-- skip over bracketed groups:
local next = curr:find_match()
if next == nil then break end
curr = next
else
if vim.tbl_contains({ ',', ';' }, curr:char()) then
-- accumulate item:
local item = vim.trim(Range.new(last_start, curr):text())
if item ~= '' then code:indent():write(item) end
local next_last_start = curr:next()
if next_last_start == nil then break end
last_start = next_last_start
end
-- Prepare for next iteration:
local next = curr:next()
if next == nil then break end
curr = next
end
end
-- accumulate last, unfinished item:
local pos_before_right = bracket_range.stop:must_next(-1)
if last_start < pos_before_right then
local item = vim.trim(Range.new(last_start, pos_before_right):text())
if item ~= '' then code:indent():write(item) end
end
code:write(right)
bracket_range:replace(code.lines)
end
---@param bracket_range Range
---@param left string
---@param right string
local function join(bracket_range, left, right)
local inner_range = Range.new(bracket_range.start:must_next(), bracket_range.stop:must_next(-1), bracket_range.mode)
local newline = vim
.iter(inner_range:lines())
:map(function(l) return vim.trim(l) end)
:filter(function(l) return l ~= '' end)
:join ' '
bracket_range:replace { left .. newline .. right }
end
local function splitjoin()
local bracket_range = Range.find_nearest_brackets()
if bracket_range == nil then return end
local lines = bracket_range:lines()
local left = lines[1]:sub(1, 1) -- left bracket
local right = lines[#lines]:sub(-1, -1) -- right bracket
if #lines == 1 then
split(bracket_range, left, right)
else
join(bracket_range, left, right)
end
end
function M.setup()
vim.keymap.set('n', 'gS', function() vim_repeat.run(splitjoin) end)
end
return M

254
examples/surround.lua Normal file
View File

@@ -0,0 +1,254 @@
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 surrounds = {
[')'] = { left = '(', right = ')' },
['('] = { left = '( ', right = ' )' },
[']'] = { left = '[', right = ']' },
['['] = { left = '[ ', right = ' ]' },
['}'] = { left = '{', right = '}' },
['{'] = { left = '{ ', right = ' }' },
['>'] = { left = '<', right = '>' },
['<'] = { left = '< ', right = ' >' },
["'"] = { left = "'", right = "'" },
['"'] = { left = '"', right = '"' },
['`'] = { left = '`', right = '`' },
}
---@return { left: string; right: string }|nil
local function prompt_for_bounds()
local cn = vim.fn.getchar()
-- Check for non-printable characters:
if type(cn) ~= 'number' or cn < 32 or cn > 126 then return end
local c = vim.fn.nr2char(cn)
if c == '<' then
-- Surround with a tag:
vim.keymap.set('c', '>', '><CR>')
local tag = '<' .. vim.fn.input '<'
if tag == '<' then return end
vim.keymap.del('c', '>')
local endtag = '</' .. tag:sub(2):match '[^ >]*' .. '>'
-- selene: allow(global_usage)
return { left = tag, right = endtag }
else
-- Default surround:
return (surrounds)[c] or { left = c, right = c }
end
end
---@param range Range
---@param bounds { left: string; right: string }
local function do_surround(range, bounds)
local left = bounds.left
local right = bounds.right
if range.mode == 'V' then
-- If we are surrounding multiple lines, we don't care about
-- space-padding:
left = vim.trim(left)
right = vim.trim(right)
end
if range.mode == 'v' then
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)
-- write the left bound at the current indent level:
cw:write(left)
local curr_ident_prefix = cw.indent_str:rep(cw.indent_level)
cw:indent(function(cw2)
for _, line in ipairs(range:lines()) do
-- trim the current indent prefix from the line:
if line:sub(1, #curr_ident_prefix) == curr_ident_prefix then
--
line = line:sub(#curr_ident_prefix + 1)
end
cw2:write(line)
end
end)
-- write the right bound at the current indent level:
cw:write(right)
range:replace(cw.lines)
end
range.start:save_to_pos '.'
end
function M.setup()
require('u.repeat').setup()
-- Visual
vim.keymap.set('v', 'S', function()
local c = vim.fn.getcharstr()
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)
end, { noremap = true, silent = true })
-- Change
vim.keymap.set('n', 'cs', function()
local from_cn = vim.fn.getchar()
-- 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()
end
return arange
end
local arange = get_fresh_arange()
if arange == nil then return nil 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 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
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 lrange = Range.new(arange.start, irange.start:must_next(-1))
local rrange = Range.new(irange.stop:must_next(1), arange.stop)
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
-- 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
end
end)
end, { noremap = true, silent = true })
-- Delete
vim.keymap.set('n', 'ds', function()
local txt_obj = vim.fn.getcharstr()
vim_repeat.run(function()
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 starting_cursor_pos = arange.start:clone()
-- Now, replace `arange` with the content of `irange`. If `arange` was multiple lines,
-- dedent the contents first, and operate in linewise mode
if arange.start.lnum ~= arange.stop.lnum then
-- Auto dedent:
vim.cmd.normal('<i' .. vim.trim(txt_obj))
-- 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)
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
)
-- delete last line, if it is empty:
local last = buf:line0(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)
if first:text():match '^%s*$' then first:replace(nil) end
else
-- trim start:
irange = irange:trim_start():trim_stop()
arange:replace(irange:lines())
end
starting_cursor_pos:save_to_pos '.'
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)
end
return M

126
examples/text-objects.lua Normal file
View File

@@ -0,0 +1,126 @@
local utils = require 'u.utils'
local Pos = require 'u.pos'
local Range = require 'u.range'
local Buffer = require 'u.buffer'
local M = {}
function M.setup()
-- Select whole file:
utils.define_text_object('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)
-- Select the nearest quote:
utils.define_text_object('aq', function() return Range.find_nearest_quotes() end)
utils.define_text_object('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
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 }
-- 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
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
end)
end
define_quote_obj [["]]
define_quote_obj [[']]
define_quote_obj [[`]]
---Selects the "last" quote object (searches backward)
---@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
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
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
end)
end
define_last_quote_obj [["]]
define_last_quote_obj [[']]
define_last_quote_obj [[`]]
-- Selects the "last" bracket object (searches backward):
local function define_last_bracket_obj(b, ...)
local function select_around()
local curr = Pos.from_pos('.'):find_next(-1, b)
if not curr then return end
local other = curr:find_match(1000)
if not other then return end
return Range.new(curr, other)
end
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()
local range = select_around()
return range and range:shrink(1)
end)
end
end
define_last_bracket_obj('}', 'B')
define_last_bracket_obj ']'
define_last_bracket_obj(')', 'b')
define_last_bracket_obj '>'
end
return M

View File

@@ -1,66 +0,0 @@
local M = {}
local function _normal(cmd) vim.cmd.normal { cmd = 'normal', args = { cmd }, bang = true } end
local function _feedkeys(keys, mode)
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(keys, true, false, true), mode or 'nx', true)
end
M.native_repeat = function() _feedkeys '.' end
M.native_undo = function() _feedkeys 'u' end
---@param cmd? string|fun():unknown
function M.set(cmd)
local ts = vim.b.changedtick
vim.b.tt_changedtick = ts
if cmd ~= nil then vim.b.tt_repeatcmd = cmd end
end
---@generic T
---@param cmd string|fun():T
---@return T
function M.run(cmd)
M.set(cmd)
local result = cmd()
M.set()
return result
end
function M.do_repeat()
local ts, tt_ts, tt_cmd = vim.b.changedtick, vim.b.tt_changedtick, vim.b.tt_repeatcmd
if
-- (force formatter break)
tt_ts == nil
or tt_cmd == nil
-- has the buffer been modified after we last modified it?
or ts > tt_ts
or (type(tt_cmd) ~= 'function' and type(tt_cmd) ~= 'string')
then
return M.native_repeat()
end
-- execute the cached command:
local count = vim.api.nvim_get_vvar 'count1'
if type(tt_cmd) == 'string' then
_normal(count .. tt_cmd --[[@as string]])
else
local last_return
for _ = 1, count do
last_return = M.run(tt_cmd --[[@as fun():any]])
end
return last_return
end
end
function M.undo()
M.native_undo()
-- Update the current TS on the next event tick,
-- to make sure we get the latest
vim.schedule(M.set)
end
function M.setup()
vim.keymap.set('n', '.', M.do_repeat)
vim.keymap.set('n', 'u', M.undo)
end
return M

View File

@@ -1,4 +1,4 @@
local Range = require 'tt.range'
local Range = require 'u.range'
---@class Buffer
---@field buf number

View File

@@ -1,4 +1,4 @@
local Buffer = require 'tt.buffer'
local Buffer = require 'u.buffer'
---@class CodeWriter
---@field lines string[]

View File

@@ -1,21 +1,21 @@
local Range = require 'tt.range'
local vim_repeat = require 'tt.repeat'
local Range = require 'u.range'
local vim_repeat = require 'u.repeat'
---@type fun(range: Range): nil|(fun():any)
local __TT__OpKeymapOpFunc_rhs = nil
local __U__OpKeymapOpFunc_rhs = nil
--- This is the global utility function used for operatorfunc
--- in opkeymap
---@type nil|fun(range: Range): fun():any|nil
---@param ty 'line'|'char'|'block'
-- selene: allow(unused_variable)
function __TT__OpKeymapOpFunc(ty)
if __TT__OpKeymapOpFunc_rhs ~= nil then
function __U__OpKeymapOpFunc(ty)
if __U__OpKeymapOpFunc_rhs ~= nil then
local range = Range.from_op_func(ty)
local repeat_inject = __TT__OpKeymapOpFunc_rhs(range)
local repeat_inject = __U__OpKeymapOpFunc_rhs(range)
vim_repeat.set(function()
vim.o.operatorfunc = 'v:lua.__TT__OpKeymapOpFunc'
vim.o.operatorfunc = 'v:lua.__U__OpKeymapOpFunc'
if repeat_inject ~= nil and type(repeat_inject) == 'function' then repeat_inject() end
vim_repeat.native_repeat()
end)
@@ -34,8 +34,8 @@ end
---@param opts? vim.keymap.set.Opts
local function opkeymap(mode, lhs, rhs, opts)
vim.keymap.set(mode, lhs, function()
__TT__OpKeymapOpFunc_rhs = rhs
vim.o.operatorfunc = 'v:lua.__TT__OpKeymapOpFunc'
__U__OpKeymapOpFunc_rhs = rhs
vim.o.operatorfunc = 'v:lua.__U__OpKeymapOpFunc'
return 'g@'
end, vim.tbl_extend('force', opts or {}, { expr = true }))
end

View File

@@ -201,17 +201,8 @@ function Pos:find_match(max_chars, invocations)
local is_closer = vim.tbl_contains(closers, c)
if not is_opener and not is_closer then return nil end
---@type number
local i
---@type string
local c_match
if is_opener then
i, _ = vim.iter(openers):enumerate():find(function(_, c2) return c == c2 end)
c_match = closers[i]
else
i, _ = vim.iter(closers):enumerate():find(function(_, c2) return c == c2 end)
c_match = openers[i]
end
local i, _ = vim.iter(is_opener and openers or closers):enumerate():find(function(_, c2) return c == c2 end)
local c_match = (is_opener and closers or openers)[i]
---@type Pos|nil
local cur = self
@@ -224,13 +215,7 @@ function Pos:find_match(max_chars, invocations)
if max_chars < 0 then return nil end
end
if is_opener then
-- scan forward
return cur:next()
else
-- scan backward
return cur:next(-1)
end
return cur:next(is_opener and 1 or -1)
end
-- scan until we find a match:

View File

@@ -1,19 +1,25 @@
local Pos = require 'tt.pos'
local State = require 'tt.state'
local utils = require 'tt.utils'
local Pos = require 'u.pos'
local State = require 'u.state'
local orig_on_yank = vim.highlight.on_yank
local on_yank_enabled = true;
(vim.highlight --[[@as any]]).on_yank = function(opts)
if not on_yank_enabled then return end
return orig_on_yank(opts)
end
---@class Range
---@field start Pos
---@field stop Pos
---@field stop Pos|nil
---@field mode 'v'|'V'
local Range = {}
---@param start Pos
---@param stop Pos
---@param stop Pos|nil
---@param mode? 'v'|'V'
---@return Range
function Range.new(start, stop, mode)
if stop < start then
if stop ~= nil and stop < start then
start, stop = stop, start
end
@@ -21,7 +27,9 @@ function Range.new(start, stop, mode)
local function str()
---@param p Pos
local function posstr(p)
if p.off ~= 0 then
if p == nil then
return 'nil'
elseif p.off ~= 0 then
return string.format('Pos(%d:%d){off=%d}', p.lnum, p.col, p.off)
else
return string.format('Pos(%d:%d)', p.lnum, p.col)
@@ -105,6 +113,7 @@ function Range.from_text_object(text_obj, opts)
local positions
vim.api.nvim_buf_call(opts.buf, function()
positions = State.run(0, function(s)
s:track_winview()
s:track_register '"'
s:track_pos '.'
s:track_pos "'["
@@ -116,10 +125,15 @@ function Range.from_text_object(text_obj, opts)
null_pos:save_to_pos "'["
null_pos:save_to_pos "']"
-- For some reason,
-- vim.cmd.normal { cmd = 'normal', args = { '""y' .. text_obj }, mods = { silent = true } }
-- does not work in all cases. We resort to using feedkeys instead:
utils.feedkeys('""y' .. text_obj, opts.user_defined and 'mx' or 'nx') -- 'm' = remap, 'n' = noremap
local prev_on_yank_enabled = on_yank_enabled
on_yank_enabled = false
vim.cmd {
cmd = 'normal',
bang = not opts.user_defined,
args = { '""y' .. text_obj },
mods = { silent = true },
}
on_yank_enabled = prev_on_yank_enabled
local start = Pos.from_pos "'["
local stop = Pos.from_pos "']"
@@ -242,22 +256,27 @@ function Range.smallest(ranges)
return result
end
function Range:clone() return Range.new(self.start:clone(), self.stop:clone(), self.mode) end
function Range:line_count() return self.stop.lnum - self.start.lnum + 1 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()
if self:is_empty() then return 0 end
return self.stop.lnum - self.start.lnum + 1
end
function Range:to_linewise()
local r = self:clone()
r.mode = 'V'
r.start.col = 0
r.stop.col = Pos.MAX_COL
if r.stop ~= nil then r.stop.col = Pos.MAX_COL end
return r
end
function Range:is_empty() return self.start == self.stop end
function Range:is_empty() return self.stop == nil end
function Range:trim_start()
if self:is_empty() then return end
local r = self:clone()
while r.start:char():match '%s' do
local next = r.start:next(1)
@@ -268,6 +287,8 @@ function Range:trim_start()
end
function Range:trim_stop()
if self:is_empty() then return end
local r = self:clone()
while r.stop:char():match '%s' do
local next = r.stop:next(-1)
@@ -278,10 +299,12 @@ function Range:trim_stop()
end
---@param p Pos
function Range:contains(p) return p >= self.start and p <= self.stop end
function Range:contains(p) return not self:is_empty() and p >= self.start and p <= self.stop end
---@return string[]
function Range:lines()
if self:is_empty() then return {} end
local lines = {}
for i = 0, self.stop.lnum - self.start.lnum do
local line = self:line0(i)
@@ -301,6 +324,8 @@ function Range:sub(i, j) return self:text():sub(i, j) end
---@return { line: string; idx0: { start: number; stop: number; }; lnum: number; range: fun():Range; text: fun():string }|nil
function Range:line0(l)
if l < 0 then return self:line0(self:line_count() + l) end
if l > self:line_count() then return end
local line = vim.api.nvim_buf_get_lines(self.start.buf, self.start.lnum + l, self.start.lnum + l + 1, false)[1]
if line == nil then return end
@@ -328,31 +353,57 @@ end
---@param replacement nil|string|string[]
function Range:replace(replacement)
if replacement == nil then replacement = {} end
if type(replacement) == 'string' then replacement = vim.fn.split(replacement, '\n') end
if replacement == nil and self.mode == 'V' then
-- delete the lines:
vim.api.nvim_buf_set_lines(self.start.buf, self.start.lnum, self.stop.lnum + 1, false, {})
else
if replacement == nil then replacement = { '' } end
local buf = self.start.buf
-- convert to start-inclusive, stop-exclusive coordinates:
local start_lnum, stop_lnum = self.start.lnum, (self.stop and self.stop.lnum or self.start.lnum) + 1
local start_col, stop_col = self.start.col, (self.stop and self.stop.col or self.start.col) + 1
if self.mode == 'v' then
-- Fixup the bounds:
local last_line = vim.api.nvim_buf_get_lines(self.stop.buf, self.stop.lnum, self.stop.lnum + 1, false)[1] or ''
local max_col = #last_line
if last_line ~= '' then max_col = max_col + 1 end
local replace_type = (self:is_empty() and 'insert') or (self.mode == 'v' and 'region') or 'lines'
vim.api.nvim_buf_set_text(
self.start.buf,
self.start.lnum,
self.start.col,
self.stop.lnum,
math.min(self.stop.col + 1, max_col),
replacement
)
---@param alnum number
---@param acol number
---@param blnum number
---@param bcol number
local function set_text(alnum, acol, blnum, bcol, repl)
-- row indices are end-inclusive, and column indices are end-exclusive.
vim.api.nvim_buf_set_text(buf, alnum, acol, blnum, bcol, repl)
local new_last_line_num = self.start.lnum + #replacement - 1
local new_last_col = #(replacement[#replacement] or '')
if new_last_line_num == start_lnum then new_last_col = new_last_col + start_col - 1 end
self.stop = Pos.new(buf, new_last_line_num, new_last_col)
end
---@param alnum number
---@param blnum number
local function set_lines(alnum, blnum, repl)
-- indexing is zero-based, end-exclusive
vim.api.nvim_buf_set_lines(buf, alnum, blnum, false, repl)
if #repl == 0 then
self.stop = nil
else
vim.api.nvim_buf_set_lines(self.start.buf, self.start.lnum, self.stop.lnum + 1, false, replacement)
local new_last_line_num = start_lnum + #replacement - 1
self.stop = Pos.new(self.start.buf, new_last_line_num, Pos.MAX_COL, self.stop.off)
end
self.mode = 'v'
end
if replace_type == 'insert' then
set_text(start_lnum, start_col, start_lnum, start_col, replacement)
elseif replace_type == 'region' then
-- Fixup the bounds:
local last_line = vim.api.nvim_buf_get_lines(buf, stop_lnum - 1, stop_lnum, false)[1] or ''
local max_col = #last_line
set_text(start_lnum, start_col, stop_lnum - 1, math.min(stop_col, max_col), replacement)
elseif replace_type == 'lines' then
set_lines(start_lnum, stop_lnum, replacement)
else
error 'unreachable'
end
end
@@ -360,6 +411,8 @@ end
function Range:shrink(amount)
local start = self.start
local stop = self.stop
if stop == nil then return self:clone() end
for _ = 1, amount do
local next_start = start:next(1)
local next_stop = stop:next(-1)
@@ -368,7 +421,7 @@ function Range:shrink(amount)
stop = next_stop
if next_start > next_stop then break end
end
if start > stop then stop = start end
if start > stop then stop = nil end
return Range.new(start, stop, self.mode)
end
@@ -382,18 +435,30 @@ end
---@param left string
---@param right string
function Range:save_to_pos(left, right)
self.start:save_to_pos(left)
self.stop:save_to_pos(right)
if self:is_empty() then
self.start:save_to_pos(left)
self.start:save_to_pos(right)
else
self.start:save_to_pos(left)
self.stop:save_to_pos(right)
end
end
---@param left string
---@param right string
function Range:save_to_marks(left, right)
self.start:save_to_mark(left)
self.stop:save_to_mark(right)
if self:is_empty() then
self.start:save_to_mark(left)
self.start:save_to_mark(right)
else
self.start:save_to_mark(left)
self.stop:save_to_mark(right)
end
end
function Range:set_visual_selection()
if self:is_empty() then return end
if vim.api.nvim_get_current_buf() ~= self.start.buf then vim.api.nvim_set_current_buf(self.start.buf) end
State.run(self.start.buf, function(s)
@@ -403,10 +468,55 @@ function Range:set_visual_selection()
self.start:save_to_mark 'a'
self.stop:save_to_mark 'b'
local mode = self.mode
if vim.api.nvim_get_mode().mode == 'n' then utils.feedkeys(mode) end
utils.feedkeys '`ao`b'
local normal_cmd_args = ''
if vim.api.nvim_get_mode().mode == 'n' then normal_cmd_args = normal_cmd_args .. mode end
normal_cmd_args = normal_cmd_args .. '`ao`b'
vim.cmd { cmd = 'normal', args = { normal_cmd_args }, bang = true }
return nil
end)
end
---@param group string
---@param opts? { timeout?: number, priority?: number, on_macro?: boolean }
function Range:highlight(group, opts)
if self:is_empty() then return end
opts = opts or { on_macro = false }
if opts.on_macro == nil then opts.on_macro = false end
local in_macro = vim.fn.reg_executing() ~= ''
if not opts.on_macro and in_macro then return { clear = function() end } end
local ns = vim.api.nvim_create_namespace ''
State.run(self.start.buf, function(s)
if not in_macro then s:track_winview() end
vim.highlight.range(
self.start.buf,
ns,
group,
{ self.start.lnum, self.start.col },
{ self.stop.lnum, self.stop.col },
{
inclusive = true,
priority = opts.priority,
regtype = self.mode,
}
)
return nil
end)
vim.cmd.redraw()
local function clear()
vim.api.nvim_buf_clear_namespace(self.start.buf, ns, self.start.lnum, self.stop.lnum + 1)
vim.cmd.redraw()
end
if opts.timeout ~= nil then vim.defer_fn(clear, opts.timeout) end
return { ns = ns, clear = clear }
end
return Range

61
lua/u/repeat.lua Normal file
View File

@@ -0,0 +1,61 @@
local M = {}
local function _normal(cmd) vim.cmd { cmd = 'normal', args = { cmd }, bang = true } end
M.native_repeat = function() _normal '.' end
M.native_undo = function() _normal 'u' end
local function update_ts() vim.b.tt_changedtick = vim.b.changedtick end
---@param cmd? string|fun():unknown
function M.set(cmd)
update_ts()
if cmd ~= nil then vim.b.tt_repeatcmd = cmd end
end
local function tt_was_last_repeatable()
local ts, tt_ts = vim.b.changedtick, vim.b.tt_changedtick
return tt_ts ~= nil and ts <= tt_ts
end
---@generic T
---@param cmd string|fun():T
---@return T
function M.run(cmd)
M.set(cmd)
local result = cmd()
update_ts()
return result
end
function M.do_repeat()
local tt_cmd = vim.b.tt_repeatcmd
if not tt_was_last_repeatable() or (type(tt_cmd) ~= 'function' and type(tt_cmd) ~= 'string') then
return M.native_repeat()
end
-- execute the cached command:
local count = vim.api.nvim_get_vvar 'count1'
if type(tt_cmd) == 'string' then
_normal(count .. tt_cmd --[[@as string]])
else
local last_return
for _ = 1, count do
last_return = M.run(tt_cmd --[[@as fun():any]])
end
return last_return
end
end
function M.undo()
local tt_was_last_repeatable_before_undo = tt_was_last_repeatable()
M.native_undo()
if tt_was_last_repeatable_before_undo then update_ts() end
end
function M.setup()
vim.keymap.set('n', '.', M.do_repeat)
vim.keymap.set('n', 'u', M.undo)
end
return M

View File

@@ -5,6 +5,7 @@
---@field positions table
---@field keymaps { mode: string; lhs: any, rhs: any, buffer?: number }[]
---@field global_options table<string, any>
---@field win_view vim.fn.winsaveview.ret|nil
local State = {}
---@param buf number
@@ -63,7 +64,9 @@ function State:track_mark(mark) self.marks[mark] = vim.api.nvim_buf_get_mark(sel
function State:track_pos(pos) self.positions[pos] = vim.fn.getpos(pos) end
---@param nm string
function State:track_global_option(nm) self.global_options[nm] = vim.g[nm] end
function State:track_global_option(nm) self.global_options[nm] = vim.go[nm] end
function State:track_winview() self.win_view = vim.fn.winsaveview() end
function State:restore()
for reg, val in pairs(self.registers) do
@@ -79,8 +82,9 @@ function State:restore()
vim.keymap.set(map.mode, map.lhs, map.rhs, { buffer = map.buffer })
end
for nm, val in pairs(self.global_options) do
vim.g[nm] = val
vim.go[nm] = val
end
if self.win_view ~= nil then vim.fn.winrestview(self.win_view) end
end
return State

View File

@@ -6,13 +6,6 @@ local M = {}
---@alias QfItem { col: number, filename: string, kind: string, lnum: number, text: string }
---@alias KeyMaps table<string, fun(): any | string> }
---@param keys string
---@param mode? string
function M.feedkeys(keys, mode)
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(keys, true, false, true), mode or 'nx', true)
end
---@alias CmdArgs { args: string; bang: boolean; count: number; fargs: string[]; line1: number; line2: number; mods: string; name: string; range: 0|1|2; reg: string; smods: any; info: Range|nil }
--- A utility for creating user commands that also pre-computes useful information
@@ -31,7 +24,7 @@ end
---@param cmd string | fun(args: CmdArgs): any
---@param opts? { nargs?: 0|1|'*'|'?'|'+'; range?: boolean|'%'|number; count?: boolean|number, addr?: string; completion?: string }
function M.ucmd(name, cmd, opts)
local Range = require 'tt.range'
local Range = require 'u.range'
opts = opts or {}
local cmd2 = cmd
@@ -48,32 +41,34 @@ end
---@param fn fun(key_seq: string):Range|Pos|nil
---@param opts? { buffer: number|nil }
function M.define_text_object(key_seq, fn, opts)
local Range = require 'tt.range'
local Pos = require 'tt.pos'
local Range = require 'u.range'
local Pos = require 'u.pos'
if opts ~= nil and opts.buffer == 0 then opts.buffer = vim.api.nvim_get_current_buf() end
local function handle_visual()
local range_or_pos = fn(key_seq)
if range_or_pos == nil then return end
if Range.is(range_or_pos) and range_or_pos:is_empty() then range_or_pos = range_or_pos.start end
if Range.is(range_or_pos) then
local range = range_or_pos --[[@as Range]]
range:set_visual_selection()
else
M.feedkeys '<Esc>'
vim.cmd { cmd = 'normal', args = { '<Esc>' }, bang = true }
end
end
vim.keymap.set({ 'x' }, key_seq, handle_visual, opts and { buffer = opts.buffer } or nil)
local function handle_normal()
local State = require 'tt.state'
local State = require 'u.state'
-- enter visual mode:
M.feedkeys 'v'
vim.cmd { cmd = 'normal', args = { 'v' }, bang = true }
local range_or_pos = fn(key_seq)
if range_or_pos == nil then return end
if Range.is(range_or_pos) and range_or_pos:is_empty() then range_or_pos = range_or_pos.start end
if Range.is(range_or_pos) then
range_or_pos:set_visual_selection()
@@ -81,7 +76,7 @@ function M.define_text_object(key_seq, fn, opts)
local p = range_or_pos --[[@as Pos]]
State.run(0, function(s)
s:track_global_option 'eventignore'
vim.opt_global.eventignore = 'all'
vim.go.eventignore = 'all'
-- insert a single space, so we can select it:
vim.api.nvim_buf_set_text(0, p.lnum, p.col, p.lnum, p.col, { ' ' })
@@ -93,6 +88,25 @@ function M.define_text_object(key_seq, fn, opts)
vim.keymap.set({ 'o' }, key_seq, handle_normal, opts and { buffer = opts.buffer } or nil)
end
---@type fun(): nil|(fun():any)
local __U__RepeatableOpFunc_rhs = nil
--- This is the global utility function used for operatorfunc
--- in repeatablemap
---@type nil|fun(range: Range): fun():any|nil
-- selene: allow(unused_variable)
function __U__RepeatableOpFunc()
if __U__RepeatableOpFunc_rhs ~= nil then __U__RepeatableOpFunc_rhs() end
end
function M.repeatablemap(mode, lhs, rhs, opts)
vim.keymap.set(mode, lhs, function()
__U__RepeatableOpFunc_rhs = rhs
vim.o.operatorfunc = 'v:lua.__U__RepeatableOpFunc'
return 'g@ '
end, vim.tbl_extend('force', opts or {}, { expr = true }))
end
function M.get_editor_dimensions()
local w = 0
local h = 0

View File

@@ -1,5 +1,5 @@
local Buffer = require 'tt.buffer'
local withbuf = require '__tt_test_tools'
local Buffer = require 'u.buffer'
local withbuf = loadfile './spec/withbuf.lua'()
describe('Buffer', function()
it('should replace all lines', function()

View File

@@ -1,4 +1,4 @@
local CodeWriter = require 'tt.codewriter'
local CodeWriter = require 'u.codewriter'
describe('CodeWriter', function()
it('should write with indentation', function()

View File

@@ -1,5 +1,5 @@
local Pos = require 'tt.pos'
local withbuf = require '__tt_test_tools'
local Pos = require 'u.pos'
local withbuf = loadfile './spec/withbuf.lua'()
describe('Pos', function()
it('get a char from a given position', function()

View File

@@ -1,6 +1,6 @@
local Range = require 'tt.range'
local Pos = require 'tt.pos'
local withbuf = require '__tt_test_tools'
local Range = require 'u.range'
local Pos = require 'u.pos'
local withbuf = loadfile './spec/withbuf.lua'()
describe('Range', function()
it('get text in buffer', function()
@@ -391,7 +391,7 @@ describe('Range', function()
it('is_empty', function()
withbuf({ 'line one', 'and line two' }, function()
local range = Range.new(Pos.new(nil, 0, 0), Pos.new(nil, 0, 0), 'v')
local range = Range.new(Pos.new(nil, 0, 0), nil, 'v')
assert.is_true(range:is_empty())
local range2 = Range.new(Pos.new(nil, 0, 0), Pos.new(nil, 0, 1), 'v')
@@ -455,4 +455,106 @@ describe('Range', function()
assert.are.same(Pos.from_pos '.', Pos.new(nil, 1, 11))
end)
end)
it('selections set to past the EOL should not error', function()
withbuf({ 'Rg SET NAMES' }, function()
local b = vim.api.nvim_get_current_buf()
local r = Range.new(Pos.new(b, 0, 3), Pos.new(b, 0, 12), 'v')
r:replace 'bleh'
assert.are.same({ 'Rg bleh' }, vim.api.nvim_buf_get_lines(b, 0, -1, false))
end)
withbuf({ 'Rg SET NAMES' }, function()
local b = vim.api.nvim_get_current_buf()
local r = Range.new(Pos.new(b, 0, 3), Pos.new(b, 0, 11), 'v')
r:replace 'bleh'
assert.are.same({ 'Rg bleh' }, vim.api.nvim_buf_get_lines(b, 0, -1, false))
end)
end)
it('replace updates Range.stop: same line', function()
withbuf({ 'The quick brown fox jumps over the lazy dog' }, function()
local b = vim.api.nvim_get_current_buf()
local r = Range.new(Pos.new(b, 0, 4), Pos.new(b, 0, 8), 'v')
r:replace 'bleh1'
assert.are.same({ 'The bleh1 brown fox jumps over the lazy dog' }, vim.api.nvim_buf_get_lines(b, 0, -1, false))
r:replace 'bleh2'
assert.are.same({ 'The bleh2 brown fox jumps over the lazy dog' }, vim.api.nvim_buf_get_lines(b, 0, -1, false))
end)
end)
it('replace updates Range.stop: multi-line', function()
withbuf({
'The quick brown fox jumps',
'over the lazy dog',
}, function()
local b = vim.api.nvim_get_current_buf()
local r = Range.new(Pos.new(b, 0, 20), Pos.new(b, 1, 3), 'v')
assert.are.same({ 'jumps', 'over' }, r:lines())
r:replace 'bleh1'
assert.are.same({ 'The quick brown fox bleh1 the lazy dog' }, vim.api.nvim_buf_get_lines(b, 0, -1, false))
r:replace 'blehGoo2'
assert.are.same({ 'The quick brown fox blehGoo2 the lazy dog' }, vim.api.nvim_buf_get_lines(b, 0, -1, false))
end)
end)
it('replace updates Range.stop: multi-line (blockwise)', function()
withbuf({
'The quick brown',
'fox',
'jumps',
'over',
'the lazy dog',
}, function()
local b = vim.api.nvim_get_current_buf()
local r = Range.new(Pos.new(b, 1, 0), Pos.new(b, 3, Pos.MAX_COL), 'V')
assert.are.same({ 'fox', 'jumps', 'over' }, r:lines())
r:replace { 'bleh1', 'bleh2' }
assert.are.same({
'The quick brown',
'bleh1',
'bleh2',
'the lazy dog',
}, vim.api.nvim_buf_get_lines(b, 0, -1, false))
r:replace 'blehGoo2'
assert.are.same({
'The quick brown',
'blehGoo2',
'the lazy dog',
}, vim.api.nvim_buf_get_lines(b, 0, -1, false))
end)
end)
it('replace after delete', function()
withbuf({
'The quick brown',
'fox',
'jumps',
'over',
'the lazy dog',
}, function()
local b = vim.api.nvim_get_current_buf()
local r = Range.new(Pos.new(b, 1, 0), Pos.new(b, 3, Pos.MAX_COL), 'V')
assert.are.same({ 'fox', 'jumps', 'over' }, r:lines())
r:replace(nil)
assert.are.same({
'The quick brown',
'the lazy dog',
}, vim.api.nvim_buf_get_lines(b, 0, -1, false))
r:replace { 'blehGoo2', '' }
assert.are.same({
'The quick brown',
'blehGoo2',
'the lazy dog',
}, vim.api.nvim_buf_get_lines(b, 0, -1, false))
end)
end)
end)

View File

@@ -1,5 +1,5 @@
local function withbuf(lines, f)
vim.opt_global.swapfile = false
vim.go.swapfile = false
vim.cmd.new()
vim.api.nvim_buf_set_lines(0, 0, -1, false, lines)

24
u.nvim-0.2.0-1.rockspec Normal file
View File

@@ -0,0 +1,24 @@
package = "u.nvim"
version = "0.2.0-1"
source = {
url = "git+https://github.com/jrop/u.nvim"
}
description = {
summary = "nvim a powerful Lua library designed to enhance your text manipulation experience in NeoVim, focusing primarily on a context-aware \"Range\" utility.",
detailed = "Welcome to u.nvim a powerful Lua library designed to enhance your text manipulation experience in NeoVim, focusing primarily on a context-aware \"Range\" utility. This utility allows you to work efficiently with text selections based on various conditions, in a variety of contexts, making coding and editing more intuitive and productive.",
homepage = "https://github.com/jrop/u.nvim",
license = "MIT"
}
build = {
type = "builtin",
modules = {
["u.buffer"] = "lua/u/buffer.lua",
["u.codewriter"] = "lua/u/codewriter.lua",
["u.opkeymap"] = "lua/u/opkeymap.lua",
["u.pos"] = "lua/u/pos.lua",
["u.range"] = "lua/u/range.lua",
["u.repeat"] = "lua/u/repeat.lua",
["u.state"] = "lua/u/state.lua",
["u.utils"] = "lua/u/utils.lua"
}
}