Skip to content
This repository has been archived by the owner on Jul 7, 2022. It is now read-only.

Commit

Permalink
allow multiple hints per line
Browse files Browse the repository at this point in the history
  • Loading branch information
ebobrow committed Jun 29, 2021
1 parent a1f12b8 commit 9838ea1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ autocmd CursorHold,CursorHoldI *.rs :lua require'lsp_extensions'.inlay_hints{ on
```

By default only ChainingHint is enabled. This is due to Neovim not able to add virtual text injected into a line. To enable all hints:
**Note:** Hints will overwrite if other hints using this. Only the last hint will be shown.
**Note:** Not all hints will be displayed if this is set. For easier readability, only hints of one type are shown per line.

```vimscript
:lua require('lsp_extensions').inlay_hints{ enabled = {"TypeHint", "ChainingHint", "ParameterHint"} }
Expand Down
38 changes: 24 additions & 14 deletions lua/lsp_extensions/inlay_hints.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface InlayHint {
label: string,
}
```
--]]
--]]

local inlay_hints = {}

Expand Down Expand Up @@ -81,8 +81,12 @@ inlay_hints.get_callback = function(opts)

for _, hint in ipairs(result) do
local finish = hint.range["end"].line
if not hint_store[finish] and in_list(enabled)(hint.kind) then
hint_store[finish] = hint
if in_list(enabled)(hint.kind) then
if not hint_store[finish] then
hint_store[finish] = {hint}
elseif hint_store[finish][1].kind == hint.kind then
table.insert(hint_store[finish], hint)
end

if aligned then
longest_line = math.max(longest_line,
Expand All @@ -91,8 +95,8 @@ inlay_hints.get_callback = function(opts)
end
end

local display_virt_text = function(hint)
local end_line = hint.range["end"].line
local display_virt_text = function(hints)
local end_line = hints[1].range["end"].line

-- Check for any existing / more important virtual text on the line.
-- TODO: Figure out how stackable virtual text works? What happens if there is more than one??
Expand All @@ -101,25 +105,31 @@ inlay_hints.get_callback = function(opts)
if not vim.tbl_isempty(existing_virt_text) then return end

local text
if aligned then
local line_length = #vim.api.nvim_buf_get_lines(bufnr, end_line, end_line + 1, false)[1]
text = string.format("%s %s", (" "):rep(longest_line - line_length), prefix .. hint.label)
else
text = prefix .. hint.label
for _, hint in ipairs(hints) do
if aligned then
local line_length = #vim.api.nvim_buf_get_lines(bufnr, end_line, end_line + 1, false)[1]
text = string.format("%s %s", (" "):rep(longest_line - line_length), prefix .. hint.label)
else
if text then
text = text .. prefix .. hint.label
else
text = prefix .. hint.label
end
end
end
vim.api.nvim_buf_set_virtual_text(bufnr, inlay_hints_ns, end_line, {{text, highlight}}, {})
end

if only_current_line then
local hint = hint_store[vim.api.nvim_win_get_cursor(0)[1] - 1]
local hints = hint_store[vim.api.nvim_win_get_cursor(0)[1] - 1]

if not hint then
if not hints then
return
else
display_virt_text(hint)
display_virt_text(hints)
end
else
for _, hint in pairs(hint_store) do display_virt_text(hint) end
for _, hints in pairs(hint_store) do display_virt_text(hints) end
end
end
end
Expand Down

0 comments on commit 9838ea1

Please sign in to comment.