From 0f2868c5e66d80ca8198ed7fe7e0a68800055b7b Mon Sep 17 00:00:00 2001 From: ray-x Date: Fri, 24 Sep 2021 11:54:03 +1000 Subject: [PATCH] Better lspinstall support --- README.md | 15 ++++++- lua/navigator.lua | 8 +++- lua/navigator/codeAction.lua | 4 +- lua/navigator/lazyloader.lua | 64 +++++++++++++++++---------- lua/navigator/lspclient/clients.lua | 68 +++++++++++++++++------------ 5 files changed, 102 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 6af74e0..b1bc644 100644 --- a/README.md +++ b/README.md @@ -405,7 +405,20 @@ If you'd like to only use the lsp servers installed by lspinstall. Please set lspinstall = false ``` -In the config +In the config. + +If you need to use the setup from navigator instead of default setup in lspinstall. Please setup + +```lua +lspinstall = false + +require'navigator'.setup({ + lsp = { + tsserver = { cmd = {'your tsserver installed by lspinstall'} } + } +}) + +``` ## Usage diff --git a/lua/navigator.lua b/lua/navigator.lua index 7247511..6c57033 100644 --- a/lua/navigator.lua +++ b/lua/navigator.lua @@ -133,11 +133,13 @@ end M.setup = function(cfg) extend_config(cfg) + + vim.cmd([[autocmd FileType * lua require'navigator.lspclient.clients'.setup()]]) -- BufWinEnter BufNewFile,BufRead ? -- local log = require"navigator.util".log -- log(debug.traceback()) -- log(cfg, _NgConfigValues) -- print("loading navigator") - require('navigator.lazyloader') + require('navigator.lazyloader').init() require('navigator.lspclient.clients').setup(_NgConfigValues) -- require("navigator.lspclient.mapping").setup(_NgConfigValues) require("navigator.reference") @@ -149,14 +151,16 @@ M.setup = function(cfg) 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") if not _NgConfigValues.loaded then - vim.cmd([[autocmd FileType * lua require'navigator.lspclient.clients'.setup()]]) -- BufWinEnter BufNewFile,BufRead ? _NgConfigValues.loaded = true end + if _NgConfigValues.ts_fold == true then require('navigator.foldts').on_attach() end + end return M diff --git a/lua/navigator/codeAction.lua b/lua/navigator/codeAction.lua index 34a17fe..6d27370 100644 --- a/lua/navigator/codeAction.lua +++ b/lua/navigator/codeAction.lua @@ -87,7 +87,7 @@ local function _update_virtual_text(line, actions) pcall(api.nvim_buf_clear_namespace, 0, namespace, 0, -1) if line then - log(line, actions) + trace(line, actions) local icon_with_indent = " " .. config.icons.code_action_icon local title = actions[1].title @@ -135,7 +135,7 @@ function code_action:render_action_virtual_text(line, diagnostics) _update_sign(nil) end else - log(err, line, diagnostics, actions, context) + trace(err, line, diagnostics, actions, context) if config.code_action_prompt.sign then if need_check_diagnostic[vim.bo.filetype] then if next(diagnostics) == nil then diff --git a/lua/navigator/lazyloader.lua b/lua/navigator/lazyloader.lua index 8f05cd7..b752f88 100644 --- a/lua/navigator/lazyloader.lua +++ b/lua/navigator/lazyloader.lua @@ -1,30 +1,46 @@ -local log = require"navigator.util".log -local loader = nil -local packer_plugins = packer_plugins or nil -- suppress warnings +return { + init = function() + local loader = nil + local packer_plugins = packer_plugins or nil -- suppress warnings + local log = require'navigator.util'.log + -- packer only + if packer_plugins ~= nil then -- packer install + local lazy_plugins = { + ["nvim-lspconfig"] = "neovim/nvim-lspconfig", + ["guihua.lua"] = "ray-x/guihua.lua" + } + if _NgConfigValues.lspinstall == true then + lazy_plugins["nvim-lspinstall"] = "kabouzeid/nvim-lspinstall" + end --- packer only -if packer_plugins ~= nil then -- packer install - local lazy_plugins = { - ["nvim-lspconfig"] = "neovim/nvim-lspconfig", - ["guihua.lua"] = "ray-x/guihua.lua" - } - if _NgConfigValues.lspinstall == true then - lazy_plugins["nvim-lspinstall"] = "kabouzeid/nvim-lspinstall" - end + -- packer installed + loader = require"packer".loader + for plugin, url in pairs(lazy_plugins) do + if not packer_plugins[url] or not packer_plugins[url].loaded then + -- log("loading ", plugin) + loader(plugin) + end + end - -- packer installed - loader = require"packer".loader - for plugin, url in pairs(lazy_plugins) do - if not packer_plugins[url] or not packer_plugins[url].loaded then - log("loading ", plugin) - loader(plugin) end - end - if _NgConfigValues.lspinstall == true then - local has_lspinst, lspinst = pcall(require, "lspinstall") - if has_lspinst then - lspinst.setup() + if _NgConfigValues.lspinstall == true then + local has_lspinst, lspinst = pcall(require, "lspinstall") + log('lspinstall', has_lspinst) + if has_lspinst then + lspinst.setup() + local configs = require "lspconfig/configs" + local servers = require'lspinstall'.installed_servers() + for _, server in pairs(servers) do + local cfg = require'navigator.lspclient.clients'.get_cfg(server) + local lsp_inst_cfg = configs[server] + if lsp_inst_cfg and lsp_inst_cfg.document_config.default_config then + lsp_inst_cfg = lsp_inst_cfg.document_config.default_config + vim.tbl_deep_extend('keep', lsp_inst_cfg, cfg) + require'lspconfig'[server].setup(lsp_inst_cfg) + end + end + end end end -end +} diff --git a/lua/navigator/lspclient/clients.lua b/lua/navigator/lspclient/clients.lua index 6a691bd..3172bcd 100644 --- a/lua/navigator/lspclient/clients.lua +++ b/lua/navigator/lspclient/clients.lua @@ -262,13 +262,18 @@ if config.lsp.disable_lsp == 'all' then config.lsp.disable_lsp = servers end -local default_cfg = { +local ng_default_cfg = { on_attach = on_attach, flags = {allow_incremental_sync = true, debounce_text_changes = 1000} } -- check and load based on file type local function load_cfg(ft, client, cfg, loaded) + -- if _NG_LSPCfgSetup ~= true then + -- log(lspconfig_setup) + -- lspconfig_setup(cfg) + -- _NG_LSPCfgSetup = true + -- end log(ft, client, loaded) if lspconfig[client] == nil then log("not supported by nvim", client) @@ -308,16 +313,21 @@ local function load_cfg(ft, client, cfg, loaded) return end - log("load cfg", cfg) + trace("load cfg", cfg) + log('lspconfig setup') + -- log(lspconfig.available_servers()) + -- force reload with config lspconfig[client].setup(cfg) - -- dont know why but 1st lsp client setup may fail.. could be a upstream defect - -- lspconfig[client].setup(cfg) + vim.defer_fn(function() + + vim.cmd([[doautocmd FileType ]] .. ft) + end, 100) log(client, "loading for", ft) end -- need to verify the lsp server is up end -local function wait_lsp_startup(ft, retry, user_lsp_opts) +local function lsp_startup(ft, retry, user_lsp_opts) retry = retry or false local clients = vim.lsp.get_active_clients() or {} @@ -371,7 +381,7 @@ local function wait_lsp_startup(ft, retry, user_lsp_opts) goto continue end - default_config = vim.tbl_deep_extend("force", default_config, default_cfg) + default_config = vim.tbl_deep_extend("force", default_config, ng_default_cfg) local cfg = setups[lspclient] or {} cfg = vim.tbl_deep_extend("keep", cfg, default_config) -- filetype disabled @@ -433,8 +443,11 @@ local function wait_lsp_startup(ft, retry, user_lsp_opts) end end + log('loading', lspclient, 'name', lspconfig[lspclient].name) -- start up lsp load_cfg(ft, lspclient, cfg, loaded) + + _NG_Loaded[lspclient] = true -- load_cfg(ft, lspclient, {}, loaded) ::continue:: end @@ -443,29 +456,24 @@ local function wait_lsp_startup(ft, retry, user_lsp_opts) local efm_cfg = user_lsp_opts['efm'] if efm_cfg then lspconfig.efm.setup(efm_cfg) + log('efm loading') + _NG_Loaded['efm'] = true end end if not retry or ft == nil then return end - -- - local timer = vim.loop.new_timer() - local i = 0 - -- jdtls can be very slow - timer:start(1000, 200, function() - clients = vim.lsp.get_active_clients() or {} - i = i + 1 - if i > 20 or #clients > 0 then - timer:close() -- Always close handles to avoid leaks. - log("active", #clients, i) - for _, client in pairs(clients) do - _NG_Loaded[client] = true - end - -- return true - end - -- giveup - -- _NG_Loaded = false - end) +end + +local function get_cfg(client) + local ng_cfg = ng_default_cfg + if setups[client] ~= nil then + local ng_setup = vim.deepcopy(setups[client]) + ng_setup.cmd = nil + return ng_setup + else + return ng_cfg + end end local function setup(user_opts) @@ -477,7 +485,7 @@ local function setup(user_opts) if user_opts ~= nil then log("navigator user setup", user_opts) end - trace(debug.traceback()) + log(debug.traceback()) user_opts = user_opts or config -- incase setup was triggered from autocmd if ft == nil then @@ -485,7 +493,11 @@ local function setup(user_opts) end if ft == nil or ft == "" then - log("nil filetype") + vim.defer_fn(function() + setup(user_opts) + end, 100) + + log("nil filetype, callback") return end local retry = true @@ -527,7 +539,7 @@ local function setup(user_opts) end end - wait_lsp_startup(ft, retry, lsp_opts) + lsp_startup(ft, retry, lsp_opts) --- if code line enabled if _NgConfigValues.lsp.code_lens then @@ -538,4 +550,4 @@ local function setup(user_opts) -- _LoadedFiletypes[ft] = vim.tbl_extend("keep", _LoadedFiletypes[ft] or {}, {ft}) end -return {setup = setup} +return {setup = setup, get_cfg = get_cfg}