aboutsummaryrefslogtreecommitdiff
path: root/store/config/nvim/lua/setup/plugins/lint.lua
blob: 23ede70701714ca0799b1e9978e683fc0a12e929 (plain)
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
local cspell = {
    name = "cspell",
    config_patterns = {
        ".cspell.json",
        "cspell.json",
        ".cSpell.json",
        "cSpell.json",
        "cspell.config.js",
        "cspell.config.cjs",
        "cspell.config.json",
        "cspell.config.yaml",
        "cspell.config.yml",
        "cspell.yaml",
        "cspell.yml",
    },
    fast = true,
}

local markdownlint = {
    name = "markdownlint",
    config_patterns = {
        ".markdownlint.jsonc",
        ".markdownlint.json",
        ".markdownlint.yaml",
        ".markdownlint.yml",
        ".markdownlintrc",
    },
    filetypes = { "markdown" },
    fast = true,
}

local linters = { cspell, markdownlint }

function vim.crupest.lint(linter, buf)
    if linter.filetypes then
        local filetype = vim.api.nvim_get_option_value("filetype", { buf = buf })
        if not vim.list_contains(linter.filetypes, filetype) then
            return
        end
    end

    if 0 ~= #vim.fs.find(linter.config_patterns, {
            path = vim.api.nvim_buf_get_name(buf), upward = true }) then
        require("lint").try_lint(linter.name)
    end
end

function vim.crupest.lint_all(buf, fast)
    for _, linter in ipairs(linters) do
        if not fast or linter.fast then
            vim.crupest.lint(linter, buf)
        end
    end
end

local function setup()
    vim.api.nvim_create_autocmd(
        { "BufReadPost", "InsertLeave", "TextChanged" },
        {
            callback = function(opt)
                if vim.api.nvim_get_option_value("buftype", { buf = opt.buf }) == "" then
                    vim.crupest.lint_all(opt.buf, true)
                end
            end
        })
end

return {
    setup = setup,
}