Skip to content

Commit

Permalink
feat: Added option to disable faking preview of subscript/superscript…
Browse files Browse the repository at this point in the history
… in LaTeX

Ref: #271
  • Loading branch information
OXY2DEV committed Jan 30, 2025
1 parent d231b66 commit c382be1
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 62 deletions.
2 changes: 2 additions & 0 deletions doc/latex_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ Configuration for subscripts(`_{}`).
---@class latex.subscripts
---
---@field enable boolean Enables preview of subscript text.
---@field fake_preview? boolean When `true`, subscript characters are *faked*.
---@field hl? string | string[] Highlight group for the subscript text. Can be a list to use different hl for nested subscripts.
subscripts = {
enable = true,
Expand Down Expand Up @@ -843,6 +844,7 @@ Configuration for superscripts(`^{}`).
---@class latex.superscripts
---
---@field enable boolean Enables preview of superscript text.
---@field fake_preview? boolean When `true`, superscript characters are *faked*.
---@field hl? string | string[] Highlight group for the superscript text. Can be a list to use different hl for nested superscripts.
subscripts = {
enable = true,
Expand Down
2 changes: 2 additions & 0 deletions lua/definitions/latex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ M.__latex_parenthesis = {
---@class latex.subscripts
---
---@field enable boolean Enables preview of subscript text.
---@field fake_preview? boolean When `true`, subscript characters are *faked*.
---@field hl? string | string[] Highlight group for the subscript text. Can be a list to use different hl for nested subscripts.
M.latex_subscripts = {
enable = true,
Expand Down Expand Up @@ -477,6 +478,7 @@ M.__latex_subscripts = {
---@class latex.superscripts
---
---@field enable boolean Enables preview of superscript text.
---@field fake_preview? boolean When `true`, superscript characters are *faked*.
---@field hl? string | string[] Highlight group for the superscript text. Can be a list to use different hl for nested superscripts.
M.latex_subscripts = {
enable = true,
Expand Down
221 changes: 160 additions & 61 deletions lua/markview/renderers/latex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -401,46 +401,89 @@ latex.subscript = function (buffer, item)
hl = config.hl;
end

vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start, {
undo_restore = false, invalidate = true,
end_col = range.col_start + (item.parenthesis and 2 or 1),
conceal = "",
---@cast hl string?

virt_text_pos = "inline",
virt_text = item.preview == false and { { "↓(", utils.set_hl(hl) } } or nil,
if config.fake_preview == false or item.preview == false then
if item.parenthesis then
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start, {
undo_restore = false, invalidate = true,
end_col = range.col_start + 2,
conceal = "",

hl_mode = "combine"
});
virt_text_pos = "inline",
virt_text = {
{ "↓(", utils.set_hl(hl) }
},

if item.parenthesis then
table.insert(latex.cache.style_regions.subscripts, item);
hl_mode = "combine"
});

vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start, {
undo_restore = false, invalidate = true,
end_row = range.row_end,
end_col = range.col_end,
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_end, range.col_end - 1, {
undo_restore = false, invalidate = true,
end_col = range.col_end,
conceal = "",

hl_group = utils.set_hl(hl)
});
virt_text_pos = "inline",
virt_text = { { ")", utils.set_hl(hl) } },

vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_end, range.col_end - 1, {
undo_restore = false, invalidate = true,
end_col = range.col_end,
conceal = "",
hl_mode = "combine"
});
else
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start, {
undo_restore = false, invalidate = true,
end_col = range.col_start + 1,
conceal = "",

virt_text_pos = "inline",
virt_text = item.preview == false and { { ")", utils.set_hl(hl) } } or nil,
virt_text_pos = "inline",
virt_text = {
{ "↓(", utils.set_hl(hl) }
},

hl_mode = "combine"
});
elseif symbols.subscripts[item.text[1]:sub(2)] then
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start + 1, {
undo_restore = false, invalidate = true,
virt_text_pos = "overlay",
virt_text = { { symbols.subscripts[item.text[1]:sub(2)], utils.set_hl(hl) } },
hl_mode = "combine"
});

hl_mode = "combine"
});
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_end, range.col_end, {
undo_restore = false, invalidate = true,
right_gravity = false,

virt_text_pos = "inline",
virt_text = { { ")", utils.set_hl(hl) } },

hl_mode = "combine"
});
end
else
if item.parenthesis then
if item.preview then
table.insert(latex.cache.style_regions.subscripts, item);
end

vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start, {
undo_restore = false, invalidate = true,
end_col = range.col_start + 2,
conceal = ""
});

vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_end, range.col_end - 1, {
undo_restore = false, invalidate = true,
end_col = range.col_end,
conceal = "",
});
elseif symbols.subscripts[item.text[1]:sub(2)] then
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start, {
undo_restore = false, invalidate = true,
end_col = range.col_start + 1,
conceal = ""
});

vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start + 1, {
undo_restore = false, invalidate = true,
virt_text_pos = "overlay",
virt_text = { { symbols.subscripts[item.text[1]:sub(2)], utils.set_hl(hl) } },

hl_mode = "combine"
});
end
end

vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start, {
Expand All @@ -458,58 +501,114 @@ end
latex.superscript = function (buffer, item)
---+${func}

---@type latex.superscripts?
---@type latex.subscripts?
local config = spec.get({ "latex", "superscripts" }, { fallback = nil, eval_args = { buffer, item } });

if not config then
return;
end

local range = item.range;
local hl;

vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start, {
undo_restore = false, invalidate = true,
end_col = range.col_start + (item.parenthesis and 2 or 1),
conceal = "",
if vim.islist(config.hl) then
hl = config.hl[utils.clamp(item.level, 1, #config.hl)];
elseif type(config.hl) == "string" then
hl = config.hl;
end

virt_text_pos = "inline",
virt_text = item.preview == false and { { "↑(", utils.set_hl(config.hl) } } or nil,
---@cast hl string?

hl_mode = "combine"
});
if config.fake_preview == false or item.preview == false then
if item.parenthesis then
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start, {
undo_restore = false, invalidate = true,
end_col = range.col_start + 2,
conceal = "",

virt_text_pos = "inline",
virt_text = {
{ "↑(", utils.set_hl(hl) }
},

hl_mode = "combine"
});

vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_end, range.col_end - 1, {
undo_restore = false, invalidate = true,
end_col = range.col_end,
conceal = "",

if item.parenthesis then
if item.preview then
table.insert(latex.cache.style_regions.superscripts, item);
virt_text_pos = "inline",
virt_text = { { ")", utils.set_hl(hl) } },

hl_mode = "combine"
});
else
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start, {
undo_restore = false, invalidate = true,
end_row = range.row_end,
end_col = range.col_end,
end_col = range.col_start + 1,
conceal = "",

virt_text_pos = "inline",
virt_text = {
{ "↑(", utils.set_hl(hl) }
},

hl_group = utils.set_hl(config.hl)
hl_mode = "combine"
});

vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_end, range.col_end, {
undo_restore = false, invalidate = true,
right_gravity = false,

virt_text_pos = "inline",
virt_text = { { ")", utils.set_hl(hl) } },

hl_mode = "combine"
});
end
else
if item.parenthesis then
if item.preview then
table.insert(latex.cache.style_regions.superscripts, item);
end

vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_end, range.col_end - 1, {
undo_restore = false, invalidate = true,
end_col = range.col_end,
conceal = "",
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start, {
undo_restore = false, invalidate = true,
end_col = range.col_start + 2,
conceal = ""
});

virt_text_pos = "inline",
virt_text = item.preview == false and { { ")", utils.set_hl(config.hl) } } or nil,
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_end, range.col_end - 1, {
undo_restore = false, invalidate = true,
end_col = range.col_end,
conceal = "",
});
elseif symbols.superscripts[item.text[1]:sub(2)] then
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start, {
undo_restore = false, invalidate = true,
end_col = range.col_start + 1,
conceal = ""
});

hl_mode = "combine"
});
elseif symbols.superscripts[item.text[1]:sub(2)] then
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start + 1, {
undo_restore = false, invalidate = true,
virt_text_pos = "overlay",
virt_text = { { symbols.superscripts[item.text[1]:sub(2)], utils.set_hl(config.hl) } },
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start + 1, {
undo_restore = false, invalidate = true,
virt_text_pos = "overlay",
virt_text = { { symbols.superscripts[item.text[1]:sub(2)], utils.set_hl(hl) } },

hl_mode = "combine"
});
hl_mode = "combine"
});
end
end

vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start, {
undo_restore = false, invalidate = true,
end_row = range.row_end,
end_col = range.col_end,

hl_group = utils.set_hl(hl)
});
---_
end

Expand Down
2 changes: 1 addition & 1 deletion markview.nvim.wiki

0 comments on commit c382be1

Please sign in to comment.