Skip to content

Commit

Permalink
fix(parser): Added support for extra info on the code block start line
Browse files Browse the repository at this point in the history
Ref: #77
  • Loading branch information
OXY2DEV committed Aug 5, 2024
1 parent 5488c07 commit 7e0ad40
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
28 changes: 28 additions & 0 deletions lua/markview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,33 @@ markview.configuration = {
end
end
},
{
group_name = "CodeInfo",
value = function ()
local bg = markview.colors.get({
markview.colors.get_hl_value(0, "Normal", "bg"),
markview.colors.get_hl_value(0, "Cursor", "fg"),

vim.o.background == "dark" and "#1e1e2e" or "#cdd6f4"
});

local luminosity = markview.colors.get_brightness(bg);

if luminosity < 0.5 then
return {
bg = markview.colors.mix(bg, bg, 1, math.max(luminosity, 0.25)),
fg = markview.colors.get_hl_value(0, "Comment", "fg"),
default = true
};
else
return {
bg = markview.colors.mix(bg, bg, 1, math.min(luminosity, 0.25) * -1),
fg = markview.colors.get_hl_value(0, "Comment", "fg"),
default = true
};
end
end
},
{
group_name = "InlineCode",
value = function ()
Expand Down Expand Up @@ -991,6 +1018,7 @@ markview.configuration = {

style = "language",
hl = "MarkviewCode",
info_hl = "MarkviewCodeInfo",

min_width = 60,
pad_amount = 3,
Expand Down
19 changes: 17 additions & 2 deletions lua/markview/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ parser.md = function (buffer, TStree, from, to)

local heading_txt = capture_node:next_sibling();
local title = heading_txt ~= nil and vim.treesitter.get_node_text(heading_txt, buffer) or nil;
local h_txt_r_start, h_txt_c_start, h_txt_r_end, h_txt_c_end;

table.insert(parser.parsed_content, {
node = capture_node,
Expand Down Expand Up @@ -205,10 +204,26 @@ parser.md = function (buffer, TStree, from, to)
table.insert(line_lens, len);
end

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*)$");
elseif block_start:match("%s*```(%S*)%s*") then
language_string = 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 = block_start:match("%s*```(%S*)$") or "",
language = language_string,

info_string = block_start:gsub("^%s*", ""),
block_info = additional_info,

line_lengths = line_lens,
largest_line = highest_len,
Expand Down
21 changes: 19 additions & 2 deletions lua/markview/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,7 @@ renderer.render_code_blocks = function (buffer, content, config_table)
end

local lang_width = vim.fn.strchars(" " .. icon .. " " .. languageName .. " ");
local info_width = math.min(vim.fn.strchars(content.info_string or ""), content.largest_line);

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 + 3 + vim.fn.strlen(content.language), {
Expand All @@ -999,10 +1000,26 @@ renderer.render_code_blocks = function (buffer, content, config_table)
hl_mode = "combine",
});
elseif config_table.language_direction == "right" then
vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, content.row_start, content.col_start + 3 + vim.fn.strlen(content.language), {
vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, content.row_start, content.col_start, {
end_col = vim.fn.strchars(content.info_string),
conceal = "",
});

local rendered_info = vim.fn.strcharpart(content.block_info or "", 0, block_length - lang_width + ((config_table.pad_amount or 1) * 2) - 4);

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",
virt_text = {
{ string.rep(config_table.pad_char or " ", block_length - lang_width + ((config_table.pad_amount or 1) * 2)), set_hl(config_table.hl) },
{ rendered_info, set_hl(config_table.info_hl or config_table.hl) },
{ string.rep(config_table.pad_char or " ", block_length - lang_width - vim.fn.strchars(rendered_info) + ((config_table.pad_amount or 1) * 2)), set_hl(config_table.hl) },
{ " " .. icon .. " ", set_hl(hl) },
{ languageName .. " ", set_hl(config_table.name_hl) or set_hl(hl) },
},
Expand Down

0 comments on commit 7e0ad40

Please sign in to comment.