diff --git a/README.md b/README.md index 4a4feb8..f0a177e 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -# 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. +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, `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. +This is meant to be used as a **library**, not a plugin. On its own, `u.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. ## Features @@ -16,8 +16,8 @@ This is meant to be used as a **library**, not a plugin. On its own, `text-tools Lazy: ```lua -- Setting `lazy = true` ensures that the library is only loaded --- when `require 'tt.' is called. -{ 'jrop/text-tools.nvim', lazy = true } +-- when `require 'u.' is called. +{ 'jrop/u.nvim', lazy = true } ``` ## Usage @@ -31,7 +31,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 +109,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, `riw`: -- `range` will contain the bounds of the motion `iw`. @@ -123,7 +123,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 +139,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 +153,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 '...' diff --git a/lua/tt/buffer.lua b/lua/u/buffer.lua similarity index 98% rename from lua/tt/buffer.lua rename to lua/u/buffer.lua index 4001461..06424f4 100644 --- a/lua/tt/buffer.lua +++ b/lua/u/buffer.lua @@ -1,4 +1,4 @@ -local Range = require 'tt.range' +local Range = require 'u.range' ---@class Buffer ---@field buf number diff --git a/lua/tt/codewriter.lua b/lua/u/codewriter.lua similarity index 98% rename from lua/tt/codewriter.lua rename to lua/u/codewriter.lua index 4e91953..f2489d9 100644 --- a/lua/tt/codewriter.lua +++ b/lua/u/codewriter.lua @@ -1,4 +1,4 @@ -local Buffer = require 'tt.buffer' +local Buffer = require 'u.buffer' ---@class CodeWriter ---@field lines string[] diff --git a/lua/tt/opkeymap.lua b/lua/u/opkeymap.lua similarity index 77% rename from lua/tt/opkeymap.lua rename to lua/u/opkeymap.lua index d509bec..18b84ef 100644 --- a/lua/tt/opkeymap.lua +++ b/lua/u/opkeymap.lua @@ -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 diff --git a/lua/tt/pos.lua b/lua/u/pos.lua similarity index 100% rename from lua/tt/pos.lua rename to lua/u/pos.lua diff --git a/lua/tt/range.lua b/lua/u/range.lua similarity index 99% rename from lua/tt/range.lua rename to lua/u/range.lua index 4fc0e50..b8199e0 100644 --- a/lua/tt/range.lua +++ b/lua/u/range.lua @@ -1,5 +1,5 @@ -local Pos = require 'tt.pos' -local State = require 'tt.state' +local Pos = require 'u.pos' +local State = require 'u.state' local orig_on_yank = vim.highlight.on_yank local on_yank_enabled = true; diff --git a/lua/tt/repeat.lua b/lua/u/repeat.lua similarity index 100% rename from lua/tt/repeat.lua rename to lua/u/repeat.lua diff --git a/lua/tt/state.lua b/lua/u/state.lua similarity index 100% rename from lua/tt/state.lua rename to lua/u/state.lua diff --git a/lua/tt/utils.lua b/lua/u/utils.lua similarity index 82% rename from lua/tt/utils.lua rename to lua/u/utils.lua index d5e57f1..8acc327 100644 --- a/lua/tt/utils.lua +++ b/lua/u/utils.lua @@ -24,7 +24,7 @@ local M = {} ---@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 @@ -41,8 +41,8 @@ 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 @@ -60,7 +60,7 @@ function M.define_text_object(key_seq, fn, opts) 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: vim.cmd { cmd = 'normal', args = { 'v' }, bang = true } @@ -86,6 +86,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 diff --git a/spec/buffer_spec.lua b/spec/buffer_spec.lua index 3ece69e..8582186 100644 --- a/spec/buffer_spec.lua +++ b/spec/buffer_spec.lua @@ -1,4 +1,4 @@ -local Buffer = require 'tt.buffer' +local Buffer = require 'u.buffer' local withbuf = loadfile './spec/withbuf.lua'() describe('Buffer', function() diff --git a/spec/codewriter_spec.lua b/spec/codewriter_spec.lua index f9c9517..c4596de 100644 --- a/spec/codewriter_spec.lua +++ b/spec/codewriter_spec.lua @@ -1,4 +1,4 @@ -local CodeWriter = require 'tt.codewriter' +local CodeWriter = require 'u.codewriter' describe('CodeWriter', function() it('should write with indentation', function() diff --git a/spec/pos_spec.lua b/spec/pos_spec.lua index 1c76748..9526d16 100644 --- a/spec/pos_spec.lua +++ b/spec/pos_spec.lua @@ -1,4 +1,4 @@ -local Pos = require 'tt.pos' +local Pos = require 'u.pos' local withbuf = loadfile './spec/withbuf.lua'() describe('Pos', function() diff --git a/spec/range_spec.lua b/spec/range_spec.lua index 8b42ae6..220d583 100644 --- a/spec/range_spec.lua +++ b/spec/range_spec.lua @@ -1,5 +1,5 @@ -local Range = require 'tt.range' -local Pos = require 'tt.pos' +local Range = require 'u.range' +local Pos = require 'u.pos' local withbuf = loadfile './spec/withbuf.lua'() describe('Range', function()