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

Commit babfaaf

Browse files
authored
Merge pull request kawre#41 from Eric-Song-Nop/inject_code
feat: custom injection code snippets
2 parents 2a079ff + 16d3188 commit babfaaf

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

@@ -198,6 +200,22 @@ Whether to log [leetcode.nvim] status notifications
198200
logging = true
199201
```
200202

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+
201219
### hooks
202220

203221
List of functions that get executed on specified event
@@ -207,7 +225,7 @@ hooks = {
207225
---@type fun()[]
208226
LeetEnter = {},
209227

210-
---@type fun(question: { lang: string })[]
228+
---@type fun(question: lc.ui.Question)[]
211229
LeetQuestionNew = {},
212230
},
213231
```

README.zh.md

Lines changed: 19 additions & 1 deletion
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
},
@@ -205,6 +207,22 @@ directory = vim.fn.stdpath("data") .. "/leetcode/"
205207
logging = true
206208
```
207209

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+
208226
### hooks
209227

210228
在指定事件上执行的函数列表
@@ -214,7 +232,7 @@ hooks = {
214232
---@type fun()[]
215233
LeetEnter = {},
216234

217-
---@type fun(question: { lang: string })[]
235+
---@type fun(question: lc.ui.Question)[]
218236
LeetQuestionNew = {},
219237
},
220238
```

lua/leetcode-ui/question.lua

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ 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 @leet start\n%s\n%s @leet end"):format(lang.comment, snip.code, lang.comment)
28+
29+
return self:injector(
30+
("%s @leet start\n%s\n%s @leet end"):format(lang.comment, snip.code, lang.comment)
31+
)
2932
end
3033

3134
---@private
@@ -37,6 +40,37 @@ function Question:create_file()
3740
if not self.file:exists() then self.file:write(self:get_snippet(), "w") end
3841
end
3942

43+
---@private
44+
---@param code string
45+
function Question:injector(code)
46+
local injector = config.user.injector
47+
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
68+
69+
return norm_inject(inject.before, true) --
70+
.. code
71+
.. norm_inject(inject.after, false)
72+
end
73+
4074
Question.unmount = vim.schedule_wrap(function(self)
4175
self.info:unmount()
4276
self.console:unmount()

lua/leetcode/config/template.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -57,6 +59,8 @@ local M = {
5759
---@type boolean
5860
logging = true,
5961

62+
injector = {}, ---@type table<lc.lang, lc.inject>
63+
6064
cache = {
6165
update_interval = 60 * 60 * 24 * 7, ---@type integer 7 days
6266
},
@@ -98,9 +102,6 @@ local M = {
98102
LeetQuestionNew = {},
99103
},
100104

101-
---@type boolean
102-
image_support = false, -- setting this to `true` will disable question description wrap
103-
104105
keys = {
105106
toggle = { "q", "<Esc>" }, ---@type string|string[]
106107
confirm = { "<CR>" }, ---@type string|string[]
@@ -110,6 +111,9 @@ local M = {
110111
focus_testcases = "H", ---@type string
111112
focus_result = "L", ---@type string
112113
},
114+
115+
---@type boolean
116+
image_support = false, -- setting this to `true` will disable question description wrap
113117
}
114118

115119
return M

0 commit comments

Comments
 (0)