Skip to content

Commit d6ef492

Browse files
authored
fix(clangd): set offset_encoding correctly #3887
Problem: Neovim's native Client:initialize implementation does not support clangd's utf8 offsets extension, resulting in an inconsistency in offsetEncoding between clangd and Neovim when using vim.lsp.config. Solution: Add a custom on_init to set the offset_encoding correctly.
1 parent 8adb3b5 commit d6ef492

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

lsp/clangd.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ local function symbol_info()
5757
end, bufnr)
5858
end
5959

60+
---@class ClangdInitializeResult: lsp.InitializeResult
61+
---@field offsetEncoding? string
62+
6063
return {
6164
cmd = { 'clangd' },
6265
filetypes = { 'c', 'cpp', 'objc', 'objcpp', 'cuda', 'proto' },
@@ -77,6 +80,13 @@ return {
7780
},
7881
offsetEncoding = { 'utf-8', 'utf-16' },
7982
},
83+
---@param client vim.lsp.Client
84+
---@param init_result ClangdInitializeResult
85+
on_init = function(client, init_result)
86+
if init_result.offsetEncoding then
87+
client.offset_encoding = init_result.offsetEncoding
88+
end
89+
end,
8090
on_attach = function()
8191
vim.api.nvim_buf_create_user_command(0, 'LspClangdSwitchSourceHeader', function()
8292
switch_source_header(0)

0 commit comments

Comments
 (0)