Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 16d3188

Browse files
committed
refactor: injector function
1 parent bd4b8ee commit 16d3188

File tree

4 files changed

+68
-53
lines changed

4 files changed

+68
-53
lines changed

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,22 @@ Whether to log [leetcode.nvim] status notifications
200200
logging = true
201201
```
202202

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+
203219
### hooks
204220

205221
List of functions that get executed on specified event
@@ -209,27 +225,11 @@ hooks = {
209225
---@type fun()[]
210226
LeetEnter = {},
211227

212-
---@type fun(question: { lang: string })[]
228+
---@type fun(question: lc.ui.Question)[]
213229
LeetQuestionNew = {},
214230
},
215231
```
216232

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-
233233
### image support
234234

235235
Whether to render question description images using [image.nvim]

README.zh.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/aee6584c-e099-4409-b114-1
9999
---@type boolean
100100
logging = true,
101101

102+
injector = {} ---@type table<lc.lang, lc.inject>
103+
102104
cache = {
103105
update_interval = 60 * 60 * 24 * 7, ---@type integer 7 days
104106
},
@@ -152,8 +154,6 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/aee6584c-e099-4409-b114-1
152154
focus_testcases = "H", ---@type string
153155
focus_result = "L", ---@type string
154156
},
155-
156-
injector = {} ---@type table<lc.lang, lc.inject>
157157
}
158158
```
159159

@@ -207,6 +207,22 @@ directory = vim.fn.stdpath("data") .. "/leetcode/"
207207
logging = true
208208
```
209209

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+
210226
### hooks
211227

212228
在指定事件上执行的函数列表
@@ -216,27 +232,11 @@ hooks = {
216232
---@type fun()[]
217233
LeetEnter = {},
218234

219-
---@type fun(question: { lang: string })[]
235+
---@type fun(question: lc.ui.Question)[]
220236
LeetQuestionNew = {},
221237
},
222238
```
223239

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-
240240
### image support
241241

242242
是否使用 [image.nvim] 渲染问题描述中的图片

lua/leetcode-ui/question.lua

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,9 @@ function Question:get_snippet()
2525

2626
local lang = utils.get_lang(self.lang)
2727
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)
3431
)
3532
end
3633

@@ -44,16 +41,34 @@ function Question:create_file()
4441
end
4542

4643
---@private
47-
---@param before boolean
48-
function Question:generate_inject_string(before)
44+
---@param code string
45+
function Question:injector(code)
4946
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
5347

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
5568

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)
5772
end
5873

5974
Question.unmount = vim.schedule_wrap(function(self)

lua/leetcode/config/template.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ local M = {
5959
---@type boolean
6060
logging = true,
6161

62+
injector = {}, ---@type table<lc.lang, lc.inject>
63+
6264
cache = {
6365
update_interval = 60 * 60 * 24 * 7, ---@type integer 7 days
6466
},
@@ -100,9 +102,6 @@ local M = {
100102
LeetQuestionNew = {},
101103
},
102104

103-
---@type boolean
104-
image_support = false, -- setting this to `true` will disable question description wrap
105-
106105
keys = {
107106
toggle = { "q", "<Esc>" }, ---@type string|string[]
108107
confirm = { "<CR>" }, ---@type string|string[]
@@ -113,7 +112,8 @@ local M = {
113112
focus_result = "L", ---@type string
114113
},
115114

116-
injector = {} ---@type table<lc.lang, lc.inject>
115+
---@type boolean
116+
image_support = false, -- setting this to `true` will disable question description wrap
117117
}
118118

119119
return M

0 commit comments

Comments
 (0)