@@ -16,6 +16,7 @@ local M = {}
16
16
--- @field public code ? string
17
17
--- @field public bullet ? string
18
18
--- @field public table ? UserTableHighlights
19
+ --- @field public latex ? string
19
20
20
21
--- @class UserConfig
21
22
--- @field public markdown_query ? string
@@ -71,6 +72,7 @@ function M.setup(opts)
71
72
head = ' @markup.heading' ,
72
73
row = ' Normal' ,
73
74
},
75
+ latex = ' Special' ,
74
76
},
75
77
}
76
78
state .enabled = true
@@ -134,16 +136,18 @@ M.refresh = function()
134
136
vim .treesitter .get_parser ():for_each_tree (function (tree , language_tree )
135
137
local language = language_tree :lang ()
136
138
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 ())
138
142
end
139
143
end )
140
144
end
141
145
142
- --- @param tree TSTree
143
- M .handle_markdown = function (tree )
146
+ --- @param root TSNode
147
+ M .handle_markdown = function (root )
144
148
local highlights = state .config .highlights
145
149
--- @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
147
151
local capture = state .markdown_query .captures [id ]
148
152
local value = vim .treesitter .get_node_text (node , 0 )
149
153
local start_row , start_col , end_row , end_col = node :range ()
@@ -212,4 +216,27 @@ M.handle_markdown = function(tree)
212
216
end
213
217
end
214
218
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
+
215
242
return M
0 commit comments