From 8c055b7b03d972eda4b78ea9028d746c8240c73d Mon Sep 17 00:00:00 2001 From: ray-x Date: Sun, 23 May 2021 14:10:49 +1000 Subject: [PATCH] Updata on_attach logic, allow lsp specific on_attach as well as generic on_attach. Document updates --- README.md | 7 +++++++ lua/navigator/lspclient/attach.lua | 29 +++++++++++++++++------------ lua/navigator/lspclient/clients.lua | 1 + lua/navigator/util.lua | 2 +- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index f811623..334bffe 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,7 @@ require.'navigator'.setup({ -- function(client, bufnr) -- -- the on_attach will be called at end of navigator on_attach -- end, + -- The attach code will apply to all LSP clients treesitter_analysis = true, -- treesitter variable context sumneko_root_path = vim.fn.expand("$HOME") .. "/github/sumneko/lua-language-server", @@ -168,6 +169,12 @@ require.'navigator'.setup({ -- set to {} to disable the lspclient for all filetype }, gopls = { -- gopls setting + on_attach = function(client, bufnr) -- on_attach for gopls + -- your special on attach here + -- e.g. disable gopls format because a known issue https://github.com/golang/go/issues/45732 + print("i am a hook, I will disable document format") + client.resolved_capabilities.document_formatting = false + end, settings = { gopls = {gofumpt = false} -- disable gofumpt etc, } diff --git a/lua/navigator/lspclient/attach.lua b/lua/navigator/lspclient/attach.lua index 163d3de..5ff476b 100644 --- a/lua/navigator/lspclient/attach.lua +++ b/lua/navigator/lspclient/attach.lua @@ -12,17 +12,12 @@ end local M = {} M.on_attach = function(client, bufnr) - local uri = vim.uri_from_bufnr(bufnr) - - log("loading for ft ", ft, uri) - if uri == 'file://' or uri == 'file:///' then - log("skip loading for ft ", ft, uri) - return - end - log("attaching", bufnr) + log("attaching", bufnr, client.name) trace(client) local hassig, sig = pcall(require, "lsp_signature") - if hassig then sig.on_attach() end + if hassig then + sig.on_attach() + end diagnostic_map(bufnr) -- lspsaga require"navigator.lspclient.highlight".add_highlight() @@ -43,10 +38,20 @@ M.on_attach = function(client, bufnr) local capabilities = vim.lsp.protocol.make_client_capabilities() capabilities.textDocument.completion.completionItem.snippetSupport = true - local config = require"navigator".config_value - if config ~= nil and config.on_attach ~= nil then config.on_attach(client, bufnr) end + local config = require"navigator".config_values() + trace(client.name, "navigator on attach") + if config.on_attach ~= nil then + trace(client.name, "general attach") + config.on_attach(client, bufnr) + end + if config.lsp and config.lsp[client.name] and config.lsp[client.name].on_attach ~= nil then + trace(client.name, "custom attach") + config.lsp[client.name].on_attach(client, bufnr) + end end -M.setup = function(cfg) return M end +M.setup = function(cfg) + return M +end return M diff --git a/lua/navigator/lspclient/clients.lua b/lua/navigator/lspclient/clients.lua index 1ccc3ab..bcfc8dd 100644 --- a/lua/navigator/lspclient/clients.lua +++ b/lua/navigator/lspclient/clients.lua @@ -336,6 +336,7 @@ local function setup(user_opts) highlight.diagnositc_config_sign() highlight.add_highlight() local lsp_opts = user_opts.lsp + _Loading = true wait_lsp_startup(ft, retry, lsp_opts) _LoadedClients[ft] = true diff --git a/lua/navigator/util.lua b/lua/navigator/util.lua index 8759f12..b71eaa1 100644 --- a/lua/navigator/util.lua +++ b/lua/navigator/util.lua @@ -85,7 +85,7 @@ function M.get_relative_path(base_path, my_path) return data end -local default_config = {plugin = "navigator", use_console = false, use_file = true, level = "info"} +local default_config = {plugin = "navigator", use_console = false, use_file = true, level = "error"} M._log = require("guihua.log").new({level = default_config.level}, true)