From a0d8a5db74107ffe91cb3cab6b3065b7baccb663 Mon Sep 17 00:00:00 2001 From: Shawon Date: Tue, 6 Aug 2024 12:57:01 +0600 Subject: [PATCH] fix(renderer): Table cells now respect the column count & the cell width Ref: #75 --- lua/definitions.lua | 6 ++++++ lua/markview/parser.lua | 18 ++++++++++++++++-- lua/markview/renderer.lua | 29 +++++++++++++++++++++++++---- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/lua/definitions.lua b/lua/definitions.lua index ff235f2..6a4a8d3 100644 --- a/lua/definitions.lua +++ b/lua/definitions.lua @@ -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 --- @@ -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[] --- diff --git a/lua/markview/parser.lua b/lua/markview/parser.lua index c47e36a..dba5fe3 100644 --- a/lua/markview/parser.lua +++ b/lua/markview/parser.lua @@ -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, { @@ -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 @@ -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, @@ -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 diff --git a/lua/markview/renderer.lua b/lua/markview/renderer.lua index 886dcd0..08e9f4c 100644 --- a/lua/markview/renderer.lua +++ b/lua/markview/renderer.lua @@ -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), { @@ -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), { @@ -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), { @@ -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",