Skip to content

Commit

Permalink
refactor: simplify lsp/dap config
Browse files Browse the repository at this point in the history
  • Loading branch information
konradmalik committed Sep 30, 2023
1 parent 8221699 commit 5e4dfce
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 47 deletions.
2 changes: 0 additions & 2 deletions config/native/after/plugin/dap.lua

This file was deleted.

2 changes: 1 addition & 1 deletion config/native/after/plugin/lsp.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require("konrad.lsp.borders")

require("konrad.lsp").initialize()
require("konrad.lsp").init_efm()

---@param client table
---@param bufnr number
Expand Down
18 changes: 3 additions & 15 deletions config/native/lua/konrad/dap/init.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
local configs = {}
local already_initialized = false

local initialize = function()
already_initialized = true
local initialize = function(configs)
if not vim.tbl_isempty(configs) then
vim.cmd("packadd nvim-dap")
vim.cmd("packadd nvim-dap-ui")
Expand Down Expand Up @@ -40,20 +36,12 @@ local M = {}
---@param tconfigs string[]|function[] - dap config names or function with no arguments if custom config
---@return nil
M.setup = function(tconfigs)
configs = {}
local configs = {}
for _, value in ipairs(tconfigs) do
table.insert(configs, value)
end

if already_initialized then
-- reinitialize essentially, this is useful mostly when sourcing .nvim.lua manually
initialize()
end
end

-- call this after .nvim.lua (from after folder eg.)
M.initialize = function()
initialize()
initialize(configs)
end

return M
2 changes: 0 additions & 2 deletions config/native/lua/konrad/lsp/efm/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ end

local M = {}

M.default_plugins = { "prettier", "jq", "shfmt", "shellcheck" }

---@param plugins string[] names of plugins to add, ex. 'prettier'
---@return table config to be put into lspconfig['efm'].setup(config)
M.build_lspconfig = function(plugins)
Expand Down
46 changes: 19 additions & 27 deletions config/native/lua/konrad/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,18 @@ local init_lspconfig = function(server, opts)
config.setup(merged)
end

local local_lsps = {}
local init_lsps = function()
local additional = local_lsps
for k, v in pairs(additional) do
---@param lsps table of server name->server config
local init_lsps = function(lsps)
for k, v in pairs(lsps) do
init_lspconfig(k, v)
end
end

local local_efm_plugins = {}
local init_efm = function()
local efm = require("konrad.lsp.efm")
local config = efm.build_lspconfig(vim.list_extend(local_efm_plugins, efm.default_plugins))
init_lspconfig("efm", config)
end

local already_initialized = false
local initialize = function()
init_efm()
init_lsps()
already_initialized = true
end
local extra_efm_plugins = {}
local initialized_efm = false

local M = {}
M.default_efm_plugins = { "prettier", "jq", "shfmt", "shellcheck" }

---@param tconfigs table of server-value mapping
--- server: any server name from nvim-lspconfig or 'efm' if this enables an efm plugin.
Expand All @@ -47,30 +36,33 @@ local M = {}
--- server == 'efm' -> a list of plugins to enable eg. {'black'} (some plugins are always enabled).
---@return nil
M.setup = function(tconfigs)
local_efm_plugins = {}
local_lsps = {}
local lsps = {}
for server, value in pairs(tconfigs) do
if type(server) == "number" then
server = value
value = nil
end

if server == "efm" then
local_efm_plugins = value
extra_efm_plugins = value
else
local_lsps[server] = value or {}
lsps[server] = value or {}
end
end

if already_initialized then
-- reinitialize essentially, this is useful mostly when sourcing .nvim.lua manually
initialize()
init_lsps(lsps)
if initialized_efm then
M.init_efm()
end
end

-- call this after .nvim.lua (from after folder eg.)
M.initialize = function()
initialize()
M.init_efm = function()
local efm = require("konrad.lsp.efm")
local all_plugins = {}
all_plugins = vim.list_extend(all_plugins, extra_efm_plugins)
local config = efm.build_lspconfig(vim.list_extend(all_plugins, M.default_efm_plugins))
init_lspconfig("efm", config)
initialized_efm = true
end

return M
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
neovim = bundle.nvim;
config = bundle.config;
nvim-luaref = pkgs.neovimPlugins.nvim-luaref;
plugins = pkgs.neovimPlugins;
});
apps = forAllSystems (pkgs: {
default = {
Expand Down

0 comments on commit 5e4dfce

Please sign in to comment.