Skip to content

Commit

Permalink
fix(renderer): Fixed incorrect alignment of nested code blocks within…
Browse files Browse the repository at this point in the history
… lists

Also tweaked logic for counting lines of list items.
  • Loading branch information
OXY2DEV committed Aug 17, 2024
1 parent a8e104b commit 738ddc0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
25 changes: 15 additions & 10 deletions lua/markview/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ parser.fiter_lines = function (buffer, from, to)
local tolarence = 3;
local found = 0;

local code_block_indent = 0;

for l, line in ipairs(captured_lines) do
if l ~= 1 then
if withinCodeBlock ~= true and line:match("^%s*([+%-*])") then
Expand All @@ -32,11 +34,11 @@ parser.fiter_lines = function (buffer, from, to)

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

Expand All @@ -48,6 +50,10 @@ parser.fiter_lines = function (buffer, from, to)

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;
end
end

::withinElement::
Expand Down Expand Up @@ -208,23 +214,22 @@ parser.md = function (buffer, TStree, from, to)

local language_string, additional_info = "", nil;

-- chore: This needs more work
if block_start:match("^%s*```{{?([^}]*)}}?") then
language_string = block_start:match("^%s*```{{?([^}]*)}}?");
additional_info = block_start:match("^%s*```{{?[^}]*}}?%s*(.*)$");
elseif block_start:match("^%s*```(%S*)$") then
language_string = block_start:match("^%s*```(%S*)$");
if block_start:match("%s*```{{?([^}]*)}}?") then
language_string = block_start:match("%s*```{{?([^}]*)}}?");
additional_info = block_start:match("%s*```{{?[^}]*}}?%s*(.*)$");
elseif block_start:match("%s*```(%S*)$") then
language_string = block_start:match("%s*```(%S*)$");
elseif block_start:match("%s*```(%S*)%s*") then
language_string = block_start:match("%s*```(%S*)%s");
additional_info = block_start:match("^%s*```%S*%s+(.*)$");
additional_info = block_start:match("%s*```%S*%s+(.*)$");
end

table.insert(parser.parsed_content, {
node = capture_node,
type = "code_block",
language = language_string,

info_string = block_start:gsub("^%s*", ""),
info_string = vim.fn.strcharpart(block_start, col_start),
block_info = additional_info,

line_lengths = line_lens,
Expand Down
4 changes: 2 additions & 2 deletions lua/markview/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ renderer.render_code_blocks = function (buffer, content, config_table)

if config_table.language_direction == nil or config_table.language_direction == "left" then
vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, content.row_start, content.col_start, {
end_col = vim.fn.strchars(content.info_string),
end_col = content.col_start + vim.fn.strchars(content.info_string),
conceal = "",
});

Expand All @@ -1105,7 +1105,7 @@ renderer.render_code_blocks = function (buffer, content, config_table)
});
elseif config_table.language_direction == "right" then
vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, content.row_start, content.col_start, {
end_col = vim.fn.strchars(content.info_string),
end_col = content.col_start + vim.fn.strchars(content.info_string),
conceal = "",
});

Expand Down

0 comments on commit 738ddc0

Please sign in to comment.