From f3bdf8cf535b1df6f7c0122d963c6546199aa0a3 Mon Sep 17 00:00:00 2001 From: L3MON4D3 Date: Wed, 19 Apr 2023 23:03:17 +0200 Subject: [PATCH] improve error-reporting. Doesn't make much sense to print that something went wrong, and then only report it in the log. Just throw and catch, and then print details. --- lua/luasnip/extras/snip_location.lua | 43 ++++++++++++++++------------ 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/lua/luasnip/extras/snip_location.lua b/lua/luasnip/extras/snip_location.lua index 5151c6ffc..076e57326 100644 --- a/lua/luasnip/extras/snip_location.lua +++ b/lua/luasnip/extras/snip_location.lua @@ -15,8 +15,7 @@ local M = {} local function lua_find_function_call_node_at(bufnr, line) local has_parser, parser = pcall(vim.treesitter.get_parser, bufnr) if not has_parser then - log.warn("Cannot determine range of function-call: treesitter-parser for lua not installed.") - return nil + error("treesitter-parser for lua not installed.") end local root = parser:parse()[1]:root() @@ -26,8 +25,7 @@ local function lua_find_function_call_node_at(bufnr, line) return {node:range()} end end - log.warn("Cannot determine range of function-call: Query for `(function_call)` starting at line %s did not yield a result.") - return nil + error("Query for `(function_call)` starting at line %s did not yield a result.") end local function range_highlight(line_start, line_end, hl_duration_ms) @@ -51,17 +49,13 @@ local function range_highlight(line_start, line_end, hl_duration_ms) end local function json_find_snippet_definition(bufnr, snippet_name) - local has_parser, parser = pcall(vim.treesitter.get_parser, bufnr) - if not has_parser then - log.warn("Cannot determine definition of snippet: treesitter-parser for json(c) not installed.") - return nil + local parser_ok, parser = pcall(vim.treesitter.get_parser, bufnr) + if not parser_ok then + error("Cannot determine definition of snippet: treesitter-parser for jsonc) not installed.") end local root = parser:parse()[1]:root() - -- for a, b in root:child(0):iter_children() do - -- Insp(a:field("key")[1]:child(1):range()) - -- end - -- don't want to pass through if this file is json or jsonc, just use + -- don't want to pass through whether this file is json or jsonc, just use -- parser-language. local query = vim.treesitter.parse_query(parser:lang(), ([[ (pair @@ -70,11 +64,12 @@ local function json_find_snippet_definition(bufnr, snippet_name) ]]):format(snippet_name)) for id, node, _ in query:iter_captures(root, bufnr) do if query.captures[id] == "snippet" and node:parent():parent() == root then + -- return first match. return {node:range()} end end - return nil + error(("Treesitter did not find the definition for snippet `%s`"):format(snippet_name)) end local function win_edit(file) @@ -106,8 +101,10 @@ function M.jump_to_snippet(snip, opts) if source.line then -- in lua-file, can get region of definition via treesitter. -- 0: current buffer. - fcall_range = lua_find_function_call_node_at(0, source.line-1) - if not fcall_range then + local ok + ok, fcall_range = pcall(lua_find_function_call_node_at, 0, source.line-1) + if not ok then + print("Could not determine range for snippet-definition: " .. fcall_range) vim.api.nvim_win_set_cursor(0, {source.line, 0}) return end @@ -118,15 +115,23 @@ function M.jump_to_snippet(snip, opts) -- matches *.json or *.jsonc. elseif vim.api.nvim_buf_get_name(0):match("%.jsonc?$") then -- 0: current buffer. - fcall_range = json_find_snippet_definition(0, snip.name) - if not fcall_range then - print("Could not find range of snippet `" .. snip.name .. "`.") + local ok + ok, fcall_range = pcall(json_find_snippet_definition, 0, snip.name) + if not ok then + print("Could not determine range of snippet-definition: " .. fcall_range) return end else - print("Unknown filetype, don't know how to highlight snippet.") + print(("Don't know how to highlight snippet-definitions in current buffer `%s`.%s"):format( + vim.api.nvim_buf_get_name(0), + source.line ~= nil and " Jumping to `source.line`" or "")) + + if source.line ~= nil then + vim.api.nvim_win_set_cursor(0, {source.line, 0}) + end return end + assert(fcall_range ~= nil, "fcall_range is not nil") -- 1 is line_from, 3 is line_end. -- +1 since range is row-0-indexed.