From 4a58870a28fdd972687b002d18157a518a84d30a Mon Sep 17 00:00:00 2001 From: ray-x Date: Tue, 4 May 2021 23:20:43 +1000 Subject: [PATCH] better autoload, prevent multiple loading for same lsp server --- lua/navigator.lua | 43 ++++++++++---------- lua/navigator/lspclient/clients.lua | 63 ++++++++++++++++++----------- 2 files changed, 61 insertions(+), 45 deletions(-) diff --git a/lua/navigator.lua b/lua/navigator.lua index b13f3a7..b62356f 100644 --- a/lua/navigator.lua +++ b/lua/navigator.lua @@ -1,8 +1,8 @@ local M = {} -_NgConfigValues ={ - debug = false, -- log output not implemented - code_action_icon = ' ', - width = nil, -- valeu of cols TODO allow float e.g. 0.6 +_NgConfigValues = { + debug = false, -- log output not implemented + code_action_icon = " ", + width = nil, -- valeu of cols TODO allow float e.g. 0.6 height = nil, on_attach = nil, -- function(client, bufnr) @@ -14,26 +14,25 @@ _NgConfigValues ={ enable = true, sign = true, sign_priority = 40, - virtual_text = true, - }, + virtual_text = true + } } - - vim.cmd("command! -nargs=0 LspLog call v:lua.open_lsp_log()") vim.cmd("command! -nargs=0 LspRestart call v:lua.reload_lsp()") -vim.cmd([[autocmd filetype * lua require'navigator'.setup()]]) -- BufWinEnter BufNewFile,BufRead ? local extend_config = function(opts) opts = opts or {} - if next(opts) == nil then return end - for key,value in pairs(opts) do + if next(opts) == nil then + return + end + for key, value in pairs(opts) do if _NgConfigValues[key] == nil then - error(string.format('[] Key %s not valid',key)) + error(string.format("[] Key %s not valid", key)) return end - if type(M.config_values[key]) == 'table' then - for k,v in pairs(value) do + if type(M.config_values[key]) == "table" then + for k, v in pairs(value) do _NgConfigValues[key][k] = v end else @@ -42,23 +41,23 @@ local extend_config = function(opts) end end - -M.config_values = function() return _NgConfigValues end +M.config_values = function() + return _NgConfigValues +end M.setup = function(cfg) extend_config(cfg) -- print("loading navigator") - require('navigator.lspclient').setup(_NgConfigValues) - require('navigator.reference') - require('navigator.definition') - require('navigator.hierarchy') - require('navigator.implementation') + require("navigator.lspclient").setup(_NgConfigValues) + require("navigator.reference") + require("navigator.definition") + require("navigator.hierarchy") + require("navigator.implementation") -- log("navigator loader") if _NgConfigValues.code_action_prompt.enable then vim.cmd [[autocmd CursorHold,CursorHoldI * lua require'navigator.codeAction'.code_action_prompt()]] end -- vim.cmd("autocmd BufNewFile,BufRead *.go setlocal noexpandtab tabstop=4 shiftwidth=4") - end return M diff --git a/lua/navigator/lspclient/clients.lua b/lua/navigator/lspclient/clients.lua index 366095a..8eee9ef 100644 --- a/lua/navigator/lspclient/clients.lua +++ b/lua/navigator/lspclient/clients.lua @@ -1,5 +1,4 @@ -- todo allow config passed in -local lspconfig = nil local log = require "navigator.util".log local verbose = require "navigator.util".verbose @@ -12,14 +11,13 @@ if packer_plugins ~= nil then -- if lazyloading end end -if package.loaded["lspconfig"] then - lspconfig = require "lspconfig" -end -local highlight = require "navigator.lspclient.highlight" -if lspconfig == nil then +local has_lsp, lspconfig = pcall(require, "lspconfig") +if not has_lsp then error("loading lsp config") end +local highlight = require "navigator.lspclient.highlight" + local util = lspconfig.util local config = require "navigator".config_values() @@ -205,16 +203,10 @@ local servers = { local default_cfg = {on_attach = on_attach} -- check and load based on file type -local function load_cfg(client, cfg) - local ft = vim.bo.filetype - if ft == nil then - ft = vim.api.nvim_buf_get_option(0, "filetype") - end - if ft == nil or ft == "" then - log("nil filetype") - return - end +local function load_cfg(ft, client, cfg, loaded) + -- log("trying", client) -- log(client, "loaded for", ft) + if lspconfig[client] == nil then log("not supported", client) return @@ -230,12 +222,21 @@ local function load_cfg(client, cfg) end end if should_load then - log(client, "loaded for", ft) + for _, c in pairs(loaded) do + if client == c then + -- loaded + log(client, "already been loaded for", ft, loaded) + return + end + end + lspconfig[client].setup(cfg) + log(client, "loaded for", ft) end end end +vim.cmd([[autocmd filetype * lua require'navigator.lspclient.clients'.setup()]]) -- BufWinEnter BufNewFile,BufRead ? local function setup(user_opts) verbose(debug.traceback()) @@ -246,14 +247,30 @@ local function setup(user_opts) highlight.diagnositc_config_sign() highlight.add_highlight() + + local ft = vim.bo.filetype + if ft == nil then + ft = vim.api.nvim_buf_get_option(0, "filetype") + end + if ft == nil or ft == "" then + log("nil filetype") + return + end + local clients = vim.lsp.get_active_clients() or {} + local loaded = {} + for _, client in ipairs(clients) do + if client ~= nil then + table.insert(loaded, client.name) + end + end for _, lspclient in ipairs(servers) do - load_cfg(lspclient, default_cfg) + load_cfg(ft, lspclient, default_cfg, loaded) end - load_cfg("gopls", golang_setup) - load_cfg("sqls", sqls_cfg) - load_cfg("sumneko_lua", lua_cfg) - load_cfg("clangd", clang_cfg) - load_cfg("rust_analyzer", rust_cfg) - load_cfg("pyright", pyright_cfg) + load_cfg(ft, "gopls", golang_setup, loaded) + load_cfg(ft, "sqls", sqls_cfg, loaded) + load_cfg(ft, "sumneko_lua", lua_cfg, loaded) + load_cfg(ft, "clangd", clang_cfg, loaded) + load_cfg(ft, "rust_analyzer", rust_cfg, loaded) + load_cfg(ft, "pyright", pyright_cfg, loaded) end return {setup = setup, cap = cap}