mirror of
https://github.com/Myzel394/easytables.nvim.git
synced 2025-06-18 14:55:26 +02:00
feat: Add swap functionality
This commit is contained in:
parent
c978a23303
commit
fa24b7f69f
@ -56,9 +56,8 @@ end
|
||||
|
||||
local function a()
|
||||
local own_table = table:create(6, 3)
|
||||
own_table:highlight_cell(1, 1)
|
||||
|
||||
local window = window:create(table)
|
||||
local window = window:create(own_table)
|
||||
|
||||
window:show()
|
||||
window:draw_table()
|
||||
|
@ -10,6 +10,10 @@ function M:create(cols, rows)
|
||||
end
|
||||
|
||||
self.table = table
|
||||
self.highlighted_cell = {
|
||||
col = 1,
|
||||
row = 1,
|
||||
}
|
||||
|
||||
return self
|
||||
end
|
||||
@ -72,8 +76,8 @@ function M:cols_amount()
|
||||
return #self.table[1]
|
||||
end
|
||||
|
||||
function M:highlight_cell(col, row)
|
||||
self.highlighted_cell = { row = row, col = col }
|
||||
function M:set_highlighted_cell(cell)
|
||||
self.highlighted_cell = cell
|
||||
end
|
||||
|
||||
function M:get_highlighted_cell()
|
||||
@ -82,10 +86,6 @@ end
|
||||
|
||||
-- Jumps to next cell in row. If there is no next cell, it jumps to the first cell in the next row.
|
||||
function M:move_highlight_to_next_cell()
|
||||
if self.highlighted_cell == nil then
|
||||
return
|
||||
end
|
||||
|
||||
if self.highlighted_cell.col == self:cols_amount() then
|
||||
if self.highlighted_cell.row == self:rows_amount() then
|
||||
-- Reset highlight to the first cell
|
||||
@ -109,10 +109,6 @@ end
|
||||
|
||||
-- Jumps to previous cell in row. If there is no previous cell, it jumps to the last cell in the previous row.
|
||||
function M:move_highlight_to_previous_cell()
|
||||
if self.highlighted_cell == nil then
|
||||
return
|
||||
end
|
||||
|
||||
if self.highlighted_cell.col == 1 then
|
||||
if self.highlighted_cell.row == 1 then
|
||||
-- Reset highlight to the last cell
|
||||
@ -136,10 +132,6 @@ end
|
||||
|
||||
-- Moves highlight to the right, jumps back to the first cell in the same row if it is already at the rightmost cell.
|
||||
function M:move_highlight_right()
|
||||
if self.highlighted_cell == nil then
|
||||
return
|
||||
end
|
||||
|
||||
if self.highlighted_cell.col == self:cols_amount() then
|
||||
self.highlighted_cell.col = 1
|
||||
else
|
||||
@ -149,10 +141,6 @@ end
|
||||
|
||||
-- Moves highlight to the left, jumps back to the last cell in the same row if it is already at the leftmost cell.
|
||||
function M:move_highlight_left()
|
||||
if self.highlighted_cell == nil then
|
||||
return
|
||||
end
|
||||
|
||||
if self.highlighted_cell.col == 1 then
|
||||
self.highlighted_cell.col = self:cols_amount()
|
||||
else
|
||||
@ -162,10 +150,6 @@ end
|
||||
|
||||
-- Moves highlight to the top, jumps back to the last row if it is already at the topmost row.
|
||||
function M:move_highlight_up()
|
||||
if self.highlighted_cell == nil then
|
||||
return
|
||||
end
|
||||
|
||||
if self.highlighted_cell.row == 1 then
|
||||
self.highlighted_cell.row = self:rows_amount()
|
||||
else
|
||||
@ -175,10 +159,6 @@ end
|
||||
|
||||
-- Moves highlight to the bottom, jumps back to the first row if it is already at the bottommost row.
|
||||
function M:move_highlight_down()
|
||||
if self.highlighted_cell == nil then
|
||||
return
|
||||
end
|
||||
|
||||
if self.highlighted_cell.row == self:rows_amount() then
|
||||
self.highlighted_cell.row = 1
|
||||
else
|
||||
@ -258,4 +238,12 @@ function M:get_horizontal_border_width(
|
||||
return start_position, end_position
|
||||
end
|
||||
|
||||
function M:swap_contents(first, second)
|
||||
local first_value = self:value_at(first.row, first.col)
|
||||
local second_value = self:value_at(second.row, second.col)
|
||||
|
||||
self:insert(first.col, first.row, second_value)
|
||||
self:insert(second.col, second.row, first_value)
|
||||
end
|
||||
|
||||
return M
|
||||
|
@ -221,6 +221,78 @@ function M:register_listeners()
|
||||
end,
|
||||
{}
|
||||
)
|
||||
|
||||
vim.api.nvim_buf_create_user_command(
|
||||
self.prompt_buffer,
|
||||
"SwapWithRightCell",
|
||||
function()
|
||||
local current_cell = self.table:get_highlighted_cell()
|
||||
|
||||
local right_cell = {
|
||||
row = current_cell.row,
|
||||
col = current_cell.col == self.table:cols_amount() and 1 or current_cell.col + 1,
|
||||
}
|
||||
self.table:swap_contents(current_cell, right_cell)
|
||||
self.table:set_highlighted_cell(right_cell)
|
||||
self:draw_table()
|
||||
self:_reset_prompt()
|
||||
end,
|
||||
{}
|
||||
)
|
||||
|
||||
vim.api.nvim_buf_create_user_command(
|
||||
self.prompt_buffer,
|
||||
"SwapWithLeftCell",
|
||||
function()
|
||||
local current_cell = self.table:get_highlighted_cell()
|
||||
|
||||
local left_cell = {
|
||||
row = current_cell.row,
|
||||
col = current_cell.col == 1 and self.table:cols_amount() or current_cell.col - 1,
|
||||
}
|
||||
self.table:swap_contents(current_cell, left_cell)
|
||||
self.table:set_highlighted_cell(left_cell)
|
||||
self:draw_table()
|
||||
self:_reset_prompt()
|
||||
end,
|
||||
{}
|
||||
)
|
||||
|
||||
vim.api.nvim_buf_create_user_command(
|
||||
self.prompt_buffer,
|
||||
"SwapWithUpperCell",
|
||||
function()
|
||||
local current_cell = self.table:get_highlighted_cell()
|
||||
|
||||
local upper_cell = {
|
||||
row = current_cell.row == 1 and self.table:rows_amount() or current_cell.row - 1,
|
||||
col = current_cell.col,
|
||||
}
|
||||
self.table:swap_contents(current_cell, upper_cell)
|
||||
self.table:set_highlighted_cell(upper_cell)
|
||||
self:draw_table()
|
||||
self:_reset_prompt()
|
||||
end,
|
||||
{}
|
||||
)
|
||||
|
||||
vim.api.nvim_buf_create_user_command(
|
||||
self.prompt_buffer,
|
||||
"SwapWithLowerCell",
|
||||
function()
|
||||
local current_cell = self.table:get_highlighted_cell()
|
||||
|
||||
local lower_cell = {
|
||||
row = current_cell.row == self.table:rows_amount() and 1 or current_cell.row + 1,
|
||||
col = current_cell.col,
|
||||
}
|
||||
self.table:swap_contents(current_cell, lower_cell)
|
||||
self.table:set_highlighted_cell(lower_cell)
|
||||
self:draw_table()
|
||||
self:_reset_prompt()
|
||||
end,
|
||||
{}
|
||||
)
|
||||
end
|
||||
|
||||
return M
|
||||
|
Loading…
x
Reference in New Issue
Block a user