Skip to content

Commit

Permalink
fix(renderer): Table cells now respect the column count & the cell width
Browse files Browse the repository at this point in the history
Ref: #75
  • Loading branch information
OXY2DEV committed Aug 6, 2024
1 parent a9f63fc commit a0d8a5d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
6 changes: 6 additions & 0 deletions lua/definitions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@
--- Used for highlighting the lines when the style is "simple"
---@field hl string?
---
--- Highlight group for the info string
---@field info_hl string?
---
--- The minimum number of columns to take(without the paddings)
---@field min_width number
---
Expand Down Expand Up @@ -463,6 +466,9 @@
--- Enable/Disable the usage of virtual lines for the top/bottom border
---@field use_virt_lines boolean?
---
--- Enable/Disable the usage of the top & bottom border
---@field block_decorator boolean?
---
--- List of various parts for the table
---@field text string[]
---
Expand Down
18 changes: 16 additions & 2 deletions lua/markview/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,15 @@ parser.md = function (buffer, TStree, from, to)
local alignments = {};

local line_positions = {};
local col_widths = {};

for row in capture_node:iter_children() do
local tmp = {};

local row_text = vim.treesitter.get_node_text(row, buffer)
local r_row_start, r_col_start, r_row_end, r_col_end = row:range();

local line = vim.api.nvim_buf_get_lines(buffer, r_row_start, r_row_start + 1, false)[1];

--- Separator gets counted from the start of the line
--- So, we will instead count the number of spaces at the start
table.insert(line_positions, {
Expand Down Expand Up @@ -323,6 +325,18 @@ parser.md = function (buffer, TStree, from, to)
elseif char_e == ":" then
table.insert(alignments, "right");
end

if line:match("|([^|]+)|") then
local col_content = line:match("|([^|]+)|");
line = line:gsub("|" .. col_content, "");

table.insert(col_widths, vim.fn.strchars(col_content));
elseif line:match("|([^|]+)$") then
local col_content = line:match("|([^|]+)$");
line = line:gsub("|" .. col_content, "");

table.insert(col_widths, vim.fn.strchars(col_content));
end
end
end

Expand Down Expand Up @@ -371,6 +385,7 @@ parser.md = function (buffer, TStree, from, to)
row_type = table_structure;
rows = rows,

col_widths = col_widths,
content_alignments = alignments,
content_positions = line_positions,

Expand Down Expand Up @@ -554,7 +569,6 @@ parser.md_inline = function (buffer, TStree, from, to)
local link_text = vim.treesitter.get_node_text(desc, buffer);
local link_address = ""

-- vim.print(sibl == nil)
if sibl then
link_address = vim.treesitter.get_node_text(sibl, buffer);
end
Expand Down
29 changes: 25 additions & 4 deletions lua/markview/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,17 @@ local table_header = function (buffer, content, config_table)
local width, actual_width = display_width(col, config_table);
local align = content.content_alignments[curr_tbl_col];

-- Extracted width of separator
local tbl_col_width = content.col_widths[curr_tbl_col];

-- The column number of headers must match the
-- column number of separators
--
-- No need to add unnecessary condition
if tbl_col_width then
actual_width = math.min(math.max(actual_width, tbl_col_width), tbl_col_width);
end

if width < actual_width then
if align == "left" then
vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, row_start, col_start + curr_col + vim.fn.strchars(col), {
Expand Down Expand Up @@ -658,6 +669,13 @@ local table_footer = function (buffer, content, config_table)
local width, actual_width = display_width(col, config_table);
local align = content.content_alignments[curr_tbl_col];

-- Extracted width of separator
local tbl_col_width = content.col_widths[curr_tbl_col];

if #content.rows[#content.rows] == #content.rows[2] and tbl_col_width then
actual_width = math.min(math.max(actual_width, tbl_col_width), tbl_col_width);
end

if width < actual_width then
if align == "left" then
vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, row_end - 1, col_start + curr_col + vim.fn.strchars(col), {
Expand Down Expand Up @@ -758,6 +776,13 @@ local table_content = function (buffer, content, config_table, r_num)
local width, actual_width = display_width(col, config_table);
local align = content.content_alignments[curr_tbl_col];

-- Extracted width of separator
local tbl_col_width = content.col_widths[curr_tbl_col];

if #content.rows[r_num] == #content.rows[2] and tbl_col_width then
actual_width = math.min(math.max(actual_width, tbl_col_width), tbl_col_width);
end

if width < actual_width then
if align == "left" then
vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, row_start + r_num - 1, col_start + curr_col + vim.fn.strchars(col), {
Expand Down Expand Up @@ -1065,11 +1090,7 @@ renderer.render_code_blocks = function (buffer, content, config_table)

if content.block_info ~= "" and vim.fn.strchars(content.block_info) > (block_length - lang_width - ((content.pad_amount or 1) * 2)) then
rendered_info = rendered_info .. "...";
-- Hi
end
vim.print(rendered_info)

-- vim.print(content.col_start + vim.fn.strchars(content.info_string))

vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, content.row_start, content.col_start + 3, {
virt_text_pos = config_table.position or "inline",
Expand Down

0 comments on commit a0d8a5d

Please sign in to comment.