From eced5f2ec4dcac38a0460f0c9e9aa23201e7902b Mon Sep 17 00:00:00 2001 From: Morten Olsen Date: Tue, 31 Aug 2021 16:20:57 +0200 Subject: [PATCH] feat: Hook based overrides --- lua/core/hooks.lua | 33 ++++++++++++++++++++++++++++++- lua/custom/init.lua | 9 +++++++++ lua/plugins/configs/lspconfig.lua | 4 +++- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/lua/core/hooks.lua b/lua/core/hooks.lua index 3353d71..8c7f351 100644 --- a/lua/core/hooks.lua +++ b/lua/core/hooks.lua @@ -1,4 +1,4 @@ -local hooks, M = {}, {}; +local hooks, overrides, M = {}, {}, {}; local allowed_hooks = { "install_plugins", "setup_mappings", @@ -35,4 +35,35 @@ M.run = function(name, args) 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; diff --git a/lua/custom/init.lua b/lua/custom/init.lua index 56508a9..5a80710 100644 --- a/lua/custom/init.lua +++ b/lua/custom/init.lua @@ -1,2 +1,11 @@ -- This is where you custom modules and plugins goes. -- 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) diff --git a/lua/plugins/configs/lspconfig.lua b/lua/plugins/configs/lspconfig.lua index 6cd102b..6704a21 100644 --- a/lua/plugins/configs/lspconfig.lua +++ b/lua/plugins/configs/lspconfig.lua @@ -1,4 +1,5 @@ local present1, nvim_lsp = pcall(require, "lspconfig") +local overrides = require("core.hooks").createOverrides("lsp") if not present1 then return @@ -81,7 +82,7 @@ lspSymbol("Information", "") lspSymbol("Hint", "") 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 = { prefix = "", spacing = 0, @@ -90,6 +91,7 @@ vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagn underline = true, 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, { border = "single", })