From 5713f79265695668ca333fdc2b1d3e3fde3dd945 Mon Sep 17 00:00:00 2001 From: ray-x Date: Fri, 29 Jul 2022 19:02:43 +1000 Subject: [PATCH] clear autocmd #191 --- lua/navigator/diagnostics.lua | 4 +-- lua/navigator/dochighlight.lua | 8 ++--- lua/navigator/lspclient/attach.lua | 9 ++++- lua/navigator/lspclient/mapping.lua | 51 +++++------------------------ 4 files changed, 22 insertions(+), 50 deletions(-) diff --git a/lua/navigator/diagnostics.lua b/lua/navigator/diagnostics.lua index 6cf39d4..d377cc3 100644 --- a/lua/navigator/diagnostics.lua +++ b/lua/navigator/diagnostics.lua @@ -394,8 +394,8 @@ M.show_buf_diagnostics = function() end -- set loc list win -M.set_diag_loclist = function() - local bufnr = api.nvim_get_current_buf() +M.set_diag_loclist = function(bufnr) + bufnr = bufnr or api.nvim_get_current_buf() local diag_cnt = get_count(bufnr, [[Error]]) + get_count(bufnr, [[Warning]]) if diag_cnt == 0 then log('great, no errors!') diff --git a/lua/navigator/dochighlight.lua b/lua/navigator/dochighlight.lua index 35fb1e4..a5dff63 100644 --- a/lua/navigator/dochighlight.lua +++ b/lua/navigator/dochighlight.lua @@ -145,7 +145,6 @@ local handle_document_highlight = function(_, result, ctx) return end if type(result) ~= 'table' or vim.fn.empty(result) == 1 then - log('clear up', result) vim.lsp.util.buf_clear_references(ctx.bufnr) return end @@ -205,11 +204,12 @@ local function cmd_nohl() end end -local nav_doc_hl = function() - local bufnr = vim.api.nvim_get_current_buf() +local nav_doc_hl = function(bufnr) + bufnr = bufnr or vim.api.nvim_get_current_buf() local ref_params = vim.lsp.util.make_position_params() vim.lsp.for_each_buffer_client(bufnr, function(client, _, _) if client.server_capabilities.documentHighlightProvider == true then + trace("sending doc highlight", client.name, bufnr) client.request('textDocument/documentHighlight', ref_params, handle_document_highlight, bufnr) end end) @@ -224,7 +224,7 @@ local function documentHighlight(bufnr) group = cmd_group, buffer = bufnr, callback = function() - require('navigator.dochighlight').nav_doc_hl() + require('navigator.dochighlight').nav_doc_hl(bufnr) end, }) diff --git a/lua/navigator/lspclient/attach.lua b/lua/navigator/lspclient/attach.lua index dcbad49..ae7009d 100644 --- a/lua/navigator/lspclient/attach.lua +++ b/lua/navigator/lspclient/attach.lua @@ -36,9 +36,16 @@ M.on_attach = function(client, bufnr) }) if client.server_capabilities.documentHighlightProvider == true then - require('navigator.dochighlight').documentHighlight(bufnr) + trace('attaching doc highlight: ', bufnr, client.name) + vim.defer_fn(function() + require('navigator.dochighlight').documentHighlight(bufnr) + end, 50) -- allow a bit time for it to settle down + else + log('skip doc highlight: ', bufnr, client.name) end + + require('navigator.lspclient.lspkind').init() local config = require('navigator').config_values() diff --git a/lua/navigator/lspclient/mapping.lua b/lua/navigator/lspclient/mapping.lua index ca2cb66..784a1f8 100644 --- a/lua/navigator/lspclient/mapping.lua +++ b/lua/navigator/lspclient/mapping.lua @@ -3,12 +3,6 @@ local log = util.log local trace = util.trace local api = vim.api -local event_hdlrs = { - { ev = 'BufWritePre', func = require('navigator.diagnostics').set_diag_loclist }, - { ev = { 'CursorHold', 'CursorHoldI' }, func = vim.lsp.buf.document_highlight }, - { ev = 'CursorMoved', func = vim.lsp.buf.clear_references }, -} - if vim.lsp.buf.format == nil then vim.lsp.buf.format = vim.lsp.buf.formatting end @@ -282,42 +276,6 @@ local function autocmd() }) end -local function set_event_handler(user_opts) - user_opts = user_opts or {} - local file_types = { - '*.c', - '*.cpp', - '*.h', - '*.go', - '*.python', - '*.vim', - '*.sh', - '*.javascript', - '*.html', - '*.css', - '*.lua', - '*.typescript', - '*.rust', - '*.javascriptreact', - '*.typescriptreact', - '*.kotlin', - '*.php', - '*.dart', - '*.nim', - '*.java', - } - -- local format_files = "c,cpp,h,go,python,vim,javascript,typescript" --html,css, - - local gn = api.nvim_create_augroup('nvim_nv_event_autos', {}) - for _, value in pairs(event_hdlrs) do - api.nvim_create_autocmd(value.ev, { - group = gn, - pattern = file_types, - callback = value.func, - }) - end -end - M.toggle_lspformat = function(on) if on == nil then _NgConfigValues.lsp.format_on_save = not _NgConfigValues.lsp.format_on_save @@ -349,7 +307,6 @@ function M.setup(attach_opts) set_cmds(attach_opts) autocmd() - set_event_handler(attach_opts) local client = attach_opts.client or {} local cap = client.server_capabilities or vim.lsp.protocol.make_client_capabilities() @@ -392,6 +349,14 @@ function M.setup(attach_opts) }) end + api.nvim_create_autocmd({ 'BufWritePre' }, { + group = api.nvim_create_augroup('nvim_nv_event_autos', {}), + buffer = attach_opts.bufnr, + callback = function() + require('navigator.diagnostics').set_diag_loclist(attach_opts.bufnr) + end, + }) + local border_style = single if _NgConfigValues.border == 'double' then border_style = double