Skip to content

chrisgrieser/nvim-lsp-endhints

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

nvim-lsp-endhints πŸͺ§

badge

Minimal plugin that displays LSP inlay hints at the end of the line, rather than within the line.

Showcase

Color scheme: nightfox.nvim, dawnfox variant

Table of Contents

Installation

Requirements

-- lazy.nvim
{
	"chrisgrieser/nvim-lsp-endhints",
	event = "LspAttach",
	opts = {}, -- required, even if empty
},

-- packer
use {
	"chrisgrieser/nvim-lsp-endhints",
	config = function()
		require("lsp-endhints").setup() -- required, even if empty
	end,
}

Configuration

The .setup() call is required.

-- default settings
require("lsp-endhints").setup {
	autoEnableHints = true,
	icons = {
		type = "󰜁 ",
		parameter = "σ°ͺ ",
		offspec = " ", -- hint kind not defined in official LSP spec
		unknown = " ", -- hint kind is nil
	},
	label = {
		truncateAtChars = 20,
		padding = 1,
		marginLeft = 0,
		sameKindSeparator = ", ",
	},
	extmark = {
		priority = 50,
	},

	---Function that overrides how hints are displayed.
	---expects as output a table for `virt_text` from `nvim_buf_set_extmark`,
	---that is a table of string tuples (text & highlight group)
	---To use filetype-specific formatting, get the filetype via
	---`vim.bo[bufnr].filetype`, to conditionally use the default formatting 
	---function, use `defaultHintFormatFunc(hints)`.
	---@type function(hints: {label: string, col: number, kind: string}[], bufnr: number, defaultHintFormatFunc: func): {[1]: string, [2]: string}[]
	hintFormatFunc = nil,
}

The hints use the default highlight group LspInlayHint.

Usage

By default, the plugin automatically enables inlay hints when attaching to an LSP, there is nothing to do other than loading the plugin.

All regular inlay hint functions like vim.lsp.inlay_hint.enable() work the same as before. Use them as described in the Neovim documentation to enable/disable/toggle hints manually.

You can switch between displaying inlay hints at the end of the line (this plugin) and within the line (Neovim default) by using the enable, disable and toggle functions:

-- inlay hints will show at the end of the line (default)
require("lsp-endhints").enable()

-- inlay hints will show as if the plugin was not installed
require("lsp-endhints").disable()

-- toggle between the two
require("lsp-endhints").toggle()

Background

FAQ

How to display hints only for the current line?

That is not supported by the plugin. However, it only takes a small snippet to implement it yourself. (Note that the linked snippet is not compatible with this plugin.)

Compatibility with other inlay hints plugins

Since this plugin overrides the nvim handler for "textDocument/inlayHint", other plugins that interact with inlay hints may be incompatible with it.

However, if the other plugin is using specific commands related to inlay hints rather than permanently displaying them like nvim-lsp-endhints, you can temporarily disable endhints, trigger the other plugin, and then re-enable endhints. Binding that to a custom function should allow you to use the other plugin without issues then.

How to enable inlay hints for an LSP?

Not all LSPs support inlay hints. The following list is not exhaustive, there are more LSPs that support inlay hints. Please refer to your LSP's documentation.

-- lua-ls
vim.lsp.config("lua_ls", {
	settings = {
		Lua = {
			hint = { enable = true },
		},
	},
}

-- typescript
local inlayHints = {
	includeInlayParameterNameHints = "all",
	includeInlayParameterNameHintsWhenArgumentMatchesName = false,
	includeInlayFunctionParameterTypeHints = true,
	includeInlayVariableTypeHints = true,
	includeInlayVariableTypeHintsWhenTypeMatchesName = false,
	includeInlayPropertyDeclarationTypeHints = true,
	includeInlayFunctionLikeReturnTypeHints = true,
	includeInlayEnumMemberValueHints = true,
}
vim.lsp.config("ts_ls", {
	settings = {
		typescript = {
			inlayHints = inlayHints,
		},
		javascript = {
			inlayHints = inlayHints,
		},
	},
}

-- gopls
vim.lsp.config("gopls", {
	settings = {
		hints = {
			rangeVariableTypes = true,
			parameterNames = true,
			constantValues = true,
			assignVariableTypes = true,
			compositeLiteralFields = true,
			compositeLiteralTypes = true,
			functionTypeParameters = true,
		},
	},
}

-- clangd
vim.lsp.config("clangd", {
	settings = {
		clangd = {
			InlayHints = {
				Designators = true,
				Enabled = true,
				ParameterNames = true,
				DeducedTypes = true,
			},
			fallbackFlags = { "-std=c++20" },
		},
	},
}

How to change the formatting just for a specific filetype or LSP?

Use the opts.hintFormatFunc like this:

require("lsp-endhints").setup {
	hintFormatFunc = function(hints, bufnr, defaultHintFormatFunc)
		local lsp = vim.lsp.get_clients({ bufnr = bufnr })[1].name
		if lsp == "rust_analyzer" then
			return "..." -- use your own formatting here
		else
			return defaultHintFormatFunc(hints) -- fallback to default formatting
		end
	end
}

About the author

In my day job, I am a sociologist studying the social mechanisms underlying the digital economy. For my PhD project, I investigate the governance of the app economy and how software ecosystems manage the tension between innovation and compatibility. If you are interested in this subject, feel free to get in touch.

Buy Me a Coffee at ko-fi.com

About

Display LSP inlay hints at the end of the line, rather than within the line.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 6

Languages