feat: Add swap whole cols and rows

This commit is contained in:
Myzel394 2023-10-07 14:58:47 +02:00
parent e77860a339
commit 6dc7948a25
3 changed files with 100 additions and 0 deletions

View File

@ -126,6 +126,35 @@ local options = {
":JumpToPreviousCell<CR>", ":JumpToPreviousCell<CR>",
{} {}
) )
vim.api.nvim_buf_set_keymap(
buf,
"n",
"<C-Left>",
":SwapWithLeftColumn<CR>",
{}
)
vim.api.nvim_buf_set_keymap(
buf,
"n",
"<C-Right>",
":SwapWithRightColumn<CR>",
{}
)
vim.api.nvim_buf_set_keymap(
buf,
"n",
"<C-Up>",
":SwapWithUpperRow<CR>",
{}
)
vim.api.nvim_buf_set_keymap(
buf,
"n",
"<C-Down>",
":SwapWithLowerRow<CR>",
{}
)
end end
} }

View File

@ -235,4 +235,22 @@ function M:delete_row(row)
self.header_enabled = #self.table > 1 self.header_enabled = #self.table > 1
end end
function M:swap_cols(first, second)
for _, row in ipairs(self.table) do
local first_value = row[first]
local second_value = row[second]
row[first] = second_value
row[second] = first_value
end
end
function M:swap_rows(first, second)
local first_row = self.table[first]
local second_row = self.table[second]
self.table[first] = second_row
self.table[second] = first_row
end
return M return M

View File

@ -439,6 +439,59 @@ function M:register_listeners()
self.table:swap_current_with_target(target) self.table:swap_current_with_target(target)
return target return target
end, end,
SwapWithLeftColumn = function()
local cell = self.table:get_highlighted_cell()
self.table:swap_cols(
cell.col,
cell.col == 1 and self.table:cols_amount() or cell.col - 1
)
return {
row = cell.row,
col = cell.col == 1 and self.table:cols_amount() or cell.col - 1,
}
end,
SwapWithRightColumn = function()
local cell = self.table:get_highlighted_cell()
self.table:swap_cols(
cell.col,
cell.col == self.table:cols_amount() and 1 or cell.col + 1
)
return {
row = cell.row,
col = cell.col == self.table:cols_amount() and 1 or cell.col + 1,
}
end,
SwapWithUpperRow = function()
local cell = self.table:get_highlighted_cell()
self.table:swap_rows(
cell.row,
cell.row == 1 and self.table:rows_amount() or cell.row - 1
)
return {
row = cell.row == 1 and self.table:rows_amount() or cell.row - 1,
col = cell.col,
}
end,
SwapWithLowerRow = function()
local cell = self.table:get_highlighted_cell()
self.table:swap_rows(
cell.row,
cell.row == self.table:rows_amount() and 1 or cell.row + 1
)
return {
row = cell.row == self.table:rows_amount() and 1 or cell.row + 1,
col = cell.col,
}
end,
} }
for cmd, func in pairs(cmds) do for cmd, func in pairs(cmds) do