feat: Hook based overrides

This commit is contained in:
Morten Olsen 2021-08-31 16:20:57 +02:00 committed by siduck76
parent bfc10e6034
commit eced5f2ec4
3 changed files with 44 additions and 2 deletions

View File

@ -1,4 +1,4 @@
local hooks, M = {}, {}; local hooks, overrides, M = {}, {}, {};
local allowed_hooks = { local allowed_hooks = {
"install_plugins", "install_plugins",
"setup_mappings", "setup_mappings",
@ -35,4 +35,35 @@ M.run = function(name, args)
end end
end end
M.createOverrides = function(module)
local O = {};
O.get = function(name, default)
local current = default;
if overrides[module] and overrides[module][name] then
if type(overrides[module][name]) == "function" then
current = overrides[module][name]
elseif type(overrides[module][name]) == "table" then
for _, override in pairs(overrides[module][name]) do
current = override(current)
end
end
end
return current;
end
return O;
end
M.override = function(module, name, overwrite)
if overrides[module] == nil then
overrides[module] = {};
end
if overrides[module][name] == nil then
overrides[module][name] = {};
end
table.insert(overrides[module][name], overwrite)
end
return M; return M;

View File

@ -1,2 +1,11 @@
-- This is where you custom modules and plugins goes. -- This is where you custom modules and plugins goes.
-- See the wiki for a guide on how to extend NvChad -- See the wiki for a guide on how to extend NvChad
local hooks = require "core.hooks"
-- For example, the hook below overrieds 'publish_diagnostics' in the 'lspconfig' file,
--
-- hooks.override("lsp", "publish_diagnostics", function(current)
-- current.virtual_text = false;
-- return current;
-- end)

View File

@ -1,4 +1,5 @@
local present1, nvim_lsp = pcall(require, "lspconfig") local present1, nvim_lsp = pcall(require, "lspconfig")
local overrides = require("core.hooks").createOverrides("lsp")
if not present1 then if not present1 then
return return
@ -81,7 +82,7 @@ lspSymbol("Information", "")
lspSymbol("Hint", "") lspSymbol("Hint", "")
lspSymbol("Warning", "") lspSymbol("Warning", "")
vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, { local lsp_publish_diagnostics_options = overrides.get('publish_diagnostics', {
virtual_text = { virtual_text = {
prefix = "", prefix = "",
spacing = 0, spacing = 0,
@ -90,6 +91,7 @@ vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagn
underline = true, underline = true,
update_in_insert = false, -- update diagnostics insert mode update_in_insert = false, -- update diagnostics insert mode
}) })
vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, lsp_publish_diagnostics_options)
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {
border = "single", border = "single",
}) })