|
| 1 | +local Base = require('render-markdown.render.base') |
| 2 | +local list = require('render-markdown.core.list') |
| 3 | +local str = require('render-markdown.core.str') |
| 4 | + |
| 5 | +---@class render.md.render.ListMarker: render.md.Renderer |
| 6 | +---@field private bullet render.md.Bullet |
| 7 | +---@field private leading_spaces integer |
| 8 | +local Render = setmetatable({}, Base) |
| 9 | +Render.__index = Render |
| 10 | + |
| 11 | +---@param marks render.md.Marks |
| 12 | +---@param config render.md.buffer.Config |
| 13 | +---@param context render.md.Context |
| 14 | +---@param info render.md.NodeInfo |
| 15 | +---@return render.md.Renderer |
| 16 | +function Render:new(marks, config, context, info) |
| 17 | + return Base.new(self, marks, config, context, info) |
| 18 | +end |
| 19 | + |
| 20 | +---@return boolean |
| 21 | +function Render:setup() |
| 22 | + self.bullet = self.config.bullet |
| 23 | + |
| 24 | + -- List markers from tree-sitter should have leading spaces removed, however there are edge |
| 25 | + -- cases in the parser: https://github.com/tree-sitter-grammars/tree-sitter-markdown/issues/127 |
| 26 | + -- As a result we account for leading spaces here, can remove if this gets fixed upstream |
| 27 | + self.leading_spaces = str.spaces('start', self.info.text) |
| 28 | + |
| 29 | + return true |
| 30 | +end |
| 31 | + |
| 32 | +function Render:render() |
| 33 | + if self:sibling_checkbox() then |
| 34 | + -- Hide the list marker for checkboxes rather than replacing with a bullet point |
| 35 | + self:hide_marker() |
| 36 | + else |
| 37 | + if not self.bullet.enabled then |
| 38 | + return |
| 39 | + end |
| 40 | + local level, root_list = self.info:level_in_section('list') |
| 41 | + self:icon(level) |
| 42 | + self:padding(root_list) |
| 43 | + end |
| 44 | +end |
| 45 | + |
| 46 | +---@private |
| 47 | +---@return boolean |
| 48 | +function Render:sibling_checkbox() |
| 49 | + if not self.config.checkbox.enabled then |
| 50 | + return false |
| 51 | + end |
| 52 | + if self.context:get_component(self.info) ~= nil then |
| 53 | + return true |
| 54 | + end |
| 55 | + if self.info:sibling('task_list_marker_unchecked') ~= nil then |
| 56 | + return true |
| 57 | + end |
| 58 | + if self.info:sibling('task_list_marker_checked') ~= nil then |
| 59 | + return true |
| 60 | + end |
| 61 | + return false |
| 62 | +end |
| 63 | + |
| 64 | +---@private |
| 65 | +function Render:hide_marker() |
| 66 | + self.marks:add(true, self.info.start_row, self.info.start_col + self.leading_spaces, { |
| 67 | + end_row = self.info.end_row, |
| 68 | + end_col = self.info.end_col, |
| 69 | + conceal = '', |
| 70 | + }) |
| 71 | +end |
| 72 | + |
| 73 | +---@private |
| 74 | +---@param level integer |
| 75 | +function Render:icon(level) |
| 76 | + local icon = list.cycle(self.bullet.icons, level) |
| 77 | + if icon == nil then |
| 78 | + return |
| 79 | + end |
| 80 | + self.marks:add(true, self.info.start_row, self.info.start_col, { |
| 81 | + end_row = self.info.end_row, |
| 82 | + end_col = self.info.end_col, |
| 83 | + virt_text = { { str.pad(self.leading_spaces) .. icon, self.bullet.highlight } }, |
| 84 | + virt_text_pos = 'overlay', |
| 85 | + }) |
| 86 | +end |
| 87 | + |
| 88 | +---@private |
| 89 | +---@param root_list? render.md.NodeInfo |
| 90 | +function Render:padding(root_list) |
| 91 | + if self.bullet.left_pad <= 0 and self.bullet.right_pad <= 0 then |
| 92 | + return |
| 93 | + end |
| 94 | + local list_item = self.info:parent('list_item') |
| 95 | + if list_item == nil then |
| 96 | + return |
| 97 | + end |
| 98 | + local left_col = root_list ~= nil and root_list.start_col or list_item.start_col |
| 99 | + |
| 100 | + local next_list = list_item:child('list') |
| 101 | + local end_row = next_list ~= nil and next_list.start_row or list_item.end_row |
| 102 | + |
| 103 | + for row = list_item.start_row, end_row - 1 do |
| 104 | + local right_col = row == list_item.start_row and self.info.end_col - 1 or left_col |
| 105 | + self:padding_mark(row, left_col, 0, self.bullet.left_pad) |
| 106 | + self:padding_mark(row, right_col, nil, self.bullet.right_pad) |
| 107 | + end |
| 108 | +end |
| 109 | + |
| 110 | +---@private |
| 111 | +---@param row integer |
| 112 | +---@param col integer |
| 113 | +---@param priority? integer |
| 114 | +---@param amount integer |
| 115 | +function Render:padding_mark(row, col, priority, amount) |
| 116 | + if amount > 0 then |
| 117 | + self.marks:add(false, row, col, { |
| 118 | + priority = priority, |
| 119 | + virt_text = { { str.pad(amount), self.config.padding.highlight } }, |
| 120 | + virt_text_pos = 'inline', |
| 121 | + }) |
| 122 | + end |
| 123 | +end |
| 124 | + |
| 125 | +return Render |
0 commit comments