From 6b1f8ea4c0af881e9479d83e6670a84abf2f378c Mon Sep 17 00:00:00 2001 From: Danil Tarasov Date: Tue, 7 Mar 2023 20:13:41 +0300 Subject: [PATCH 1/3] feat(inlay-hints): add inlay hints global state and toggle function --- lua/rust-tools/inlay_hints.lua | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lua/rust-tools/inlay_hints.lua b/lua/rust-tools/inlay_hints.lua index 0df31a1..19e895e 100644 --- a/lua/rust-tools/inlay_hints.lua +++ b/lua/rust-tools/inlay_hints.lua @@ -4,7 +4,7 @@ local M = {} function M.new() M.namespace = vim.api.nvim_create_namespace("textDocument/inlayHints") - local self = setmetatable({ cache = {}, enabled = false }, { __index = M }) + local self = setmetatable({ cache = {} }, { __index = M }) return self end @@ -17,6 +17,7 @@ end -- Disable hints and clear all cached buffers function M.disable(self) self.disable = false + M.is_enabled = false M.disable_cache_autocmd() for k, _ in pairs(self.cache) do @@ -34,11 +35,22 @@ end -- Enable auto hints and set hints for the current buffer function M.enable(self) - self.enabled = true + self.enable = false + M.is_enabled = true M.enable_cache_autocmd() set_all(self) end +-- Toggles inlay hints state globally. Uses disable and enable internally +function M.toggle(self) + if self.is_enabled then + M.disabled(self) + else + M.enable(self) + end + M.is_enabled = not M.is_enabled +end + -- Set inlay hints only for the current buffer function M.set(self) M.cache_render(self, 0) From 560059b48d61733ad9680f0f6ec5b02fad57c8f8 Mon Sep 17 00:00:00 2001 From: Danil Tarasov Date: Wed, 3 May 2023 16:18:23 +0300 Subject: [PATCH 2/3] feat(inlay-hints): fix implementation, improve documentation --- README.md | 3 +++ lua/rust-tools/init.lua | 4 ++++ lua/rust-tools/inlay_hints.lua | 3 +-- lua/rust-tools/lsp.lua | 3 +++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 72f67a8..47c3d21 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ rt.setup({ -- Commands: -- RustEnableInlayHints -- RustDisableInlayHints + -- RustToggleInlayHints -- RustSetInlayHints -- RustUnsetInlayHints @@ -85,6 +86,8 @@ rt.setup({ require('rust-tools').inlay_hints.enable() -- Disable inlay hints auto update and unset them for all buffers require('rust-tools').inlay_hints.disable() + -- Toggle between enabled and disabled inlay hints states for all buffer + require('rust-tools').inlay_hints.toggle() ``` diff --git a/lua/rust-tools/init.lua b/lua/rust-tools/init.lua index ca07557..cbfe05e 100644 --- a/lua/rust-tools/init.lua +++ b/lua/rust-tools/init.lua @@ -11,6 +11,7 @@ local M = { inlay_hints = { enable = nil, disable = nil, + toggle = nil, set = nil, unset = nil, cache = nil, @@ -82,6 +83,9 @@ function M.setup(opts) render = function() inlay.render(hints) end, + toggle = function() + inlay.toggle(hints) + end, } local join_lines = require("rust-tools.join_lines") diff --git a/lua/rust-tools/inlay_hints.lua b/lua/rust-tools/inlay_hints.lua index 19e895e..c270052 100644 --- a/lua/rust-tools/inlay_hints.lua +++ b/lua/rust-tools/inlay_hints.lua @@ -44,11 +44,10 @@ end -- Toggles inlay hints state globally. Uses disable and enable internally function M.toggle(self) if self.is_enabled then - M.disabled(self) + M.disable(self) else M.enable(self) end - M.is_enabled = not M.is_enabled end -- Set inlay hints only for the current buffer diff --git a/lua/rust-tools/lsp.lua b/lua/rust-tools/lsp.lua index 3288eeb..a2f2cdd 100644 --- a/lua/rust-tools/lsp.lua +++ b/lua/rust-tools/lsp.lua @@ -52,6 +52,9 @@ local function setup_commands() RustDisableInlayHints = { rt.inlay_hints.disable, }, + RustToggleInlayHints = { + rt.inlay_hints.toggle, + }, RustLastDebug = { rt.cached_commands.execute_last_debuggable, }, From acc304d1b9148b0a84f2255c961429f2ade7e059 Mon Sep 17 00:00:00 2001 From: Danil Tarasov Date: Mon, 5 Jun 2023 03:31:53 +0300 Subject: [PATCH 3/3] feat(hints): remove useless variables to control toggle --- lua/rust-tools/inlay_hints.lua | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/lua/rust-tools/inlay_hints.lua b/lua/rust-tools/inlay_hints.lua index c270052..bf73565 100644 --- a/lua/rust-tools/inlay_hints.lua +++ b/lua/rust-tools/inlay_hints.lua @@ -4,7 +4,7 @@ local M = {} function M.new() M.namespace = vim.api.nvim_create_namespace("textDocument/inlayHints") - local self = setmetatable({ cache = {} }, { __index = M }) + local self = setmetatable({ cache = {}, enabled = false }, { __index = M }) return self end @@ -16,8 +16,7 @@ end -- Disable hints and clear all cached buffers function M.disable(self) - self.disable = false - M.is_enabled = false + self.enabled = false M.disable_cache_autocmd() for k, _ in pairs(self.cache) do @@ -35,26 +34,20 @@ end -- Enable auto hints and set hints for the current buffer function M.enable(self) - self.enable = false - M.is_enabled = true + self.enabled = true M.enable_cache_autocmd() set_all(self) end -- Toggles inlay hints state globally. Uses disable and enable internally function M.toggle(self) - if self.is_enabled then + if self.enabled then M.disable(self) else M.enable(self) end end --- Set inlay hints only for the current buffer -function M.set(self) - M.cache_render(self, 0) -end - -- Clear hints only for the current buffer function M.unset() clear_ns(0)