better Range:highlight(..) handling
This commit is contained in:
parent
03c5500966
commit
8bbb2ba9c8
@ -1,6 +1,13 @@
|
|||||||
local Pos = require 'tt.pos'
|
local Pos = require 'tt.pos'
|
||||||
local State = require 'tt.state'
|
local State = require 'tt.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
|
---@class Range
|
||||||
---@field start Pos
|
---@field start Pos
|
||||||
---@field stop Pos
|
---@field stop Pos
|
||||||
@ -116,12 +123,15 @@ function Range.from_text_object(text_obj, opts)
|
|||||||
null_pos:save_to_pos "'["
|
null_pos:save_to_pos "'["
|
||||||
null_pos:save_to_pos "']"
|
null_pos:save_to_pos "']"
|
||||||
|
|
||||||
|
local prev_on_yank_enabled = on_yank_enabled
|
||||||
|
on_yank_enabled = false
|
||||||
vim.cmd.normal {
|
vim.cmd.normal {
|
||||||
cmd = 'normal',
|
cmd = 'normal',
|
||||||
bang = not opts.user_defined,
|
bang = not opts.user_defined,
|
||||||
args = { '""y' .. text_obj },
|
args = { '""y' .. text_obj },
|
||||||
mods = { silent = true },
|
mods = { silent = true },
|
||||||
}
|
}
|
||||||
|
on_yank_enabled = prev_on_yank_enabled
|
||||||
|
|
||||||
local start = Pos.from_pos "'["
|
local start = Pos.from_pos "'["
|
||||||
local stop = Pos.from_pos "']"
|
local stop = Pos.from_pos "']"
|
||||||
@ -416,27 +426,38 @@ function Range:set_visual_selection()
|
|||||||
end
|
end
|
||||||
|
|
||||||
---@param group string
|
---@param group string
|
||||||
---@param opts? { timeout?: number, priority?: number }
|
---@param opts? { timeout?: number, priority?: number, on_macro?: boolean }
|
||||||
function Range:highlight(group, opts)
|
function Range:highlight(group, opts)
|
||||||
opts = opts or {}
|
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 ''
|
local ns = vim.api.nvim_create_namespace ''
|
||||||
vim.highlight.range(
|
State.run(self.start.buf, function(s)
|
||||||
self.start.buf,
|
if not in_macro then s:track_winview() end
|
||||||
ns,
|
|
||||||
group,
|
vim.highlight.range(
|
||||||
{ self.start.lnum, self.start.col },
|
self.start.buf,
|
||||||
{ self.stop.lnum, self.stop.col },
|
ns,
|
||||||
{
|
group,
|
||||||
inclusive = true,
|
{ self.start.lnum, self.start.col },
|
||||||
priority = opts.priority,
|
{ self.stop.lnum, self.stop.col },
|
||||||
regtype = 'v',
|
{
|
||||||
}
|
inclusive = true,
|
||||||
)
|
priority = opts.priority,
|
||||||
|
regtype = self.mode,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
end)
|
||||||
vim.cmd.redraw()
|
vim.cmd.redraw()
|
||||||
|
|
||||||
local function clear()
|
local function clear()
|
||||||
vim.api.nvim_buf_clear_namespace(self.start.buf, ns, self.start.lnum, self.stop.lnum + 1)
|
vim.api.nvim_buf_clear_namespace(self.start.buf, ns, self.start.lnum, self.stop.lnum + 1)
|
||||||
|
vim.cmd.redraw()
|
||||||
end
|
end
|
||||||
if opts.timeout ~= nil then vim.defer_fn(clear, opts.timeout) end
|
if opts.timeout ~= nil then vim.defer_fn(clear, opts.timeout) end
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
local Buffer = require 'tt.buffer'
|
local Buffer = require 'tt.buffer'
|
||||||
local withbuf = require '__tt_test_tools'
|
local withbuf = loadfile './spec/withbuf.lua'()
|
||||||
|
|
||||||
describe('Buffer', function()
|
describe('Buffer', function()
|
||||||
it('should replace all lines', function()
|
it('should replace all lines', function()
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
local Pos = require 'tt.pos'
|
local Pos = require 'tt.pos'
|
||||||
local withbuf = require '__tt_test_tools'
|
local withbuf = loadfile './spec/withbuf.lua'()
|
||||||
|
|
||||||
describe('Pos', function()
|
describe('Pos', function()
|
||||||
it('get a char from a given position', function()
|
it('get a char from a given position', function()
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
local Range = require 'tt.range'
|
local Range = require 'tt.range'
|
||||||
local Pos = require 'tt.pos'
|
local Pos = require 'tt.pos'
|
||||||
local withbuf = require '__tt_test_tools'
|
local withbuf = loadfile './spec/withbuf.lua'()
|
||||||
|
|
||||||
describe('Range', function()
|
describe('Range', function()
|
||||||
it('get text in buffer', function()
|
it('get text in buffer', function()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user