Skip to content

Commit cc810f7

Browse files
helpers/factory: default formatter valid exit code to 0 (#221)
Co-authored-by: Zephyr Lykos <[email protected]>
1 parent 7fd9756 commit cc810f7

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
return function(codes, silent, command)
2+
local logger = require("null-ls.logger")
3+
4+
local check = codes
5+
if type(codes) == "table" then
6+
check = function(code, _)
7+
return vim.tbl_contains(codes, code)
8+
end
9+
elseif type(codes) == "number" then
10+
check = function(code, _)
11+
return code <= codes
12+
end
13+
end
14+
15+
if not silent and type(check) == "function" then
16+
local _check = check
17+
check = function(code, stderr)
18+
local result = _check(code)
19+
20+
if not result then
21+
logger:warn(string.format("failed to run %s; see `:NullLsLog`", command))
22+
logger:add_entry(string.format("failed to run %s: %s", command, stderr), "warn")
23+
end
24+
end
25+
end
26+
return check
27+
end

lua/null-ls/helpers/formatter_factory.lua

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
return function(opts)
2+
local h = require("null-ls.helpers")
3+
24
-- ignore errors unless otherwise specified
35
if opts.ignore_stderr == nil then
46
opts.ignore_stderr = true
57
end
6-
78
-- for formatters, to_temp_file only works if from_temp_file is also set
89
if opts.to_temp_file then
910
opts.from_temp_file = true
@@ -18,5 +19,10 @@ return function(opts)
1819
return done({ { text = output } })
1920
end
2021

21-
return require("null-ls.helpers").generator_factory(opts)
22+
if opts.check_exit_code == nil then
23+
opts.check_exit_code = 0
24+
end
25+
opts.check_exit_code = h.check_exit_code(opts.check_exit_code, false, opts.command)
26+
27+
return h.generator_factory(opts)
2228
end

lua/null-ls/helpers/generator_factory.lua

+6-2
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,15 @@ return function(opts)
132132
opts.temp_dir,
133133
opts.prepend_extra_args
134134

135-
if type(check_exit_code) == "table" then
136-
local codes = check_exit_code
135+
local codes = check_exit_code
136+
if type(codes) == "table" then
137137
check_exit_code = function(code)
138138
return vim.tbl_contains(codes, code)
139139
end
140+
elseif type(codes) == "number" then
141+
check_exit_code = function(code)
142+
return code <= codes
143+
end
140144
end
141145

142146
local is_nil_table_or_func = function(v)

lua/null-ls/helpers/init.lua

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
return {
22
cache = require("null-ls.helpers.cache"),
3+
check_exit_code = require("null-ls.helpers.check_exit_code"),
34
diagnostics = require("null-ls.helpers.diagnostics"),
45
formatter_factory = require("null-ls.helpers.formatter_factory"),
56
generator_factory = require("null-ls.helpers.generator_factory"),

0 commit comments

Comments
 (0)