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

Commit 93a04b4

Browse files
committed
allow multiple hints per line
1 parent a1f12b8 commit 93a04b4

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ autocmd CursorHold,CursorHoldI *.rs :lua require'lsp_extensions'.inlay_hints{ on
5353
```
5454

5555
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:
56-
**Note:** Hints will overwrite if other hints using this. Only the last hint will be shown.
56+
**Note:** Not all hints will be displayed if this is set. For easier readability, only hints of one type are shown per line.
5757

5858
```vimscript
5959
:lua require('lsp_extensions').inlay_hints{ enabled = {"TypeHint", "ChainingHint", "ParameterHint"} }

lua/lsp_extensions/inlay_hints.lua

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface InlayHint {
2525
label: string,
2626
}
2727
```
28-
--]]
28+
--]]
2929

3030
local inlay_hints = {}
3131

@@ -81,8 +81,12 @@ inlay_hints.get_callback = function(opts)
8181

8282
for _, hint in ipairs(result) do
8383
local finish = hint.range["end"].line
84-
if not hint_store[finish] and in_list(enabled)(hint.kind) then
85-
hint_store[finish] = hint
84+
if in_list(enabled)(hint.kind) then
85+
if not hint_store[finish] then
86+
hint_store[finish] = {hint}
87+
elseif hint_store[finish][1].kind == hint.kind then
88+
table.insert(hint_store[finish], hint)
89+
end
8690

8791
if aligned then
8892
longest_line = math.max(longest_line,
@@ -91,8 +95,8 @@ inlay_hints.get_callback = function(opts)
9195
end
9296
end
9397

94-
local display_virt_text = function(hint)
95-
local end_line = hint.range["end"].line
98+
local display_virt_text = function(hints)
99+
local end_line = hints[1].range["end"].line
96100

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

103107
local text
104-
if aligned then
105-
local line_length = #vim.api.nvim_buf_get_lines(bufnr, end_line, end_line + 1, false)[1]
106-
text = string.format("%s %s", (" "):rep(longest_line - line_length), prefix .. hint.label)
107-
else
108-
text = prefix .. hint.label
108+
for _, hint in ipairs(hints) do
109+
if aligned then
110+
local line_length = #vim.api.nvim_buf_get_lines(bufnr, end_line, end_line + 1, false)[1]
111+
text = string.format("%s %s", (" "):rep(longest_line - line_length), prefix .. hint.label)
112+
else
113+
text = (text or "") .. prefix .. hint.label
114+
end
109115
end
110116
vim.api.nvim_buf_set_virtual_text(bufnr, inlay_hints_ns, end_line, {{text, highlight}}, {})
111117
end
112118

113119
if only_current_line then
114-
local hint = hint_store[vim.api.nvim_win_get_cursor(0)[1] - 1]
120+
local hints = hint_store[vim.api.nvim_win_get_cursor(0)[1] - 1]
115121

116-
if not hint then
122+
if not hints then
117123
return
118124
else
119-
display_virt_text(hint)
125+
display_virt_text(hints)
120126
end
121127
else
122-
for _, hint in pairs(hint_store) do display_virt_text(hint) end
128+
for _, hints in pairs(hint_store) do display_virt_text(hints) end
123129
end
124130
end
125131
end

0 commit comments

Comments
 (0)