Skip to content

Commit

Permalink
fix: range formatting (#149)
Browse files Browse the repository at this point in the history
* chore(doc): auto generate docs

* fix: range formatting

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
xiaoshihou514 and github-actions[bot] authored Jun 5, 2024
1 parent 0dca2a9 commit 69c25c4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 23 deletions.
2 changes: 1 addition & 1 deletion doc/guard.nvim.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*guard.nvim.txt* For NVIM v0.8.0 Last change: 2024 May 17
*guard.nvim.txt* For NVIM v0.8.0 Last change: 2024 May 29

==============================================================================
Table of Contents *guard.nvim-table-of-contents*
Expand Down
26 changes: 4 additions & 22 deletions lua/guard/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ local function restore_views(views)
end
end

local function update_buffer(bufnr, prev_lines, new_lines)
local function update_buffer(bufnr, prev_lines, new_lines, srow, erow)
if not new_lines or #new_lines == 0 then
return
end
Expand All @@ -47,31 +47,13 @@ local function update_buffer(bufnr, prev_lines, new_lines)
new_lines[#new_lines] = nil
end

if #new_lines ~= #prev_lines then
api.nvim_buf_set_lines(bufnr, 0, -1, false, new_lines)
if new_lines ~= prev_lines then
api.nvim_buf_set_lines(bufnr, srow, erow, false, new_lines)
if require("guard").config.opts.save_on_fmt then
api.nvim_command('silent! noautocmd write!')
end
restore_views(views)
return
end

--TODO(glpnir): before use diff update minimal area has bug line flush
--not correct so retrun to update whole buffer.
local diffs = vim.diff(table.concat(new_lines, '\n'), prev_lines, {
algorithm = 'minimal',
ctxlen = 0,
result_type = 'indices',
})
if not diffs or #diffs == 0 then
return
end

api.nvim_buf_set_lines(bufnr, 0, -1, false, new_lines)
if require("guard").config.opts.save_on_fmt then
api.nvim_command('silent! noautocmd write!')
end
restore_views(views)
end

local function find(startpath, patterns, root_dir)
Expand Down Expand Up @@ -193,7 +175,7 @@ local function do_fmt(buf)
})
return
end
update_buffer(buf, prev_lines, new_lines)
update_buffer(buf, prev_lines, new_lines, srow, erow)
if reload and api.nvim_get_current_buf() == buf then
vim.cmd.edit()
end
Expand Down

0 comments on commit 69c25c4

Please sign in to comment.