refactor: create function for deferred loading

The benefit of this is that users only pay for what they use. If e.g.
only `vim.lsp.buf_get_clients()` is called then they don't need to load
all modules under `vim.lsp` which could lead to significant startuptime
saving.

Also `vim.lsp.module` is a bit nicer to user compared to
`require("vim.lsp.module")`.

This isn't used for some nested modules such as `filetype` as it breaks
tests with error messages such as "attempt to index field 'detect'".
It's not entirely certain the reason for this, but it is likely it is
due to filetype being precompiled which would imply deferred loading
isn't needed for performance reasons.
This commit is contained in:
dundargoc
2024-01-22 18:23:28 +01:00
parent 1bf645918e
commit 24cea4c7f7
23 changed files with 119 additions and 117 deletions

View File

@@ -3,13 +3,13 @@ if exists('g:loaded_perl_provider')
endif endif
function! provider#perl#Call(method, args) abort function! provider#perl#Call(method, args) abort
return v:lua.require'vim.provider.perl'.call(a:method, a:args) return v:lua.vim.provider.perl.call(a:method, a:args)
endfunction endfunction
function! provider#perl#Require(host) abort function! provider#perl#Require(host) abort
return v:lua.require'vim.provider.perl'.require(a:host, s:prog) return v:lua.vim.provider.perl.require(a:host, s:prog)
endfunction endfunction
let s:prog = v:lua.require'vim.provider.perl'.detect() let s:prog = v:lua.vim.provider.perl.detect()
let g:loaded_perl_provider = empty(s:prog) ? 1 : 2 let g:loaded_perl_provider = empty(s:prog) ? 1 : 2
call v:lua.require'vim.provider.perl'.start() call v:lua.require'vim.provider.perl'.start()

View File

@@ -3,13 +3,13 @@ if exists('g:loaded_python3_provider')
endif endif
function! provider#python3#Call(method, args) abort function! provider#python3#Call(method, args) abort
return v:lua.require'vim.provider.python'.call(a:method, a:args) return v:lua.vim.provider.python.call(a:method, a:args)
endfunction endfunction
function! provider#python3#Require(host) abort function! provider#python3#Require(host) abort
return v:lua.require'vim.provider.python'.require(a:host) return v:lua.vim.provider.python.require(a:host)
endfunction endfunction
let s:prog = v:lua.require'vim.provider.python'.detect_by_module('neovim') let s:prog = v:lua.vim.provider.python.detect_by_module('neovim')
let g:loaded_python3_provider = empty(s:prog) ? 1 : 2 let g:loaded_python3_provider = empty(s:prog) ? 1 : 2
call v:lua.require'vim.provider.python'.start() call v:lua.require'vim.provider.python'.start()

View File

@@ -3,14 +3,14 @@ if exists('g:loaded_ruby_provider')
endif endif
function! provider#ruby#Require(host) abort function! provider#ruby#Require(host) abort
return v:lua.require'vim.provider.ruby'.require(a:host) return v:lua.vim.provider.ruby.require(a:host)
endfunction endfunction
function! provider#ruby#Call(method, args) abort function! provider#ruby#Call(method, args) abort
return v:lua.require'vim.provider.ruby'.call(a:method, a:args) return v:lua.vim.provider.ruby.call(a:method, a:args)
endfunction endfunction
let s:prog = v:lua.require'vim.provider.ruby'.detect() let s:prog = v:lua.vim.provider.ruby.detect()
let g:loaded_ruby_provider = empty(s:prog) ? 1 : 2 let g:loaded_ruby_provider = empty(s:prog) ? 1 : 2
let s:plugin_path = expand('<sfile>:p:h') . '/script_host.rb' let s:plugin_path = expand('<sfile>:p:h') . '/script_host.rb'
call v:lua.require'vim.provider.ruby'.start(s:plugin_path) call v:lua.require'vim.provider.ruby'.start(s:plugin_path)

View File

