bugfixes; update README.md
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
local function withbuf(lines, f)
|
||||
vim.opt_global.swapfile = false
|
||||
|
||||
vim.cmd.new()
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, lines)
|
||||
local ok, result = pcall(f)
|
||||
|
||||
@@ -6,7 +6,7 @@ local Buffer = {}
|
||||
|
||||
---@param buf? number
|
||||
---@return Buffer
|
||||
function Buffer.new(buf)
|
||||
function Buffer.from_nr(buf)
|
||||
if buf == nil or buf == 0 then buf = vim.api.nvim_get_current_buf() end
|
||||
local b = { buf = buf }
|
||||
setmetatable(b, { __index = Buffer })
|
||||
@@ -14,12 +14,12 @@ function Buffer.new(buf)
|
||||
end
|
||||
|
||||
---@return Buffer
|
||||
function Buffer.current() return Buffer.new(0) end
|
||||
function Buffer.current() return Buffer.from_nr(0) end
|
||||
|
||||
---@param listed boolean
|
||||
---@param scratch boolean
|
||||
---@return Buffer
|
||||
function Buffer.create(listed, scratch) return Buffer.new(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()
|
||||
self:set_option('bufhidden', 'delete')
|
||||
|
||||
@@ -24,7 +24,7 @@ end
|
||||
|
||||
---@param p Pos
|
||||
function CodeWriter.from_pos(p)
|
||||
local line = Buffer.new(p.buf):line0(p.lnum):text()
|
||||
local line = Buffer.from_nr(p.buf):line0(p.lnum):text()
|
||||
return CodeWriter.from_line(line, p.buf)
|
||||
end
|
||||
|
||||
@@ -55,8 +55,6 @@ end
|
||||
---@param line string
|
||||
function CodeWriter:write_raw(line)
|
||||
if line:find '\n' then error 'line contains newline character' end
|
||||
line = line:gsub('^\n+', '')
|
||||
line = line:gsub('\n+$', '')
|
||||
table.insert(self.lines, line)
|
||||
end
|
||||
|
||||
|
||||
@@ -2,20 +2,20 @@ local Range = require 'tt.range'
|
||||
local vim_repeat = require 'tt.repeat'
|
||||
|
||||
---@type fun(range: Range): nil|(fun():any)
|
||||
local MyOpKeymapOpFunc_rhs = nil
|
||||
local __TT__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 MyOpKeymapOpFunc(ty)
|
||||
if MyOpKeymapOpFunc_rhs ~= nil then
|
||||
function __TT__OpKeymapOpFunc(ty)
|
||||
if __TT__OpKeymapOpFunc_rhs ~= nil then
|
||||
local range = Range.from_op_func(ty)
|
||||
local repeat_inject = MyOpKeymapOpFunc_rhs(range)
|
||||
local repeat_inject = __TT__OpKeymapOpFunc_rhs(range)
|
||||
|
||||
vim_repeat.set(function()
|
||||
vim.o.operatorfunc = 'v:lua.MyOpKeymapOpFunc'
|
||||
vim.o.operatorfunc = 'v:lua.__TT__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()
|
||||
MyOpKeymapOpFunc_rhs = rhs
|
||||
vim.o.operatorfunc = 'v:lua.MyOpKeymapOpFunc'
|
||||
__TT__OpKeymapOpFunc_rhs = rhs
|
||||
vim.o.operatorfunc = 'v:lua.__TT__OpKeymapOpFunc'
|
||||
return 'g@'
|
||||
end, vim.tbl_extend('force', opts or {}, { expr = true }))
|
||||
end
|
||||
|
||||
@@ -78,6 +78,10 @@ function Range.from_line(buf, line) return Range.from_lines(buf, line, line) end
|
||||
---@param stop_line number 0-based line index
|
||||
function Range.from_lines(buf, start_line, stop_line)
|
||||
if buf == nil or buf == 0 then buf = vim.api.nvim_get_current_buf() end
|
||||
if stop_line < 0 then
|
||||
local num_lines = vim.api.nvim_buf_line_count(buf)
|
||||
stop_line = num_lines + stop_line
|
||||
end
|
||||
return Range.new(Pos.new(buf, start_line, 0), Pos.new(buf, stop_line, Pos.MAX_COL), 'V')
|
||||
end
|
||||
|
||||
@@ -237,7 +241,7 @@ function Range.smallest(ranges)
|
||||
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:line_count() return self.stop.lnum - self.start.lnum + 1 end
|
||||
|
||||
function Range:to_linewise()
|
||||
local r = self:clone()
|
||||
@@ -369,7 +373,7 @@ end
|
||||
---@param amount number
|
||||
function Range:must_shrink(amount)
|
||||
local shrunk = self:shrink(amount)
|
||||
if shrunk == nil 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
|
||||
end
|
||||
|
||||
@@ -397,12 +401,8 @@ 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
|
||||
vim.cmd.normal { cmd = 'normal', args = { '`a' .. mode .. '`b' }, bang = true }
|
||||
else
|
||||
utils.feedkeys '`ao`b'
|
||||
end
|
||||
|
||||
if vim.api.nvim_get_mode().mode == 'n' then utils.feedkeys(mode) end
|
||||
utils.feedkeys '`ao`b'
|
||||
return nil
|
||||
end)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user