Plugin for displaying the inlay hints for the current line in the echo area.
(Left to right, top to bottom, click to zoom in) Lua, Rust, TypeScript (TSX). Theming: modus-operandi, lualine.nvim (ayu), kitty.
- Neovim 0.10+.
- Language server support for inlay hints.
- Treesitter support (optional) to display source expressions.
Install using lazy.nvim,
{
"amnn/lsp-echohint.nvim",
opts = {},
}
Explicit configuration is not required, but the following options are available:
require("lsp-echohint").setup {
-- Whether to automatically enable inlay hints if the language server
-- advertises support for them.
auto_enable = true,
-- How to convert the list of hints into a displayable string. The example
-- below combines all the hints into one string, delimited by " | ", and
-- prints that with a plain format. The default implementation is similar,
-- but includes the following additional features:
--
-- - Cleans up hints of punctuation (leading and trailing colons).
--
-- - Attempts to display the source expressions (usually variables) for
-- type hints.
--
-- - Highlights (roughly) where the cursor is by highlighting the nearest
-- delimiter in the output.
--
-- - Applies the `Identifier` highlight group to source expressions and
-- parameter hints, the `Type` highlight to type hints, and the
-- `Delimiter` highlight to surrounding punctuation.
--
-- @param line number -- the current line number.
-- @param hints table -- the list of hints for that line. Each hint is a
-- table containing:
-- - label string -- the hint text.
-- - character number -- the character (column position)
-- - kind number -- the hint kind (1 = Type, 2 = Parameter).
--
-- @return table -- a list of { text, highlight } pairs, to be passed to
-- `vim.api.nvim_echo`.
display = function(line, hints)
local output = vim.iter(hints)
:map(function(hint) return hint.label end)
:join(" | ")
return { { output, "Normal" } }
end
}
There are a couple of plugins (listed below) that provide related functionality (alternative ways of displaying inlay hints), typically by displaying them at the end of the line. This plugin tries to combine the parts I found most useful from both of these plugins (credited below), while also providing an even more pared down experience, as I find it too distracting to see hints on every line, all the time.
- chrisgrieser/nvim-lsp-endhints:
I used this plugin as a reference for the
textDocument/inlayHint
callback, the logic for detecting server support for inlay hints and auto-enabling the feature, the clean-ups of leading and trailing colons, and the GitHub workflows for managing a Neovim plugin repository. - felpafel/inlay-hint.nvim: I borrowed the idea of exposing a display callback from this plugin, as well as the idea of extracting the source expression using treesitter.
Displaying the hint is triggered by the CursorHold
autocmd. By default, this
has a delay of 4 seconds, which may be too long for you. This can be controlled
using the updatetime
setting:
vim.opt.updatetime = 1500 -- show hints after 1.5s
But note that this controls the delay for all cursor hold events, not just displaying inlay hints.
Please take a look at my personal configuration for an example of enabling inlay hints across some common language servers.
Take a look at the Related Plugins for plugins that offer other ways to display inlay hints. Alternatively, simply enabling inlay hints using
vim.api.inlay_hint.enable(true)
will enable it for your current buffer, and the default rendering will display hints inline (as originally intended), and this can be done automatically by adding the following autocmd to your configuration:
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("EnableInlayHints", { clear = true }),
desc = "Enable inlay hints if the server supports them",
callback = function(ctx)
local client = ctx.data.client_id
and vim.lsp.get_client_by_id(ctx.data.client_id)
if not client then return end
if client.server_capabilities.inlayHintProvider then
vim.lsp.inlay_hint.enable(true, { bufnr = ctx.buf })
end
end,
})
Contributions are very welcome! If you notice a bug, try updating to the latest version and if it is still there please share a report as an issue, with:
- Details on your configuration (operating system, Neovim version, minimal
init.vim, version or git revision of
lsp-echohint.nvim
). - The sequence of actions you took.
- The expected outcome.
- The actual outcome, with screenshots if relevant.
If you are interested in working on features please take a look at current open issues for inspiration!