A Neovim plugin enabling you to solve LeetCode problems.
| 1 | local utils = require("leetcode.utils") |
| 2 | |
| 3 | local Pre = require("leetcode-ui.group.pre") |
| 4 | local Input = require("leetcode-ui.group.pre.input") |
| 5 | local Stdout = require("leetcode-ui.group.pre.stdout") |
| 6 | local Group = require("leetcode-ui.group") |
| 7 | local Line = require("leetcode-ui.line") |
| 8 | |
| 9 | local t = require("leetcode.translator") |
| 10 | local log = require("leetcode.logger") |
| 11 | |
| 12 | ---@alias case_body { input: string[], raw_input: string, output: string, expected: string, std_output: string } |
| 13 | |
| 14 | ---@class lc.ui.Case : lc.ui.Group |
| 15 | ---@field pre lc.ui.Lines |
| 16 | ---@field stdout lc.ui.Lines |
| 17 | ---@field passed boolean |
| 18 | ---@field index integer |
| 19 | ---@field body case_body |
| 20 | ---@field question lc.ui.Question |
| 21 | local Case = Group:extend("LeetCase") |
| 22 | |
| 23 | ---@private |
| 24 | ---@param input string[] |
| 25 | |
| 26 | input = vim.tbl_map(utils.norm_ins, input) |
| 27 | return Input("Input", input, self.question.q.meta_data.params) |
| 28 | end |
| 29 | |
| 30 | ---@private |
| 31 | ---@param output string |
| 32 | ---@param expected string |
| 33 | |
| 34 | local key = t("Output") |
| 35 | |
| 36 | local title = Line():append(key, "leetcode_normal") |
| 37 | local pre = Pre(title, Line():append(utils.norm_ins(output))) |
| 38 | |
| 39 | return pre |
| 40 | end |
| 41 | |
| 42 | ---@private |
| 43 | ---@param expected string |
| 44 | ---@param output string |
| 45 | |
| 46 | local key = t("Expected") |
| 47 | local title = Line():append(key, "leetcode_normal") |
| 48 | |
| 49 | local pre = Pre(title, Line():append(utils.norm_ins(expected))) |
| 50 | |
| 51 | return pre |
| 52 | end |
| 53 | |
| 54 | ---@param body case_body |
| 55 | ---@param passed boolean |
| 56 | --- |
| 57 | ---@return lc.ui.Case |
| 58 | |
| 59 | Case.super.init(self, {}, { spacing = 1 }) |
| 60 | |
| 61 | self.body = body |
| 62 | self.question = question |
| 63 | |
| 64 | self:insert(self:input(body.input)) |
| 65 | self:insert(self:output(body.output, body.expected)) |
| 66 | self:insert(self:expected(body.expected, body.output)) |
| 67 | |
| 68 | local stdout = Stdout(body.std_output) |
| 69 | self:insert(stdout) |
| 70 | |
| 71 | self.passed = passed |
| 72 | end |
| 73 | |
| 74 | ---@alias lc.Result.Case.constructor fun(body: case_body, passed: boolean, question: lc.ui.Question): lc.ui.Case |
| 75 | ---@type lc.Result.Case.constructor |
| 76 | local LeetCase = Case |
| 77 | |
| 78 | return LeetCase |