mirror of
https://github.com/Myzel394/easytables.nvim.git
synced 2025-06-18 14:55:26 +02:00
fix: Fix window positioning
This commit is contained in:
parent
6b3f3d9140
commit
6a0e8fc556
@ -5,15 +5,7 @@ local math = require("math")
|
|||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local function get_window_size(cols, rows)
|
local function _get_auto_size(cols, rows)
|
||||||
local option_value = o.options.table.window.size
|
|
||||||
|
|
||||||
if option_value ~= "auto" then
|
|
||||||
local width, height = option_value:match("(%d+)x(%d+)")
|
|
||||||
|
|
||||||
return tonumber(width), tonumber(height)
|
|
||||||
end
|
|
||||||
|
|
||||||
local width =
|
local width =
|
||||||
-- Border widths
|
-- Border widths
|
||||||
(1 + cols)
|
(1 + cols)
|
||||||
@ -26,6 +18,18 @@ local function get_window_size(cols, rows)
|
|||||||
return width, height
|
return width, height
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function get_window_size(cols, rows)
|
||||||
|
local option_value = o.options.table.window.size
|
||||||
|
|
||||||
|
if option_value ~= "auto" then
|
||||||
|
local width, height = option_value:match("(%d+)x(%d+)")
|
||||||
|
|
||||||
|
return tonumber(width), tonumber(height)
|
||||||
|
end
|
||||||
|
|
||||||
|
return _get_auto_size(cols, rows)
|
||||||
|
end
|
||||||
|
|
||||||
function M:create(table, options)
|
function M:create(table, options)
|
||||||
options = options or {}
|
options = options or {}
|
||||||
|
|
||||||
@ -35,12 +39,20 @@ function M:create(table, options)
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
function M:get_x()
|
function M:get_table_width()
|
||||||
local width, _ = get_window_size(self.table:cols_amount(), self.table:rows_amount())
|
local widths = self.table:get_widths_for_columns()
|
||||||
local diff = vim.o.columns - vim.api.nvim_win_get_width(self.previous_window)
|
local width = 1
|
||||||
local pos = vim.o.columns - width - diff
|
|
||||||
|
|
||||||
return math.floor(pos / 2)
|
for _, w in ipairs(widths) do
|
||||||
|
width = width + w + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
return width
|
||||||
|
end
|
||||||
|
|
||||||
|
function M:get_x()
|
||||||
|
local width, _ = self:get_table_width()
|
||||||
|
return math.floor((vim.o.columns - width) / 2)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M:get_y()
|
function M:get_y()
|
||||||
@ -49,21 +61,50 @@ function M:get_y()
|
|||||||
return math.floor(((vim.o.lines - height) / 2) - 1)
|
return math.floor(((vim.o.lines - height) / 2) - 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M:_open_preview_window()
|
function M:_set_window_positions()
|
||||||
local width, height = get_window_size(self.table:cols_amount(), self.table:rows_amount())
|
local width = self:get_table_width()
|
||||||
|
local _, height = get_window_size(self.table:cols_amount(), self.table:rows_amount())
|
||||||
|
|
||||||
self.preview_buffer = vim.api.nvim_create_buf(false, true)
|
print("updating", vim.inspect(width), vim.inspect(height))
|
||||||
self.preview_window = vim.api.nvim_open_win(self.preview_buffer, false, {
|
|
||||||
relative = "win",
|
vim.api.nvim_win_set_config(self.preview_window, {
|
||||||
col = self:get_x(),
|
|
||||||
row = self:get_y(),
|
|
||||||
width = width,
|
width = width,
|
||||||
height = height,
|
height = height,
|
||||||
|
col = self:get_x(),
|
||||||
|
row = self:get_y(),
|
||||||
|
relative = "editor"
|
||||||
|
})
|
||||||
|
vim.api.nvim_win_set_config(self.prompt_window, {
|
||||||
|
width = width,
|
||||||
|
height = 1,
|
||||||
|
col = self:get_x(),
|
||||||
|
row = self:get_y() + height + 2,
|
||||||
|
relative = "editor"
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
function M:_update_active_cell(cell)
|
||||||
|
self.table:set_highlighted_cell(cell)
|
||||||
|
|
||||||
|
self:draw_table()
|
||||||
|
|
||||||
|
vim.api.nvim_buf_set_name(self.prompt_buffer, "[Table Cell: " .. cell.row .. "x" .. cell.col .. "]")
|
||||||
|
end
|
||||||
|
|
||||||
|
function M:_open_preview_window()
|
||||||
|
self.preview_buffer = vim.api.nvim_create_buf(false, true)
|
||||||
|
self.preview_window = vim.api.nvim_open_win(self.preview_buffer, false, {
|
||||||
style = "minimal",
|
style = "minimal",
|
||||||
border = "rounded",
|
border = "rounded",
|
||||||
title = o.options.table.window.preview_title,
|
title = o.options.table.window.preview_title,
|
||||||
title_pos = "center",
|
title_pos = "center",
|
||||||
focusable = false,
|
focusable = false,
|
||||||
|
-- Required for function, will be overwritten by :_set_window_positions`
|
||||||
|
relative = "editor",
|
||||||
|
row = 0,
|
||||||
|
col = 0,
|
||||||
|
width = 1,
|
||||||
|
height = 1
|
||||||
})
|
})
|
||||||
|
|
||||||
-- Disable default highlight
|
-- Disable default highlight
|
||||||
@ -80,20 +121,19 @@ function M:_open_preview_window()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function M:_open_prompt_window()
|
function M:_open_prompt_window()
|
||||||
local width, preview_height = get_window_size(self.table:cols_amount(), self.table:rows_amount())
|
|
||||||
|
|
||||||
self.prompt_buffer = vim.api.nvim_create_buf(false, false)
|
self.prompt_buffer = vim.api.nvim_create_buf(false, false)
|
||||||
self.prompt_window = vim.api.nvim_open_win(self.prompt_buffer, true, {
|
self.prompt_window = vim.api.nvim_open_win(self.prompt_buffer, true, {
|
||||||
relative = "win",
|
|
||||||
-- No idea why, but the window is shifted one cell to the right by default
|
-- No idea why, but the window is shifted one cell to the right by default
|
||||||
col = self:get_x(),
|
|
||||||
row = self:get_y() + preview_height + 2,
|
|
||||||
width = width,
|
|
||||||
height = 2,
|
|
||||||
style = "minimal",
|
style = "minimal",
|
||||||
border = "rounded",
|
border = "rounded",
|
||||||
title = o.options.table.window.prompt_title,
|
title = o.options.table.window.prompt_title,
|
||||||
title_pos = "center",
|
title_pos = "center",
|
||||||
|
-- Required for function, will be overwritten by :_set_window_positions`
|
||||||
|
relative = "editor",
|
||||||
|
col = 0,
|
||||||
|
row = 0,
|
||||||
|
width = 1,
|
||||||
|
height = 1
|
||||||
})
|
})
|
||||||
|
|
||||||
vim.api.nvim_set_option_value('winhighlight', "Normal:Normal", { win = self.prompt_window })
|
vim.api.nvim_set_option_value('winhighlight', "Normal:Normal", { win = self.prompt_window })
|
||||||
@ -107,6 +147,7 @@ function M:show()
|
|||||||
|
|
||||||
self:_open_preview_window()
|
self:_open_preview_window()
|
||||||
self:_open_prompt_window()
|
self:_open_prompt_window()
|
||||||
|
self:_set_window_positions()
|
||||||
end
|
end
|
||||||
|
|
||||||
function M:_reset_prompt()
|
function M:_reset_prompt()
|
||||||
@ -125,7 +166,7 @@ function M:_draw_highlight()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local row = 1 + math.max(0, cell.row - 1) * 2
|
local row = 1 + math.max(0, cell.row - 1) * 2
|
||||||
local widths = self.table:get_widths_for_columns(self.min_value_width)
|
local widths = self.table:get_widths_for_columns()
|
||||||
local cell_start, cell_end = self.table:get_cell_positions(cell.col, cell.row, widths)
|
local cell_start, cell_end = self.table:get_cell_positions(cell.col, cell.row, widths)
|
||||||
local border_start, border_end = self.table:get_horizontal_border_width(cell.col, cell.row, self.min_value_width)
|
local border_start, border_end = self.table:get_horizontal_border_width(cell.col, cell.row, self.min_value_width)
|
||||||
|
|
||||||
@ -172,6 +213,12 @@ function M:draw_table()
|
|||||||
self:_draw_highlight()
|
self:_draw_highlight()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M:update_window()
|
||||||
|
if o.options.table.window.size == "auto" then
|
||||||
|
self:_set_window_positions()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function M:close()
|
function M:close()
|
||||||
vim.api.nvim_win_close(self.preview_window, true)
|
vim.api.nvim_win_close(self.preview_window, true)
|
||||||
vim.api.nvim_win_close(self.prompt_window, true)
|
vim.api.nvim_win_close(self.prompt_window, true)
|
||||||
@ -197,6 +244,7 @@ function M:register_listeners()
|
|||||||
|
|
||||||
self.table:insert(selected_cell.col, selected_cell.row, lines[1])
|
self.table:insert(selected_cell.col, selected_cell.row, lines[1])
|
||||||
self:draw_table()
|
self:draw_table()
|
||||||
|
self:update_window()
|
||||||
end)
|
end)
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
@ -216,7 +264,7 @@ function M:register_listeners()
|
|||||||
"JumpToNextCell",
|
"JumpToNextCell",
|
||||||
function()
|
function()
|
||||||
self.table:move_highlight_to_next_cell()
|
self.table:move_highlight_to_next_cell()
|
||||||
self:draw_table()
|
self:update_window()
|
||||||
self:_reset_prompt()
|
self:_reset_prompt()
|
||||||
end,
|
end,
|
||||||
{}
|
{}
|
||||||
@ -227,7 +275,7 @@ function M:register_listeners()
|
|||||||
"JumpToPreviousCell",
|
"JumpToPreviousCell",
|
||||||
function()
|
function()
|
||||||
self.table:move_highlight_to_previous_cell()
|
self.table:move_highlight_to_previous_cell()
|
||||||
self:draw_table()
|
self:update_window()
|
||||||
self:_reset_prompt()
|
self:_reset_prompt()
|
||||||
end,
|
end,
|
||||||
{}
|
{}
|
||||||
@ -238,7 +286,7 @@ function M:register_listeners()
|
|||||||
"JumpDown",
|
"JumpDown",
|
||||||
function()
|
function()
|
||||||
self.table:move_highlight_down()
|
self.table:move_highlight_down()
|
||||||
self:draw_table()
|
self:update_window()
|
||||||
self:_reset_prompt()
|
self:_reset_prompt()
|
||||||
end,
|
end,
|
||||||
{}
|
{}
|
||||||
@ -249,7 +297,7 @@ function M:register_listeners()
|
|||||||
"JumpUp",
|
"JumpUp",
|
||||||
function()
|
function()
|
||||||
self.table:move_highlight_up()
|
self.table:move_highlight_up()
|
||||||
self:draw_table()
|
self:update_window()
|
||||||
self:_reset_prompt()
|
self:_reset_prompt()
|
||||||
end,
|
end,
|
||||||
{}
|
{}
|
||||||
@ -260,7 +308,7 @@ function M:register_listeners()
|
|||||||
"JumpLeft",
|
"JumpLeft",
|
||||||
function()
|
function()
|
||||||
self.table:move_highlight_left()
|
self.table:move_highlight_left()
|
||||||
self:draw_table()
|
self:update_window()
|
||||||
self:_reset_prompt()
|
self:_reset_prompt()
|
||||||
end,
|
end,
|
||||||
{}
|
{}
|
||||||
@ -271,7 +319,7 @@ function M:register_listeners()
|
|||||||
"JumpRight",
|
"JumpRight",
|
||||||
function()
|
function()
|
||||||
self.table:move_highlight_right()
|
self.table:move_highlight_right()
|
||||||
self:draw_table()
|
self:update_window()
|
||||||
self:_reset_prompt()
|
self:_reset_prompt()
|
||||||
end,
|
end,
|
||||||
{}
|
{}
|
||||||
@ -288,8 +336,8 @@ function M:register_listeners()
|
|||||||
col = current_cell.col == self.table:cols_amount() and 1 or current_cell.col + 1,
|
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:swap_contents(current_cell, right_cell)
|
||||||
self.table:set_highlighted_cell(right_cell)
|
self:_update_active_cell(right_cell)
|
||||||
self:draw_table()
|
self:update_window()
|
||||||
self:_reset_prompt()
|
self:_reset_prompt()
|
||||||
end,
|
end,
|
||||||
{}
|
{}
|
||||||
@ -306,7 +354,8 @@ function M:register_listeners()
|
|||||||
col = current_cell.col == 1 and self.table:cols_amount() or current_cell.col - 1,
|
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:swap_contents(current_cell, left_cell)
|
||||||
self.table:set_highlighted_cell(left_cell)
|
self.table:_update_active_cell(left_cell)
|
||||||
|
self:update_window()
|
||||||
self:draw_table()
|
self:draw_table()
|
||||||
self:_reset_prompt()
|
self:_reset_prompt()
|
||||||
end,
|
end,
|
||||||
@ -324,7 +373,8 @@ function M:register_listeners()
|
|||||||
col = current_cell.col,
|
col = current_cell.col,
|
||||||
}
|
}
|
||||||
self.table:swap_contents(current_cell, upper_cell)
|
self.table:swap_contents(current_cell, upper_cell)
|
||||||
self.table:set_highlighted_cell(upper_cell)
|
self.table:_update_active_cell(upper_cell)
|
||||||
|
self:update_window()
|
||||||
self:draw_table()
|
self:draw_table()
|
||||||
self:_reset_prompt()
|
self:_reset_prompt()
|
||||||
end,
|
end,
|
||||||
@ -342,7 +392,8 @@ function M:register_listeners()
|
|||||||
col = current_cell.col,
|
col = current_cell.col,
|
||||||
}
|
}
|
||||||
self.table:swap_contents(current_cell, lower_cell)
|
self.table:swap_contents(current_cell, lower_cell)
|
||||||
self.table:set_highlighted_cell(lower_cell)
|
self.table:_update_active_cell(lower_cell)
|
||||||
|
self:update_window()
|
||||||
self:draw_table()
|
self:draw_table()
|
||||||
self:_reset_prompt()
|
self:_reset_prompt()
|
||||||
end,
|
end,
|
||||||
@ -354,6 +405,7 @@ function M:register_listeners()
|
|||||||
"ToggleHeader",
|
"ToggleHeader",
|
||||||
function()
|
function()
|
||||||
self.table:toggle_header()
|
self.table:toggle_header()
|
||||||
|
self:update_window()
|
||||||
self:draw_table()
|
self:draw_table()
|
||||||
end,
|
end,
|
||||||
{}
|
{}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user