Skip to content

Commit

Permalink
fix: diff whole document in rare cases
Browse files Browse the repository at this point in the history
  • Loading branch information
neubaner authored and tris203 committed Nov 4, 2024
1 parent 677d908 commit 7cbe1b5
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lua/rzls/utils/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,24 @@ function M.compute_minimal_edits(source_buf, target_edit)
vim.list_extend(edits, text_edits)
end

-- return vim.print(edits)
local contains_non_whitespace_edit = vim.iter(edits):any(function(edit)
return edit.newText:find("%S") ~= nil
end)

-- Diff the whole text if we encounter a non whitespace character in the edit.
-- This might happen when the formatted document deletes many lines
-- and `vim.diff` split those deletions into multiple hunks.
--
-- This is rare but it might happen.
if contains_non_whitespace_edit then
vim.print("Performing slow formatting diff")
edits = lcs.to_lsp_edits(
lcs.diff(source_text, target_text),
target_edit.range.start.line,
target_edit.range.start.character
)
end

return edits
end

Expand Down

0 comments on commit 7cbe1b5

Please sign in to comment.