|
1 | | -local Context = require('render-markdown.core.context') |
2 | | -local Env = require('render-markdown.lib.env') |
3 | | -local Str = require('render-markdown.lib.str') |
4 | | -local log = require('render-markdown.core.log') |
5 | | -local state = require('render-markdown.state') |
6 | | - |
7 | | ----@class render.md.Marks |
8 | | ----@field private context render.md.Context |
9 | | ----@field private ignore render.md.config.conceal.Ignore |
10 | | ----@field private update boolean |
11 | | ----@field private marks render.md.Mark[] |
12 | | -local Marks = {} |
13 | | -Marks.__index = Marks |
14 | | - |
15 | | ----@param buf integer |
16 | | ----@param update boolean |
17 | | ----@return render.md.Marks |
18 | | -function Marks.new(buf, update) |
19 | | - local self = setmetatable({}, Marks) |
20 | | - self.context = Context.get(buf) |
21 | | - self.ignore = state.get(buf).anti_conceal.ignore |
22 | | - self.update = update |
23 | | - self.marks = {} |
24 | | - return self |
25 | | -end |
26 | | - |
27 | | ----@return render.md.Mark[] |
28 | | -function Marks:get() |
29 | | - return self.marks |
30 | | -end |
31 | | - |
32 | | ----@param element render.md.mark.Element |
33 | | ----@param node render.md.Node |
34 | | ----@param opts render.md.MarkOpts |
35 | | ----@param offset? Range4 |
36 | | ----@return boolean |
37 | | -function Marks:add_over(element, node, opts, offset) |
38 | | - offset = offset or { 0, 0, 0, 0 } |
39 | | - opts.end_row = node.end_row + offset[3] |
40 | | - opts.end_col = node.end_col + offset[4] |
41 | | - return self:add(element, node.start_row + offset[1], node.start_col + offset[2], opts) |
42 | | -end |
43 | | - |
44 | | ----@param element render.md.mark.Element |
45 | | ----@param start_row integer |
46 | | ----@param start_col integer |
47 | | ----@param opts render.md.MarkOpts |
48 | | ----@return boolean |
49 | | -function Marks:add(element, start_row, start_col, opts) |
50 | | - ---@type render.md.Mark |
51 | | - local mark = { |
52 | | - conceal = self:conceal(element), |
53 | | - start_row = start_row, |
54 | | - start_col = start_col, |
55 | | - opts = opts, |
56 | | - } |
57 | | - local valid, feature, min_version = self:validate(opts) |
58 | | - if not valid then |
59 | | - log.add('error', 'mark', string.format('%s requires neovim >= %s', feature, min_version), mark) |
60 | | - return false |
61 | | - end |
62 | | - log.add('debug', 'mark', mark) |
63 | | - if self.update then |
64 | | - self:update_context(mark) |
65 | | - end |
66 | | - table.insert(self.marks, mark) |
67 | | - return true |
68 | | -end |
69 | | - |
70 | | ----@private |
71 | | ----@param element render.md.mark.Element |
72 | | ----@return boolean |
73 | | -function Marks:conceal(element) |
74 | | - if type(element) == 'boolean' then |
75 | | - return element |
76 | | - end |
77 | | - local modes = self.ignore[element] |
78 | | - if modes == nil then |
79 | | - return true |
80 | | - else |
81 | | - return not Env.mode.is(self.context.mode, modes) |
82 | | - end |
83 | | -end |
84 | | - |
85 | | ----@private |
86 | | ----@param opts render.md.MarkOpts |
87 | | ----@return boolean, string, string |
88 | | -function Marks:validate(opts) |
89 | | - if opts.virt_text_pos == 'inline' and not Env.has_10 then |
90 | | - return false, "virt_text_pos = 'inline'", '0.10.0' |
91 | | - end |
92 | | - if opts.virt_text_repeat_linebreak ~= nil and not Env.has_10 then |
93 | | - return false, 'virt_text_repeat_linebreak', '0.10.0' |
94 | | - end |
95 | | - if opts.conceal_lines ~= nil and not Env.has_11 then |
96 | | - return false, 'conceal_lines', '0.11.0' |
97 | | - end |
98 | | - return true, '', '' |
99 | | -end |
100 | | - |
101 | | ----@private |
102 | | ----@param mark render.md.Mark |
103 | | -function Marks:update_context(mark) |
104 | | - local row, start_col = mark.start_row, mark.start_col |
105 | | - local end_col = mark.opts.end_col or start_col |
106 | | - if mark.opts.conceal ~= nil then |
107 | | - self.context.conceal:add(row, { |
108 | | - start_col = start_col, |
109 | | - end_col = end_col, |
110 | | - width = end_col - start_col, |
111 | | - character = mark.opts.conceal, |
112 | | - }) |
113 | | - end |
114 | | - if mark.opts.virt_text_pos == 'inline' then |
115 | | - local width = Str.line_width(mark.opts.virt_text) |
116 | | - self.context:add_offset(row, start_col, end_col, width) |
117 | | - end |
118 | | -end |
119 | | - |
120 | 1 | ---@class render.md.ListHelper |
121 | 2 | local M = {} |
122 | 3 |
|
123 | | -M.new_marks = Marks.new |
124 | | - |
125 | 4 | ---@generic T |
126 | 5 | ---@param values T[] |
127 | 6 | ---@param index integer |
|
0 commit comments