A Neovim plugin enabling you to solve LeetCode problems.
feat: custom injection code snippets
2 files changed, +29 -1
+24-1lua/leetcode-ui/question.lua
| @@ -19,13 +19,36 @@ local log = require("leetcode.logger") | ||
| 19 | 19 | local Question = Object("LeetQuestion") |
| 20 | 20 | |
| 21 | 21 | function Question:get_snippet() |
| 22 | + local inject = config.user.inject | |
| 23 | + -- If inject.start is a string, assign to inject_start | |
| 24 | + -- else if inject.start is a string[], join into inject_start with "\n" as separator | |
| 25 | + local inject_before = "" | |
| 26 | + local inject_after = "" | |
| 27 | + | |
| 28 | + if type(inject.before) == "string" then | |
| 29 | + inject_before = inject.before | |
| 30 | + else | |
| 31 | + inject_before = table.concat(inject.before, "\n") | |
| 32 | + end | |
| 33 | + if type(inject.after) == "string" then | |
| 34 | + inject_after = inject.after | |
| 35 | + else | |
| 36 | + inject_after = table.concat(inject.after, "\n") | |
| 37 | + end | |
| 38 | + | |
| 22 | 39 | local snippets = self.q.code_snippets ~= vim.NIL and self.q.code_snippets or {} |
| 23 | 40 | local snip = vim.tbl_filter(function(snip) return snip.lang_slug == self.lang end, snippets)[1] |
| 24 | 41 | if not snip then return end |
| 25 | 42 | |
| 26 | 43 | local lang = utils.get_lang(self.lang) |
| 27 | 44 | snip.code = (snip.code or ""):gsub("\r\n", "\n") |
| 28 | - return ("%s @leet start\n%s\n%s @leet end"):format(lang.comment, snip.code, lang.comment) | |
| 45 | + return ("%s\n%s @leet start\n%s\n%s @leet end\n%s"):format( | |
| 46 | + inject_before, | |
| 47 | + lang.comment, | |
| 48 | + snip.code, | |
| 49 | + lang.comment, | |
| 50 | + inject_after | |
| 51 | + ) | |
| 29 | 52 | end |
| 30 | 53 | |
| 31 | 54 | ---@private |
+5-0lua/leetcode/config/template.lua
| @@ -110,6 +110,11 @@ local M = { | ||
| 110 | 110 | focus_testcases = "H", ---@type string |
| 111 | 111 | focus_result = "L", ---@type string |
| 112 | 112 | }, |
| 113 | + | |
| 114 | + inject = { | |
| 115 | + before = "", ---@type string|string[] | |
| 116 | + after = "", ---@type string|string[] | |
| 117 | + } | |
| 113 | 118 | } |
| 114 | 119 | |
| 115 | 120 | return M |