A Neovim plugin enabling you to solve LeetCode problems.
| 1 | local Line = require("leetcode-ui.line") |
| 2 | local Opts = require("leetcode-ui.opts") |
| 3 | local O = require("nui.object") |
| 4 | local utils = require("leetcode-ui.utils") |
| 5 | local 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 |
| 11 | local Lines = Line:extend("LeetLines") |
| 12 | |
| 13 | |
| 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 |
| 22 | end |
| 23 | |
| 24 | local |
| 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 |
| 32 | end |
| 33 | |
| 34 | -- |
| 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) |
| 49 | end |
| 50 | |
| 51 | ---@param layout lc.ui.Renderer |
| 52 | |
| 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 |
| 74 | end |
| 75 | |
| 76 | |
| 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 |
| 91 | end |
| 92 | |
| 93 | |
| 94 | Lines.super.clear(self) |
| 95 | self._.lines = {} |
| 96 | |
| 97 | return self |
| 98 | end |
| 99 | |
| 100 | -- |
| 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 |
| 107 | end |
| 108 | |
| 109 | |
| 110 | self:clear() |
| 111 | self._.lines = lines |
| 112 | |
| 113 | return self |
| 114 | end |
| 115 | |
| 116 | |
| 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 |
| 126 | end |
| 127 | |
| 128 | |
| 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 {} |
| 135 | end |
| 136 | |
| 137 | ---@type fun(lines?: lc.ui.Line[], opts?: lc.ui.opts): lc.ui.Lines |
| 138 | local LeetLines = Lines |
| 139 | |
| 140 | return LeetLines |