irongit

A Neovim plugin enabling you to solve LeetCode problems.

139 lines3.1 KBLua
1local Pad = require("leetcode-ui.lines.padding")
2local Lines = require("leetcode-ui.lines")
3local Opts = require("leetcode-ui.opts")
4local O = require("nui.object")
5local utils = require("leetcode-ui.utils")
6local log = require("leetcode.logger")
7
8---@alias lc.ui.Group.params { items: lc.ui.Lines[], opts: lc.ui.opts }
9
10---@class lc.ui.Group : lc.ui.Lines
11---@field _ lc.ui.Group.params | lines.params
12local Group = Lines:extend("LeetGroup")
13
14function Group:lines()
15 local tbl = {}
16 for _, item in ipairs(self:contents()) do
17 self:lines_helper(item, tbl)
18 end
19 return tbl
20end
21
22---@private
23---@param tbl table
24function Group:lines_helper(item, tbl) --
25 if item.class and item.class.name == "LeetLine" then
26 table.insert(tbl, item)
27 return
28 end
29
30 for _, c in ipairs(item:contents()) do
31 self:lines_helper(c, tbl)
32 end
33end
34
35function Group:contents()
36 local items = utils.shallowcopy(self._.items)
37
38 local contents = Group.super.contents(self)
39 if not vim.tbl_isempty(contents) then
40 table.insert(items, Lines(contents))
41 end
42
43 return items
44end
45
46---@param layout lc.ui.Renderer
47function Group:draw(layout, opts)
48 local options = Opts(self._.opts):merge(opts)
49
50 local padding = options:get_padding()
51 local spacing = options:get_spacing()
52
53 local toppad = padding and padding.top
54 local botpad = padding and padding.bot
55
56 if toppad then
57 Pad(toppad):draw(layout)
58 end
59
60 local items = self:contents()
61 for i, item in ipairs(items) do
62 item:draw(layout, options:get())
63 if i ~= #items and spacing then
64 Pad(spacing):draw(layout)
65 end
66 end
67
68 if botpad then
69 Pad(botpad):draw(layout)
70 end
71end
72
73---@param item lc.ui.Lines
74function Group:insert(item)
75 if not vim.tbl_isempty(Group.super.contents(self)) then
76 self:endgrp()
77 end
78 table.insert(self._.items, item)
79
80 return self
81end
82
83function Group:append(content, highlight)
84 if type(content) == "table" and O.is_instance(content, Group) then
85 local items = content:contents()
86
87 for _, item in ipairs(items) do
88 Group.super.append(self, item, highlight)
89 end
90 else
91 Group.super.append(self, content, highlight)
92 end
93
94 return self
95end
96
97function Group:endgrp()
98 local contents = Group.super.contents(self)
99
100 if not vim.tbl_isempty(contents) then
101 table.insert(self._.items, Lines(contents))
102 Group.super.clear(self)
103 else
104 Group.super.clear(self)
105 end
106
107 return self
108end
109
110function Group:clear()
111 Group.super.clear(self)
112 self._.items = {}
113
114 return self
115end
116
117function Group:replace(items)
118 self:clear()
119 self._.items = items
120
121 return self
122end
123
124--@param components lc-ui.Component[]
125---@param opts? lc.ui.opts
126---
127---@return lc.ui.Group
128function Group:init(items, opts) --
129 local options = vim.tbl_deep_extend("force", {}, opts or {})
130
131 Group.super.init(self, {}, options)
132
133 self._.items = items or {}
134end
135
136---@type fun(items?: table, opts?: lc.ui.opts): lc.ui.Group
137local LeetGroup = Group
138
139return LeetGroup