@@ -16,6 +16,7 @@ local M = {}
1616--- @field public code ? string
1717--- @field public bullet ? string
1818--- @field public table ? UserTableHighlights
19+ --- @field public latex ? string
1920
2021--- @class UserConfig
2122--- @field public markdown_query ? string
@@ -71,6 +72,7 @@ function M.setup(opts)
7172 head = ' @markup.heading' ,
7273 row = ' Normal' ,
7374 },
75+ latex = ' Special' ,
7476 },
7577 }
7678 state .enabled = true
@@ -134,16 +136,18 @@ M.refresh = function()
134136 vim .treesitter .get_parser ():for_each_tree (function (tree , language_tree )
135137 local language = language_tree :lang ()
136138 if language == ' markdown' then
137- M .handle_markdown (tree )
139+ M .handle_markdown (tree :root ())
140+ elseif language == ' latex' then
141+ M .handle_latex (tree :root ())
138142 end
139143 end )
140144end
141145
142- --- @param tree TSTree
143- M .handle_markdown = function (tree )
146+ --- @param root TSNode
147+ M .handle_markdown = function (root )
144148 local highlights = state .config .highlights
145149 --- @diagnostic disable-next-line : missing-parameter
146- for id , node in state .markdown_query :iter_captures (tree : root () , 0 ) do
150+ for id , node in state .markdown_query :iter_captures (root , 0 ) do
147151 local capture = state .markdown_query .captures [id ]
148152 local value = vim .treesitter .get_node_text (node , 0 )
149153 local start_row , start_col , end_row , end_col = node :range ()
@@ -212,4 +216,27 @@ M.handle_markdown = function(tree)
212216 end
213217end
214218
219+ --- @param root TSNode
220+ M .handle_latex = function (root )
221+ if vim .fn .executable (' latex2text' ) ~= 1 then
222+ return
223+ end
224+
225+ local latex = vim .treesitter .get_node_text (root , 0 )
226+ local expression = vim .trim (vim .fn .system (' latex2text' , latex ))
227+ local extra_space = vim .fn .strdisplaywidth (latex ) - vim .fn .strdisplaywidth (expression )
228+ if extra_space < 0 then
229+ return
230+ end
231+
232+ local start_row , start_col , end_row , end_col = root :range ()
233+ local virt_text = { expression .. string.rep (' ' , extra_space ), state .config .highlights .latex }
234+ vim .api .nvim_buf_set_extmark (0 , M .namespace , start_row , start_col , {
235+ end_row = end_row ,
236+ end_col = end_col ,
237+ virt_text = { virt_text },
238+ virt_text_pos = ' overlay' ,
239+ })
240+ end
241+
215242return M
0 commit comments