A Neovim plugin enabling you to solve LeetCode problems.
refactor: `injector` function
4 files changed, +68 -53
+17-17README.md
| @@ -200,6 +200,22 @@ Whether to log [leetcode.nvim] status notifications | ||
| 200 | 200 | logging = true |
| 201 | 201 | ``` |
| 202 | 202 | |
| 203 | +### injector | |
| 204 | + | |
| 205 | +Inject code before or after your solution, injected code won't be submitted or run. | |
| 206 | + | |
| 207 | +```lua | |
| 208 | +injector = { ---@type table<lc.lang, lc.inject> | |
| 209 | + ["cpp"] = { | |
| 210 | + before = { "#include <bits/stdc++.h>", "using namespace std;" }, | |
| 211 | + after = "int main() {}", | |
| 212 | + }, | |
| 213 | + ["java"] = { | |
| 214 | + before = "import java.util.*;", | |
| 215 | + }, | |
| 216 | +} | |
| 217 | +``` | |
| 218 | + | |
| 203 | 219 | ### hooks |
| 204 | 220 | |
| 205 | 221 | List of functions that get executed on specified event |
| @@ -209,27 +225,11 @@ hooks = { | ||
| 209 | 225 | ---@type fun()[] |
| 210 | 226 | LeetEnter = {}, |
| 211 | 227 | |
| 212 | - ---@type fun(question: { lang: string })[] | |
| 228 | + ---@type fun(question: lc.ui.Question)[] | |
| 213 | 229 | LeetQuestionNew = {}, |
| 214 | 230 | }, |
| 215 | 231 | ``` |
| 216 | 232 | |
| 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 | - | |
| 233 | 233 | ### image support |
| 234 | 234 | |
| 235 | 235 | Whether to render question description images using [image.nvim] |
+19-19README.zh.md
| @@ -99,6 +99,8 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/aee6584c-e099-4409-b114-1 | ||
| 99 | 99 | ---@type boolean |
| 100 | 100 | logging = true, |
| 101 | 101 | |
| 102 | + injector = {} ---@type table<lc.lang, lc.inject> | |
| 103 | + | |
| 102 | 104 | cache = { |
| 103 | 105 | update_interval = 60 * 60 * 24 * 7, ---@type integer 7 days |
| 104 | 106 | }, |
| @@ -152,8 +154,6 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/aee6584c-e099-4409-b114-1 | ||
| 152 | 154 | focus_testcases = "H", ---@type string |
| 153 | 155 | focus_result = "L", ---@type string |
| 154 | 156 | }, |
| 155 | - | |
| 156 | - injector = {} ---@type table<lc.lang, lc.inject> | |
| 157 | 157 | } |
| 158 | 158 | ``` |
| 159 | 159 | |
| @@ -207,6 +207,22 @@ directory = vim.fn.stdpath("data") .. "/leetcode/" | ||
| 207 | 207 | logging = true |
| 208 | 208 | ``` |
| 209 | 209 | |
| 210 | +### injector | |
| 211 | + | |
| 212 | +在你的答案前后注入额外代码,注入的代码不会被提交或测试。 | |
| 213 | + | |
| 214 | +```lua | |
| 215 | +injector = { ---@type table<lc.lang, lc.inject> | |
| 216 | + ["cpp"] = { | |
| 217 | + before = { "#include <bits/stdc++.h>", "using namespace std;" }, | |
| 218 | + after = "int main() {}", | |
| 219 | + }, | |
| 220 | + ["java"] = { | |
| 221 | + before = "import java.util.*;", | |
| 222 | + }, | |
| 223 | +} | |
| 224 | +``` | |
| 225 | + | |
| 210 | 226 | ### hooks |
| 211 | 227 | |
| 212 | 228 | 在指定事件上执行的函数列表 |
| @@ -216,27 +232,11 @@ hooks = { | ||
| 216 | 232 | ---@type fun()[] |
| 217 | 233 | LeetEnter = {}, |
| 218 | 234 | |
| 219 | - ---@type fun(question: { lang: string })[] | |
| 235 | + ---@type fun(question: lc.ui.Question)[] | |
| 220 | 236 | LeetQuestionNew = {}, |
| 221 | 237 | }, |
| 222 | 238 | ``` |
| 223 | 239 | |
| 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 | - | |
| 240 | 240 | ### image support |
| 241 | 241 | |
| 242 | 242 | 是否使用 [image.nvim] 渲染问题描述中的图片 |
+28-13lua/leetcode-ui/question.lua
| @@ -25,12 +25,9 @@ function Question:get_snippet() | ||
| 25 | 25 | |
| 26 | 26 | local lang = utils.get_lang(self.lang) |
| 27 | 27 | snip.code = (snip.code or ""):gsub("\r\n", "\n") |
| 28 | - return ("%s%s @leet start\n%s\n%s @leet end%s"):format( | |
| 29 | - self:generate_inject_string(true), | |
| 30 | - lang.comment, | |
| 31 | - snip.code, | |
| 32 | - lang.comment, | |
| 33 | - self:generate_inject_string(false) | |
| 28 | + | |
| 29 | + return self:injector( | |
| 30 | + ("%s @leet start\n%s\n%s @leet end"):format(lang.comment, snip.code, lang.comment) | |
| 34 | 31 | ) |
| 35 | 32 | end |
| 36 | 33 | |
| @@ -44,16 +41,34 @@ function Question:create_file() | ||
| 44 | 41 | end |
| 45 | 42 | |
| 46 | 43 | ---@private |
| 47 | ----@param before boolean | |
| 48 | -function Question:generate_inject_string(before) | |
| 44 | +---@param code string | |
| 45 | +function Question:injector(code) | |
| 49 | 46 | 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 | 47 | |
| 54 | - if type(to_inject) ~= "string" then to_inject = table.concat(to_inject, "\n") end | |
| 48 | + local inject = injector[self.lang] | |
| 49 | + if not inject or vim.tbl_isempty(inject) then return code end | |
| 50 | + | |
| 51 | + ---@param inj? string|string[] | |
| 52 | + ---@param before boolean | |
| 53 | + local function norm_inject(inj, before) | |
| 54 | + local res | |
| 55 | + | |
| 56 | + if type(inj) == "table" then | |
| 57 | + res = table.concat(inj, "\n") | |
| 58 | + elseif type(inj) == "string" then | |
| 59 | + res = inj | |
| 60 | + end | |
| 61 | + | |
| 62 | + if res and res ~= "" then | |
| 63 | + return before and (res .. "\n\n") or ("\n\n" .. res) | |
| 64 | + else | |
| 65 | + return "" | |
| 66 | + end | |
| 67 | + end | |
| 55 | 68 | |
| 56 | - return before and to_inject .. "\n" or "\n" .. to_inject | |
| 69 | + return norm_inject(inject.before, true) -- | |
| 70 | + .. code | |
| 71 | + .. norm_inject(inject.after, false) | |
| 57 | 72 | end |
| 58 | 73 | |
| 59 | 74 | Question.unmount = vim.schedule_wrap(function(self) |
+4-4lua/leetcode/config/template.lua
| @@ -59,6 +59,8 @@ local M = { | ||
| 59 | 59 | ---@type boolean |
| 60 | 60 | logging = true, |
| 61 | 61 | |
| 62 | + injector = {}, ---@type table<lc.lang, lc.inject> | |
| 63 | + | |
| 62 | 64 | cache = { |
| 63 | 65 | update_interval = 60 * 60 * 24 * 7, ---@type integer 7 days |
| 64 | 66 | }, |
| @@ -100,9 +102,6 @@ local M = { | ||
| 100 | 102 | LeetQuestionNew = {}, |
| 101 | 103 | }, |
| 102 | 104 | |
| 103 | - ---@type boolean | |
| 104 | - image_support = false, -- setting this to `true` will disable question description wrap | |
| 105 | - | |
| 106 | 105 | keys = { |
| 107 | 106 | toggle = { "q", "<Esc>" }, ---@type string|string[] |
| 108 | 107 | confirm = { "<CR>" }, ---@type string|string[] |
| @@ -113,7 +112,8 @@ local M = { | ||
| 113 | 112 | focus_result = "L", ---@type string |
| 114 | 113 | }, |
| 115 | 114 | |
| 116 | - injector = {} ---@type table<lc.lang, lc.inject> | |
| 115 | + ---@type boolean | |
| 116 | + image_support = false, -- setting this to `true` will disable question description wrap | |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | return M |