Skip to content

Commit

Permalink
improve error-reporting.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
L3MON4D3 committed Apr 19, 2023
1 parent 9b725e8 commit f3bdf8c
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions lua/luasnip/extras/snip_location.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit f3bdf8c

Please sign in to comment.