diff options
Diffstat (limited to 'runtime/lua/vim')
-rw-r--r-- | runtime/lua/vim/lsp.lua | 58 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/buf.lua | 12 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/callbacks.lua | 4 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/diagnostic.lua | 22 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/handlers.lua | 8 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/log.lua | 12 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/protocol.lua | 2 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/rpc.lua | 5 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/util.lua | 84 | ||||
-rw-r--r-- | runtime/lua/vim/treesitter.lua | 9 | ||||
-rw-r--r-- | runtime/lua/vim/treesitter/highlighter.lua | 9 | ||||
-rw-r--r-- | runtime/lua/vim/treesitter/language.lua | 4 | ||||
-rw-r--r-- | runtime/lua/vim/treesitter/languagetree.lua | 62 | ||||
-rw-r--r-- | runtime/lua/vim/treesitter/query.lua | 18 |
14 files changed, 131 insertions, 178 deletions
diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua index a6f118abde..841c365cbe 100644 --- a/runtime/lua/vim/lsp.lua +++ b/runtime/lua/vim/lsp.lua @@ -16,10 +16,7 @@ local validate = vim.validate local lsp = { protocol = protocol; - -- TODO(tjdevries): Add in the warning that `callbacks` is no longer supported. - -- util.warn_once("vim.lsp.callbacks is deprecated. Use vim.lsp.handlers instead.") handlers = default_handlers; - callbacks = default_handlers; buf = require'vim.lsp.buf'; diagnostic = require'vim.lsp.diagnostic'; @@ -219,8 +216,6 @@ local function validate_client_config(config) } validate { root_dir = { config.root_dir, is_dir, "directory" }; - -- TODO(remove-callbacks) - callbacks = { config.callbacks, "t", true }; handlers = { config.handlers, "t", true }; capabilities = { config.capabilities, "t", true }; cmd_cwd = { config.cmd_cwd, optional_validator(is_dir), "directory" }; @@ -235,13 +230,6 @@ local function validate_client_config(config) flags = { config.flags, "t", true }; } - -- TODO(remove-callbacks) - if config.handlers and config.callbacks then - error(debug.traceback( - "Unable to configure LSP with both 'config.handlers' and 'config.callbacks'. Use 'config.handlers' exclusively." - )) - end - local cmd, cmd_args = lsp._cmd_parts(config.cmd) local offset_encoding = valid_encodings.UTF16 if config.offset_encoding then @@ -473,8 +461,7 @@ function lsp.start_client(config) local client_id = next_client_id() - -- TODO(remove-callbacks) - local handlers = config.handlers or config.callbacks or {} + local handlers = config.handlers or {} local name = config.name or tostring(client_id) local log_prefix = string.format("LSP[%s]", name) @@ -548,19 +535,20 @@ function lsp.start_client(config) function dispatch.on_exit(code, signal) active_clients[client_id] = nil uninitialized_clients[client_id] = nil - local active_buffers = {} - for bufnr, client_ids in pairs(all_buffer_active_clients) do - if client_ids[client_id] then - table.insert(active_buffers, bufnr) - end + + lsp.diagnostic.reset(client_id, all_buffer_active_clients) + all_client_active_buffers[client_id] = nil + for _, client_ids in pairs(all_buffer_active_clients) do client_ids[client_id] = nil end - -- Buffer level cleanup - vim.schedule(function() - for _, bufnr in ipairs(active_buffers) do - lsp.diagnostic.clear(bufnr) - end - end) + + if code ~= 0 or (signal ~= 0 and signal ~= 15) then + local msg = string.format("Client %s quit with exit code %s and signal %s", client_id, code, signal) + vim.schedule(function() + vim.notify(msg, vim.log.levels.WARN) + end) + end + if config.on_exit then pcall(config.on_exit, code, signal, client_id) end @@ -579,8 +567,6 @@ function lsp.start_client(config) offset_encoding = offset_encoding; config = config; - -- TODO(remove-callbacks) - callbacks = handlers; handlers = handlers; -- for $/progress report messages = { name = name, messages = {}, progress = {}, status = {} } @@ -751,6 +737,13 @@ function lsp.start_client(config) --- --@param force (bool, optional) function client.stop(force) + + lsp.diagnostic.reset(client_id, all_buffer_active_clients) + all_client_active_buffers[client_id] = nil + for _, client_ids in pairs(all_buffer_active_clients) do + client_ids[client_id] = nil + end + local handle = rpc.handle if handle:is_closing() then return @@ -1016,23 +1009,12 @@ end function lsp.stop_client(client_id, force) local ids = type(client_id) == 'table' and client_id or {client_id} for _, id in ipairs(ids) do - local resolved_client_id if type(id) == 'table' and id.stop ~= nil then id.stop(force) - resolved_client_id = id.id elseif active_clients[id] then active_clients[id].stop(force) - resolved_client_id = id elseif uninitialized_clients[id] then uninitialized_clients[id].stop(true) - resolved_client_id = id - end - if resolved_client_id then - local client_buffers = lsp.get_buffers_by_client_id(resolved_client_id) - for idx = 1, #client_buffers do - lsp.diagnostic.clear(client_buffers[idx], resolved_client_id) - end - all_client_active_buffers[resolved_client_id] = nil end end end diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua index 00219b6d98..31116985e2 100644 --- a/runtime/lua/vim/lsp/buf.lua +++ b/runtime/lua/vim/lsp/buf.lua @@ -310,15 +310,21 @@ function M.workspace_symbol(query) request('workspace/symbol', params) end ---- Send request to server to resolve document highlights for the ---- current text document position. This request can be associated ---- to key mapping or to events such as `CursorHold`, eg: +--- Send request to the server to resolve document highlights for the current +--- text document position. This request can be triggered by a key mapping or +--- by events such as `CursorHold`, eg: --- --- <pre> --- vim.api.nvim_command [[autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()]] --- vim.api.nvim_command [[autocmd CursorHoldI <buffer> lua vim.lsp.buf.document_highlight()]] --- vim.api.nvim_command [[autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()]] --- </pre> +--- +--- Note: Usage of |vim.lsp.buf.document_highlight()| requires the following highlight groups +--- to be defined or you won't be able to see the actual highlights. +--- |LspReferenceText| +--- |LspReferenceRead| +--- |LspReferenceWrite| function M.document_highlight() local params = util.make_position_params() request('textDocument/documentHighlight', params) diff --git a/runtime/lua/vim/lsp/callbacks.lua b/runtime/lua/vim/lsp/callbacks.lua deleted file mode 100644 index 1da92b900d..0000000000 --- a/runtime/lua/vim/lsp/callbacks.lua +++ /dev/null @@ -1,4 +0,0 @@ -local util = require 'vim.lsp.util' - -util._warn_once("require('vim.lsp.callbacks') is deprecated. Use vim.lsp.handlers instead.") -return require('vim.lsp.handlers') diff --git a/runtime/lua/vim/lsp/diagnostic.lua b/runtime/lua/vim/lsp/diagnostic.lua index a625098bab..a1f24706c0 100644 --- a/runtime/lua/vim/lsp/diagnostic.lua +++ b/runtime/lua/vim/lsp/diagnostic.lua @@ -264,10 +264,14 @@ local function set_diagnostic_cache(diagnostics, bufnr, client_id) -- The diagnostic's severity. Can be omitted. If omitted it is up to the -- client to interpret diagnostics as error, warning, info or hint. -- TODO: Replace this with server-specific heuristics to infer severity. + local buf_line_count = vim.api.nvim_buf_line_count(bufnr) for _, diagnostic in ipairs(diagnostics) do if diagnostic.severity == nil then diagnostic.severity = DiagnosticSeverity.Error end + -- Account for servers that place diagnostics on terminating newline + local start = diagnostic.range.start + start.line = math.min(start.line, buf_line_count - 1) end diagnostic_cache[bufnr][client_id] = diagnostics @@ -1158,6 +1162,24 @@ local loclist_type_map = { [DiagnosticSeverity.Hint] = 'I', } + +--- Clear diagnotics and diagnostic cache +--- +--- Handles saving diagnostics from multiple clients in the same buffer. +---@param client_id number +---@param buffer_client_map table map of buffers to active clients +function M.reset(client_id, buffer_client_map) + buffer_client_map = vim.deepcopy(buffer_client_map) + vim.schedule(function() + for bufnr, client_ids in pairs(buffer_client_map) do + if client_ids[client_id] then + clear_diagnostic_cache(bufnr, client_id) + M.clear(bufnr, client_id) + end + end + end) +end + --- Sets the location list ---@param opts table|nil Configuration table. Keys: --- - {open_loclist}: (boolean, default true) diff --git a/runtime/lua/vim/lsp/handlers.lua b/runtime/lua/vim/lsp/handlers.lua index 7eac3febd9..0cf80e1443 100644 --- a/runtime/lua/vim/lsp/handlers.lua +++ b/runtime/lua/vim/lsp/handlers.lua @@ -13,7 +13,7 @@ local M = {} --- Writes to error buffer. --@param ... (table of strings) Will be concatenated before being written local function err_message(...) - api.nvim_err_writeln(table.concat(vim.tbl_flatten{...})) + vim.notify(table.concat(vim.tbl_flatten{...}), vim.log.levels.ERROR) api.nvim_command("redraw") end @@ -26,7 +26,7 @@ end -- @msg of type ProgressParams -- Basically a token of type number/string -local function progress_callback(_, _, params, client_id) +local function progress_handler(_, _, params, client_id) local client = vim.lsp.get_client_by_id(client_id) local client_name = client and client.name or string.format("id=%d", client_id) if not client then @@ -62,7 +62,7 @@ local function progress_callback(_, _, params, client_id) end --@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#progress -M['$/progress'] = progress_callback +M['$/progress'] = progress_handler --@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#window_workDoneProgress_create M['window/workDoneProgress/create'] = function(_, _, params, client_id) @@ -409,7 +409,7 @@ for k, fn in pairs(M) do }) if err then - error(tostring(err)) + return err_message(tostring(err)) end return fn(err, method, params, client_id, bufnr, config) diff --git a/runtime/lua/vim/lsp/log.lua b/runtime/lua/vim/lsp/log.lua index b6e91e37b9..331e980e67 100644 --- a/runtime/lua/vim/lsp/log.lua +++ b/runtime/lua/vim/lsp/log.lua @@ -10,13 +10,7 @@ local log = {} -- Can be used to lookup the number from the name or the name from the number. -- Levels by name: 'trace', 'debug', 'info', 'warn', 'error' -- Level numbers begin with 'trace' at 0 -log.levels = { - TRACE = 0; - DEBUG = 1; - INFO = 2; - WARN = 3; - ERROR = 4; -} +log.levels = vim.log.levels -- Default log level is warn. local current_log_level = log.levels.WARN @@ -38,6 +32,8 @@ do vim.fn.mkdir(vim.fn.stdpath('cache'), "p") local logfile = assert(io.open(logfilename, "a+")) + -- Start message for logging + logfile:write(string.format("[ START ] %s ] LSP logging initiated\n", os.date(log_date_format))) for level, levelnr in pairs(log.levels) do -- Also export the log level on the root object. log[level] = levelnr @@ -74,8 +70,6 @@ do logfile:flush() end end - -- Add some space to make it easier to distinguish different neovim runs. - logfile:write("\n") end -- This is put here on purpose after the loop above so that it doesn't diff --git a/runtime/lua/vim/lsp/protocol.lua b/runtime/lua/vim/lsp/protocol.lua index 3e111c154a..388f65c180 100644 --- a/runtime/lua/vim/lsp/protocol.lua +++ b/runtime/lua/vim/lsp/protocol.lua @@ -938,7 +938,7 @@ function protocol.resolve_capabilities(server_capabilities) text_document_did_change = textDocumentSync; text_document_will_save = false; text_document_will_save_wait_until = false; - text_document_save = false; + text_document_save = true; text_document_save_include_text = false; } elseif type(textDocumentSync) == 'table' then diff --git a/runtime/lua/vim/lsp/rpc.lua b/runtime/lua/vim/lsp/rpc.lua index 90f51dfc5a..4e2dd7c8e8 100644 --- a/runtime/lua/vim/lsp/rpc.lua +++ b/runtime/lua/vim/lsp/rpc.lua @@ -376,6 +376,9 @@ local function start(cmd, cmd_args, dispatchers, extra_spawn_params) spawn_params.env = env_merge(extra_spawn_params.env) end handle, pid = uv.spawn(cmd, spawn_params, onexit) + if handle == nil then + error(string.format("start `%s` failed: %s", cmd, pid)) + end end --@private @@ -386,7 +389,7 @@ local function start(cmd, cmd_args, dispatchers, extra_spawn_params) --@returns true if the payload could be scheduled, false if the main event-loop is in the process of closing. local function encode_and_send(payload) local _ = log.debug() and log.debug("rpc.send.payload", payload) - if handle:is_closing() then return false end + if handle == nil or handle:is_closing() then return false end -- TODO(ashkan) remove this once we have a Lua json_encode schedule(function() local encoded = assert(json_encode(payload)) diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua index e33e0109b6..6fb9f09c99 100644 --- a/runtime/lua/vim/lsp/util.lua +++ b/runtime/lua/vim/lsp/util.lua @@ -18,14 +18,6 @@ end local M = {} --- TODO(remove-callbacks) -M.diagnostics_by_buf = setmetatable({}, { - __index = function(_, bufnr) - warn_once("diagnostics_by_buf is deprecated. Use 'vim.lsp.diagnostic.get'") - return vim.lsp.diagnostic.get(bufnr) - end -}) - --@private local function split_lines(value) return split(value, '\n', true) @@ -211,7 +203,7 @@ function M.apply_text_edits(text_edits, bufnr) local lines = api.nvim_buf_get_lines(bufnr, start_line, finish_line + 1, false) local fix_eol = api.nvim_buf_get_option(bufnr, 'fixeol') local set_eol = fix_eol and api.nvim_buf_line_count(bufnr) <= finish_line + 1 - if set_eol and #lines[#lines] ~= 0 then + if set_eol and (#lines == 0 or #lines[#lines] ~= 0) then table.insert(lines, '') end @@ -726,7 +718,7 @@ function M.focusable_float(unique_name, fn) local bufnr = api.nvim_get_current_buf() do local win = find_window_by_var(unique_name, bufnr) - if win and api.nvim_win_is_valid(win) and not vim.fn.pumvisible() then + if win and api.nvim_win_is_valid(win) and vim.fn.pumvisible() == 0 then api.nvim_set_current_win(win) api.nvim_command("stopinsert") return @@ -1027,78 +1019,6 @@ function M.open_floating_preview(contents, filetype, opts) return floating_bufnr, floating_winnr end --- TODO(remove-callbacks) -do - --@deprecated - function M.get_severity_highlight_name(severity) - warn_once("vim.lsp.util.get_severity_highlight_name is deprecated.") - return vim.lsp.diagnostic._get_severity_highlight_name(severity) - end - - --@deprecated - function M.buf_clear_diagnostics(bufnr, client_id) - warn_once("buf_clear_diagnostics is deprecated. Use vim.lsp.diagnostic.clear") - return vim.lsp.diagnostic.clear(bufnr, client_id) - end - - --@deprecated - function M.get_line_diagnostics() - warn_once("get_line_diagnostics is deprecated. Use vim.lsp.diagnostic.get_line_diagnostics") - - local bufnr = api.nvim_get_current_buf() - local line_nr = api.nvim_win_get_cursor(0)[1] - 1 - - return vim.lsp.diagnostic.get_line_diagnostics(bufnr, line_nr) - end - - --@deprecated - function M.show_line_diagnostics() - warn_once("show_line_diagnostics is deprecated. Use vim.lsp.diagnostic.show_line_diagnostics") - - local bufnr = api.nvim_get_current_buf() - local line_nr = api.nvim_win_get_cursor(0)[1] - 1 - - return vim.lsp.diagnostic.show_line_diagnostics(bufnr, line_nr) - end - - --@deprecated - function M.buf_diagnostics_save_positions(bufnr, diagnostics, client_id) - warn_once("buf_diagnostics_save_positions is deprecated. Use vim.lsp.diagnostic.save") - return vim.lsp.diagnostic.save(diagnostics, bufnr, client_id) - end - - --@deprecated - function M.buf_diagnostics_get_positions(bufnr, client_id) - warn_once("buf_diagnostics_get_positions is deprecated. Use vim.lsp.diagnostic.get") - return vim.lsp.diagnostic.get(bufnr, client_id) - end - - --@deprecated - function M.buf_diagnostics_underline(bufnr, diagnostics, client_id) - warn_once("buf_diagnostics_underline is deprecated. Use 'vim.lsp.diagnostic.set_underline'") - return vim.lsp.diagnostic.set_underline(diagnostics, bufnr, client_id) - end - - --@deprecated - function M.buf_diagnostics_virtual_text(bufnr, diagnostics, client_id) - warn_once("buf_diagnostics_virtual_text is deprecated. Use 'vim.lsp.diagnostic.set_virtual_text'") - return vim.lsp.diagnostic.set_virtual_text(diagnostics, bufnr, client_id) - end - - --@deprecated - function M.buf_diagnostics_signs(bufnr, diagnostics, client_id) - warn_once("buf_diagnostics_signs is deprecated. Use 'vim.lsp.diagnostic.set_signs'") - return vim.lsp.diagnostic.set_signs(diagnostics, bufnr, client_id) - end - - --@deprecated - function M.buf_diagnostics_count(kind, client_id) - warn_once("buf_diagnostics_count is deprecated. Use 'vim.lsp.diagnostic.get_count'") - return vim.lsp.diagnostic.get_count(vim.api.nvim_get_current_buf(), client_id, kind) - end - -end - do --[[ References ]] local reference_ns = api.nvim_create_namespace("vim_lsp_references") diff --git a/runtime/lua/vim/treesitter.lua b/runtime/lua/vim/treesitter.lua index 38ac182e32..3af66b134c 100644 --- a/runtime/lua/vim/treesitter.lua +++ b/runtime/lua/vim/treesitter.lua @@ -44,13 +44,18 @@ function M._create_parser(bufnr, lang, opts) self:_on_bytes(...) end - local function detach_cb() + local function detach_cb(_, ...) if parsers[bufnr] == self then parsers[bufnr] = nil end + self:_on_detach(...) end - a.nvim_buf_attach(self:source(), false, {on_bytes=bytes_cb, on_detach=detach_cb, preview=true}) + local function reload_cb(_, ...) + self:_on_reload(...) + end + + a.nvim_buf_attach(self:source(), false, {on_bytes=bytes_cb, on_detach=detach_cb, on_reload=reload_cb, preview=true}) self:parse() diff --git a/runtime/lua/vim/treesitter/highlighter.lua b/runtime/lua/vim/treesitter/highlighter.lua index 275e960e28..6b833c6d35 100644 --- a/runtime/lua/vim/treesitter/highlighter.lua +++ b/runtime/lua/vim/treesitter/highlighter.lua @@ -113,8 +113,9 @@ function TSHighlighter.new(tree, opts) opts = opts or {} self.tree = tree tree:register_cbs { - on_changedtree = function(...) self:on_changedtree(...) end, - on_bytes = function(...) self:on_bytes(...) end + on_changedtree = function(...) self:on_changedtree(...) end; + on_bytes = function(...) self:on_bytes(...) end; + on_detach = function(...) self:on_detach(...) end; } self.bufnr = tree:source() @@ -176,6 +177,10 @@ function TSHighlighter:on_bytes(_, _, start_row, _, _, _, _, _, new_end) a.nvim__buf_redraw_range(self.bufnr, start_row, start_row + new_end + 1) end +function TSHighlighter:on_detach() + self:destroy() +end + function TSHighlighter:on_changedtree(changes) for _, ch in ipairs(changes or {}) do a.nvim__buf_redraw_range(self.bufnr, ch[1], ch[3]+1) diff --git a/runtime/lua/vim/treesitter/language.lua b/runtime/lua/vim/treesitter/language.lua index d60cd2d0c7..eed28e0e41 100644 --- a/runtime/lua/vim/treesitter/language.lua +++ b/runtime/lua/vim/treesitter/language.lua @@ -2,12 +2,12 @@ local a = vim.api local M = {} ---- Asserts that the provided language is installed, and optionnaly provide a path for the parser +--- Asserts that the provided language is installed, and optionally provide a path for the parser -- -- Parsers are searched in the `parser` runtime directory. -- -- @param lang The language the parser should parse --- @param path Optionnal path the parser is located at +-- @param path Optional path the parser is located at -- @param silent Don't throw an error if language not found function M.require_language(lang, path, silent) if vim._ts_has_language(lang) then diff --git a/runtime/lua/vim/treesitter/languagetree.lua b/runtime/lua/vim/treesitter/languagetree.lua index c864fe5878..4168c1e365 100644 --- a/runtime/lua/vim/treesitter/languagetree.lua +++ b/runtime/lua/vim/treesitter/languagetree.lua @@ -6,14 +6,14 @@ local LanguageTree = {} LanguageTree.__index = LanguageTree -- Represents a single treesitter parser for a language. --- The language can contain child languages with in it's range, +-- The language can contain child languages with in its range, -- hence the tree. -- -- @param source Can be a bufnr or a string of text to parse -- @param lang The language this tree represents -- @param opts Options table --- @param opts.queries A table of language to injection query strings --- This is useful for overridding the built in runtime file +-- @param opts.queries A table of language to injection query strings. +-- This is useful for overriding the built-in runtime file -- searching for the injection language query per language. function LanguageTree.new(source, lang, opts) language.require_language(lang) @@ -21,8 +21,8 @@ function LanguageTree.new(source, lang, opts) local custom_queries = opts.queries or {} local self = setmetatable({ - _source=source, - _lang=lang, + _source = source, + _lang = lang, _children = {}, _regions = {}, _trees = {}, @@ -35,6 +35,7 @@ function LanguageTree.new(source, lang, opts) _callbacks = { changedtree = {}, bytes = {}, + detach = {}, child_added = {}, child_removed = {} }, @@ -44,12 +45,17 @@ function LanguageTree.new(source, lang, opts) return self end --- Invalidates this parser and all it's children -function LanguageTree:invalidate() +-- Invalidates this parser and all its children +function LanguageTree:invalidate(reload) self._valid = false + -- buffer was reloaded, reparse all trees + if reload then + self._trees = {} + end + for _, child in ipairs(self._children) do - child:invalidate() + child:invalidate(reload) end end @@ -97,7 +103,7 @@ function LanguageTree:parse() self._trees = {} -- If there are no ranges, set to an empty list - -- so the included ranges in the parser ar cleared. + -- so the included ranges in the parser are cleared. if self._regions and #self._regions > 0 then for i, ranges in ipairs(self._regions) do local old_tree = old_trees[i] @@ -214,7 +220,7 @@ function LanguageTree:remove_child(lang) end end --- Destroys this language tree and all it's children. +-- Destroys this language tree and all its children. -- Any cleanup logic should be performed here. -- Note, this DOES NOT remove this tree from a parent. -- `remove_child` must be called on the parent to remove it. @@ -241,19 +247,19 @@ end -- -- Note, this call invalidates the tree and requires it to be parsed again. -- --- @param regions A list of regions this tree should manange and parse. +-- @param regions A list of regions this tree should manage and parse. function LanguageTree:set_included_regions(regions) - -- Transform the tables from 4 element long to 6 element long (with byte offset) - for _, region in ipairs(regions) do - for i, range in ipairs(region) do - if type(range) == "table" and #range == 4 then - -- TODO(vigoux): I don't think string parsers are useful for now - if type(self._source) == "number" then + -- TODO(vigoux): I don't think string parsers are useful for now + if type(self._source) == "number" then + -- Transform the tables from 4 element long to 6 element long (with byte offset) + for _, region in ipairs(regions) do + for i, range in ipairs(region) do + if type(range) == "table" and #range == 4 then local start_row, start_col, end_row, end_col = unpack(range) -- Easy case, this is a buffer parser -- TODO(vigoux): proper byte computation here, and account for EOL ? - local start_byte = a.nvim_buf_get_offset(self.bufnr, start_row) + start_col - local end_byte = a.nvim_buf_get_offset(self.bufnr, end_row) + end_col + local start_byte = a.nvim_buf_get_offset(self._source, start_row) + start_col + local end_byte = a.nvim_buf_get_offset(self._source, end_row) + end_col region[i] = { start_row, start_col, start_byte, end_row, end_col, end_byte } end @@ -397,14 +403,24 @@ function LanguageTree:_on_bytes(bufnr, changed_tick, new_row, new_col, new_byte) end +function LanguageTree:_on_reload() + self:invalidate(true) +end + + +function LanguageTree:_on_detach(...) + self:invalidate(true) + self:_do_callback('detach', ...) +end + --- Registers callbacks for the parser -- @param cbs An `nvim_buf_attach`-like table argument with the following keys : -- `on_bytes` : see `nvim_buf_attach`, but this will be called _after_ the parsers callback. --- `on_changedtree` : a callback that will be called everytime the tree has syntactical changes. +-- `on_changedtree` : a callback that will be called every time the tree has syntactical changes. -- it will only be passed one argument, that is a table of the ranges (as node ranges) that -- changed. -- `on_child_added` : emitted when a child is added to the tree. --- `on_child_removed` : emitted when a child is remvoed from the tree. +-- `on_child_removed` : emitted when a child is removed from the tree. function LanguageTree:register_cbs(cbs) if not cbs then return end @@ -416,6 +432,10 @@ function LanguageTree:register_cbs(cbs) table.insert(self._callbacks.bytes, cbs.on_bytes) end + if cbs.on_detach then + table.insert(self._callbacks.detach, cbs.on_detach) + end + if cbs.on_child_added then table.insert(self._callbacks.child_added, cbs.on_child_added) end diff --git a/runtime/lua/vim/treesitter/query.lua b/runtime/lua/vim/treesitter/query.lua index e49f54681d..8b94348994 100644 --- a/runtime/lua/vim/treesitter/query.lua +++ b/runtime/lua/vim/treesitter/query.lua @@ -111,7 +111,7 @@ end --- Gets the text corresponding to a given node -- @param node the node --- @param bufnr the buffer from which the node in extracted. +-- @param bufnr the buffer from which the node is extracted. function M.get_node_text(node, source) local start_row, start_col, start_byte = node:start() local end_row, end_col, end_byte = node:end_() @@ -215,10 +215,10 @@ predicate_handlers["vim-match?"] = predicate_handlers["match?"] local directive_handlers = { ["set!"] = function(_, _, _, pred, metadata) if #pred == 4 then - -- (set! @capture "key" "value") + -- (#set! @capture "key" "value") metadata[pred[2]][pred[3]] = pred[4] else - -- (set! "key" "value") + -- (#set! "key" "value") metadata[pred[2]] = pred[3] end end, @@ -245,7 +245,7 @@ local directive_handlers = { end } ---- Adds a new predicates to be used in queries +--- Adds a new predicate to be used in queries -- -- @param name the name of the predicate, without leading # -- @param handler the handler function to be used @@ -355,10 +355,10 @@ end --- Iterates of the captures of self on a given range. -- --- @param node The node under witch the search will occur +-- @param node The node under which the search will occur -- @param buffer The source buffer to search -- @param start The starting line of the search --- @param stop The stoping line of the search (end-exclusive) +-- @param stop The stopping line of the search (end-exclusive) -- -- @returns The matching capture id -- @returns The captured node @@ -388,12 +388,12 @@ function Query:iter_captures(node, source, start, stop) return iter end ---- Iterates of the matches of self on a given range. +--- Iterates the matches of self on a given range. -- --- @param node The node under witch the search will occur +-- @param node The node under which the search will occur -- @param buffer The source buffer to search -- @param start The starting line of the search --- @param stop The stoping line of the search (end-exclusive) +-- @param stop The stopping line of the search (end-exclusive) -- -- @returns The matching pattern id -- @returns The matching match |