mirror of
https://github.com/Myzel394/easytables.nvim.git
synced 2025-06-18 23:05:27 +02:00
fix: Add support for linter formatted markdown tables
This commit is contained in:
parent
f1ed142aec
commit
4337faf33f
@ -24,8 +24,7 @@ local function find_row_start(buffer)
|
|||||||
if find_col_start(buffer, current_line) == nil then
|
if find_col_start(buffer, current_line) == nil then
|
||||||
return current_line + 1
|
return current_line + 1
|
||||||
elseif current_line == 1 then
|
elseif current_line == 1 then
|
||||||
-- Nothing found
|
return 0
|
||||||
return nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
current_line = current_line - 1
|
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, {})
|
return #vim.api.nvim_buf_get_text(buffer, row_start, -1, row_start, -1, {})
|
||||||
end
|
end
|
||||||
|
|
||||||
local function _is_header_line(line)
|
local function _is_header_line(line, widths)
|
||||||
for i = 1, #line do
|
-- Check for each cell whether it only contains horizontal chars and is the same length as widths
|
||||||
local char = line:sub(i, i)
|
local previous_width = 0
|
||||||
if char ~= o.options.export.markdown.characters.horizontal and char ~= o.options.export.markdown.characters.vertical then
|
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
|
return false
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
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.
|
---Extracts the raw table string from the given buffer.
|
||||||
---@param buffer number
|
---@param buffer number
|
||||||
---@param row_start number
|
---@param row_start number
|
||||||
@ -82,8 +120,12 @@ local function extract_table(
|
|||||||
|
|
||||||
local table = {}
|
local table = {}
|
||||||
|
|
||||||
for _, line in ipairs(lines) do
|
local widths = _get_widths(lines[1])
|
||||||
if not _is_header_line(line) then
|
|
||||||
|
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] = {}
|
table[#table + 1] = {}
|
||||||
|
|
||||||
for content in string.gmatch(line, "[^" .. o.options.export.markdown.characters.vertical .. "]+") do
|
for content in string.gmatch(line, "[^" .. o.options.export.markdown.characters.vertical .. "]+") do
|
||||||
|
@ -540,7 +540,6 @@ function M:register_listeners()
|
|||||||
function()
|
function()
|
||||||
self:close()
|
self:close()
|
||||||
|
|
||||||
print("export")
|
|
||||||
self.on_export()
|
self.on_export()
|
||||||
end,
|
end,
|
||||||
{}
|
{}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user