Skip to content

Commit

Permalink
fix: handle one failure mode with range formatting (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Oct 9, 2023
1 parent a94f686 commit b5a2da9
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lua/conform/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ M.build_cmd = function(ctx, config)
end
end

---@param range? conform.Range
---@param range conform.Range
---@param start_a integer
---@param end_a integer
---@return boolean
local function indices_in_range(range, start_a, end_a)
return not range or (start_a <= range["end"][1] and range["start"][1] <= end_a)
return start_a <= range["end"][1] and range["start"][1] <= end_a
end

---@param a? string
Expand Down Expand Up @@ -189,7 +190,10 @@ M.apply_format = function(bufnr, original_lines, new_lines, range, only_apply_ra
if is_replace then
orig_line_end = orig_line_end - 1
end
if not only_apply_range or indices_in_range(range, orig_line_start, orig_line_end) then
local should_apply_diff = not only_apply_range
or not range
or indices_in_range(range, orig_line_start, orig_line_end)
if should_apply_diff then
local text_edit = create_text_edit(
original_lines,
replacement,
Expand All @@ -199,6 +203,14 @@ M.apply_format = function(bufnr, original_lines, new_lines, range, only_apply_ra
orig_line_end
)
table.insert(text_edits, text_edit)

-- If we're using the aftermarket range formatting, diffs often have paired delete/insert
-- diffs. We should make sure that if one of them overlaps our selected range, extend the
-- range so that we pick up the other diff as well.
if range and only_apply_range then
range = vim.deepcopy(range)
range["end"][1] = math.max(range["end"][1], orig_line_end + 1)
end
end
end

Expand Down

0 comments on commit b5a2da9

Please sign in to comment.