|
1 | 1 | local md = require('render-markdown') |
2 | 2 | local state = require('render-markdown.state') |
3 | 3 |
|
4 | | -local function validate_treesitter() |
5 | | - local ok, ts = pcall(require, 'nvim-treesitter.parsers') |
6 | | - if not ok then |
7 | | - vim.health.error('treesitter is not installed') |
8 | | - return |
9 | | - end |
10 | | - vim.health.ok('treesitter is installed') |
| 4 | +local M = {} |
| 5 | + |
| 6 | +function M.check() |
| 7 | + local latex_advice = 'If you do not want LaTeX support avoid this warning by setting { latex_enabled = false }' |
11 | 8 |
|
12 | | - for _, name in ipairs({ 'markdown', 'markdown_inline' }) do |
13 | | - if ts.has_parser(name) then |
14 | | - vim.health.ok(name .. ' parser installed') |
| 9 | + vim.health.start('markdown.nvim [nvim-treesitter]') |
| 10 | + local ok = pcall(require, 'nvim-treesitter') |
| 11 | + if ok then |
| 12 | + vim.health.ok('installed') |
| 13 | + |
| 14 | + M.check_parser('markdown') |
| 15 | + M.check_parser('markdown_inline') |
| 16 | + if state.config.latex_enabled then |
| 17 | + M.check_parser('latex', latex_advice) |
| 18 | + end |
| 19 | + |
| 20 | + local highlight = require('nvim-treesitter.configs').get_module('highlight') |
| 21 | + if highlight ~= nil and highlight.enable then |
| 22 | + vim.health.ok('highlights enabled') |
15 | 23 | else |
16 | | - vim.health.error(name .. ' parser not installed') |
| 24 | + vim.health.error('highlights not enabled') |
17 | 25 | end |
| 26 | + else |
| 27 | + vim.health.error('not installed') |
| 28 | + end |
| 29 | + |
| 30 | + vim.health.start('markdown.nvim [executables]') |
| 31 | + if state.config.latex_enabled then |
| 32 | + M.check_executable(state.config.latex_converter, latex_advice) |
| 33 | + else |
| 34 | + vim.health.ok('none to check') |
| 35 | + end |
| 36 | + |
| 37 | + vim.health.start('markdown.nvim [configuration]') |
| 38 | + local errors = M.check_keys(md.default_config, state.config, {}) |
| 39 | + if #errors == 0 then |
| 40 | + vim.health.ok('valid') |
| 41 | + end |
| 42 | + for _, message in ipairs(errors) do |
| 43 | + vim.health.error(message) |
| 44 | + end |
| 45 | +end |
| 46 | + |
| 47 | +---@param name string |
| 48 | +---@param advice string? |
| 49 | +function M.check_parser(name, advice) |
| 50 | + local parsers = require('nvim-treesitter.parsers') |
| 51 | + if parsers.has_parser(name) then |
| 52 | + vim.health.ok(name .. ': parser installed') |
| 53 | + elseif advice == nil then |
| 54 | + vim.health.error(name .. ': parser not installed') |
| 55 | + else |
| 56 | + vim.health.warn(name .. ': parser not installed', advice) |
18 | 57 | end |
| 58 | +end |
19 | 59 |
|
20 | | - local highlight = require('nvim-treesitter.configs').get_module('highlight') |
21 | | - if highlight ~= nil and highlight.enable then |
22 | | - vim.health.ok('treesitter highlights enabled') |
| 60 | +---@param name string |
| 61 | +---@param advice string? |
| 62 | +function M.check_executable(name, advice) |
| 63 | + if vim.fn.executable(name) == 1 then |
| 64 | + vim.health.ok(name .. ': installed') |
| 65 | + elseif advice == nil then |
| 66 | + vim.health.error(name .. ': not installed') |
23 | 67 | else |
24 | | - vim.health.error('treesitter highlights not enabled') |
| 68 | + vim.health.warn(name .. ': not installed', advice) |
25 | 69 | end |
26 | 70 | end |
27 | 71 |
|
28 | 72 | ---@param t1 table<any, any> |
29 | 73 | ---@param t2 table<any, any> |
30 | 74 | ---@param path string[] |
31 | 75 | ---@return string[] |
32 | | -local function check_keys(t1, t2, path) |
| 76 | +function M.check_keys(t1, t2, path) |
33 | 77 | local errors = {} |
34 | 78 | for k, v2 in pairs(t2) do |
35 | 79 | local v1 = t1[k] |
36 | 80 | local key_path = vim.list_extend(vim.list_extend({}, path), { k }) |
37 | | - local key = vim.fn.join(key_path, ' -> ') |
| 81 | + local key = vim.fn.join(key_path, '.') |
38 | 82 | if v1 == nil then |
39 | | - table.insert(errors, string.format('Invalid parameter: %s', key)) |
| 83 | + table.insert(errors, string.format('Invalid key: %s', key)) |
40 | 84 | elseif type(v1) ~= type(v2) then |
41 | | - table.insert(errors, string.format('Invalid type: %s, expected %s but found %s', key, type(v1), type(v2))) |
| 85 | + table.insert(errors, string.format('Invalid type: %s, expected %s, found %s', key, type(v1), type(v2))) |
42 | 86 | elseif type(v1) == 'table' and type(v2) == 'table' then |
43 | 87 | -- Some tables are meant to have unrestricted keys |
44 | 88 | if not vim.list_contains({ 'win_options', 'custom_handlers' }, k) then |
45 | | - vim.list_extend(errors, check_keys(v1, v2, key_path)) |
| 89 | + vim.list_extend(errors, M.check_keys(v1, v2, key_path)) |
46 | 90 | end |
47 | 91 | end |
48 | 92 | end |
49 | 93 | return errors |
50 | 94 | end |
51 | 95 |
|
52 | | -local M = {} |
53 | | - |
54 | | -function M.check() |
55 | | - vim.health.start('Validating treesitter parsers & settings') |
56 | | - validate_treesitter() |
57 | | - vim.health.start('Validating configuration') |
58 | | - local errors = check_keys(md.default_config, state.config, {}) |
59 | | - if #errors == 0 then |
60 | | - vim.health.ok('Configuration is valid') |
61 | | - end |
62 | | - for _, message in ipairs(errors) do |
63 | | - vim.health.error(message) |
64 | | - end |
65 | | -end |
66 | | - |
67 | 96 | return M |
0 commit comments