@@ -9,7 +9,7 @@ function M.check()
return return
end end
local perl_exec, perl_warnings = require('vim.provider.perl').detect() local perl_exec, perl_warnings = vim.provider.perl.detect()
if not perl_exec then if not perl_exec then
health.warn(assert(perl_warnings), { health.warn(assert(perl_warnings), {

View File

@@ -238,7 +238,7 @@ function M.check()
end end
local pythonx_warnings local pythonx_warnings
pyname, pythonx_warnings = require('vim.provider.python').detect_by_module('neovim') pyname, pythonx_warnings = vim.provider.python.detect_by_module('neovim')
if not pyname then if not pyname then
health.warn( health.warn(
@@ -363,7 +363,7 @@ function M.check()
-- can import 'pynvim'. If so, that Python failed to import 'neovim' as -- can import 'pynvim'. If so, that Python failed to import 'neovim' as
-- well, which is most probably due to a failed pip upgrade: -- well, which is most probably due to a failed pip upgrade:
-- https://github.com/neovim/neovim/wiki/Following-HEAD#20181118 -- https://github.com/neovim/neovim/wiki/Following-HEAD#20181118
local pynvim_exe = require('vim.provider.python').detect_by_module('pynvim') local pynvim_exe = vim.provider.python.detect_by_module('pynvim')
if pynvim_exe then if pynvim_exe then
local message = 'Detected pip upgrade failure: Python executable can import "pynvim" but not "neovim": ' local message = 'Detected pip upgrade failure: Python executable can import "pynvim" but not "neovim": '
.. pynvim_exe .. pynvim_exe

View File

@@ -20,7 +20,7 @@ function M.check()
end end
health.info('Ruby: ' .. health.system({ 'ruby', '-v' })) health.info('Ruby: ' .. health.system({ 'ruby', '-v' }))
local ruby_detect_table = require('vim.provider.ruby').detect() local ruby_detect_table = vim.provider.ruby.detect()
local host = ruby_detect_table[1] local host = ruby_detect_table[1]
if (not host) or host:find('^%s*$') then if (not host) or host:find('^%s*$') then
health.warn('`neovim-ruby-host` not found.', { health.warn('`neovim-ruby-host` not found.', {

View File

@@ -60,6 +60,7 @@ vim._submodules = {
iter = true, iter = true,
re = true, re = true,
text = true, text = true,
provider = true,
} }
-- These are for loading runtime modules in the vim namespace lazily. -- These are for loading runtime modules in the vim namespace lazily.

View File

@@ -1732,7 +1732,7 @@ function M.open_float(opts, ...)
if not opts.focus_id then if not opts.focus_id then
opts.focus_id = scope opts.focus_id = scope
end end
local float_bufnr, winnr = require('vim.lsp.util').open_floating_preview(lines, 'plaintext', opts) local float_bufnr, winnr = vim.lsp.util.open_floating_preview(lines, 'plaintext', opts)
for i, hl in ipairs(highlights) do for i, hl in ipairs(highlights) do
local line = lines[i] local line = lines[i]
local prefix_len = hl.prefix and hl.prefix.length or 0 local prefix_len = hl.prefix and hl.prefix.length or 0

View File

@@ -1,13 +1,4 @@
---@diagnostic disable: invisible ---@diagnostic disable: invisible
local default_handlers = require('vim.lsp.handlers')
local log = require('vim.lsp.log')
local lsp_rpc = require('vim.lsp.rpc')
local protocol = require('vim.lsp.protocol')
local ms = protocol.Methods
local util = require('vim.lsp.util')
local changetracking = require('vim.lsp._changetracking')
local semantic_tokens = require('vim.lsp.semantic_tokens')
local api = vim.api local api = vim.api
local nvim_err_writeln, nvim_buf_get_lines, nvim_command, nvim_exec_autocmds = local nvim_err_writeln, nvim_buf_get_lines, nvim_command, nvim_exec_autocmds =
api.nvim_err_writeln, api.nvim_buf_get_lines, api.nvim_command, api.nvim_exec_autocmds api.nvim_err_writeln, api.nvim_buf_get_lines, api.nvim_command, api.nvim_exec_autocmds
@@ -16,24 +7,34 @@ local tbl_isempty, tbl_extend = vim.tbl_isempty, vim.tbl_extend
local validate = vim.validate local validate = vim.validate
local if_nil = vim.F.if_nil local if_nil = vim.F.if_nil
local lsp = { local lsp = vim._defer_require('vim.lsp', {
protocol = protocol, _changetracking = ..., --- @module 'vim.lsp._changetracking'
_completion = ..., --- @module 'vim.lsp._completion'
_dynamic = ..., --- @module 'vim.lsp._dynamic'
_snippet_grammar = ..., --- @module 'vim.lsp._snippet_grammar'
_watchfiles = ..., --- @module 'vim.lsp._watchfiles'
buf = ..., --- @module 'vim.lsp.buf'
codelens = ..., --- @module 'vim.lsp.codelens'
diagnostic = ..., --- @module 'vim.lsp.diagnostic'
handlers = ..., --- @module 'vim.lsp.handlers'
inlay_hint = ..., --- @module 'vim.lsp.inlay_hint'
log = ..., --- @module 'vim.lsp.log'
protocol = ..., --- @module 'vim.lsp.protocol'
rpc = ..., --- @module 'vim.lsp.rpc'
semantic_tokens = ..., --- @module 'vim.lsp.semantic_tokens'
tagfunc = ..., --- @module 'vim.lsp.tagfunc'
util = ..., --- @module 'vim.lsp.util'
})
handlers = default_handlers, local log = lsp.log
local protocol = lsp.protocol
local ms = protocol.Methods
local util = lsp.util
local changetracking = lsp._changetracking
buf = require('vim.lsp.buf'), -- Export these directly from rpc.
diagnostic = require('vim.lsp.diagnostic'), ---@nodoc
codelens = require('vim.lsp.codelens'), lsp.rpc_response_error = lsp.rpc.rpc_response_error
inlay_hint = require('vim.lsp.inlay_hint'),
semantic_tokens = semantic_tokens,
util = util,
-- Allow raw RPC access.
rpc = lsp_rpc,
-- Export these directly from rpc.
rpc_response_error = lsp_rpc.rpc_response_error,
}
-- maps request name to the required server_capability in the client. -- maps request name to the required server_capability in the client.
lsp._request_name_to_capability = { lsp._request_name_to_capability = {
@@ -189,11 +190,11 @@ end
--- @nodoc --- @nodoc
lsp.client_errors = tbl_extend( lsp.client_errors = tbl_extend(
'error', 'error',
lsp_rpc.client_errors, lsp.rpc.client_errors,
vim.tbl_add_reverse_lookup({ vim.tbl_add_reverse_lookup({
BEFORE_INIT_CALLBACK_ERROR = table.maxn(lsp_rpc.client_errors) + 1, BEFORE_INIT_CALLBACK_ERROR = table.maxn(lsp.rpc.client_errors) + 1,
ON_INIT_CALLBACK_ERROR = table.maxn(lsp_rpc.client_errors) + 2, ON_INIT_CALLBACK_ERROR = table.maxn(lsp.rpc.client_errors) + 2,
ON_ATTACH_ERROR = table.maxn(lsp_rpc.client_errors) + 3, ON_ATTACH_ERROR = table.maxn(lsp.rpc.client_errors) + 3,
}) })
) )
@@ -800,7 +801,7 @@ function lsp.start_client(config)
---@param method (string) LSP method name ---@param method (string) LSP method name
---@return lsp.Handler|nil handler for the given method, if defined, or the default from |vim.lsp.handlers| ---@return lsp.Handler|nil handler for the given method, if defined, or the default from |vim.lsp.handlers|
local function resolve_handler(method) local function resolve_handler(method)
return handlers[method] or default_handlers[method] return handlers[method] or lsp.handlers[method]
end end
---@private ---@private
@@ -958,7 +959,7 @@ function lsp.start_client(config)
if type(cmd) == 'function' then if type(cmd) == 'function' then
rpc = cmd(dispatch) rpc = cmd(dispatch)
else else
rpc = lsp_rpc.start(cmd, cmd_args, dispatch, { rpc = lsp.rpc.start(cmd, cmd_args, dispatch, {
cwd = config.cmd_cwd, cwd = config.cmd_cwd,
env = config.cmd_env, env = config.cmd_env,
detached = config.detached, detached = config.detached,
@@ -999,7 +1000,7 @@ function lsp.start_client(config)
---@deprecated use client.progress instead ---@deprecated use client.progress instead
messages = { name = name, messages = {}, progress = {}, status = {} }, messages = { name = name, messages = {}, progress = {}, status = {} },
dynamic_capabilities = require('vim.lsp._dynamic').new(client_id), dynamic_capabilities = vim.lsp._dynamic.new(client_id),
} }
---@type table<string|integer, string> title of unfinished progress sequences by token ---@type table<string|integer, string> title of unfinished progress sequences by token
@@ -1412,7 +1413,7 @@ function lsp.start_client(config)
-- opt-out (deleting the semanticTokensProvider from capabilities) -- opt-out (deleting the semanticTokensProvider from capabilities)
vim.schedule(function() vim.schedule(function()
if vim.tbl_get(client.server_capabilities, 'semanticTokensProvider', 'full') then if vim.tbl_get(client.server_capabilities, 'semanticTokensProvider', 'full') then
semantic_tokens.start(bufnr, client.id) lsp.semantic_tokens.start(bufnr, client.id)
end end
end) end)
@@ -1969,7 +1970,7 @@ function lsp.omnifunc(findstart, base)
if log.debug() then if log.debug() then
log.debug('omnifunc.findstart', { findstart = findstart, base = base }) log.debug('omnifunc.findstart', { findstart = findstart, base = base })
end end
return require('vim.lsp._completion').omnifunc(findstart, base) return vim.lsp._completion.omnifunc(findstart, base)
end end
--- Provides an interface between the built-in client and a `formatexpr` function. --- Provides an interface between the built-in client and a `formatexpr` function.
@@ -2039,7 +2040,7 @@ end
--- ---
---@return table[] tags A list of matching tags ---@return table[] tags A list of matching tags
function lsp.tagfunc(pattern, flags) function lsp.tagfunc(pattern, flags)
return require('vim.lsp.tagfunc')(pattern, flags) return vim.lsp.tagfunc(pattern, flags)
end end
---Checks whether a client is stopped. ---Checks whether a client is stopped.

View File

@@ -8,7 +8,7 @@ local ms = protocol.Methods
---@return string parsed snippet ---@return string parsed snippet
local function parse_snippet(input) local function parse_snippet(input)
local ok, parsed = pcall(function() local ok, parsed = pcall(function()
return require('vim.lsp._snippet_grammar').parse(input) return vim.lsp._snippet_grammar.parse(input)
end) end)
return ok and tostring(parsed) or input return ok and tostring(parsed) or input
end end
@@ -206,7 +206,7 @@ function M.omnifunc(findstart, base)
local params = util.make_position_params(win, client.offset_encoding) local params = util.make_position_params(win, client.offset_encoding)
client.request(ms.textDocument_completion, params, function(err, result) client.request(ms.textDocument_completion, params, function(err, result)
if err then if err then
require('vim.lsp.log').warn(err.message) vim.lsp.log.warn(err.message)
end end
if result and vim.fn.mode() == 'i' then if result and vim.fn.mode() == 'i' then
local matches local matches

View File

@@ -1,4 +1,4 @@
local glob = require('vim.glob') local glob = vim.glob
--- @class lsp.DynamicCapabilities --- @class lsp.DynamicCapabilities
--- @field capabilities table<string, lsp.Registration[]> --- @field capabilities table<string, lsp.Registration[]>

View File

@@ -1,6 +1,6 @@
local bit = require('bit') local bit = require('bit')
local glob = require('vim.glob') local glob = vim.glob
local watch = require('vim._watch') local watch = vim._watch
local protocol = require('vim.lsp.protocol') local protocol = require('vim.lsp.protocol')
local ms = protocol.Methods local ms = protocol.Methods
local lpeg = vim.lpeg local lpeg = vim.lpeg

View File

@@ -1,8 +1,6 @@
---@brief lsp-diagnostic ---@brief lsp-diagnostic
local util = require('vim.lsp.util')
local protocol = require('vim.lsp.protocol') local protocol = require('vim.lsp.protocol')
local log = require('vim.lsp.log')
local ms = protocol.Methods local ms = protocol.Methods
local api = vim.api local api = vim.api
@@ -95,7 +93,7 @@ local function tags_lsp_to_vim(diagnostic, client_id)
tags = tags or {} tags = tags or {}
tags.deprecated = true tags.deprecated = true
else else
log.info(string.format('Unknown DiagnosticTag %d from LSP client %d', tag, client_id)) vim.lsp.log.info(string.format('Unknown DiagnosticTag %d from LSP client %d', tag, client_id))
end end
end end
return tags return tags
@@ -425,7 +423,7 @@ end
local function _refresh(bufnr, opts) local function _refresh(bufnr, opts)
opts = opts or {} opts = opts or {}
opts['bufnr'] = bufnr opts['bufnr'] = bufnr
util._refresh(ms.textDocument_diagnostic, opts) vim.lsp.util._refresh(ms.textDocument_diagnostic, opts)
end end
--- Enable pull diagnostics for a buffer --- Enable pull diagnostics for a buffer

View File

@@ -120,7 +120,7 @@ M[ms.client_registerCapability] = function(_, result, ctx)
local unsupported = {} local unsupported = {}
for _, reg in ipairs(result.registrations) do for _, reg in ipairs(result.registrations) do
if reg.method == ms.workspace_didChangeWatchedFiles then if reg.method == ms.workspace_didChangeWatchedFiles then
require('vim.lsp._watchfiles').register(reg, ctx) vim.lsp._watchfiles.register(reg, ctx)
elseif not client.dynamic_capabilities:supports_registration(reg.method) then elseif not client.dynamic_capabilities:supports_registration(reg.method) then
unsupported[#unsupported + 1] = reg.method unsupported[#unsupported + 1] = reg.method
end end
@@ -144,7 +144,7 @@ M[ms.client_unregisterCapability] = function(_, result, ctx)
for _, unreg in ipairs(result.unregisterations) do for _, unreg in ipairs(result.unregisterations) do
if unreg.method == ms.workspace_didChangeWatchedFiles then if unreg.method == ms.workspace_didChangeWatchedFiles then
require('vim.lsp._watchfiles').unregister(unreg, ctx) vim.lsp._watchfiles.unregister(unreg, ctx)
end end
end end
return vim.NIL return vim.NIL
@@ -223,19 +223,19 @@ M[ms.workspace_workspaceFolders] = function(_, _, ctx)
end end
M[ms.textDocument_publishDiagnostics] = function(...) M[ms.textDocument_publishDiagnostics] = function(...)
return require('vim.lsp.diagnostic').on_publish_diagnostics(...) return vim.lsp.diagnostic.on_publish_diagnostics(...)
end end
M[ms.textDocument_diagnostic] = function(...) M[ms.textDocument_diagnostic] = function(...)
return require('vim.lsp.diagnostic').on_diagnostic(...) return vim.lsp.diagnostic.on_diagnostic(...)
end end
M[ms.textDocument_codeLens] = function(...) M[ms.textDocument_codeLens] = function(...)
return require('vim.lsp.codelens').on_codelens(...) return vim.lsp.codelens.on_codelens(...)
end end
M[ms.textDocument_inlayHint] = function(...) M[ms.textDocument_inlayHint] = function(...)
return require('vim.lsp.inlay_hint').on_inlayhint(...) return vim.lsp.inlay_hint.on_inlayhint(...)
end end
--see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references
@@ -643,7 +643,7 @@ end
---@see https://microsoft.github.io/language-server-protocol/specification/#workspace_inlayHint_refresh ---@see https://microsoft.github.io/language-server-protocol/specification/#workspace_inlayHint_refresh
M[ms.workspace_inlayHint_refresh] = function(err, result, ctx, config) M[ms.workspace_inlayHint_refresh] = function(err, result, ctx, config)
return require('vim.lsp.inlay_hint').on_refresh(err, result, ctx, config) return vim.lsp.inlay_hint.on_refresh(err, result, ctx, config)
end end
-- Add boilerplate error validation and logging for all of these. -- Add boilerplate error validation and logging for all of these.

View File

@@ -5,7 +5,7 @@ function M.check()
local report_info = vim.health.info local report_info = vim.health.info
local report_warn = vim.health.warn local report_warn = vim.health.warn
local log = require('vim.lsp.log') local log = vim.lsp.log
local current_log_level = log.get_level() local current_log_level = log.get_level()
local log_level_string = log.levels[current_log_level] local log_level_string = log.levels[current_log_level]
report_info(string.format('LSP log level : %s', log_level_string)) report_info(string.format('LSP log level : %s', log_level_string))

View File

@@ -3,7 +3,7 @@ local snippet = require('vim.lsp._snippet_grammar')
local validate = vim.validate local validate = vim.validate
local api = vim.api local api = vim.api
local list_extend = vim.list_extend local list_extend = vim.list_extend
local highlight = require('vim.highlight') local highlight = vim.highlight
local uv = vim.uv local uv = vim.uv
local npcall = vim.F.npcall local npcall = vim.F.npcall
@@ -636,7 +636,7 @@ end
---@see complete-items ---@see complete-items
function M.text_document_completion_list_to_complete_items(result, prefix) function M.text_document_completion_list_to_complete_items(result, prefix)
vim.deprecate('vim.lsp.util.text_document_completion_list_to_complete_items()', nil, '0.11') vim.deprecate('vim.lsp.util.text_document_completion_list_to_complete_items()', nil, '0.11')
return require('vim.lsp._completion')._lsp_to_complete_items(result, prefix) return vim.lsp._completion._lsp_to_complete_items(result, prefix)
end end
--- Like vim.fn.bufwinid except it works across tabpages. --- Like vim.fn.bufwinid except it works across tabpages.

View File

@@ -0,0 +1,7 @@
local M = vim._defer_require('vim.provider', {
perl = ..., --- @module 'vim.provider.perl'
python = ..., --- @module 'vim.provider.python'
ruby = ..., --- @module 'vim.provider.ruby'
})
return M

View File

@@ -983,4 +983,24 @@ do
end end
end end
--- @private
--- @generic T
--- @param root string
--- @param mod T
--- @return T
function vim._defer_require(root, mod)
return setmetatable({}, {
---@param t table<string, any>
---@param k string
__index = function(t, k)
if not mod[k] then
return
end
local name = string.format('%s.%s', root, k)
t[k] = require(name)
return t[k]
end,
})
end
return vim return vim

View File

@@ -1,4 +1,4 @@
local G = require('vim.lsp._snippet_grammar') local G = vim.lsp._snippet_grammar
local snippet_group = vim.api.nvim_create_augroup('vim/snippet', {}) local snippet_group = vim.api.nvim_create_augroup('vim/snippet', {})
local snippet_ns = vim.api.nvim_create_namespace('vim/snippet') local snippet_ns = vim.api.nvim_create_namespace('vim/snippet')

View File

@@ -1,44 +1,21 @@
local api = vim.api local api = vim.api
local LanguageTree = require('vim.treesitter.languagetree')
local Range = require('vim.treesitter._range')
---@type table<integer,LanguageTree> ---@type table<integer,LanguageTree>
local parsers = setmetatable({}, { __mode = 'v' }) local parsers = setmetatable({}, { __mode = 'v' })
---@class vim.treesitter local M = vim._defer_require('vim.treesitter', {
---@field highlighter vim.treesitter.highlighter _fold = ..., --- @module 'vim.treesitter._fold'
---@field query vim.treesitter.query _query_linter = ..., --- @module 'vim.treesitter._query_linter'
---@field language vim.treesitter.language _range = ..., --- @module 'vim.treesitter._range'
local M = setmetatable({}, { dev = ..., --- @module 'vim.treesitter.dev'
__index = function(t, k) highlighter = ..., --- @module 'vim.treesitter.highlighter'
---@diagnostic disable:no-unknown language = ..., --- @module 'vim.treesitter.language'
if k == 'highlighter' then languagetree = ..., --- @module 'vim.treesitter.languagetree'
t[k] = require('vim.treesitter.highlighter') query = ..., --- @module 'vim.treesitter.query'
return t[k]
elseif k == 'language' then
t[k] = require('vim.treesitter.language')
return t[k]
elseif k == 'query' then
t[k] = require('vim.treesitter.query')
return t[k]
end
local query = require('vim.treesitter.query')
if query[k] then
vim.deprecate('vim.treesitter.' .. k .. '()', 'vim.treesitter.query.' .. k .. '()', '0.10')
t[k] = query[k]
return t[k]
end
local language = require('vim.treesitter.language')
if language[k] then
vim.deprecate('vim.treesitter.' .. k .. '()', 'vim.treesitter.language.' .. k .. '()', '0.10')
t[k] = language[k]
return t[k]
end
end,
}) })
local LanguageTree = M.languagetree
--- @nodoc --- @nodoc
M.language_version = vim._ts_get_language_version() M.language_version = vim._ts_get_language_version()
@@ -200,7 +177,7 @@ end
function M.get_range(node, source, metadata) function M.get_range(node, source, metadata)
if metadata and metadata.range then if metadata and metadata.range then
assert(source) assert(source)
return Range.add_bytes(source, metadata.range) return M._range.add_bytes(source, metadata.range)
end end
return { node:range(true) } return { node:range(true) }
end end
@@ -209,7 +186,7 @@ end
---@param range Range ---@param range Range
---@returns string ---@returns string
local function buf_range_get_text(buf, range) local function buf_range_get_text(buf, range)
local start_row, start_col, end_row, end_col = Range.unpack4(range) local start_row, start_col, end_row, end_col = M._range.unpack4(range)
if end_col == 0 then if end_col == 0 then
if start_row == end_row then if start_row == end_row then
start_col = -1 start_col = -1
@@ -237,7 +214,7 @@ function M.get_node_text(node, source, opts)
if metadata.text then if metadata.text then
return metadata.text return metadata.text
elseif type(source) == 'number' then elseif type(source) == 'number' then
local range = vim.treesitter.get_range(node, source, metadata) local range = M.get_range(node, source, metadata)
return buf_range_get_text(source, range) return buf_range_get_text(source, range)
end end
@@ -266,9 +243,9 @@ function M.node_contains(node, range)
vim.validate({ vim.validate({
-- allow a table so nodes can be mocked -- allow a table so nodes can be mocked
node = { node, { 'userdata', 'table' } }, node = { node, { 'userdata', 'table' } },
range = { range, Range.validate, 'integer list with 4 or 6 elements' }, range = { range, M._range.validate, 'integer list with 4 or 6 elements' },
}) })
return Range.contains({ node:range() }, range) return M._range.contains({ node:range() }, range)
end end
--- Returns a list of highlight captures at the given position --- Returns a list of highlight captures at the given position
@@ -502,7 +479,7 @@ end
--- argument and should return a string. --- argument and should return a string.
function M.inspect_tree(opts) function M.inspect_tree(opts)
---@diagnostic disable-next-line: invisible ---@diagnostic disable-next-line: invisible
require('vim.treesitter.dev').inspect_tree(opts) M.dev.inspect_tree(opts)
end end
--- Returns the fold level for {lnum} in the current buffer. Can be set directly to 'foldexpr': --- Returns the fold level for {lnum} in the current buffer. Can be set directly to 'foldexpr':
@@ -514,7 +491,7 @@ end
---@param lnum integer|nil Line number to calculate fold level for ---@param lnum integer|nil Line number to calculate fold level for
---@return string ---@return string
function M.foldexpr(lnum) function M.foldexpr(lnum)
return require('vim.treesitter._fold').foldexpr(lnum) return M._fold.foldexpr(lnum)
end end
return M return M

View File

@@ -1,6 +1,6 @@
local M = {} local M = {}
local ts = vim.treesitter local ts = vim.treesitter
local health = require('vim.health') local health = vim.health
--- Performs a healthcheck for treesitter integration --- Performs a healthcheck for treesitter integration
function M.check() function M.check()

View File

@@ -1,6 +1,5 @@
local api = vim.api local api = vim.api
---@class vim.treesitter.language
local M = {} local M = {}
---@type table<string,string> ---@type table<string,string>

View File

@@ -12,7 +12,6 @@ Query.__index = Query
---@field captures table ---@field captures table
---@field patterns table<string,any[][]> ---@field patterns table<string,any[][]>
---@class vim.treesitter.query
local M = {} local M = {}
---@param files string[] ---@param files string[]
@@ -799,9 +798,9 @@ end
--- - clear (boolean) if `true`, just clear current lint errors --- - clear (boolean) if `true`, just clear current lint errors
function M.lint(buf, opts) function M.lint(buf, opts)
if opts and opts.clear then if opts and opts.clear then
require('vim.treesitter._query_linter').clear(buf) vim.treesitter._query_linter.clear(buf)
else else
require('vim.treesitter._query_linter').lint(buf, opts) vim.treesitter._query_linter.lint(buf, opts)
end end
end end
@@ -814,7 +813,7 @@ end
--- ``` --- ```
--- ---
function M.omnifunc(findstart, base) function M.omnifunc(findstart, base)
return require('vim.treesitter._query_linter').omnifunc(findstart, base) return vim.treesitter._query_linter.omnifunc(findstart, base)
end end
--- Opens a live editor to query the buffer you started from. --- Opens a live editor to query the buffer you started from.
@@ -827,7 +826,7 @@ end
--- ---
--- @param lang? string language to open the query editor for. If omitted, inferred from the current buffer's filetype. --- @param lang? string language to open the query editor for. If omitted, inferred from the current buffer's filetype.
function M.edit(lang) function M.edit(lang)
require('vim.treesitter.dev').edit_query(lang) vim.treesitter.dev.edit_query(lang)
end end
return M return M