-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathconfig.lua
More file actions
272 lines (238 loc) Β· 8.48 KB
/
config.lua
File metadata and controls
272 lines (238 loc) Β· 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
---@diagnostic disable-next-line: deprecated
local unpack = unpack or table.unpack
local M = {}
local validation_rules = {
['enabled'] = { 'boolean' },
['log_level'] = { 'string', 'number' },
['editor'] = { 'table' },
['editor.client'] = { 'string' },
['editor.tooltip'] = { 'string' },
['editor.icon'] = { 'string' },
['display'] = { 'table' },
['display.theme'] = { 'string' },
['display.flavor'] = { 'string' },
['display.view'] = { 'string' },
['display.swap_fields'] = { 'boolean' },
['display.swap_icons'] = { 'boolean' },
['timestamp'] = { 'table' },
['timestamp.enabled'] = { 'boolean' },
['timestamp.reset_on_idle'] = { 'boolean' },
['timestamp.reset_on_change'] = { 'boolean' },
['timestamp.shared'] = { 'boolean' },
['idle'] = { 'table' },
['idle.enabled'] = { 'boolean' },
['idle.timeout'] = { 'number' },
['idle.show_status'] = { 'boolean' },
['idle.ignore_focus'] = { 'boolean' },
['idle.unidle_on_focus'] = { 'boolean' },
['idle.smart_idle'] = { 'boolean' },
['idle.details'] = { 'string', 'function' },
['idle.state'] = { 'string', 'function' },
['idle.tooltip'] = { 'string', 'function' },
['idle.icon'] = { 'string', 'function' },
['text'] = { 'table' },
['text.default'] = { 'string', 'boolean', 'function' },
['text.workspace'] = { 'string', 'boolean', 'function' },
['text.viewing'] = { 'string', 'boolean', 'function' },
['text.editing'] = { 'string', 'boolean', 'function' },
['text.file_browser'] = { 'string', 'boolean', 'function' },
['text.plugin_manager'] = { 'string', 'boolean', 'function' },
['text.lsp'] = { 'string', 'boolean', 'function' },
['text.docs'] = { 'string', 'boolean', 'function' },
['text.vcs'] = { 'string', 'boolean', 'function' },
['text.notes'] = { 'string', 'boolean', 'function' },
['text.debug'] = { 'string', 'boolean', 'function' },
['text.test'] = { 'string', 'boolean', 'function' },
['text.games'] = { 'string', 'boolean', 'function' },
['text.diagnostics'] = { 'string', 'boolean', 'function' },
['text.terminal'] = { 'string', 'boolean', 'function' },
['text.dashboard'] = { 'string', 'boolean', 'function' },
['buttons'] = { 'table' },
['buttons.*.label'] = { 'string', 'function' },
['buttons.*.url'] = { 'string', 'function' },
['assets'] = { 'table' },
['variables'] = { 'boolean', 'table' },
['plugins'] = { 'table' },
['hooks'] = { 'table' },
['hooks.ready'] = { 'function', 'table' },
['hooks.shutdown'] = { 'function', 'table' },
['hooks.pre_activity'] = { 'function', 'table' },
['hooks.post_activity'] = { 'function', 'table' },
['hooks.idle_enter'] = { 'function', 'table' },
['hooks.idle_leave'] = { 'function', 'table' },
['hooks.workspace_change'] = { 'function', 'table' },
['advanced'] = { 'table' },
['advanced.plugin'] = { 'table' },
['advanced.plugin.autocmds'] = { 'boolean' },
['advanced.plugin.cursor_update'] = { 'string' },
['advanced.plugin.match_in_mappings'] = { 'boolean' },
['advanced.server'] = { 'table' },
['advanced.server.update'] = { 'string' },
['advanced.server.pipe_path'] = { 'string' },
['advanced.server.executable_path'] = { 'string' },
['advanced.server.timeout'] = { 'number' },
['advanced.discord'] = { 'table' },
['advanced.discord.pipe_paths'] = { 'table' },
['advanced.discord.reconnect'] = { 'table' },
['advanced.discord.reconnect.enabled'] = { 'boolean' },
['advanced.discord.reconnect.interval'] = { 'number' },
['advanced.discord.reconnect.initial'] = { 'boolean', 'table' },
['advanced.workspace'] = { 'table' },
['advanced.workspace.root_markers'] = { 'table' },
['advanced.workspace.limit_to_cwd'] = { 'boolean' },
}
local array_paths = {
['buttons'] = true,
['plugins'] = true,
['advanced.discord.pipe_paths'] = true,
['advanced.workspace.root_markers'] = true,
}
local skip_subtrees = {
['plugins'] = true,
}
local dict_paths = {
['assets'] = { 'string', 'table' },
}
local function validate_type(value, allowed_types)
for _, t in ipairs(allowed_types) do
local ty = type(value)
if t == 'table' and ty == 'table' then
return true
elseif t == 'string' and ty == 'string' then
return true
elseif t == 'number' and ty == 'number' then
return true
elseif t == 'boolean' and ty == 'boolean' then
return true
elseif t == 'function' and ty == 'function' then
return true
end
end
return false
end
local function is_valid_path(path)
if validation_rules[path] then return true end
local parts = vim.split(path, '.', { plain = true })
if #parts >= 2 then
local parent = parts[1]
if dict_paths[parent] then return true end
local wildcard_path = table.concat(
vim.tbl_flatten {
{ parts[1] },
{ '*' },
{ unpack(parts, 3) },
},
'.'
)
return validation_rules[wildcard_path] ~= nil
end
return false
end
local function get_nested_value(config, path)
local parts = vim.split(path, '.', { plain = true })
local current = config
for _, part in ipairs(parts) do
if type(current) ~= 'table' then return nil end
current = current[part]
end
return current
end
M.validate = function(user_config)
if not user_config then return { is_valid = true } end
local errors = {}
local warnings = {}
local function check_unknown_entries(config, prefix)
prefix = prefix or ''
for k, v in pairs(config) do
local full_path = prefix == '' and k or (prefix .. '.' .. k)
local base_path = vim.split(full_path, '.', { plain = true })[1]
local is_plugin_config = base_path == 'plugins' and type(k) == 'number'
if
not (
(array_paths[prefix] and type(k) == 'number')
or (array_paths[base_path] and type(k) == 'number')
or (dict_paths[base_path] and type(k) == 'string')
or is_plugin_config
) and not is_valid_path(full_path)
then
table.insert(warnings, string.format('Unknown configuration entry: `%s`', full_path))
end
if dict_paths[base_path] and type(k) == 'string' then
if not validate_type(v, dict_paths[base_path]) then
table.insert(errors, {
msg = string.format('Invalid type \'%s\' for `%s`', type(v), full_path),
hint = string.format(
'Allowed types: \'%s\'',
table.concat(dict_paths[base_path], '\', \'')
),
})
end
end
if type(v) == 'table' and not (skip_subtrees[base_path] and type(k) == 'number') then
check_unknown_entries(v, full_path)
end
end
end
check_unknown_entries(user_config)
for path, allowed_types in pairs(validation_rules) do
local value = get_nested_value(user_config, path)
if value ~= nil and not validate_type(value, allowed_types) then
table.insert(errors, {
msg = string.format('Invalid type \'%s\' for `%s`', type(value), path),
hint = string.format('Allowed types: \'%s\'', table.concat(allowed_types, '\', \'')),
})
end
end
return {
is_valid = #errors == 0 and #warnings == 0,
errors = errors,
warnings = warnings,
}
end
M.check = function()
local health = vim.health or require 'cord.api.config'
local start = health.start or health.report_start
local ok = health.ok or health.report_ok
local info = health.info or health.report_info
local warn = health.warn or health.report_warn
local err = health.error or health.report_error
start 'cord.nvim'
local os_info = vim.loop.os_uname()
local wsl_info = os.getenv 'WSL_DISTRO_NAME'
info(
'System information:\n'
.. ' Sysname: `'
.. os_info.sysname
.. '`\n'
.. ' Architecture: `'
.. os_info.machine
.. '`\n'
.. ' Release: `'
.. os_info.release
.. '`\n'
.. ' Version: `'
.. os_info.version
.. '`'
.. (wsl_info and ('\n Running inside WSL (`' .. wsl_info .. '`)') or '')
)
info('Neovim version: `' .. tostring(vim.version()) .. '`')
info('Lua version: `' .. tostring(_VERSION) .. (jit and ' (with LuaJIT)`' or '`'))
info('Cord connection status: `' .. tostring(require('cord.server').status) .. '`\n')
if vim.fn.executable 'curl' == 1 then
ok '`curl` is installed'
else
warn '`curl` is not installed or not in PATH'
end
local results = M.validate(require('cord').user_config)
if results.is_valid then
ok 'No configuration issues found'
else
for _, error in ipairs(results.errors) do
err(error.msg, error.hint)
end
for _, warning in ipairs(results.warnings) do
warn(warning)
end
end
end
return M