Skip to content

Commit

Permalink
fix: Added experimental support for aligned checkbox desvription
Browse files Browse the repository at this point in the history
Closes #107
  • Loading branch information
OXY2DEV committed Aug 18, 2024
1 parent 738ddc0 commit f9aba72
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
35 changes: 26 additions & 9 deletions lua/markview/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ parser.fiter_lines = function (buffer, from, to)
local filtered_lines = {};
local indexes = {};
local spaces = {};
local align_spaces = {};

local withinCodeBlock;
local withinCodeBlock, insideDescription;
local parent_marker;

local tolarence = 3;
local found = 0;

local code_block_indent = 0;
local desc_indent = 0;

for l, line in ipairs(captured_lines) do
if l ~= 1 then
Expand All @@ -32,42 +34,56 @@ parser.fiter_lines = function (buffer, from, to)

local spaces_before = vim.fn.strchars(line:match("^(%s*)"));

if line:match("^%s*([+%-*])") then
parent_marker = line:match("^%s*([+%-*])");
elseif line:match("^%s*(%d+%.)") then
parent_marker = line:match("^%s*(%d+%.)");
end

if line:match("(```)") and withinCodeBlock ~= true then
withinCodeBlock = true;
code_block_indent = spaces_before;
elseif line:match("(```)") and withinCodeBlock == true then
withinCodeBlock = false;
elseif withinCodeBlock == true then
spaces_before = spaces_before > code_block_indent and spaces_before - code_block_indent or spaces_before;
spaces_before = spaces_before > code_block_indent and
spaces_before - code_block_indent - 2 or
spaces_before
;

goto withinElement;
end

if line:match("^%s*([+%-*])") then
parent_marker = line:match("^%s*([+%-*])");
elseif line:match("^%s*(%d+%.)") then
parent_marker = line:match("^%s*(%d+%.)");
if withinCodeBlock ~= true and line:match("^%s*[+%-*](%s+%[.-%])") then
insideDescription = true;
desc_indent = vim.fn.strchars(line:match("^%s*[+%-*](%s+%[.-%])"));
elseif withinCodeBlock ~= true and insideDescription == true and spaces_before < desc_indent then
insideDescription = false;
end

if not line:match("^%s*([+%-*])") and not line:match("^%s*(%d+%.)") and parent_marker then
spaces_before = math.max(0, spaces_before - vim.fn.strchars((parent_marker or "") .. " "));

if line:match("(```)") then
code_block_indent = spaces_before;
elseif insideDescription == true then
align_spaces[l] = 2;
spaces_before = math.max(-10, spaces_before - desc_indent - 2);
end
end

::withinElement::

table.insert(filtered_lines, line);
table.insert(indexes, l);
table.insert(spaces, spaces_before)
table.insert(spaces, spaces_before);

if line == "" then
found = found + 1;
end
end

return filtered_lines, indexes, spaces;
return filtered_lines, indexes, spaces, align_spaces;
end

parser.get_list_end_range = function (buffer, from, to, marker)
Expand Down Expand Up @@ -411,7 +427,7 @@ parser.md = function (buffer, TStree, from, to)
local marker_text = vim.treesitter.get_node_text(marker, buffer);
local symbol = marker_text:gsub("%s", "");

local list_lines, lines, spaces = parser.fiter_lines(buffer, row_start, row_end);
local list_lines, lines, spaces, align_spaces = parser.fiter_lines(buffer, row_start, row_end);
local spaces_before_marker = list_lines[1]:match("^(%s*)" .. symbol .. "%s*");

local c_end, _ = parser.get_list_end_range(buffer, row_start, row_end, symbol)
Expand All @@ -425,6 +441,7 @@ parser.md = function (buffer, TStree, from, to)
list_lines = list_lines,

spaces = spaces,
align_spaces = align_spaces,
conceal_spaces = vim.fn.strchars(spaces_before_marker),

row_start = row_start,
Expand Down
5 changes: 3 additions & 2 deletions lua/markview/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1538,10 +1538,11 @@ renderer.render_lists = function (buffer, content, config_table)
vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, line_num, 0, {
virt_text_pos = "inline",
virt_text = {
{ string.rep(" ", level * shift) }
{ string.rep(" ", level * shift) },
{ string.rep(" ", content.align_spaces[l] or 0) },
},

end_col = line_len < before and line_len or before,
end_col = line_len < before and line_len + (content.align_spaces[l] or 0) or before + (content.align_spaces[l] or 0),
conceal = ""
})
end
Expand Down

0 comments on commit f9aba72

Please sign in to comment.