Skip to content

Commit c5146a5

Browse files
committed
feat: notify user on json parsing error for snippets
closes #132
1 parent ffc4282 commit c5146a5

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

lua/blink/cmp/sources/snippets/registry.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function registry:get_snippets_for_ft(filetype)
4646
for _, f in ipairs(files) do
4747
local contents = utils.read_file(f)
4848
if contents then
49-
local snippets = vim.json.decode(contents)
49+
local snippets = utils.parse_json_with_error_msg(f, contents)
5050
for _, key in ipairs(vim.tbl_keys(snippets)) do
5151
local snippet = utils.read_snippet(snippets[key], key)
5252
for snippet_name, snippet_def in pairs(snippet) do
@@ -58,7 +58,7 @@ function registry:get_snippets_for_ft(filetype)
5858
else
5959
local contents = utils.read_file(files)
6060
if contents then
61-
local snippets = vim.json.decode(contents)
61+
local snippets = utils.parse_json_with_error_msg(files, contents)
6262
for _, key in ipairs(vim.tbl_keys(snippets)) do
6363
local snippet = utils.read_snippet(snippets[key], key)
6464
for key, snippet in pairs(snippet) do

lua/blink/cmp/sources/snippets/scan.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ function scan.load_package_json(path)
7272
local data = utils.read_file(file)
7373
if not data then return end
7474

75-
local pkg = vim.json.decode(data)
75+
local pkg = require('blink.cmp.sources.snippets.utils').parse_json_with_error_msg(file, data)
76+
7677
---@type {path: string, language: string|string[]}[]
7778
local snippets = vim.tbl_get(pkg, 'contributes', 'snippets')
78-
7979
if not snippets then return end
8080

8181
local ret = {} ---@type table<string, string[]>

lua/blink/cmp/sources/snippets/utils.lua

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
local utils = {}
22

3+
--- Parses the json file and notifies the user if there's an error
4+
---@param path string
5+
---@param json string
6+
function utils.parse_json_with_error_msg(path, json)
7+
local ok, parsed = pcall(vim.json.decode, json)
8+
if not ok then
9+
vim.notify(
10+
'Failed to parse json file "' .. path .. '" for blink.cmp snippets. Error: ' .. parsed,
11+
vim.log.levels.ERROR
12+
)
13+
return {}
14+
end
15+
return parsed
16+
end
17+
318
---@type fun(path: string): string|nil
419
function utils.read_file(path)
520
local file = io.open(path, 'r')

0 commit comments

Comments
 (0)