Skip to content

Commit

Permalink
naming and bugfix for nvim 0.10 inlay
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-x committed Nov 7, 2023
1 parent 88ff8aa commit 4a58e82
Showing 1 changed file with 59 additions and 58 deletions.
117 changes: 59 additions & 58 deletions lua/go/inlay.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ local api = vim.api
local fn = vim.fn
local utils = require('go.utils')
local log = utils.log
local trace = utils.trace
-- local trace = utils.trace
trace = log
local config
local nvim10 = vim.fn.has('nvim-0.10') == 1 and _GO_NVIM_CFG.lsp_inlay_hints.style == 'inlay'
-- local nvim10 = true
local inlay = vim.fn.has('nvim-0.10') == 1 and _GO_NVIM_CFG.lsp_inlay_hints.style == 'inlay'
-- local inlay = true
-- whether the hints are enabled or not
local enabled = nil
-- Update inlay hints when opening a new buffer and when writing a buffer to a
Expand Down Expand Up @@ -78,39 +79,7 @@ end

local namespace = vim.api.nvim_create_namespace('experimental/inlayHints')

-- parses the result into a easily parsable format
-- input
-- kind=1: return ; kind = 2: param
-- { {
-- kind = 1,
-- label = { {
-- value = "[]int"
-- } },
-- paddingLeft = true,
-- position = {
-- character = 7,
-- line = 8
-- }
-- }, {
-- kind = 2,
-- label = { {
-- value = "stack:"
-- } },
-- paddingRight = true,
-- position = {
-- character = 29,
-- line = 8
-- }
-- },

-- example:
-- {
-- ["12"] = { {
-- kind = "TypeHint",
-- label = "String"
-- } },
-- }

-- parses the result into a easily parsable format see comments EOF
local function parseHints(result)
trace(result)
local map = {}
Expand Down Expand Up @@ -161,23 +130,23 @@ local function get_max_len(bufnr, parsed_data)
return max_len
end

-- inlay hints are supported natively in 0.10 nightly
function nvim10_inline_hints(bufnr, vtext, hint, cfg)
cfg = cfg or config
if hint and hint.kind == 1 then
vtext = ' ' .. vtext
end
pcall(function()
vim.api.nvim_buf_set_extmark(bufnr, namespace, hint.range.line, hint.range.character, {
virt_text_pos = 'inline',
virt_text = {
{ vtext, config.highlight },
},
strict = false,
hl_mode = 'combine',
})
end)
end
-- -- inlay hints are supported natively in 0.10 nightly
-- local function inlay_inline_hints(bufnr, vtext, hint, cfg)
-- cfg = cfg or config
-- if hint and hint.kind == 1 then
-- vtext = ' ' .. vtext
-- end
-- pcall(function()
-- vim.api.nvim_buf_set_extmark(bufnr, namespace, hint.range.line, hint.range.character, {
-- virt_text_pos = 'inline',
-- virt_text = {
-- { vtext, config.highlight },
-- },
-- strict = false,
-- hl_mode = 'combine',
-- })
-- end)
-- end

local function handler(err, result, ctx)
trace(result, ctx)
Expand Down Expand Up @@ -295,7 +264,7 @@ local function handler(err, result, ctx)
end

function M.toggle_inlay_hints()
if nvim10 then
if inlay then
vim.lsp.inlay_hint(vim.api.nvim_get_current_buf())
elseif enabled then
M.disable_inlay_hints(true)
Expand All @@ -306,7 +275,7 @@ function M.toggle_inlay_hints()
end

function M.disable_inlay_hints(update)
if nvim10 then
if inlay or _GO_NVIM_CFG.lsp_inlay_hints.sytle == 'eol' then
local bufnr = vim.api.nvim_get_current_buf()
vim.lsp.inlay_hint(bufnr, false)
return
Expand Down Expand Up @@ -339,12 +308,12 @@ function M.set_inlay_hints()
end
local fname = fn.expand('%:p')
local filetime = fn.getftime(fname)
if nvim10 then
if inlay then
local wrap = utils.throttle(function()
vim.lsp.inlay_hint(bufnr, enabled)
should_update[fname] = filetime
end, 300)
wrap()
return wrap()
end
trace('old style inlay')
local fname = fn.expand('%:p')
Expand Down Expand Up @@ -389,6 +358,38 @@ return M
]]
--

-- input
-- kind=1: return ; kind = 2: param
-- { {
-- kind = 1,
-- label = { {
-- value = "[]int"
-- } },
-- paddingLeft = true,
-- position = {
-- character = 7,
-- line = 8
-- }
-- }, {
-- kind = 2,
-- label = { {
-- value = "stack:"
-- } },
-- paddingRight = true,
-- position = {
-- character = 29,
-- line = 8
-- }
-- },

-- example:
-- {
-- ["12"] = { {
-- kind = "TypeHint",
-- label = "String"
-- } },
-- }

-- local function handler_inline(err, result, ctx)
-- trace(result, ctx)
--
Expand Down Expand Up @@ -425,7 +426,7 @@ return M
-- trace(hint)
-- local label = unpack_label(hint.label)
-- trace(bufnr, namespace, label, hint, config)
-- nvim10_inline_hints(bufnr, label, hint, config)
-- inlay_inline_hints(bufnr, label, hint, config)
-- end
-- end
-- end

0 comments on commit 4a58e82

Please sign in to comment.