This commit is contained in:
parent
f3e352ceda
commit
121e0c0f7a
22
README.md
22
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.<utility>' is called.
|
||||
{ 'jrop/text-tools.nvim', lazy = true }
|
||||
-- when `require 'u.<utility>' 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, `<leader>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 '...'
|
||||
|
@ -1,4 +1,4 @@
|
||||
local Range = require 'tt.range'
|
||||
local Range = require 'u.range'
|
||||
|
||||
---@class Buffer
|
||||
---@field buf number
|
@ -1,4 +1,4 @@
|
||||
local Buffer = require 'tt.buffer'
|
||||
local Buffer = require 'u.buffer'
|
||||
|
||||
---@class CodeWriter
|
||||
---@field lines string[]
|
@ -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
|
@ -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;
|
@ -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
|
@ -1,4 +1,4 @@
|
||||
local Buffer = require 'tt.buffer'
|
||||
local Buffer = require 'u.buffer'
|
||||
local withbuf = loadfile './spec/withbuf.lua'()
|
||||
|
||||
describe('Buffer', function()
|
||||
|
@ -1,4 +1,4 @@
|
||||
local CodeWriter = require 'tt.codewriter'
|
||||
local CodeWriter = require 'u.codewriter'
|
||||
|
||||
describe('CodeWriter', function()
|
||||
it('should write with indentation', function()
|
||||
|
@ -1,4 +1,4 @@
|
||||
local Pos = require 'tt.pos'
|
||||
local Pos = require 'u.pos'
|
||||
local withbuf = loadfile './spec/withbuf.lua'()
|
||||
|
||||
describe('Pos', function()
|
||||
|
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user