Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

feat(inlay-hints): add inlay hints toggle function #346

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ rt.setup({
-- Commands:
-- RustEnableInlayHints
-- RustDisableInlayHints
-- RustToggleInlayHints
-- RustSetInlayHints
-- RustUnsetInlayHints

Expand All @@ -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()
```
</details>

Expand Down
4 changes: 4 additions & 0 deletions lua/rust-tools/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local M = {
inlay_hints = {
enable = nil,
disable = nil,
toggle = nil,
set = nil,
unset = nil,
cache = nil,
Expand Down Expand Up @@ -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")
Expand Down
15 changes: 13 additions & 2 deletions lua/rust-tools/inlay_hints.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
TheFedaikin marked this conversation as resolved.
Show resolved Hide resolved
local self = setmetatable({ cache = {} }, { __index = M })
TheFedaikin marked this conversation as resolved.
Show resolved Hide resolved

return self
end
Expand All @@ -17,6 +17,7 @@ end
-- Disable hints and clear all cached buffers
function M.disable(self)
self.disable = false
M.is_enabled = false
TheFedaikin marked this conversation as resolved.
Show resolved Hide resolved
M.disable_cache_autocmd()

for k, _ in pairs(self.cache) do
Expand All @@ -34,11 +35,21 @@ end

-- Enable auto hints and set hints for the current buffer
function M.enable(self)
self.enabled = true
self.enable = false
TheFedaikin marked this conversation as resolved.
Show resolved Hide resolved
M.is_enabled = true
TheFedaikin marked this conversation as resolved.
Show resolved Hide resolved
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
TheFedaikin marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand Down
3 changes: 3 additions & 0 deletions lua/rust-tools/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down