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

View File

@ -99,11 +99,12 @@ local function tokens_to_ranges(data, bufnr, client, request)
local legend = client.server_capabilities.semanticTokensProvider.legend local legend = client.server_capabilities.semanticTokensProvider.legend
local token_types = legend.tokenTypes local token_types = legend.tokenTypes
local token_modifiers = legend.tokenModifiers local token_modifiers = legend.tokenModifiers
local encoding = client.offset_encoding
local lines = api.nvim_buf_get_lines(bufnr, 0, -1, false) local lines = api.nvim_buf_get_lines(bufnr, 0, -1, false)
local ranges = {} ---@type STTokenRange[] local ranges = {} ---@type STTokenRange[]
local start = uv.hrtime() local start = uv.hrtime()
local ms_to_ns = 1000 * 1000 local ms_to_ns = 1e6
local yield_interval_ns = 5 * ms_to_ns local yield_interval_ns = 5 * ms_to_ns
local co, is_main = coroutine.running() 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 -- data[i+3] +1 because Lua tables are 1-indexed
local token_type = token_types[data[i + 3] + 1] 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 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] = { ranges[#ranges + 1] = {
line = line, line = line,
start_col = start_col, start_col = start_col,

View File

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