deprecate nvim 0.7 and 0.8.0; use vim.lsp.inlay_hint for nightly

pull/390/head
ray-x 8 months ago
parent 44a32ebb4f
commit 23d2f63ee9

@ -280,9 +280,7 @@ M.setups = function()
}, },
}, },
} }
if vim.fn.has('nvim-0.8.3') == 1 then setups.settings.gopls.semanticTokens = true
setups.settings.gopls.semanticTokens = true
end
local v = M.version() local v = M.version()
if v == nil then if v == nil then
return return
@ -300,18 +298,13 @@ M.setups = function()
setups.settings.gopls.buildFlags = { tags } setups.settings.gopls.buildFlags = { tags }
end end
if ver > 70 and ver < 100 then setups.settings.gopls = vim.tbl_deep_extend('force', setups.settings.gopls, {
setups.settings.gopls = vim.tbl_deep_extend('force', setups.settings.gopls, { experimentalUseInvalidMetadata = true,
experimentalUseInvalidMetadata = true, })
-- hoverKind = "Structured",
})
end
if ver > 80 and ver < 100 then setups.settings.gopls = vim.tbl_deep_extend('force', setups.settings.gopls, {
setups.settings.gopls = vim.tbl_deep_extend('force', setups.settings.gopls, { experimentalWatchedFileDelay = '200ms',
experimentalWatchedFileDelay = '200ms', })
})
end
if ver > 90 and _GO_NVIM_CFG.lsp_inlay_hints.enable then if ver > 90 and _GO_NVIM_CFG.lsp_inlay_hints.enable then
setups.settings.gopls = vim.tbl_deep_extend('force', setups.settings.gopls, { setups.settings.gopls = vim.tbl_deep_extend('force', setups.settings.gopls, {
hints = { hints = {

@ -8,21 +8,25 @@ local utils = require('go.utils')
local log = utils.log local log = utils.log
local trace = utils.trace local trace = utils.trace
local config local config
local nvim10 = vim.fn.has('nvim-0.10') == 1
-- Update inlay hints when opening a new buffer and when writing a buffer to a -- Update inlay hints when opening a new buffer and when writing a buffer to a
-- file -- file
-- opts is a string representation of the table of options -- opts is a string representation of the table of options
local should_update = {} local should_update = {}
function M.setup() function M.setup()
local events = { 'BufWritePost', 'BufEnter', 'InsertLeave', 'FocusGained', 'CursorHold'} local events = { 'BufWritePost', 'BufEnter', 'InsertLeave', 'FocusGained', 'CursorHold' }
config = _GO_NVIM_CFG.lsp_inlay_hints config = _GO_NVIM_CFG.lsp_inlay_hints
if not config or config.enable == false then -- diabled
return
end
if config.only_current_line then if config.only_current_line then
local user_events = vim.split(config.only_current_line_autocmd, ',') local user_events = vim.split(config.only_current_line_autocmd, ',')
events = vim.tbl_extend('keep', events, user_events) events = vim.tbl_extend('keep', events, user_events)
end end
local cmd_group = api.nvim_create_augroup('gopls_inlay', {}) local cmd_group = api.nvim_create_augroup('gopls_inlay', {})
api.nvim_create_autocmd(events, { api.nvim_create_autocmd({ 'BufEnter', 'InsertLeave', 'FocusGained', 'CursorHold' }, {
group = cmd_group, group = cmd_group,
pattern = { '*.go', '*.mod' }, pattern = { '*.go', '*.mod' },
callback = function() callback = function()
@ -31,6 +35,17 @@ function M.setup()
end end
end, end,
}) })
api.nvim_create_autocmd({ 'BufWritePost' }, {
group = cmd_group,
pattern = { '*.go', '*.mod' },
callback = function()
if not vim.wo.diff then
local inlay = require('go.inlay')
inlay.disable_inlay_hints(true)
inlay.set_inlay_hints()
end
end,
})
api.nvim_create_user_command('GoToggleInlay', function(_) api.nvim_create_user_command('GoToggleInlay', function(_)
require('go.inlay').toggle_inlay_hints() require('go.inlay').toggle_inlay_hints()
@ -103,6 +118,7 @@ local function parseHints(result)
end end
for _, value in pairs(result) do for _, value in pairs(result) do
local range = value.position local range = value.position
-- range.character = range.character
local line = value.position.line local line = value.position.line
local label = value.label local label = value.label
local kind = value.kind local kind = value.kind
@ -142,24 +158,24 @@ local function get_max_len(bufnr, parsed_data)
return max_len return max_len
end end
-- inlay hints are supported natively in 0.10 nightly
function nvim10_inline_hints(bufnr, vtext, hint, config) function nvim10_inline_hints(bufnr, vtext, hint, cfg)
cfg = cfg or config
if hint and hint.kind == 1 then if hint and hint.kind == 1 then
vtext = ' ' .. vtext vtext = ' ' .. vtext
end end
pcall(function() pcall(function()
vim.api.nvim_buf_set_extmark(bufnr, namespace, hint.range.line, hint.range.character, { vim.api.nvim_buf_set_extmark(bufnr, namespace, hint.range.line, hint.range.character, {
virt_text_pos = "inline", virt_text_pos = 'inline',
virt_text = { virt_text = {
{ vtext, config.highlight }, { vtext, config.highlight },
}, },
strict = false, strict = false,
hl_mode = 'combine', hl_mode = 'combine',
}) })
end) end)
end end
local function handler_inline(err, result, ctx) local function handler_inline(err, result, ctx)
trace(result, ctx) trace(result, ctx)
@ -186,7 +202,7 @@ local function handler_inline(err, result, ctx)
local parsed = parseHints(result) local parsed = parseHints(result)
trace(parsed) trace(parsed)
-- parsed is a map of line numbers to hints, -- parsed is a map of line numbers to hints,
-- hint includes label, range, and kind -- hint includes label, range, and kind
-- I only plan to deal gopls response -- I only plan to deal gopls response
@ -317,6 +333,9 @@ local function handler(err, result, ctx)
end end
function M.toggle_inlay_hints() function M.toggle_inlay_hints()
if nvim10 then
return vim.lsp.inlay_hint(vim.buf.nvim_get_current_buf())
end
if enabled then if enabled then
M.disable_inlay_hints(true) M.disable_inlay_hints(true)
else else
@ -326,6 +345,11 @@ function M.toggle_inlay_hints()
end end
function M.disable_inlay_hints(update) function M.disable_inlay_hints(update)
if nvim10 then
local bufnr = vim.api.nvim_get_current_buf()
vim.lsp.inlay_hint(bufnr, false)
return
end
-- clear namespace which clears the virtual text as well -- clear namespace which clears the virtual text as well
vim.api.nvim_buf_clear_namespace(0, namespace, 0, -1) vim.api.nvim_buf_clear_namespace(0, namespace, 0, -1)
@ -354,22 +378,20 @@ function M.set_inlay_hints()
if vim.wo.diff then if vim.wo.diff then
return return
end end
if nvim10 then
local bufnr = vim.api.nvim_get_current_buf()
return vim.lsp.inlay_hint(bufnr, true)
end
local fname = fn.expand('%:p') local fname = fn.expand('%:p')
local filetime = fn.getftime(fname) local filetime = fn.getftime(fname)
if should_update[fname] == filetime then if should_update[fname] == filetime then
return return
end end
local h = handler local h = handler
local nvim10 = vim.fn.has('nvim-0.10') == 1
if nvim10 then
h = handler_inline
end
vim.defer_fn(function() vim.defer_fn(function()
vim.lsp.buf_request(bufnr, 'textDocument/inlayHint', get_params(), h) vim.lsp.buf_request(bufnr, 'textDocument/inlayHint', get_params(), h)
should_update[fname] = filetime should_update[fname] = filetime
end, 100) end, 100)
end end
return M return M
@ -396,4 +418,5 @@ return M
line = 78 line = 78
} }
} }
]]-- ]]
--

Loading…
Cancel
Save