From 4337faf33fa3c3dc5618dc81fb950baa3e90cf49 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 8 Oct 2023 18:33:38 +0200 Subject: [PATCH] fix: Add support for linter formatted markdown tables --- lua/easytables/import.lua | 58 +++++++++++++++++++++++++++++++++------ lua/easytables/window.lua | 1 - 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/lua/easytables/import.lua b/lua/easytables/import.lua index ac449c2..558642b 100644 --- a/lua/easytables/import.lua +++ b/lua/easytables/import.lua @@ -24,8 +24,7 @@ local function find_row_start(buffer) if find_col_start(buffer, current_line) == nil then return current_line + 1 elseif current_line == 1 then - -- Nothing found - return nil + return 0 end current_line = current_line - 1 @@ -57,17 +56,56 @@ local function find_col_end( return #vim.api.nvim_buf_get_text(buffer, row_start, -1, row_start, -1, {}) end -local function _is_header_line(line) - for i = 1, #line do - local char = line:sub(i, i) - if char ~= o.options.export.markdown.characters.horizontal and char ~= o.options.export.markdown.characters.vertical then +local function _is_header_line(line, widths) + -- Check for each cell whether it only contains horizontal chars and is the same length as widths + local previous_width = 0 + local cell_index = 1 + + for i = 2, #line, 1 do + local char = string.sub(line, i, i) + + if char == o.options.export.markdown.characters.vertical then + local expected_width = widths[cell_index] + + -- Allow also - 2 width as some formatters apply spaces around the horizontal lines + if previous_width ~= expected_width and previous_width ~= expected_width - 2 then + return false + end + + previous_width = 0 + cell_index = cell_index + 1 + elseif char ~= o.options.export.markdown.characters.horizontal and char ~= o.options.export.markdown.characters.filler then return false + else + previous_width = previous_width + 1 end end return true end +---Calculates the width of each column in the given line. +---@param line string +---@return table +local function _get_widths(line) + local widths = {} + + local current = 0 + -- Skip first as it's a vertical line + for i = 2, #line do + local char = string.sub(line, i, i) + + if char == o.options.export.markdown.characters.vertical then + widths[#widths + 1] = current + current = 0 + else + current = current + 1 + end + end + + return widths +end + ---Extracts the raw table string from the given buffer. ---@param buffer number ---@param row_start number @@ -82,8 +120,12 @@ local function extract_table( local table = {} - for _, line in ipairs(lines) do - if not _is_header_line(line) then + local widths = _get_widths(lines[1]) + + for i, line in ipairs(lines) do + local is_header = i == 2 and _is_header_line(line, widths) + + if not is_header then table[#table + 1] = {} for content in string.gmatch(line, "[^" .. o.options.export.markdown.characters.vertical .. "]+") do diff --git a/lua/easytables/window.lua b/lua/easytables/window.lua index f2b957b..27096cf 100644 --- a/lua/easytables/window.lua +++ b/lua/easytables/window.lua @@ -540,7 +540,6 @@ function M:register_listeners() function() self:close() - print("export") self.on_export() end, {}