diff --git a/lua/blink/cmp/sources/snippets/registry.lua b/lua/blink/cmp/sources/snippets/registry.lua index fadae27f..8ca11ba2 100644 --- a/lua/blink/cmp/sources/snippets/registry.lua +++ b/lua/blink/cmp/sources/snippets/registry.lua @@ -46,7 +46,7 @@ function registry:get_snippets_for_ft(filetype) for _, f in ipairs(files) do local contents = utils.read_file(f) if contents then - local snippets = vim.json.decode(contents) + local snippets = utils.parse_json_with_error_msg(f, contents) for _, key in ipairs(vim.tbl_keys(snippets)) do local snippet = utils.read_snippet(snippets[key], key) for snippet_name, snippet_def in pairs(snippet) do @@ -58,7 +58,7 @@ function registry:get_snippets_for_ft(filetype) else local contents = utils.read_file(files) if contents then - local snippets = vim.json.decode(contents) + local snippets = utils.parse_json_with_error_msg(files, contents) for _, key in ipairs(vim.tbl_keys(snippets)) do local snippet = utils.read_snippet(snippets[key], key) for key, snippet in pairs(snippet) do diff --git a/lua/blink/cmp/sources/snippets/scan.lua b/lua/blink/cmp/sources/snippets/scan.lua index 0999fe22..6971691b 100644 --- a/lua/blink/cmp/sources/snippets/scan.lua +++ b/lua/blink/cmp/sources/snippets/scan.lua @@ -72,10 +72,10 @@ function scan.load_package_json(path) local data = utils.read_file(file) if not data then return end - local pkg = vim.json.decode(data) + local pkg = require('blink.cmp.sources.snippets.utils').parse_json_with_error_msg(file, data) + ---@type {path: string, language: string|string[]}[] local snippets = vim.tbl_get(pkg, 'contributes', 'snippets') - if not snippets then return end local ret = {} ---@type table diff --git a/lua/blink/cmp/sources/snippets/utils.lua b/lua/blink/cmp/sources/snippets/utils.lua index 7c9ad335..4d6fb78f 100644 --- a/lua/blink/cmp/sources/snippets/utils.lua +++ b/lua/blink/cmp/sources/snippets/utils.lua @@ -1,5 +1,20 @@ local utils = {} +--- Parses the json file and notifies the user if there's an error +---@param path string +---@param json string +function utils.parse_json_with_error_msg(path, json) + local ok, parsed = pcall(vim.json.decode, json) + if not ok then + vim.notify( + 'Failed to parse json file "' .. path .. '" for blink.cmp snippets. Error: ' .. parsed, + vim.log.levels.ERROR + ) + return {} + end + return parsed +end + ---@type fun(path: string): string|nil function utils.read_file(path) local file = io.open(path, 'r')