diff options
author | Mathias Fussenegger <f.mathias@zignar.net> | 2024-09-13 19:57:04 +0200 |
---|---|---|
committer | Mathias Fußenegger <mfussenegger@users.noreply.github.com> | 2024-09-13 22:34:49 +0200 |
commit | 8512f669f0e095df99e0456db11a0e0b5a2b0485 (patch) | |
tree | 85e8bdce410a78196f4736f6e11cb789257a43a6 | |
parent | 755512ed60fe6c9b7fbd3bf57c827d938ff6d743 (diff) | |
download | rneovim-8512f669f0e095df99e0456db11a0e0b5a2b0485.tar.gz rneovim-8512f669f0e095df99e0456db11a0e0b5a2b0485.tar.bz2 rneovim-8512f669f0e095df99e0456db11a0e0b5a2b0485.zip |
fix(lsp): handle nil bytes in strings
Problem:
The LSP omnifunc can insert nil bytes, which when read in other places
(like semantic token) could cause an error:
semantic_tokens.lua:304: Vim:E976: Using a Blob as a String
Solution:
Use `#line` instead of `vim.fn.strlen(line)`. Both return UTF-8 bytes
but the latter can't handle nil bytes.
Completion candidates can currently insert nil bytes, if other parts of
Alternative fix to https://github.com/neovim/neovim/pull/30359
Note that https://github.com/neovim/neovim/pull/30315 will avoid the
insertion of nil bytes from the LSP omnifunc, but the change of this PR
can more easily be backported.
-rw-r--r-- | runtime/lua/vim/lsp/util.lua | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua index 09b97ac861..65f84e8f87 100644 --- a/runtime/lua/vim/lsp/util.lua +++ b/runtime/lua/vim/lsp/util.lua @@ -154,7 +154,7 @@ end ---@param encoding string utf-8|utf-16|utf-32| defaults to utf-16 ---@return integer byte (utf-8) index of `encoding` index `index` in `line` function M._str_byteindex_enc(line, index, encoding) - local len = vim.fn.strlen(line) + local len = #line if index > len then -- LSP spec: if character > line length, default to the line length. -- https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position @@ -167,7 +167,7 @@ function M._str_byteindex_enc(line, index, encoding) if index then return index else - return #line + return len end elseif encoding == 'utf-16' then return vim.str_byteindex(line, index, true) |