From ed508361150de26df3e6fad8376dc6238f3cc847 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 19 Dec 2024 15:05:46 -0600 Subject: [PATCH 1/7] feat(formatting): validate format exit codes --- lua/null-ls/builtins/formatting/stylua.lua | 1 + lua/null-ls/helpers/check_exit_code.lua | 17 +++++++++++++++++ lua/null-ls/helpers/init.lua | 1 + 3 files changed, 19 insertions(+) create mode 100644 lua/null-ls/helpers/check_exit_code.lua diff --git a/lua/null-ls/builtins/formatting/stylua.lua b/lua/null-ls/builtins/formatting/stylua.lua index 2065fab5..a1b5006b 100644 --- a/lua/null-ls/builtins/formatting/stylua.lua +++ b/lua/null-ls/builtins/formatting/stylua.lua @@ -12,6 +12,7 @@ return h.make_builtin({ }, method = { FORMATTING, RANGE_FORMATTING }, filetypes = { "lua", "luau" }, + check_exit_code = h.check_exit_code(0), generator_opts = { command = "stylua", args = h.range_formatting_args_factory({ diff --git a/lua/null-ls/helpers/check_exit_code.lua b/lua/null-ls/helpers/check_exit_code.lua new file mode 100644 index 00000000..65e06669 --- /dev/null +++ b/lua/null-ls/helpers/check_exit_code.lua @@ -0,0 +1,17 @@ +return function(success_codes) + return function(code, stderr) + local success + + if type(success_codes) == "number" then + success = code <= success_codes + else + success = vim.tbl_contains(success_codes, code) + end + + if not success then + vim.schedule(function() + require("null-ls.logger"):warn(("failed to format: %s"):format(stderr)) + end) + end + end +end diff --git a/lua/null-ls/helpers/init.lua b/lua/null-ls/helpers/init.lua index b956c8fc..f8183bbc 100644 --- a/lua/null-ls/helpers/init.lua +++ b/lua/null-ls/helpers/init.lua @@ -3,6 +3,7 @@ return { diagnostics = require("null-ls.helpers.diagnostics"), formatter_factory = require("null-ls.helpers.formatter_factory"), generator_factory = require("null-ls.helpers.generator_factory"), + check_exit_code = require("null-ls.helpers.check_exit_code"), make_builtin = require("null-ls.helpers.make_builtin"), range_formatting_args_factory = require("null-ls.helpers.range_formatting_args_factory"), } From f78d280cb9f85dccbed7e8918c6f66b7018fc5d7 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 19 Dec 2024 15:08:55 -0600 Subject: [PATCH 2/7] fix(helper): default valid format codes to 0 --- lua/null-ls/builtins/formatting/stylua.lua | 1 - lua/null-ls/helpers/formatter_factory.lua | 4 ++++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lua/null-ls/builtins/formatting/stylua.lua b/lua/null-ls/builtins/formatting/stylua.lua index a1b5006b..2065fab5 100644 --- a/lua/null-ls/builtins/formatting/stylua.lua +++ b/lua/null-ls/builtins/formatting/stylua.lua @@ -12,7 +12,6 @@ return h.make_builtin({ }, method = { FORMATTING, RANGE_FORMATTING }, filetypes = { "lua", "luau" }, - check_exit_code = h.check_exit_code(0), generator_opts = { command = "stylua", args = h.range_formatting_args_factory({ diff --git a/lua/null-ls/helpers/formatter_factory.lua b/lua/null-ls/helpers/formatter_factory.lua index 724d0e7e..336ede25 100644 --- a/lua/null-ls/helpers/formatter_factory.lua +++ b/lua/null-ls/helpers/formatter_factory.lua @@ -18,5 +18,9 @@ return function(opts) return done({ { text = output } }) end + if opts.check_exit_code == nil then + opts.check_exit_code = require("null-ls.helpers").check_exit_code(0) + end + return require("null-ls.helpers").generator_factory(opts) end From 09a82f920d201077ef0ffb5e4552054f5f86d9a6 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sun, 22 Dec 2024 12:40:37 -0600 Subject: [PATCH 3/7] helpers: shorten warning message --- lua/null-ls/helpers/check_exit_code.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lua/null-ls/helpers/check_exit_code.lua b/lua/null-ls/helpers/check_exit_code.lua index 65e06669..2675e40a 100644 --- a/lua/null-ls/helpers/check_exit_code.lua +++ b/lua/null-ls/helpers/check_exit_code.lua @@ -1,3 +1,5 @@ +local logger = require("null-ls.logger") + return function(success_codes) return function(code, stderr) local success @@ -9,8 +11,9 @@ return function(success_codes) end if not success then + logger:add_entry(("failed to run formatter: %s"):format(stderr), "warn") vim.schedule(function() - require("null-ls.logger"):warn(("failed to format: %s"):format(stderr)) + logger:warn("failed to run formatter; see `:NullLsLog`") end) end end From e891c36a8895fadb634a1e99563ca0283429810f Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sun, 22 Dec 2024 12:42:01 -0600 Subject: [PATCH 4/7] fix: use string.format over :format --- lua/null-ls/helpers/check_exit_code.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/null-ls/helpers/check_exit_code.lua b/lua/null-ls/helpers/check_exit_code.lua index 2675e40a..f7efd254 100644 --- a/lua/null-ls/helpers/check_exit_code.lua +++ b/lua/null-ls/helpers/check_exit_code.lua @@ -11,7 +11,7 @@ return function(success_codes) end if not success then - logger:add_entry(("failed to run formatter: %s"):format(stderr), "warn") + logger:add_entry(string.format("failed to run formatter: %s", stderr), "warn") vim.schedule(function() logger:warn("failed to run formatter; see `:NullLsLog`") end) From 85cf80f3f0e3356b89cc73daac7c197f96efb686 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sun, 22 Dec 2024 13:30:47 -0600 Subject: [PATCH 5/7] fix(helpers): correct ordering of exit code logs --- lua/null-ls/helpers/check_exit_code.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/null-ls/helpers/check_exit_code.lua b/lua/null-ls/helpers/check_exit_code.lua index f7efd254..6c696b45 100644 --- a/lua/null-ls/helpers/check_exit_code.lua +++ b/lua/null-ls/helpers/check_exit_code.lua @@ -11,10 +11,10 @@ return function(success_codes) end if not success then - logger:add_entry(string.format("failed to run formatter: %s", stderr), "warn") vim.schedule(function() logger:warn("failed to run formatter; see `:NullLsLog`") end) + logger:add_entry(string.format("failed to run formatter: %s", stderr), "warn") end end end From 4d30499d0b7a39a6a9ff6f2a74a002ecbd571368 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sun, 22 Dec 2024 13:36:36 -0600 Subject: [PATCH 6/7] fix(helpers): pass command name for more informative error msgs --- lua/null-ls/helpers/check_exit_code.lua | 6 +++--- lua/null-ls/helpers/formatter_factory.lua | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lua/null-ls/helpers/check_exit_code.lua b/lua/null-ls/helpers/check_exit_code.lua index 6c696b45..6fad07b9 100644 --- a/lua/null-ls/helpers/check_exit_code.lua +++ b/lua/null-ls/helpers/check_exit_code.lua @@ -1,6 +1,6 @@ local logger = require("null-ls.logger") -return function(success_codes) +return function(success_codes, command) return function(code, stderr) local success @@ -12,9 +12,9 @@ return function(success_codes) if not success then vim.schedule(function() - logger:warn("failed to run formatter; see `:NullLsLog`") + logger:warn(string.format("failed to run formatter %s; see `:NullLsLog`", command)) + logger:add_entry(string.format("failed to run formatter %s: %s", command, stderr), "warn") end) - logger:add_entry(string.format("failed to run formatter: %s", stderr), "warn") end end end diff --git a/lua/null-ls/helpers/formatter_factory.lua b/lua/null-ls/helpers/formatter_factory.lua index 336ede25..5b34a0c4 100644 --- a/lua/null-ls/helpers/formatter_factory.lua +++ b/lua/null-ls/helpers/formatter_factory.lua @@ -19,7 +19,8 @@ return function(opts) end if opts.check_exit_code == nil then - opts.check_exit_code = require("null-ls.helpers").check_exit_code(0) + vim.print(opts) + opts.check_exit_code = require("null-ls.helpers").check_exit_code(0, opts.command) end return require("null-ls.helpers").generator_factory(opts) From f37b4fe02600b04bc70eda1419389665175e523a Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 28 Dec 2024 07:50:24 -0600 Subject: [PATCH 7/7] fix: remove debug print --- lua/null-ls/helpers/formatter_factory.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/null-ls/helpers/formatter_factory.lua b/lua/null-ls/helpers/formatter_factory.lua index 5b34a0c4..9cec41bd 100644 --- a/lua/null-ls/helpers/formatter_factory.lua +++ b/lua/null-ls/helpers/formatter_factory.lua @@ -19,7 +19,6 @@ return function(opts) end if opts.check_exit_code == nil then - vim.print(opts) opts.check_exit_code = require("null-ls.helpers").check_exit_code(0, opts.command) end