diff --git a/lua/easytables/options.lua b/lua/easytables/options.lua index 98954d5..7996314 100644 --- a/lua/easytables/options.lua +++ b/lua/easytables/options.lua @@ -126,6 +126,35 @@ local options = { ":JumpToPreviousCell", {} ) + + vim.api.nvim_buf_set_keymap( + buf, + "n", + "", + ":SwapWithLeftColumn", + {} + ) + vim.api.nvim_buf_set_keymap( + buf, + "n", + "", + ":SwapWithRightColumn", + {} + ) + vim.api.nvim_buf_set_keymap( + buf, + "n", + "", + ":SwapWithUpperRow", + {} + ) + vim.api.nvim_buf_set_keymap( + buf, + "n", + "", + ":SwapWithLowerRow", + {} + ) end } diff --git a/lua/easytables/table.lua b/lua/easytables/table.lua index 14f35b3..66b98ff 100644 --- a/lua/easytables/table.lua +++ b/lua/easytables/table.lua @@ -235,4 +235,22 @@ function M:delete_row(row) self.header_enabled = #self.table > 1 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 diff --git a/lua/easytables/window.lua b/lua/easytables/window.lua index e2431e0..bb60c1f 100644 --- a/lua/easytables/window.lua +++ b/lua/easytables/window.lua @@ -439,6 +439,59 @@ function M:register_listeners() self.table:swap_current_with_target(target) return target 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