-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Swap both elements of a pair together
Currently when performing a swap, each element in a pair is swapped with it's corresponding paired element. This works for simple cases but breaks when the number of lines are different between the two pairs as the ranges the nodes represent have now changed from previous edits. This fixes the issue by finding the full range represented by both elements in a pair and swapping this entire range in one go.
- Loading branch information
1 parent
f842d84
commit 885657d
Showing
5 changed files
with
126 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
local M = {} | ||
|
||
-- Swap the text in the given buffer represented by left_range and right_range | ||
function M.swap_ranges(buf, left_range, right_range, cursor_pos) | ||
-- stylua: ignore | ||
local left_text = vim.api.nvim_buf_get_text( | ||
buf, | ||
left_range[1], left_range[2], | ||
left_range[3], left_range[4], | ||
{} | ||
) | ||
-- stylua: ignore | ||
local right_text = vim.api.nvim_buf_get_text(buf, | ||
right_range[1], right_range[2], | ||
right_range[3], right_range[4], | ||
{} | ||
) | ||
|
||
-- stylua: ignore | ||
vim.api.nvim_buf_set_text(buf, | ||
right_range[1], right_range[2], | ||
right_range[3], right_range[4], | ||
left_text | ||
) | ||
if cursor_pos == 1 then | ||
vim.api.nvim_win_set_cursor(0, { right_range[1] + 1, right_range[2] }) | ||
end | ||
|
||
-- stylua: ignore | ||
vim.api.nvim_buf_set_text(buf, | ||
left_range[1], left_range[2], | ||
left_range[3], left_range[4], | ||
right_text | ||
) | ||
if cursor_pos == 0 then | ||
vim.api.nvim_win_set_cursor(0, { left_range[1] + 1, left_range[2] }) | ||
end | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters