|
| 1 | +---@class OrgFootnotesHighlighter : OrgMarkupHighlighter |
| 2 | +---@field private markup OrgMarkupHighlighter |
| 3 | +local OrgFootnotes = { |
| 4 | + valid_capture_names = { |
| 5 | + ['footnote.start'] = true, |
| 6 | + ['footnote.end'] = true, |
| 7 | + }, |
| 8 | +} |
| 9 | + |
| 10 | +---@param opts { markup: OrgMarkupHighlighter } |
| 11 | +function OrgFootnotes:new(opts) |
| 12 | + local data = { |
| 13 | + markup = opts.markup, |
| 14 | + } |
| 15 | + setmetatable(data, self) |
| 16 | + self.__index = self |
| 17 | + return data |
| 18 | +end |
| 19 | + |
| 20 | +---@param node TSNode |
| 21 | +---@param name string |
| 22 | +---@return OrgMarkupNode | false |
| 23 | +function OrgFootnotes:parse_node(node, name) |
| 24 | + if not self.valid_capture_names[name] then |
| 25 | + return false |
| 26 | + end |
| 27 | + local type = node:type() |
| 28 | + if type == '[' then |
| 29 | + return self:_parse_start_node(node) |
| 30 | + end |
| 31 | + |
| 32 | + if type == ']' then |
| 33 | + return self:_parse_end_node(node) |
| 34 | + end |
| 35 | + |
| 36 | + return false |
| 37 | +end |
| 38 | + |
| 39 | +---@private |
| 40 | +---@param node TSNode |
| 41 | +---@return OrgMarkupNode | false |
| 42 | +function OrgFootnotes:_parse_start_node(node) |
| 43 | + local node_type = node:type() |
| 44 | + local first_sibling = node:next_sibling() |
| 45 | + local second_sibling = first_sibling and first_sibling:next_sibling() |
| 46 | + |
| 47 | + if not first_sibling or not second_sibling then |
| 48 | + return false |
| 49 | + end |
| 50 | + if first_sibling:type() ~= 'str' or second_sibling:type() ~= ':' then |
| 51 | + return false |
| 52 | + end |
| 53 | + |
| 54 | + return { |
| 55 | + type = 'footnote', |
| 56 | + id = 'footnote_start', |
| 57 | + char = node_type, |
| 58 | + seek_id = 'footnote_end', |
| 59 | + nestable = false, |
| 60 | + range = self.markup:node_to_range(node), |
| 61 | + node = node, |
| 62 | + } |
| 63 | +end |
| 64 | + |
| 65 | +---@private |
| 66 | +---@param node TSNode |
| 67 | +---@return OrgMarkupNode | false |
| 68 | +function OrgFootnotes:_parse_end_node(node) |
| 69 | + local node_type = node:type() |
| 70 | + local prev_sibling = node:prev_sibling() |
| 71 | + |
| 72 | + if not prev_sibling then |
| 73 | + return false |
| 74 | + end |
| 75 | + |
| 76 | + return { |
| 77 | + type = 'footnote', |
| 78 | + id = 'footnote_end', |
| 79 | + seek_id = 'footnote_start', |
| 80 | + char = node_type, |
| 81 | + nestable = false, |
| 82 | + range = self.markup:node_to_range(node), |
| 83 | + node = node, |
| 84 | + } |
| 85 | +end |
| 86 | + |
| 87 | +---@param entry OrgMarkupNode |
| 88 | +---@return boolean |
| 89 | +function OrgFootnotes:is_valid_start_node(entry) |
| 90 | + return entry.type == 'footnote' and entry.id == 'footnote_start' |
| 91 | +end |
| 92 | + |
| 93 | +---@param entry OrgMarkupNode |
| 94 | +---@return boolean |
| 95 | +function OrgFootnotes:is_valid_end_node(entry) |
| 96 | + return entry.type == 'footnote' and entry.id == 'footnote_end' |
| 97 | +end |
| 98 | + |
| 99 | +---@param highlights OrgMarkupHighlight[] |
| 100 | +---@param bufnr number |
| 101 | +function OrgFootnotes:highlight(highlights, bufnr) |
| 102 | + local namespace = self.markup.highlighter.namespace |
| 103 | + local ephemeral = self.markup:use_ephemeral() |
| 104 | + |
| 105 | + for _, entry in ipairs(highlights) do |
| 106 | + vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col, { |
| 107 | + ephemeral = ephemeral, |
| 108 | + end_col = entry.to.end_col, |
| 109 | + hl_group = '@org.footnote', |
| 110 | + priority = 110, |
| 111 | + }) |
| 112 | + end |
| 113 | +end |
| 114 | + |
| 115 | +---@param highlights OrgMarkupHighlight[] |
| 116 | +---@return OrgMarkupPreparedHighlight[] |
| 117 | +function OrgFootnotes:prepare_highlights(highlights) |
| 118 | + local ephemeral = self.markup:use_ephemeral() |
| 119 | + local extmarks = {} |
| 120 | + for _, entry in ipairs(highlights) do |
| 121 | + table.insert(extmarks, { |
| 122 | + start_line = entry.from.line, |
| 123 | + start_col = entry.from.start_col, |
| 124 | + end_col = entry.to.end_col, |
| 125 | + ephemeral = ephemeral, |
| 126 | + hl_group = '@org.footnote', |
| 127 | + priority = 110, |
| 128 | + }) |
| 129 | + end |
| 130 | + return extmarks |
| 131 | +end |
| 132 | + |
| 133 | +return OrgFootnotes |
0 commit comments