|
| 1 | +local util = require('render-markdown.util') |
| 2 | + |
| 3 | +---@class render.md.ContextCache |
| 4 | +local cache = { |
| 5 | + ---@type table<integer, table<integer, [integer, integer][]>> |
| 6 | + conceal = {}, |
| 7 | + ---@type table<integer, table<integer, [integer, integer, string][]>> |
| 8 | + inline_links = {}, |
| 9 | +} |
| 10 | + |
| 11 | +---@class render.md.Context |
| 12 | +local M = {} |
| 13 | + |
| 14 | +---@param buf integer |
| 15 | +function M.reset_buf(buf) |
| 16 | + cache.conceal[buf] = {} |
| 17 | + cache.inline_links[buf] = {} |
| 18 | +end |
| 19 | + |
| 20 | +---@param buf integer |
| 21 | +---@param parser vim.treesitter.LanguageTree |
| 22 | +function M.compute_conceal(buf, parser) |
| 23 | + local ranges = {} |
| 24 | + if util.get_win(util.buf_to_win(buf), 'conceallevel') > 0 then |
| 25 | + parser:for_each_tree(function(tree, language_tree) |
| 26 | + local nodes = M.get_conceal_nodes(buf, language_tree:lang(), tree:root()) |
| 27 | + for _, node in ipairs(nodes) do |
| 28 | + local row, start_col, _, end_col = node:range() |
| 29 | + if ranges[row] == nil then |
| 30 | + ranges[row] = {} |
| 31 | + end |
| 32 | + table.insert(ranges[row], { start_col, end_col }) |
| 33 | + end |
| 34 | + end) |
| 35 | + end |
| 36 | + cache.conceal[buf] = ranges |
| 37 | +end |
| 38 | + |
| 39 | +---@private |
| 40 | +---@param buf integer |
| 41 | +---@param language string |
| 42 | +---@param root TSNode |
| 43 | +---@return TSNode[] |
| 44 | +function M.get_conceal_nodes(buf, language, root) |
| 45 | + if not vim.tbl_contains({ 'markdown', 'markdown_inline' }, language) then |
| 46 | + return {} |
| 47 | + end |
| 48 | + local query = vim.treesitter.query.get(language, 'highlights') |
| 49 | + if query == nil then |
| 50 | + return {} |
| 51 | + end |
| 52 | + local nodes = {} |
| 53 | + for _, node, metadata in query:iter_captures(root, buf) do |
| 54 | + if metadata.conceal ~= nil then |
| 55 | + table.insert(nodes, node) |
| 56 | + end |
| 57 | + end |
| 58 | + return nodes |
| 59 | +end |
| 60 | + |
| 61 | +---@param buf integer |
| 62 | +---@param row integer |
| 63 | +---@return [integer, integer][] |
| 64 | +function M.concealed(buf, row) |
| 65 | + return cache.conceal[buf][row] or {} |
| 66 | +end |
| 67 | + |
| 68 | +---@param buf integer |
| 69 | +---@param info render.md.NodeInfo |
| 70 | +---@param icon string |
| 71 | +function M.add_inline_link(buf, info, icon) |
| 72 | + local inline_links = cache.inline_links[buf] |
| 73 | + local row = info.start_row |
| 74 | + if inline_links[row] == nil then |
| 75 | + inline_links[row] = {} |
| 76 | + end |
| 77 | + table.insert(inline_links[row], { info.start_col, info.end_col, icon }) |
| 78 | +end |
| 79 | + |
| 80 | +---@param buf integer |
| 81 | +---@param row integer |
| 82 | +---@return [integer, integer, string][] |
| 83 | +function M.inline_links(buf, row) |
| 84 | + return cache.inline_links[buf][row] or {} |
| 85 | +end |
| 86 | + |
| 87 | +return M |
0 commit comments