irongit

A Neovim plugin enabling you to solve LeetCode problems.

refactor: per lang injection and clean extra \n

Yifan Songauthored
parent 93e5485commit bd4b8ee93dee8880b5ac6769b3b4a33d3a9e6986Browse files

4 files changed, +55 -24

+18-0README.md
@@ -145,6 +145,8 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat
145145 focus_testcases = "H", ---@type string
146146 focus_result = "L", ---@type string
147147 },
148+
149+ injector = {} ---@type table<lc.lang, lc.inject>
148150 }
149151 ```
150152
@@ -212,6 +214,22 @@ hooks = {
212214 },
213215 ```
214216
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+
215233 ### image support
216234
217235 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
152152 focus_testcases = "H", ---@type string
153153 focus_result = "L", ---@type string
154154 },
155+
156+ injector = {} ---@type table<lc.lang, lc.inject>
155157 }
156158 ```
157159
@@ -219,6 +221,22 @@ hooks = {
219221 },
220222 ```
221223
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+
222240 ### image support
223241
224242 是否使用 [image.nvim] 渲染问题描述中的图片
+16-20lua/leetcode-ui/question.lua
@@ -19,35 +19,18 @@ local log = require("leetcode.logger")
1919 local Question = Object("LeetQuestion")
2020
2121 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-
3922 local snippets = self.q.code_snippets ~= vim.NIL and self.q.code_snippets or {}
4023 local snip = vim.tbl_filter(function(snip) return snip.lang_slug == self.lang end, snippets)[1]
4124 if not snip then return end
4225
4326 local lang = utils.get_lang(self.lang)
4427 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),
4730 lang.comment,
4831 snip.code,
4932 lang.comment,
50- inject_after
33+ self:generate_inject_string(false)
5134 )
5235 end
5336
@@ -60,6 +43,19 @@ function Question:create_file()
6043 if not self.file:exists() then self.file:write(self:get_snippet(), "w") end
6144 end
6245
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+
6359 Question.unmount = vim.schedule_wrap(function(self)
6460 self.info:unmount()
6561 self.console:unmount()
+3-4lua/leetcode/config/template.lua
@@ -37,6 +37,8 @@
3737
3838 ---@alias lc.direction "col" | "row"
3939
40+---@alias lc.inject { before?: string|string[], after?: string|string[] }
41+
4042 ---@class lc.UserConfig
4143 local M = {
4244 ---@type string
@@ -111,10 +113,7 @@ local M = {
111113 focus_result = "L", ---@type string
112114 },
113115
114- inject = {
115- before = "", ---@type string|string[]
116- after = "", ---@type string|string[]
117- }
116+ injector = {} ---@type table<lc.lang, lc.inject>
118117 }
119118
120119 return M