fix(lsp): handle out-of-bounds character positions #30288

Problem:
str_byteindex_enc could return an error if the index was longer than the
lline length. This was handled in each of the calls to it individually

Solution:
* Fix the call at the source level so that if the index is higher than
  the line length, line length is returned as per LSP specification
* Remove pcalls on str_byteindex_enc calls. No longer needed now that
  str_byteindex_enc has a bounds check.
This commit is contained in:
Tristan Knight
2024-09-08 11:44:46 +01:00
committed by GitHub
parent 0cfbc6eaff
commit 003b8a251d
4 changed files with 71 additions and 55 deletions

View File

@@ -3607,21 +3607,21 @@ describe('LSP', function()
range = {
['end'] = {
character = 8,
line = 9,
line = 3,
},
start = {
character = 6,
line = 9,
line = 3,
},
},
selectionRange = {
['end'] = {
character = 8,
line = 9,
line = 3,
},
start = {
character = 6,
line = 9,
line = 3,
},
},
uri = 'file:///home/jiangyinzuo/hello.cpp',
@@ -3651,21 +3651,21 @@ describe('LSP', function()
range = {
['end'] = {
character = 8,
line = 8,
line = 2,
},
start = {
character = 6,
line = 8,
line = 2,
},
},
selectionRange = {
['end'] = {
character = 8,
line = 8,
line = 2,
},
start = {
character = 6,
line = 8,
line = 2,
},
},
uri = 'file:///home/jiangyinzuo/hello.cpp',
@@ -3679,7 +3679,15 @@ describe('LSP', function()
})
local client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd })
local handler = require 'vim.lsp.handlers'['typeHierarchy/subtypes']
handler(nil, clangd_response, { client_id = client_id, bufnr = 1 })
local bufnr = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, {
'class B : public A{};',
'class C : public B{};',
'class D1 : public C{};',
'class D2 : public C{};',
'class E : public D1, D2 {};',
})
handler(nil, clangd_response, { client_id = client_id, bufnr = bufnr })
return vim.fn.getqflist()
end)
@@ -3689,7 +3697,7 @@ describe('LSP', function()
col = 7,
end_col = 0,
end_lnum = 0,
lnum = 10,
lnum = 4,
module = '',
nr = 0,
pattern = '',
@@ -3703,7 +3711,7 @@ describe('LSP', function()
col = 7,
end_col = 0,
end_lnum = 0,
lnum = 9,
lnum = 3,
module = '',
nr = 0,
pattern = '',
@@ -3763,7 +3771,15 @@ describe('LSP', function()
})
local client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd })
local handler = require 'vim.lsp.handlers'['typeHierarchy/subtypes']
handler(nil, jdtls_response, { client_id = client_id, bufnr = 1 })
local bufnr = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, {
'package mylist;',
'',
'public class MyList {',
' static class Inner extends MyList{}',
'~}',
})
handler(nil, jdtls_response, { client_id = client_id, bufnr = bufnr })
return vim.fn.getqflist()
end)
@@ -3840,21 +3856,21 @@ describe('LSP', function()
range = {
['end'] = {
character = 8,
line = 9,
line = 3,
},
start = {
character = 6,
line = 9,
line = 3,
},
},
selectionRange = {
['end'] = {
character = 8,
line = 9,
line = 3,
},
start = {
character = 6,
line = 9,
line = 3,
},
},
uri = 'file:///home/jiangyinzuo/hello.cpp',
@@ -3884,21 +3900,21 @@ describe('LSP', function()
range = {
['end'] = {
character = 8,
line = 8,
line = 2,
},
start = {
character = 6,
line = 8,
line = 2,
},
},
selectionRange = {
['end'] = {
character = 8,
line = 8,
line = 2,
},
start = {
character = 6,
line = 8,
line = 2,
},
},
uri = 'file:///home/jiangyinzuo/hello.cpp',
@@ -3912,7 +3928,16 @@ describe('LSP', function()
})
local client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd })
local handler = require 'vim.lsp.handlers'['typeHierarchy/supertypes']
handler(nil, clangd_response, { client_id = client_id, bufnr = 1 })
local bufnr = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, {
'class B : public A{};',
'class C : public B{};',
'class D1 : public C{};',
'class D2 : public C{};',
'class E : public D1, D2 {};',
})
handler(nil, clangd_response, { client_id = client_id, bufnr = bufnr })
return vim.fn.getqflist()
end)
@@ -3922,7 +3947,7 @@ describe('LSP', function()
col = 7,
end_col = 0,
end_lnum = 0,
lnum = 10,
lnum = 4,
module = '',
nr = 0,
pattern = '',
@@ -3936,7 +3961,7 @@ describe('LSP', function()
col = 7,
end_col = 0,
end_lnum = 0,
lnum = 9,
lnum = 3,
module = '',
nr = 0,
pattern = '',
@@ -3996,7 +4021,15 @@ describe('LSP', function()
})
local client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd })
local handler = require 'vim.lsp.handlers'['typeHierarchy/supertypes']
handler(nil, jdtls_response, { client_id = client_id, bufnr = 1 })
local bufnr = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, {
'package mylist;',
'',
'public class MyList {',
' static class Inner extends MyList{}',
'~}',
})
handler(nil, jdtls_response, { client_id = client_id, bufnr = bufnr })
return vim.fn.getqflist()
end)