From c4b2efb8aee4af0ef179a9b49ba401de3c4ef5d2 Mon Sep 17 00:00:00 2001 From: Eric Bartels Date: Tue, 2 Jan 2024 08:26:14 -0800 Subject: Fix(black): formatting excluded files results in blank buffer (#254) * Fix(black): formatting excluded files results in blank buffer (#249) * Fixed stylua formatting * Fixed more stylua formatting * Log buffer name and change from trace to warning * Test fix: set_formatter_output funtion was moved out of test_util * Avoid blank output triggering errors in fuzzer tests --- lua/conform/runner.lua | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'lua') diff --git a/lua/conform/runner.lua b/lua/conform/runner.lua index 62d158e..0f4604d 100644 --- a/lua/conform/runner.lua +++ b/lua/conform/runner.lua @@ -169,6 +169,13 @@ M.apply_format = function(bufnr, original_lines, new_lines, range, only_apply_ra table.remove(original_lines) table.remove(new_lines) + -- Abort if output is empty but input is not (i.e. has some non-whitespace characters). + -- This is to hack around oddly behaving formatters (e.g black outputs nothing for excluded files). + if new_text:match("^%s*$") and not original_text:match("^%s*$") then + log.warn("Aborting because a formatter returned empty output for buffer %s", bufname) + return + end + log.trace("Comparing lines %s and %s", original_lines, new_lines) local indices = vim.diff(original_text, new_text, { result_type = "indices", -- cgit v1.2.3-70-g09d2