From 6853b785d9916be6ffe4965aefd8554ed276f802 Mon Sep 17 00:00:00 2001 From: Gary Murray Date: Thu, 30 Nov 2023 11:53:40 -0700 Subject: [PATCH] fix(lsp): detect if using nvim-0.10 and use new inlay_hint.enable method (#2007) * Detect if using nvim 0.10 and use new inlay_hint.enable method * Add lsp util for inlay-hints and update keymap * Remove the need to check vim version * Support older nightly builds * Move inlay_hint toggle in Util.toggle --------- Co-authored-by: Gary Murray --- lua/lazyvim/config/keymaps.lua | 4 ++-- lua/lazyvim/plugins/lsp/init.lua | 2 +- lua/lazyvim/util/toggle.lua | 11 +++++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lua/lazyvim/config/keymaps.lua b/lua/lazyvim/config/keymaps.lua index c5f70016..f7fe57d6 100644 --- a/lua/lazyvim/config/keymaps.lua +++ b/lua/lazyvim/config/keymaps.lua @@ -119,8 +119,8 @@ map("n", "ul", function() Util.toggle.number() end, { desc = "Toggle Lin map("n", "ud", function() Util.toggle.diagnostics() end, { desc = "Toggle Diagnostics" }) local conceallevel = vim.o.conceallevel > 0 and vim.o.conceallevel or 3 map("n", "uc", function() Util.toggle("conceallevel", false, {0, conceallevel}) end, { desc = "Toggle Conceal" }) -if vim.lsp.inlay_hint then - map("n", "uh", function() vim.lsp.inlay_hint(0, nil) end, { desc = "Toggle Inlay Hints" }) +if vim.lsp.buf.inlay_hint or vim.lsp.inlay_hint then + map( "n", "uh", function() Util.toggle.inlay_hints() end, { desc = "Toggle Inlay Hints" }) end map("n", "uT", function() if vim.b.ts_highlight then vim.treesitter.stop() else vim.treesitter.start() end end, { desc = "Toggle Treesitter Highlight" }) diff --git a/lua/lazyvim/plugins/lsp/init.lua b/lua/lazyvim/plugins/lsp/init.lua index f0823e46..f454ac1c 100644 --- a/lua/lazyvim/plugins/lsp/init.lua +++ b/lua/lazyvim/plugins/lsp/init.lua @@ -115,7 +115,7 @@ return { vim.fn.sign_define(name, { text = icon, texthl = name, numhl = "" }) end - local inlay_hint = vim.lsp.buf.inlay_hint or vim.lsp.inlay_hint + local inlay_hint = vim.lsp.buf.inlay_hint or vim.lsp.inlay_hint.enable if opts.inlay_hints.enabled and inlay_hint then Util.lsp.on_attach(function(client, buffer) diff --git a/lua/lazyvim/util/toggle.lua b/lua/lazyvim/util/toggle.lua index b520070b..51e81744 100644 --- a/lua/lazyvim/util/toggle.lua +++ b/lua/lazyvim/util/toggle.lua @@ -53,6 +53,17 @@ function M.diagnostics() end end +---@param bufnr? number +function M.inlay_hints(bufnr) + bufnr = bufnr or 0 + local inlay_hint = vim.lsp.buf.inlay_hint or vim.lsp.inlay_hint + if inlay_hint.enable then + vim.lsp.inlay_hint.enable(bufnr, not inlay_hint.is_enabled()) + else + vim.lsp.inlay_hint(bufnr, nil) + end +end + setmetatable(M, { __call = function(m, ...) return m.option(...)