๐Ÿ“ฆ yochem / chime.nvim

๐Ÿ“„ chime.lua ยท 110 lines
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
110local M = {}

--- Trim message to fit the |v:echospace| area.
--- @param msg_chunks { [1]: string, [2]: string }[]
--- @return { [1]: string, [2]: string }[]
local function trim_msg(msg_chunks)
	local curlen = 0
	local maxlen = vim.v.echospace

	for i, chunk in ipairs(msg_chunks) do
		local text, _ = unpack(chunk)
		if curlen > maxlen then
			msg_chunks[i][1] = ''
		elseif curlen + #text > maxlen then
			msg_chunks[i][1] = text:sub(0, maxlen - curlen - 1) .. 'โ€ฆ'
		end
		curlen = curlen + #text
	end

	return msg_chunks
end

--- @param diagnostic vim.Diagnostic
--- @return string
local function default_format(diagnostic)
	local fmt = ('[%s] %s (%s)'):format(
		vim.diagnostic.severity[diagnostic.severity],
		diagnostic.message,
		diagnostic.source
	)
	return fmt
end

--- @param key string The config field.
local function config(key)
	-- straight from :h vim.diagnostic.Opts
	local default = {
		format = default_format,
		severity = nil,
		severity_sort = false,
		trim = true,
	}
	local global = vim.diagnostic.config() or {}
	local chime = global['chime']
	if type(chime) ~= 'table' then chime = {} end

	return vim.F.if_nil(chime[key], global[key], default[key])
end

--- Sorts diagnostics based on severity *in-place*.
--- @param diagnostics vim.Diagnostic[]
local function severity_sort(diagnostics)
	local sort = config('severity_sort')
	local reverse = type(sort) == 'table' and sort.reverse == true
	table.sort(diagnostics, function(a, b)
		return reverse and (a.severity > b.severity) or (a.severity < b.severity)
	end)
end

local function clear_msg_on_move()
	vim.api.nvim_create_autocmd('CursorMoved', {
		callback = function()
			vim.api.nvim_echo({ { ' ' } }, false, { kind = 'empty' })
		end,
		once = true,
		group = vim.api.nvim_create_augroup('chime.clear', {}),
	})
end

function M.show()
	local diagnostics = vim.diagnostic.get(0, {
		lnum = vim.fn.line('.') - 1,
		severity = config('severity')
	})

	if not vim.tbl_isempty(diagnostics) then
		if config('severity_sort') then
			severity_sort(diagnostics)
		end

		local formatfunc = config('format')
		local msg = formatfunc(diagnostics[1])
		-- TODO: this just assumes that it's a table otherwise
		local chunks = type(msg) == 'string' and { { vim.split(msg, '\n')[1] } } or msg
		if config('trim') then
			chunks = trim_msg(chunks)
		end

		vim.api.nvim_echo(chunks, false, { kind = 'echo' })
		clear_msg_on_move()
	end
end

function M.handler()
	local augroup = vim.api.nvim_create_augroup('chime', {})
	vim.api.nvim_create_autocmd({ 'WinResized', 'CursorMoved' }, {
		group = augroup,
		callback = function()
			-- self-destruct if explicitly set to false (and not just `nil`)
			if config('chime') == false then
				vim.api.nvim_del_augroup_by_id(augroup)
				return
			end
			M.show()
		end,
	})
end

return M