Fix focus

This commit is contained in:
Myzel394 2023-10-06 18:56:53 +02:00
parent 3a056a03f6
commit 070947007a
3 changed files with 77 additions and 39 deletions

View File

@ -55,6 +55,9 @@ local function get_size()
end end
local function a() local function a()
print("size")
print(#"┐┐")
print(vim.api.nvim_strwidth("┐┐"))
local own_table = table:create(6, 3) local own_table = table:create(6, 3)
own_table:highlight_cell(1, 1) own_table:highlight_cell(1, 1)
@ -63,11 +66,6 @@ local function a()
window:show() window:show()
window:draw_table(own_table) window:draw_table(own_table)
window:register_listeners(own_table) window:register_listeners(own_table)
print("size")
print(#"")
print(#" ")
print(#"")
end end
return { return {

View File

@ -23,25 +23,15 @@ function M:value_at(row, col)
end end
function M:get_largest_length_for_column( function M:get_largest_length_for_column(
col --[[ int ]] col, --[[ int ]]
should_use_strwidth --[[ bool ]]
) -- int ) -- int
should_use_strwidth = should_use_strwidth or false
local largest = #self.table[1][col] local largest = #self.table[1][col]
for _, row in ipairs(self.table) do for _, row in ipairs(self.table) do
if #row[col] > largest then if #row[col] > largest then
largest = #row[col] largest = should_use_strwidth and vim.api.nvim_strwidth(row[col]) or #row[col]
end
end
return largest
end
function M:get_largest_length_for_row(
row --[[ int ]]
) -- int
local largest = #self.table[row][1]
for _, col in ipairs(self.table[row]) do
if #col > largest then
largest = #col
end end
end end
@ -62,12 +52,13 @@ function M:get_largest_length()
end end
function M:get_widths_for_columns( function M:get_widths_for_columns(
min_width --[[ int ]] min_width --[[ int ]],
should_use_strwidth --[[ bool ]]
) -- table ) -- table
local widths = {} local widths = {}
for i = 1, #self.table[1] do for i = 1, #self.table[1] do
widths[i] = math.max(min_width, self:get_largest_length_for_column(i)) widths[i] = math.max(min_width, self:get_largest_length_for_column(i, should_use_strwidth))
end end
return widths return widths
@ -109,17 +100,73 @@ function M:move_highlight_to_next_cell()
end end
function M:get_cell_positions(col, row, min_value_width) function M:get_cell_positions(col, row, min_value_width)
local start_position = 1 local length = #""
local start_position = 0
for i, cell in ipairs(self.table[row]) do for i, cell in ipairs(self.table[row]) do
if i == col then if i == col then
break break
end end
start_position = start_position + math.max(min_value_width, #cell) + 1 start_position = start_position + math.max(min_value_width, #cell) + length
end end
local end_position = start_position + math.max(min_value_width, #self.table[row][col]) local end_position = math.max(length, start_position) + math.max(min_value_width, #self.table[row][col]) + length
if col ~= 1 then
-- Add `length again because of the border left and right
end_position = end_position + length
end
return start_position, end_position
end
function M:get_horizontal_border_width(
col, -- [[ int ]]
row, -- [[ int ]]
min_value_width -- [[ int ]]
)
local length = #""
local start_position = 0
local widths = self:get_widths_for_columns(min_value_width, true)
for i, _ in ipairs(self.table[1]) do
if i == col then
break
end
start_position = start_position + math.max(min_value_width, widths[i]) * length
if row == 1 then
start_position = start_position + #""
else
start_position = start_position + #""
end
end
local end_position = 0
if col == 1 then
end_position = #""
else
end_position = #""
end
end_position = end_position + start_position + math.max(min_value_width, widths[col]) * length
if row == 1 then
if col == 1 then
end_position = end_position + #""
else
end_position = end_position + #""
end
else
if col == 1 then
end_position = end_position + #""
else
end_position = end_position + #""
end
end
return start_position, end_position return start_position, end_position
end end

View File

@ -87,21 +87,15 @@ function M:_draw_highlight(table)
local row = 1 + math.max(0, cell.row - 1) * 2 local row = 1 + math.max(0, cell.row - 1) * 2
local cell_start, cell_end = table:get_cell_positions(cell.col, cell.row, self.min_value_width) local cell_start, cell_end = table:get_cell_positions(cell.col, cell.row, self.min_value_width)
local border_start, border_end = table:get_horizontal_border_width(cell.col, cell.row, self.min_value_width)
print(cell_start, cell_end)
--cell_start = 10 + (cell_start - 2) * 3
-- No idea why, but the table characters take up multiple characters per one characters allegedly
local table_cell_end = 10 + (cell_end - 2) * 3
vim.api.nvim_buf_set_extmark( vim.api.nvim_buf_set_extmark(
self.preview_buffer, self.preview_buffer,
vim.api.nvim_create_namespace("easytables"), vim.api.nvim_create_namespace("easytables"),
row - 1, row - 1,
cell_start, border_start,
{ {
end_col = table_cell_end, end_col = border_end,
hl_group = "NormalFloat", hl_group = "NormalFloat",
hl_mode = "combine", hl_mode = "combine",
} }
@ -110,9 +104,9 @@ function M:_draw_highlight(table)
self.preview_buffer, self.preview_buffer,
vim.api.nvim_create_namespace("easytables"), vim.api.nvim_create_namespace("easytables"),
row + 1, row + 1,
0, border_start,
{ {
end_col = 6, end_col = border_end,
hl_group = "NormalFloat", hl_group = "NormalFloat",
hl_mode = "combine", hl_mode = "combine",
} }
@ -121,10 +115,9 @@ function M:_draw_highlight(table)
self.preview_buffer, self.preview_buffer,
vim.api.nvim_create_namespace("easytables"), vim.api.nvim_create_namespace("easytables"),
row, row,
0, cell_start,
{ {
-- +2 because 2 are missing and +4 for the table character end_col = cell_end,
end_col = 3,
hl_group = "NormalFloat", hl_group = "NormalFloat",
hl_mode = "combine", hl_mode = "combine",
} }