Skip to content

Commit

Permalink
Fix(black): formatting excluded files results in blank buffer (#254)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bartels authored Jan 2, 2024
1 parent 053b8b8 commit c4b2efb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lua/conform/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions tests/fuzzer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ describe("fuzzer", function()
end

local function make_edits(lines)
local was_empty = table.concat(lines):match("^%s*$")
lines = vim.deepcopy(lines)
for _ = 1, math.random(0, 3) do
do_insert(lines)
Expand All @@ -107,6 +108,12 @@ describe("fuzzer", function()
for _ = 1, math.random(0, 3) do
do_delete(lines)
end
-- avoid blank output (whitepsace only) which is ignored when applying formatting
if not was_empty then
while table.concat(lines):match("^%s*$") do
do_replace(lines)
end
end
return lines
end

Expand Down
13 changes: 13 additions & 0 deletions tests/runner_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,19 @@ print("a")
assert.are.same({ "newcontent" }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
end)

it("discards formatting changes if formatter output is empty /w non-empty input", function()
local bufnr = vim.fn.bufadd("testfile")
vim.fn.bufload(bufnr)
vim.api.nvim_set_current_buf(bufnr)
local original_lines = { "line one", "line two" }
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, original_lines)
vim.bo[bufnr].modified = false
set_formatter_output({ "" })
conform.format({ formatters = { "test" }, quiet = true })
local output_lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
assert.are.same(original_lines, output_lines)
end)

it("formats on save", function()
conform.setup({
formatters_by_ft = { ["*"] = { "test" } },
Expand Down

0 comments on commit c4b2efb

Please sign in to comment.