irongit

A Neovim plugin enabling you to solve LeetCode problems.

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