A Neovim plugin enabling you to solve LeetCode problems.
refactor: per lang injection and clean extra \n
4 files changed, +55 -24
+18-0README.md
| @@ -145,6 +145,8 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat | ||
| 145 | 145 | focus_testcases = "H", ---@type string |
| 146 | 146 | focus_result = "L", ---@type string |
| 147 | 147 | }, |
| 148 | + | |
| 149 | + injector = {} ---@type table<lc.lang, lc.inject> | |
| 148 | 150 | } |
| 149 | 151 | ``` |
| 150 | 152 | |
| @@ -212,6 +214,22 @@ hooks = { | ||
| 212 | 214 | }, |
| 213 | 215 | ``` |
| 214 | 216 | |
| 217 | +### injector | |
| 218 | + | |
| 219 | +Inject code before or after your soluation, injected code won't be submitted or run. | |
| 220 | + | |
| 221 | +```lua | |
| 222 | +injector = { | |
| 223 | + ["cpp"] = { | |
| 224 | + before = { "#include <bits/stdc++.h>", "using namespace std;" }, ---@type string|string[] | |
| 225 | + after = "int main(){}" ---@type string|string[] | |
| 226 | + }, | |
| 227 | + ["java"] = { | |
| 228 | + before = "import java.util.*;", ---@type string|string[] | |
| 229 | + } | |
| 230 | +} | |
| 231 | +``` | |
| 232 | + | |
| 215 | 233 | ### image support |
| 216 | 234 | |
| 217 | 235 | Whether to render question description images using [image.nvim] |
+18-0README.zh.md
| @@ -152,6 +152,8 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/aee6584c-e099-4409-b114-1 | ||
| 152 | 152 | focus_testcases = "H", ---@type string |
| 153 | 153 | focus_result = "L", ---@type string |
| 154 | 154 | }, |
| 155 | + | |
| 156 | + injector = {} ---@type table<lc.lang, lc.inject> | |
| 155 | 157 | } |
| 156 | 158 | ``` |
| 157 | 159 | |
| @@ -219,6 +221,22 @@ hooks = { | ||
| 219 | 221 | }, |
| 220 | 222 | ``` |
| 221 | 223 | |
| 224 | +### injector | |
| 225 | + | |
| 226 | +在你的答案前后注入额外代码,注入的代码不会被提交或测试。 | |
| 227 | + | |
| 228 | +```lua | |
| 229 | +injector = { | |
| 230 | + ["cpp"] = { | |
| 231 | + before = { "#include <bits/stdc++.h>", "using namespace std;" }, ---@type string|string[] | |
| 232 | + after = "int main(){}" ---@type string|string[] | |
| 233 | + }, | |
| 234 | + ["java"] = { | |
| 235 | + before = "import java.util.*;", ---@type string|string[] | |
| 236 | + } | |
| 237 | +} | |
| 238 | +``` | |
| 239 | + | |
| 222 | 240 | ### image support |
| 223 | 241 | |
| 224 | 242 | 是否使用 [image.nvim] 渲染问题描述中的图片 |
+16-20lua/leetcode-ui/question.lua
| @@ -19,35 +19,18 @@ 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 | - | |
| 39 | 22 | local snippets = self.q.code_snippets ~= vim.NIL and self.q.code_snippets or {} |
| 40 | 23 | local snip = vim.tbl_filter(function(snip) return snip.lang_slug == self.lang end, snippets)[1] |
| 41 | 24 | if not snip then return end |
| 42 | 25 | |
| 43 | 26 | local lang = utils.get_lang(self.lang) |
| 44 | 27 | snip.code = (snip.code or ""):gsub("\r\n", "\n") |
| 45 | - return ("%s\n%s @leet start\n%s\n%s @leet end\n%s"):format( | |
| 46 | - inject_before, | |
| 28 | + return ("%s%s @leet start\n%s\n%s @leet end%s"):format( | |
| 29 | + self:generate_inject_string(true), | |
| 47 | 30 | lang.comment, |
| 48 | 31 | snip.code, |
| 49 | 32 | lang.comment, |
| 50 | - inject_after | |
| 33 | + self:generate_inject_string(false) | |
| 51 | 34 | ) |
| 52 | 35 | end |
| 53 | 36 | |
| @@ -60,6 +43,19 @@ function Question:create_file() | ||
| 60 | 43 | if not self.file:exists() then self.file:write(self:get_snippet(), "w") end |
| 61 | 44 | end |
| 62 | 45 | |
| 46 | +---@private | |
| 47 | +---@param before boolean | |
| 48 | +function Question:generate_inject_string(before) | |
| 49 | + local injector = config.user.injector | |
| 50 | + if injector == nil or injector[self.lang] == nil then return "" end | |
| 51 | + local to_inject = before and injector[self.lang].before or injector[self.lang].after | |
| 52 | + if to_inject == nil or #to_inject == 0 then return "" end | |
| 53 | + | |
| 54 | + if type(to_inject) ~= "string" then to_inject = table.concat(to_inject, "\n") end | |
| 55 | + | |
| 56 | + return before and to_inject .. "\n" or "\n" .. to_inject | |
| 57 | +end | |
| 58 | + | |
| 63 | 59 | Question.unmount = vim.schedule_wrap(function(self) |
| 64 | 60 | self.info:unmount() |
| 65 | 61 | self.console:unmount() |
+3-4lua/leetcode/config/template.lua
| @@ -37,6 +37,8 @@ | ||
| 37 | 37 | |
| 38 | 38 | ---@alias lc.direction "col" | "row" |
| 39 | 39 | |
| 40 | +---@alias lc.inject { before?: string|string[], after?: string|string[] } | |
| 41 | + | |
| 40 | 42 | ---@class lc.UserConfig |
| 41 | 43 | local M = { |
| 42 | 44 | ---@type string |
| @@ -111,10 +113,7 @@ local M = { | ||
| 111 | 113 | focus_result = "L", ---@type string |
| 112 | 114 | }, |
| 113 | 115 | |
| 114 | - inject = { | |
| 115 | - before = "", ---@type string|string[] | |
| 116 | - after = "", ---@type string|string[] | |
| 117 | - } | |
| 116 | + injector = {} ---@type table<lc.lang, lc.inject> | |
| 118 | 117 | } |
| 119 | 118 | |
| 120 | 119 | return M |