feat: add :Noh

master
Pegasust 2023-05-30 09:35:27 -07:00
parent 25d824714e
commit c3bfb4c48f
2 changed files with 87 additions and 26 deletions

View File

@ -1,3 +1,7 @@
---TODO: how does this work with changing texts?
---TODO: add reducer as formatter
---TODO: Add reducer as buf_select predicate
---@type mod_buf_select ---@type mod_buf_select
local buf_select = require('tsql.buf_select') local buf_select = require('tsql.buf_select')
---@type mod_token_select ---@type mod_token_select
@ -13,9 +17,6 @@ M.sink_by = {}
---@alias Format fun(nodes: QNode[]): string ---@alias Format fun(nodes: QNode[]): string
M.format = {} M.format = {}
M.nvim_ns = vim.api.nvim_create_namespace("tsql")
M.nvim_hl_group = "Search"
---@class Sink ---@class Sink
---@field sink fun(self, nodes: QNode[]) ---@field sink fun(self, nodes: QNode[])
M.Sink = {} M.Sink = {}
@ -29,8 +30,8 @@ function M.sink_by.highlight()
for _, node in ipairs(nodes) do for _, node in ipairs(nodes) do
vim.highlight.range( vim.highlight.range(
node.buf.bufnr, node.buf.bufnr,
M.nvim_ns, M.config.nvim_ns,
M.nvim_hl_group, M.config.nvim_hl_group,
{ node.start.row_0, node.start.col_0 }, { node.start.row_0, node.start.col_0 },
{ node.end_ex_col.row_0, node.end_ex_col.col_0 }, { node.end_ex_col.row_0, node.end_ex_col.col_0 },
{} {}
@ -128,14 +129,88 @@ function M.t(buf_match, codeql, sink)
}, M.Tsql) }, M.Tsql)
end end
---NOTE: This is now exiting the functional core and entering ---NOTE: requires nvim runtime
--- imperative shell function M.Tsql:q_nodes()
function M.Tsql:do_nvim() return self.codeql:find_nodes(
self.sink:sink(
self.codeql:find_nodes(
self.buf_match:filter_on(M.nvim_get_qbufs()) self.buf_match:filter_on(M.nvim_get_qbufs())
) )
) end
---NOTE: This is now exiting the functional core and entering
--- imperative shell
--- @param store StaticStore
function M.Tsql:do_nvim(store)
self.sink:sink(self:q_nodes())
store:add_highlight(self)
end
---@param config RtConfig
---@param store StaticStore
function M._delete_all_highlights(config, store)
---@type table<integer, QBuf>
local bufs = {}
for _, highlight_q in pairs(store.highlighting) do
for _, qnode in ipairs(highlight_q:q_nodes()) do
table.insert(bufs, qnode.buf.bufnr, qnode.buf)
end
end
for _, buf in pairs(bufs) do
vim.api.nvim_buf_clear_namespace(buf.bufnr, config.nvim_ns, 0, -1)
end
store:clear_highlights()
end
---NOTE: Collocated with `M.setup`
---@class Config
---@field nvim_hl_group string
local Config = {}
---@type Config
M.config_default = {
nvim_hl_group = "Search"
}
---@class RtConfig: Config
---@field nvim_ns number
M.RtConfig = {}
---@type RtConfig
M.config = {}
---@class StaticStore
---@field highlighting Tsql[]
---This is needed to undo the highlighting done by this plugin and potentially
---subscribe to the changing buffers to re-highlight if necessary
M.Store = {}
M.Store.__index = M.Store
---@param tsql Tsql
function M.Store:add_highlight(tsql)
table.insert(self.highlighting, tsql)
end
function M.Store:clear_highlights()
self.highlighting = {}
end
---@return StaticStore
function M.Store:new()
local o = { highlighting = {} }
setmetatable(o, self)
return o
end
function M.clear_highlights()
return M._delete_all_highlights(M.config, M.store)
end
---@param config Config
function M.setup(config)
M.config = vim.tbl_deep_extend("force", M.config, config or M.config_default)
M.config.nvim_ns = vim.api.nvim_create_namespace("tsql")
vim.api.nvim_create_user_command("Noh", M.clear_highlights)
M.store = M.Store:new()
end end
return M return M

View File

@ -1,14 +0,0 @@
local M = {}
M.Sink = {}
function M.print()
end
function M.highlight()
end
function M.mutate()
end
return M