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

Commit 3f7e096

Browse files
committed
Add clangd-14 inlay hints support
1 parent 90f15d3 commit 3f7e096

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Plug 'nvim-lua/lsp_extensions.nvim'
1313

1414
### Available Features
1515

16-
#### Rust
17-
- [Inlay Hints](#inlay-hints-rust-analyzer)
16+
#### Rust/C++
17+
- [Inlay Hints](#inlay-hints-rust-analyzerclangd-14)
1818

1919
#### Dart
2020
- [Closing Labels](#closing-labels-dartls)
@@ -24,39 +24,61 @@ Plug 'nvim-lua/lsp_extensions.nvim'
2424
- [Diagnostics](#workspace-diagnostics)
2525

2626

27-
## Inlay Hints (rust-analyzer)
27+
## Inlay Hints (rust-analyzer/clangd-14)
2828

2929
![Customized](https://i.imgur.com/FRRas1c.png)
30+
![CustomizedCpp](https://imgur.com/a/iKAk0b9)
31+
32+
**Note**: Minial requirement for clangd inlay hints is clangd-14, you need to set `clangdInlayHintsProvider` to true in clangd's `init_options`
33+
```lua
34+
lspconfig.clangd.setup {
35+
...
36+
init_options = {
37+
clangdInlayHintsProvider = true,
38+
...
39+
},
40+
...
41+
}
42+
```
3043

3144
Inlay hints for the whole file:
3245

3346
```vimscript
3447
nnoremap <Leader>T :lua require'lsp_extensions'.inlay_hints()
48+
" For C++ set lsp_client to clangd
49+
nnoremap <Leader>T :lua require'lsp_extensions'.inlay_hints{ lsp_client = "clangd" }
3550
```
3651

3752
Only current line:
3853

3954
```vimscript
4055
nnoremap <Leader>t :lua require'lsp_extensions'.inlay_hints{ only_current_line = true }
56+
" For C++ set lsp_client to clangd
57+
nnoremap <Leader>t :lua require'lsp_extensions'.inlay_hints{ lsp_client = "clangd", only_current_line = true }
4158
```
4259

4360
Run on showing file or new file in buffer:
4461

4562
```vimscript
4663
autocmd BufEnter,BufWinEnter,TabEnter *.rs :lua require'lsp_extensions'.inlay_hints{}
64+
" For C++ set lsp_client to clangd
65+
autocmd BufEnter,BufWinEnter,TabEnter *.cpp :lua require'lsp_extensions'.inlay_hints{ lsp_client = "clangd" }
4766
```
4867

4968
On cursor hover, get hints for current line:
5069

5170
```vimscript
5271
autocmd CursorHold,CursorHoldI *.rs :lua require'lsp_extensions'.inlay_hints{ only_current_line = true }
72+
" For C++ set lsp_client to clangd
73+
autocmd CursorHold,CursorHoldI *.cpp :lua require'lsp_extensions'.inlay_hints{ lsp_client = "clangd", only_current_line = true }
5374
```
5475

55-
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:
76+
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:
5677
**Note:** Not all hints will be displayed if this is set. For easier readability, only hints of one type are shown per line.
57-
78+
**Note:** For clangd you have to explicitly specify the type of the hints to provide, currently, it have "parameter" and "type" [clangd doc](https://clangd.llvm.org/extensions#inlay-hints)
5879
```vimscript
5980
:lua require('lsp_extensions').inlay_hints{ enabled = {"TypeHint", "ChainingHint", "ParameterHint"} }
81+
:lua require('lsp_extensions').inlay_hints{ lsp_client = "clangd", enabled = {"parameter", "type"} }
6082
```
6183

6284
Available Options (Showing defaults):
@@ -65,6 +87,7 @@ Available Options (Showing defaults):
6587
require'lsp_extensions'.inlay_hints{
6688
highlight = "Comment",
6789
prefix = " > ",
90+
lsp_client = "rust-analyzer",
6891
aligned = false,
6992
only_current_line = false,
7093
enabled = { "ChainingHint" }
@@ -73,6 +96,8 @@ require'lsp_extensions'.inlay_hints{
7396

7497
```vimscript
7598
autocmd InsertLeave,BufEnter,BufWinEnter,TabEnter,BufWritePost *.rs :lua require'lsp_extensions'.inlay_hints{ prefix = ' » ', highlight = "NonText", enabled = {"ChainingHint"} }
99+
" For C++
100+
autocmd InsertLeave,BufEnter,BufWinEnter,TabEnter,BufWritePost *.cpp :lua require'lsp_extensions'.inlay_hints{ lsp_client = "clangd" prefix = ' » ', highlight = "NonText", enabled = {"type"} }
76101
```
77102

78103
## Closing Labels (dartls)

examples/inlay_hints_inline.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ local M = {}
44

55
-- Global function, so you can just call it on the lua side
66
ShowInlineInlayHints = function()
7-
vim.lsp.buf_request(0, 'rust-analyzer/inlayHints', inlay_hints.get_params(), inlay_hints.get_callback {
7+
-- For C++ replace "rust-analyzer" with "clangd"
8+
vim.lsp.buf_request(0, inlay_hints.request_name{ lsp_client = "rust-analyzer"}, inlay_hints.get_params(), inlay_hints.get_callback {
89
only_current_line = true
910
})
1011
end
1112

1213
-- @rockerboo
1314
M.show_line_hints_on_cursor_events = function()
15+
-- For C++ replace *.rs with *.cpp
1416
vim.cmd [[augroup ShowLineHints]]
1517
vim.cmd [[ au!]]
1618
vim.cmd [[ autocmd CursorHold,CursorHoldI,CursorMoved *.rs :lua ShowInlineInlayHints()]]

lua/lsp_extensions/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ local extensions = {}
2121
local inlay_hints = require('lsp_extensions.inlay_hints')
2222

2323
extensions.inlay_hints = function(opts)
24-
vim.lsp.buf_request(0, 'rust-analyzer/inlayHints', inlay_hints.get_params(), inlay_hints.get_callback(opts))
24+
vim.lsp.buf_request(0, inlay_hints.request_name(opts), inlay_hints.get_params(), inlay_hints.get_callback(opts))
2525
end
2626

2727
return extensions

lua/lsp_extensions/inlay_hints.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ local inlay_hints = {}
3333

3434
local inlay_hints_ns = vim.api.nvim_create_namespace("lsp_extensions.inlay_hints")
3535

36+
inlay_hints.request_name = function(opts)
37+
return string.format("%s/inlayHints", opts.lsp_client or "rust-analyzer")
38+
end
39+
3640
inlay_hints.request = function(opts, bufnr)
37-
vim.lsp.buf_request(bufnr or 0, "rust-analyzer/inlayHints", inlay_hints.get_params(),
41+
vim.lsp.buf_request(bufnr or 0, inlay_hints.request_name(opts), inlay_hints.get_params(),
3842
inlay_hints.get_callback(opts))
3943

4044
-- TODO: At some point, rust probably adds this?

0 commit comments

Comments
 (0)