Skip to content

Commit

Permalink
fix: Fixed how widths are calculated in code blocks
Browse files Browse the repository at this point in the history
Code blocks now support emojis!
  • Loading branch information
OXY2DEV committed Aug 31, 2024
1 parent 33e6f83 commit 59c15a2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lua/markview/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ parser.md = function (buffer, TStree, from, to)

for i = 1,(row_end - row_start) - 2 do
local this_code = vim.api.nvim_buf_get_lines(buffer, row_start + i, row_start + i + 1, false)[1];
local len = vim.fn.strchars(this_code) or 0;
local len = vim.fn.strdisplaywidth(this_code) or 0;

if vim.list_contains(parser.cached_conf.filetypes or {}, string.lower(lang.get_name(language_string))) then
len = parser.get_md_len(this_code)
Expand Down
24 changes: 4 additions & 20 deletions lua/markview/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,6 @@ end

_G.__markview_views = {};

renderer.view_ranges = {};
renderer.removed_elements = {};

local get_str_width = function (str)
local width = 0;
local overlflow = 0;

for match in str:gmatch("[%z\1-\127\194-\244][\128-\191]*") do
overlflow = overlflow + (vim.fn.strdisplaywidth(match) - 1);
width = width + #match
end

return width, overlflow
end


--- Returns a value with the specified index from entry
--- If index is nil then return the last value
--- If entry isn't a table then return it
Expand Down Expand Up @@ -1104,12 +1088,12 @@ renderer.render_code_blocks = function (buffer, content, config_table)
}
})

local position, reduce_cols = get_str_width(text)
local position, width = #text, vim.fn.strdisplaywidth(vim.fn.strcharpart(text, content.col_start) or "");

vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, content.row_start + line, position, {
virt_text_pos = "inline",
virt_text = {
{ string.rep(config_table.pad_char or " ", block_length - length - reduce_cols), set_hl(config_table.hl) },
{ string.rep(config_table.pad_char or " ", block_length - width), set_hl(config_table.hl) },
{ string.rep(config_table.pad_char or " ", config_table.pad_amount or 1), set_hl(config_table.hl) }
}
})
Expand Down Expand Up @@ -1227,12 +1211,12 @@ renderer.render_code_blocks = function (buffer, content, config_table)
}
})

local position, reduce_cols = get_str_width(text);
local position, width = #text, vim.fn.strdisplaywidth(vim.fn.strcharpart(text, content.col_start) or "");

vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, content.row_start + line, position, {
virt_text_pos = "inline",
virt_text = {
{ string.rep(config_table.pad_char or " ", block_length - length - reduce_cols), set_hl(config_table.hl) },
{ string.rep(config_table.pad_char or " ", block_length - width), set_hl(config_table.hl) },
{ string.rep(config_table.pad_char or " ", config_table.pad_amount or 1), set_hl(config_table.hl) }
}
})
Expand Down

0 comments on commit 59c15a2

Please sign in to comment.