1
1
local M = {}
2
2
3
- local function tbl_set (tbl , keys , value )
3
+ local warnings = {}
4
+ local errors = {}
5
+ local is_json_config_loaded = false
6
+
7
+ --- @param warning string
8
+ local function add_warning (warning )
9
+ table.insert (warnings , warning )
10
+ end
11
+
12
+ --- @param err string
13
+ local function add_error (err )
14
+ table.insert (errors , err )
15
+ end
16
+
17
+ --- @param field_name string | nil
18
+ --- @param tbl unknown
19
+ --- @param keys string[]
20
+ --- @param value unknown
21
+ local function tbl_set (field_name , tbl , keys , value )
4
22
local next = table.remove (keys , 1 )
23
+ if type (tbl ) ~= ' table' then
24
+ add_warning (([[
25
+ Ignored field '%s' of invalid type '%s': %s
26
+ Please refer to the rust-analyzer documentation at
27
+ https://rust-analyzer.github.io/manual.html#%s
28
+ ]] ):format (field_name , type (value ), vim .inspect (value ), field_name ))
29
+ return
30
+ end
5
31
if # keys > 0 then
6
32
tbl [next ] = tbl [next ] or {}
7
- tbl_set (tbl [next ], keys , value )
33
+ field_name = (field_name and field_name .. ' .' or ' ' ) .. next
34
+ tbl_set (field_name , tbl [next ], keys , value )
8
35
else
9
36
tbl [next ] = value
10
37
end
15
42
--- @param json_value unknown
16
43
local function override_tbl_values (tbl , json_key , json_value )
17
44
local keys = vim .split (json_key , ' %.' )
18
- tbl_set (tbl , keys , json_value )
45
+ tbl_set (nil , tbl , keys , json_value )
19
46
end
20
47
21
48
--- @param json_content string
22
49
--- @return table
23
50
function M .silent_decode (json_content )
51
+ warnings = {}
52
+ errors = {}
24
53
local ok , json_tbl = pcall (vim .json .decode , json_content )
25
54
if not ok or type (json_tbl ) ~= ' table' then
55
+ add_error ((' Failed to decode json: %s' ):format (json_tbl or ' unknown error' ))
26
56
return {}
27
57
end
28
58
return json_tbl
32
62
--- @param json_tbl { [string] : unknown }
33
63
--- @param key_predicate ? fun ( string ): boolean
34
64
function M .override_with_json_keys (tbl , json_tbl , key_predicate )
65
+ is_json_config_loaded = true
35
66
for json_key , value in pairs (json_tbl ) do
36
67
if not key_predicate or key_predicate (json_key ) then
37
68
override_tbl_values (tbl , json_key , value )
@@ -47,4 +78,16 @@ function M.override_with_rust_analyzer_json_keys(tbl, json_tbl)
47
78
end )
48
79
end
49
80
81
+ function M .is_json_config_loaded ()
82
+ return is_json_config_loaded
83
+ end
84
+
85
+ function M .get_warnings ()
86
+ return warnings
87
+ end
88
+
89
+ function M .get_errors ()
90
+ return errors
91
+ end
92
+
50
93
return M
0 commit comments