summaryrefslogtreecommitdiffstatshomepage
path: root/nvim/.config/nvim/lua/plugins/nvim-lint.lua
blob: a6ceaa7d1ed9509cd16de53a39cb5699106f4ea1 (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
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
local utils = require("tobyvin.utils")

local function find_config(filename)
	local config = unpack(vim.fs.find(filename, {
		path = vim.api.nvim_buf_get_name(0),
		upward = true,
	})) or ("%s/%s/%s"):format(vim.env.XDG_CONFIG_HOME, vim.fs.basename(filename), filename)
	if vim.fn.filereadable(config) == 1 then
		return config
	end
end

local function try_lint()
	local lint = require("lint")
	local names = lint._resolve_linter_by_ft(vim.bo.filetype)

	if #names == 0 then
		vim.list_extend(names, lint.linters_by_ft["_"] or {})
	end

	vim.list_extend(names, lint.linters_by_ft["*"] or {})

	names = vim.tbl_filter(function(name)
		local linter = lint.linters[name]
		if not linter then
			vim.notify("Linter not found: " .. name, vim.log.levels.WARN, { title = "nvim-lint" })
		end
		return linter and not (type(linter) == "table" and linter.condition and not linter.condition())
	end, names)

	if #names > 0 then
		lint.try_lint(names)
	end
end

---@type LazyPluginSpec
local M = {
	"mfussenegger/nvim-lint",
	opts = {
		linters_by_ft = {
			django = { "djlint" },
			htmldjango = { "djlint" },
			["jinja.html"] = { "djlint" },
			lua = { "selene" },
			markdown = { "markdownlint" },
		},
		linters = {
			markdownlint = {
				prepend_args = {
					"--config",
					function()
						return find_config("markdownlint.yaml")
					end,
				},
				condition = function()
					return find_config("markdownlint.yaml")
				end,
			},
			selene = {
				prepend_args = {
					"--config",
					function()
						return find_config("selene.toml")
					end,
				},
				condition = function()
					return find_config("selene.toml")
				end,
			},
		},
	},
}

function M:init()
	vim.api.nvim_create_autocmd({ "BufWritePost", "BufReadPost", "InsertLeave" }, {
		group = vim.api.nvim_create_augroup("nvim-lint", { clear = true }),
		callback = utils.debounce(100, try_lint),
	})
end

function M:config(opts)
	local lint = require("lint")

	for name, linter in pairs(opts.linters) do
		if type(linter) == "table" and type(lint.linters[name]) == "table" then
			lint.linters[name] = vim.tbl_deep_extend("force", lint.linters[name], linter)
			for _, arg in pairs(lint.linters[name].prepend_args) do
				lint.linters[name].args = lint.linters[name].args or {}
				table.insert(lint.linters[name].args, arg)
			end
		else
			lint.linters[name] = linter
		end
	end
	lint.linters_by_ft = opts.linters_by_ft
end

return M