aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/lsp/util.lua
diff options
context:
space:
mode:
authorTristan Knight <admin@snappeh.com>2024-09-05 08:23:11 +0100
committerGitHub <noreply@github.com>2024-09-05 00:23:11 -0700
commit882a450a2982caa05fcbbe90e94246a8c2b45b8b (patch)
treee42f1e5ea42016e4aca4afbfc641592dd8ded98b /runtime/lua/vim/lsp/util.lua
parent220b8aa6fe4be4e8f13600a5d7ae19fef79ba53a (diff)
downloadrneovim-882a450a2982caa05fcbbe90e94246a8c2b45b8b.tar.gz
rneovim-882a450a2982caa05fcbbe90e94246a8c2b45b8b.tar.bz2
rneovim-882a450a2982caa05fcbbe90e94246a8c2b45b8b.zip
fix(lsp): handle locations exceeding line length #30253
Problem: LSP spec [states](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position) that "if the character value is greater than the line length it defaults back to the line length", but `locations_to_items` fails in that case. Solution: Adjust locations_to_items to follow the spec. closes #28281
Diffstat (limited to 'runtime/lua/vim/lsp/util.lua')
-rw-r--r--runtime/lua/vim/lsp/util.lua14
1 files changed, 12 insertions, 2 deletions
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua
index cbbe3a66b7..ec66449b54 100644
--- a/runtime/lua/vim/lsp/util.lua
+++ b/runtime/lua/vim/lsp/util.lua
@@ -1795,8 +1795,18 @@ function M.locations_to_items(locations, offset_encoding)
local row = pos.line
local end_row = end_pos.line
local line = lines[row] or ''
- local col = M._str_byteindex_enc(line, pos.character, offset_encoding)
- local end_col = M._str_byteindex_enc(lines[end_row] or '', end_pos.character, offset_encoding)
+ local line_len = vim.fn.strcharlen(line)
+ local end_line = lines[end_row] or ''
+ local end_line_len = vim.fn.strcharlen(end_line)
+ -- LSP spec: if character > line length, default to the line length.
+ -- https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position
+ local col = pos.character <= line_len
+ and M._str_byteindex_enc(line, pos.character, offset_encoding)
+ or line_len
+ local end_col = end_pos.character <= end_line_len
+ and M._str_byteindex_enc(end_line, end_pos.character, offset_encoding)
+ or end_line_len
+
table.insert(items, {
filename = filename,
lnum = row + 1,