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
|
local M = {}
local formatters_by_ft = {}
local null_ls = require "null-ls"
local services = require "lsp.null-ls.services"
local Log = require("core.log")
local function list_names(formatters, option)
option = option or {}
local filter = option.filter or "supported"
return vim.tbl_keys(formatters[filter])
end
function M.list_supported_names(filetype)
if not formatters_by_ft[filetype] then
return {}
end
return list_names(formatters_by_ft[filetype], { filter = "supported" })
end
function M.list_unsupported_names(filetype)
if not formatters_by_ft[filetype] then
return {}
end
return list_names(formatters_by_ft[filetype], { filter = "unsupported" })
end
function M.list_available(filetype)
local formatters = {}
for _, provider in pairs(null_ls.builtins.formatting) do
-- TODO: Add support for wildcard filetypes
if vim.tbl_contains(provider.filetypes or {}, filetype) then
table.insert(formatters, provider.name)
end
end
return formatters
end
function M.list_configured(formatter_configs)
local formatters, errors = {}, {}
for _, fmt_config in ipairs(formatter_configs) do
local formatter = null_ls.builtins.formatting[fmt_config.exe]
if not formatter then
Log:error("Not a valid formatter:" .. fmt_config.exe)
errors[fmt_config.exe] = {} -- Add data here when necessary
else
local formatter_cmd = services.find_command(formatter._opts.command)
if not formatter_cmd then
Log:warn("Not found:" .. formatter._opts.command)
errors[fmt_config.exe] = {} -- Add data here when necessary
else
Log:debug("Using formatter:" .. formatter_cmd)
formatters[fmt_config.exe] = formatter.with {
command = formatter_cmd,
extra_args = fmt_config.args,
}
end
end
end
return { supported = formatters, unsupported = errors }
end
function M.setup(filetype, option)
if not options.lang[filetype] or (formatters_by_ft[filetype] and not option.force_reload) then
return
end
formatters_by_ft[filetype] = M.list_configured(options.lang[filetype].formatters)
null_ls.register { sources = formatters_by_ft[filetype].supported }
end
return M
|