From 74cbed573b7153755c802c4627beeb0eb808cda7 Mon Sep 17 00:00:00 2001 From: ray-x Date: Wed, 5 May 2021 21:37:55 +1000 Subject: [PATCH] refact lspclient --- lua/navigator/lspclient/clients.lua | 331 ++++++++++++++-------------- lua/navigator/util.lua | 2 +- 2 files changed, 169 insertions(+), 164 deletions(-) diff --git a/lua/navigator/lspclient/clients.lua b/lua/navigator/lspclient/clients.lua index b45e5a1..8cf4f26 100644 --- a/lua/navigator/lspclient/clients.lua +++ b/lua/navigator/lspclient/clients.lua @@ -23,162 +23,154 @@ local config = require "navigator".config_values() local cap = vim.lsp.protocol.make_client_capabilities() local on_attach = require("navigator.lspclient.attach").on_attach --- local gopls = {} -- gopls["ui.completion.usePlaceholders"] = true -local golang_setup = { - on_attach = on_attach, - capabilities = cap, - filetypes = {"go", "gomod"}, - -- init_options = { - -- useplaceholders = true, - -- completeunimported = true - -- }, - message_level = vim.lsp.protocol.MessageType.Error, - cmd = { - "gopls" - - -- share the gopls instance if there is one already - -- "-remote=auto", +-- lua setup +local sumneko_root_path = config.sumneko_root_path +local sumneko_binary = config.sumneko_binary - --[[ debug options ]] - -- - -- "-logfile=auto", - -- "-debug=:0", - -- "-remote.debug=:0", - -- "-rpc.trace", +local setups = { + gopls = { + on_attach = on_attach, + capabilities = cap, + filetypes = {"go", "gomod"}, + message_level = vim.lsp.protocol.MessageType.Error, + cmd = { + "gopls", + -- share the gopls instance if there is one already + "-remote=auto", + --[[ debug options ]] + -- + -- "-logfile=auto", + -- "-debug=:0", + "-remote.debug=:0" + -- "-rpc.trace", + }, + settings = { + gopls = { + analyses = {unusedparams = true, unreachable = false}, + codelenses = { + generate = true, -- show the `go generate` lens. + gc_details = true -- // Show a code lens toggling the display of gc's choices. + }, + usePlaceholders = true, + completeUnimported = true, + staticcheck = true, + matcher = "fuzzy", + symbolMatcher = "fuzzy", + gofumpt = true, + buildFlags = {"-tags", "integration"} + -- buildFlags = {"-tags", "functional"} + } + }, + root_dir = function(fname) + return util.root_pattern("go.mod", ".git")(fname) or util.path.dirname(fname) + end }, - settings = { - gopls = { - analyses = {unusedparams = true, unreachable = false}, - codelenses = { - generate = true, -- show the `go generate` lens. - gc_details = true -- // Show a code lens toggling the display of gc's choices. - }, - usePlaceholders = true, - completeUnimported = true, - staticcheck = true, - matcher = "fuzzy", - symbolMatcher = "fuzzy", - gofumpt = true, - buildFlags = {"-tags", "integration"} - -- buildFlags = {"-tags", "functional"} - } + clangd = { + cmd = { + "clangd", + "--background-index", + "--suggest-missing-includes", + "--clang-tidy", + "--header-insertion=iwyu" + }, + filetypes = {"c", "cpp", "objc", "objcpp"}, + on_attach = function(client) + client.resolved_capabilities.document_formatting = true + on_attach(client) + end }, - root_dir = function(fname) - return util.root_pattern("go.mod", ".git")(fname) or util.path.dirname(fname) - end -} -local clang_cfg = { - cmd = { - "clangd", - "--background-index", - "--suggest-missing-includes", - "--clang-tidy", - "--header-insertion=iwyu" + rust_analyzer = { + root_dir = util.root_pattern("Cargo.toml", "rust-project.json", ".git"), + filetypes = {"rust"}, + message_level = vim.lsp.protocol.MessageType.error, + on_attach = on_attach, + settings = { + ["rust-analyzer"] = { + assist = {importMergeBehavior = "last", importPrefix = "by_self"}, + cargo = {loadOutDirsFromCheck = true}, + procMacro = {enable = true} + } + } }, - filetypes = {"c", "cpp", "objc", "objcpp"}, - on_attach = function(client) - client.resolved_capabilities.document_formatting = true - on_attach(client) - end -} -local rust_cfg = { - root_dir = util.root_pattern("Cargo.toml", "rust-project.json", ".git"), - filetypes = {"rust"}, - message_level = vim.lsp.protocol.MessageType.error, - on_attach = on_attach, - settings = { - ["rust-analyzer"] = { - assist = {importMergeBehavior = "last", importPrefix = "by_self"}, - cargo = {loadOutDirsFromCheck = true}, - procMacro = {enable = true} + sqls = { + filetypes = {"sql"}, + on_attach = function(client, bufnr) + client.resolved_capabilities.execute_command = true + highlight.diagnositc_config_sign() + require "sqls".setup {picker = "telescope"} -- or default + end, + settings = { + cmd = {"sqls", "-config", "$HOME/.config/sqls/config.yml"} + -- alterantively: + -- connections = { + -- { + -- driver = 'postgresql', + -- datasourcename = 'host=127.0.0.1 port=5432 user=postgres password=password dbname=user_db sslmode=disable', + -- }, + -- }, } - } -} - -local sqls_cfg = { - filetypes = {"sql"}, - on_attach = function(client, bufnr) - client.resolved_capabilities.execute_command = true - highlight.diagnositc_config_sign() - require "sqls".setup {picker = "telescope"} -- or default - end, - settings = { - cmd = {"sqls", "-config", "$HOME/.config/sqls/config.yml"} - -- alterantively: - -- connections = { - -- { - -- driver = 'postgresql', - -- datasourcename = 'host=127.0.0.1 port=5432 user=postgres password=password dbname=user_db sslmode=disable', - -- }, - -- }, - } -} --- lua setup -local sumneko_root_path = config.sumneko_root_path -local sumneko_binary = config.sumneko_binary - -local lua_cfg = { - cmd = {sumneko_binary, "-E", sumneko_root_path .. "/main.lua"}, - filetypes = {"lua"}, - on_attach = on_attach, - settings = { - Lua = { - runtime = { - -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim) - version = "LuaJIT", - -- Setup your lua path - path = vim.split(package.path, ";") - }, - diagnostics = { - enable = true, - -- Get the language server to recognize the `vim` global - globals = { - "vim", - "describe", - "it", - "before_each", - "after_each", - "teardown", - "pending" - } - }, - workspace = { - -- Make the server aware of Neovim runtime files - library = { - [vim.fn.expand("$VIMRUNTIME/lua")] = true, - [vim.fn.expand("$VIMRUNTIME/lua/vim")] = true, - [vim.fn.expand("$VIMRUNTIME/lua/vim/lsp")] = true - -- [vim.fn.expand("~/repos/nvim/lua")] = true + }, + sumneko_lua = { + cmd = {sumneko_binary, "-E", sumneko_root_path .. "/main.lua"}, + filetypes = {"lua"}, + on_attach = on_attach, + settings = { + Lua = { + runtime = { + -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim) + version = "LuaJIT", + -- Setup your lua path + path = vim.split(package.path, ";") + }, + diagnostics = { + enable = true, + -- Get the language server to recognize the `vim` global + globals = { + "vim", + "describe", + "it", + "before_each", + "after_each", + "teardown", + "pending" + } + }, + workspace = { + -- Make the server aware of Neovim runtime files + library = { + [vim.fn.expand("$VIMRUNTIME/lua")] = true, + [vim.fn.expand("$VIMRUNTIME/lua/vim")] = true, + [vim.fn.expand("$VIMRUNTIME/lua/vim/lsp")] = true + -- [vim.fn.expand("~/repos/nvim/lua")] = true + } } } } - } -} - -local pyright_cfg = { - cmd = {"pyright-langserver", "--stdio"}, - filetypes = {"python"}, - settings = { - python = { - analysis = { - autoSearchPaths = true, - useLibraryCodeForTypes = true + }, + pyright = { + cmd = {"pyright-langserver", "--stdio"}, + filetypes = {"python"}, + settings = { + python = { + analysis = { + autoSearchPaths = true, + useLibraryCodeForTypes = true + } } } - } -} - -local ccls_cfg = { - init_options = { - compilationDatabaseDirectory = "build", - root_dir = [[ util.root_pattern("compile_commands.json", "compile_flags.txt", "CMakeLists.txt", "Makefile", ".git") or util.path.dirname ]], - index = { - threads = 2 - }, - clang = { - excludeArgs = {"-frounding-math"} + }, + ccls = { + init_options = { + compilationDatabaseDirectory = "build", + root_dir = [[ util.root_pattern("compile_commands.json", "compile_flags.txt", "CMakeLists.txt", "Makefile", ".git") or util.path.dirname ]], + index = { + threads = 2 + }, + clang = { + excludeArgs = {"-frounding-math"} + } } } } @@ -218,12 +210,12 @@ local servers = { "rust_analyzer", "terraformls" } + local default_cfg = {on_attach = on_attach} -- check and load based on file type 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) @@ -233,7 +225,6 @@ local function load_cfg(ft, client, cfg, loaded) local should_load = false if lspft ~= nil and #lspft > 0 then - -- log(client, "loaded for", ft, lspft) for _, value in ipairs(lspft) do if ft == value then should_load = true @@ -247,11 +238,11 @@ local function load_cfg(ft, client, cfg, loaded) return end end - lspconfig[client].setup(cfg) - log(client, "loaded for", ft) + log(client, "loading for", ft) end end + -- need to verify the lsp server is up end vim.cmd([[autocmd filetype * lua require'navigator.lspclient.clients'.setup()]]) -- BufWinEnter BufNewFile,BufRead ? @@ -274,22 +265,36 @@ local function setup(user_opts) 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) + + for i = 1, 2 do + 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 + local cfg = setups[lspclient] or default_cfg + load_cfg(ft, lspclient, cfg, loaded) + end + + local timer = vim.loop.new_timer() + local i = 0 + -- Waits 20ms, then repeats every 20ms until lsp is loaded. + timer:start( + 20, + 20, + function() + local clients = vim.lsp.get_active_clients() or {} + if i > 20 or #clients > 0 then + timer:close() -- Always close handles to avoid leaks. + log("active", #clients) + return true + end + i = i + 1 + end + ) end - for _, lspclient in ipairs(servers) do - load_cfg(ft, lspclient, default_cfg, loaded) - end - 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) - load_cfg(ft, "ccls", ccls_cfg, loaded) end return {setup = setup, cap = cap} diff --git a/lua/navigator/util.lua b/lua/navigator/util.lua index d431a51..51753d2 100644 --- a/lua/navigator/util.lua +++ b/lua/navigator/util.lua @@ -95,7 +95,7 @@ local default_config = { plugin = "navigator", use_console = false, use_file = true, - level = "error" + level = "info" } M._log = require("guihua.log").new({level = default_config.level}, true)