diff options
author | Lewis Russell <lewis6991@gmail.com> | 2024-11-11 14:23:38 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-11 14:23:38 +0000 |
commit | 3e855d533f4477ffddfe94ccea48255979c8b7fb (patch) | |
tree | f3f29ad97d1a15c9b379a1dce629267b9f1256e8 | |
parent | ff575b38864022b0b27527f11636c49107acd1bc (diff) | |
download | rneovim-3e855d533f4477ffddfe94ccea48255979c8b7fb.tar.gz rneovim-3e855d533f4477ffddfe94ccea48255979c8b7fb.tar.bz2 rneovim-3e855d533f4477ffddfe94ccea48255979c8b7fb.zip |
perf(lsp): use faster version of str_byteindex
-rw-r--r-- | runtime/lua/vim/_editor.lua | 38 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/semantic_tokens.lua | 14 | ||||
-rw-r--r-- | runtime/lua/vim/shared.lua | 2 |
3 files changed, 34 insertions, 20 deletions
diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua index cbab2a0165..b4a1e0fc15 100644 --- a/runtime/lua/vim/_editor.lua +++ b/runtime/lua/vim/_editor.lua @@ -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 - vim.validate('s', s, 'string') - vim.validate('index', index, 'number') + -- 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 - vim.validate('encoding', encoding, function(v) - return utfs[v], 'invalid encoding' - end) + if not utfs[encoding] then + vim.validate('encoding', encoding, function(v) + 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 strict_indexing = true end @@ -828,8 +835,11 @@ function vim.str_utfindex(s, encoding, index, strict_indexing) return col32, col16 end - vim.validate('s', s, 'string') - vim.validate('index', index, 'number', true) + 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 - vim.validate('encoding', encoding, function(v) - return utfs[v], 'invalid encoding' - end) + if not utfs[encoding] then + vim.validate('encoding', encoding, function(v) + 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 strict_indexing = true end diff --git a/runtime/lua/vim/lsp/semantic_tokens.lua b/runtime/lua/vim/lsp/semantic_tokens.lua index f23c694c4b..215e5f41aa 100644 --- a/runtime/lua/vim/lsp/semantic_tokens.lua +++ b/runtime/lua/vim/lsp/semantic_tokens.lua @@ -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, diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua index 1a3b33c885..4f2373b182 100644 --- a/runtime/lua/vim/shared.lua +++ b/runtime/lua/vim/shared.lua @@ -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) |