A Neovim plugin enabling you to solve LeetCode problems.
| 1 | local Pad = require("leetcode-ui.lines.padding") |
| 2 | local Lines = require("leetcode-ui.lines") |
| 3 | local Opts = require("leetcode-ui.opts") |
| 4 | local O = require("nui.object") |
| 5 | local utils = require("leetcode-ui.utils") |
| 6 | local 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 |
| 12 | local Group = Lines:extend("LeetGroup") |
| 13 | |
| 14 | |
| 15 | local tbl = {} |
| 16 | for _, item in ipairs(self:contents()) do |
| 17 | self:lines_helper(item, tbl) |
| 18 | end |
| 19 | return tbl |
| 20 | end |
| 21 | |
| 22 | ---@private |
| 23 | ---@param tbl table |
| 24 | -- |
| 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 |
| 33 | end |
| 34 | |
| 35 | |
| 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 |
| 44 | end |
| 45 | |
| 46 | ---@param layout lc.ui.Renderer |
| 47 | |
| 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 |
| 71 | end |
| 72 | |
| 73 | ---@param item lc.ui.Lines |
| 74 | |
| 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 |
| 81 | end |
| 82 | |
| 83 | |
| 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 |
| 95 | end |
| 96 | |
| 97 | |
| 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 |
| 108 | end |
| 109 | |
| 110 | |
| 111 | Group.super.clear(self) |
| 112 | self._.items = {} |
| 113 | |
| 114 | return self |
| 115 | end |
| 116 | |
| 117 | |
| 118 | self:clear() |
| 119 | self._.items = items |
| 120 | |
| 121 | return self |
| 122 | end |
| 123 | |
| 124 | --@param components lc-ui.Component[] |
| 125 | ---@param opts? lc.ui.opts |
| 126 | --- |
| 127 | ---@return lc.ui.Group |
| 128 | -- |
| 129 | local options = vim.tbl_deep_extend("force", {}, opts or {}) |
| 130 | |
| 131 | Group.super.init(self, {}, options) |
| 132 | |
| 133 | self._.items = items or {} |
| 134 | end |
| 135 | |
| 136 | ---@type fun(items?: table, opts?: lc.ui.opts): lc.ui.Group |
| 137 | local LeetGroup = Group |
| 138 | |
| 139 | return LeetGroup |