Skip to content

Commit 5af19d2

Browse files
bartelsF1lipB
authored andcommitted
Fix(black): formatting excluded files results in blank buffer (nvim-lua#254)
* Fix(black): formatting excluded files results in blank buffer (nvim-lua#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
1 parent 2900aa5 commit 5af19d2

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

lua/conform/runner.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ M.apply_format = function(bufnr, original_lines, new_lines, range, only_apply_ra
169169
table.remove(original_lines)
170170
table.remove(new_lines)
171171

172+
-- Abort if output is empty but input is not (i.e. has some non-whitespace characters).
173+
-- This is to hack around oddly behaving formatters (e.g black outputs nothing for excluded files).
174+
if new_text:match("^%s*$") and not original_text:match("^%s*$") then
175+
log.warn("Aborting because a formatter returned empty output for buffer %s", bufname)
176+
return
177+
end
178+
172179
log.trace("Comparing lines %s and %s", original_lines, new_lines)
173180
local indices = vim.diff(original_text, new_text, {
174181
result_type = "indices",

tests/fuzzer_spec.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ describe("fuzzer", function()
9797
end
9898

9999
local function make_edits(lines)
100+
local was_empty = table.concat(lines):match("^%s*$")
100101
lines = vim.deepcopy(lines)
101102
for _ = 1, math.random(0, 3) do
102103
do_insert(lines)
@@ -107,6 +108,12 @@ describe("fuzzer", function()
107108
for _ = 1, math.random(0, 3) do
108109
do_delete(lines)
109110
end
111+
-- avoid blank output (whitepsace only) which is ignored when applying formatting
112+
if not was_empty then
113+
while table.concat(lines):match("^%s*$") do
114+
do_replace(lines)
115+
end
116+
end
110117
return lines
111118
end
112119

tests/runner_spec.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,19 @@ print("a")
307307
assert.are.same({ "newcontent" }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
308308
end)
309309

310+
it("discards formatting changes if formatter output is empty /w non-empty input", function()
311+
local bufnr = vim.fn.bufadd("testfile")
312+
vim.fn.bufload(bufnr)
313+
vim.api.nvim_set_current_buf(bufnr)
314+
local original_lines = { "line one", "line two" }
315+
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, original_lines)
316+
vim.bo[bufnr].modified = false
317+
set_formatter_output({ "" })
318+
conform.format({ formatters = { "test" }, quiet = true })
319+
local output_lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
320+
assert.are.same(original_lines, output_lines)
321+
end)
322+
310323
it("formats on save", function()
311324
conform.setup({
312325
formatters_by_ft = { ["*"] = { "test" } },

0 commit comments

Comments
 (0)