From 4ca6b376a705240d88e0b90f59462e699f77b2a0 Mon Sep 17 00:00:00 2001 From: ray-x Date: Mon, 17 Jan 2022 16:03:48 +1100 Subject: [PATCH] breaking changes: https://github.com/neovim/neovim/issues/14090, issue #136 --- lua/navigator/definition.lua | 5 ++-- lua/navigator/dochighlight.lua | 4 +-- lua/navigator/formatting.lua | 2 +- lua/navigator/lspclient/attach.lua | 45 +++++++++++++++++------------- lua/navigator/util.lua | 11 ++++++++ 5 files changed, 42 insertions(+), 25 deletions(-) diff --git a/lua/navigator/definition.lua b/lua/navigator/definition.lua index 42150ae..a6bed0a 100644 --- a/lua/navigator/definition.lua +++ b/lua/navigator/definition.lua @@ -21,15 +21,16 @@ local definition_hdlr = util.mk_handler(function(err, locations, ctx, _) end local client = vim.lsp.get_client_by_id(ctx.client_id) + local oe = require('navigator.util').encoding() if vim.tbl_islist(locations) then if #locations > 1 then local items = locations_to_items(locations) gui.new_list_view({ items = items, api = 'Definition' }) else - vim.lsp.util.jump_to_location(locations[1], client.offset_encoding) + vim.lsp.util.jump_to_location(locations[1], oe) end else - vim.lsp.util.jump_to_location(locations, client.offset_encoding) + vim.lsp.util.jump_to_location(locations, oe) end end) diff --git a/lua/navigator/dochighlight.lua b/lua/navigator/dochighlight.lua index 98e04b2..a2aecc9 100644 --- a/lua/navigator/dochighlight.lua +++ b/lua/navigator/dochighlight.lua @@ -154,7 +154,7 @@ local handle_document_highlight = mk_handler(function(_, result, ctx) references[ctx.bufnr] = result local client_id = ctx.client_id local client = vim.lsp.get_client_by_id(client_id) - vim.lsp.util.buf_highlight_references(ctx.bufnr, result, client.offset_encoding) + vim.lsp.util.buf_highlight_references(ctx.bufnr, result, util.encoding()) end) -- modify from vim-illuminate local function goto_adjent_reference(opt) @@ -248,7 +248,7 @@ local function documentHighlight() local client_id = ctx.client_id local client = vim.lsp.get_client_by_id(client_id) vim.lsp.util.buf_clear_references(bufnr) - vim.lsp.util.buf_highlight_references(bufnr, result, client.offset_encoding) + vim.lsp.util.buf_highlight_references(bufnr, result, util.encoding()) table.sort(result, function(a, b) return before(a.range, b.range) end) diff --git a/lua/navigator/formatting.lua b/lua/navigator/formatting.lua index 2d61df3..ad9ce6c 100644 --- a/lua/navigator/formatting.lua +++ b/lua/navigator/formatting.lua @@ -10,7 +10,7 @@ return { local util = require('navigator.util') local log = util.log - local offset_encoding = vim.lsp.get_client_by_id(ctx.client_id).offset_encoding + local offset_encoding = util.encoding(vim.lsp.get_client_by_id(ctx.client_id)) -- If the buffer hasn't been modified before the formatting has finished, -- update the buffer diff --git a/lua/navigator/lspclient/attach.lua b/lua/navigator/lspclient/attach.lua index 1381184..e21fad5 100644 --- a/lua/navigator/lspclient/attach.lua +++ b/lua/navigator/lspclient/attach.lua @@ -1,62 +1,67 @@ local vim, api = vim, vim.api -local lsp = require("vim.lsp") +local lsp = require('vim.lsp') -local util = require "navigator.util" +local util = require('navigator.util') local log = util.log local trace = util.trace local diagnostic_map = function(bufnr) - local opts = {noremap = true, silent = true} - api.nvim_buf_set_keymap(bufnr, "n", "]O", ":lua vim.lsp.diagnostic.set_loclist()", opts) + local opts = { noremap = true, silent = true } + api.nvim_buf_set_keymap(bufnr, 'n', ']O', ':lua vim.lsp.diagnostic.set_loclist()', opts) end local M = {} M.on_attach = function(client, bufnr) + bufnr = bufnr or 0 + + if bufnr == 0 then + log('no bufnr provided') + end local uri = vim.uri_from_bufnr(bufnr) - if uri == "file://" or uri == "file:///" or #uri < 11 then - log("skip for float buffer", uri) - return {error = "invalid file", result = nil} + if uri == 'file://' or uri == 'file:///' or #uri < 11 then + log('skip for float buffer', uri) + return { error = 'invalid file', result = nil } end - log("attaching: ", bufnr, client.name, uri) + log('attaching: ', bufnr, client.name, uri) trace(client) diagnostic_map(bufnr) -- add highlight for Lspxxx - require"navigator.lspclient.highlight".add_highlight() - require"navigator.lspclient.highlight".diagnositc_config_sign() - api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc") + require('navigator.lspclient.highlight').add_highlight() + require('navigator.lspclient.highlight').diagnositc_config_sign() + api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') - require("navigator.lspclient.mapping").setup({ + require('navigator.lspclient.mapping').setup({ client = client, bufnr = bufnr, - cap = client.resolved_capabilities + cap = client.resolved_capabilities, }) if client.resolved_capabilities.document_highlight then - require("navigator.dochighlight").documentHighlight() + require('navigator.dochighlight').documentHighlight() end - require"navigator.lspclient.lspkind".init() + require('navigator.lspclient.lspkind').init() - local config = require"navigator".config_values() - trace(client.name, "navigator on attach") + local config = require('navigator').config_values() + trace(client.name, 'navigator on attach') if config.on_attach ~= nil then - log(client.name, "customized attach for all clients") + log(client.name, 'customized attach for all clients') config.on_attach(client, bufnr) end if config.lsp and config.lsp[client.name] and config.lsp[client.name].on_attach ~= nil then - log("lsp client specific attach for", client.name) + log('lsp client specific attach for', client.name) config.lsp[client.name].on_attach(client, bufnr) end if _NgConfigValues.lsp.code_action.enable then if client.resolved_capabilities.code_action then log('code action enabled for client', client.resolved_capabilities.code_action) - vim.cmd [[autocmd CursorHold,CursorHoldI lua require'navigator.codeAction'.code_action_prompt()]] + vim.cmd([[autocmd CursorHold,CursorHoldI lua require'navigator.codeAction'.code_action_prompt()]]) end end end diff --git a/lua/navigator/util.lua b/lua/navigator/util.lua index 722dd57..ad2e539 100644 --- a/lua/navigator/util.lua +++ b/lua/navigator/util.lua @@ -402,6 +402,17 @@ function M.empty(t) return next(t) == nil end +function M.encoding(client) + local oe = client.offset_encoding + if oe == nil then + return 'utf-8' + end + if type(oe) == 'table' then + oe = oe[1] or 'utf-8' + end + return oe +end + -- alternatively: use vim.notify("namespace does not exist or is anonymous", vim.log.levels.ERROR) function M.warn(msg)