perf(lsp): use faster version of str_byteindex

This commit is contained in:
Lewis Russell 2024-11-11 14:23:38 +00:00 committed by GitHub
parent ff575b3886
commit 3e855d533f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 20 deletions

View File

@ -764,8 +764,11 @@ function vim.str_byteindex(s, encoding, index, strict_indexing)
return vim._str_byteindex(s, old_index, use_utf16) or error('index out of range')
end
-- Avoid vim.validate for performance.
if type(s) ~= 'string' or type(index) ~= 'number' then
vim.validate('s', s, 'string')
vim.validate('index', index, 'number')
end
local len = #s
@ -773,11 +776,15 @@ function vim.str_byteindex(s, encoding, index, strict_indexing)
return 0
end
if not utfs[encoding] then
vim.validate('encoding', encoding, function(v)
return utfs[v], 'invalid encoding'
end)
end
if strict_indexing ~= nil and type(strict_indexing) ~= 'boolean' then
vim.validate('strict_indexing', strict_indexing, 'boolean', true)
end
if strict_indexing == nil then
strict_indexing = true
end
@ -828,8 +835,11 @@ function vim.str_utfindex(s, encoding, index, strict_indexing)
return col32, col16
end
if type(s) ~= 'string' or (index ~= nil and type(index) ~= 'number') then
vim.validate('s', s, 'string')
vim.validate('index', index, 'number', true)
end
if not index then
index = math.huge
strict_indexing = false
@ -839,11 +849,15 @@ function vim.str_utfindex(s, encoding, index, strict_indexing)
return 0
end
if not utfs[encoding] then
vim.validate('encoding', encoding, function(v)
return utfs[v], 'invalid encoding'
end)
end
if strict_indexing ~= nil and type(strict_indexing) ~= 'boolean' then
vim.validate('strict_indexing', strict_indexing, 'boolean', true)
end
if strict_indexing == nil then
strict_indexing = true
end

View File

@ -99,11 +99,12 @@ local function tokens_to_ranges(data, bufnr, client, request)
local legend = client.server_capabilities.semanticTokensProvider.legend
local token_types = legend.tokenTypes
local token_modifiers = legend.tokenModifiers
local encoding = client.offset_encoding
local lines = api.nvim_buf_get_lines(bufnr, 0, -1, false)
local ranges = {} ---@type STTokenRange[]
local start = uv.hrtime()
local ms_to_ns = 1000 * 1000
local ms_to_ns = 1e6
local yield_interval_ns = 5 * ms_to_ns
local co, is_main = coroutine.running()
@ -135,14 +136,13 @@ local function tokens_to_ranges(data, bufnr, client, request)
-- data[i+3] +1 because Lua tables are 1-indexed
local token_type = token_types[data[i + 3] + 1]
local modifiers = modifiers_from_number(data[i + 4], token_modifiers)
local end_char = start_char + data[i + 2]
local buf_line = lines and lines[line + 1] or ''
local start_col = vim.str_byteindex(buf_line, client.offset_encoding, start_char, false)
local end_col = vim.str_byteindex(buf_line, client.offset_encoding, end_char, false)
if token_type then
local modifiers = modifiers_from_number(data[i + 4], token_modifiers)
local end_char = start_char + data[i + 2]
local buf_line = lines and lines[line + 1] or ''
local start_col = vim.str_byteindex(buf_line, encoding, start_char, false)
local end_col = vim.str_byteindex(buf_line, encoding, end_char, false)
ranges[#ranges + 1] = {
line = line,
start_col = start_col,

View File

@ -1142,7 +1142,7 @@ end
--- @param mod T
--- @return T
function vim._defer_require(root, mod)
return setmetatable({}, {
return setmetatable({ _submodules = mod }, {
---@param t table<string, any>
---@param k string
__index = function(t, k)