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

Commit 910bff9

Browse files
committed
feat: @leet tags
1 parent 3a2e852 commit 910bff9

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

lua/leetcode-ui/question.lua

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ function Question:inject(before)
5858

5959
local res
6060

61+
if type(inj) == "boolean" and inj == true and before then --
62+
inj = config.imports[self.lang]
63+
end
64+
6165
if type(inj) == "table" then
6266
res = table.concat(inj, "\n")
6367
elseif type(inj) == "string" then
@@ -129,7 +133,7 @@ function Question:handle_mount()
129133
self.console = Console(self)
130134
self.info = Info(self)
131135

132-
utils.exec_hooks("LeetQuestionNew", self)
136+
utils.exec_hook("LeetQuestionNew", self)
133137

134138
return self
135139
end
@@ -163,7 +167,7 @@ end
163167
---@return integer, integer, string[]
164168
function Question:range(inclusive)
165169
local lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false)
166-
local start_i, end_i = 1, #lines
170+
local start_i, end_i
167171

168172
for i, line in ipairs(lines) do
169173
if line:match("@leet start") then
@@ -180,6 +184,10 @@ end
180184
---@return string
181185
function Question:lines(submit)
182186
local start_i, end_i, lines = self:range()
187+
188+
start_i = start_i or 1
189+
end_i = end_i or #lines
190+
183191
local prefix = not submit and ("\n"):rep(start_i - 1) or ""
184192
return prefix .. table.concat(lines, "\n", start_i, end_i)
185193
end
@@ -201,7 +209,7 @@ Question.change_lang = vim.schedule_wrap(function(self, lang)
201209

202210
self.bufnr = new_bufnr
203211
if bufloaded == 0 then --
204-
utils.exec_hooks("LeetQuestionNew", self)
212+
utils.exec_hook("LeetQuestionNew", self)
205213
end
206214
else
207215
log.error("Changing language failed")

lua/leetcode.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function leetcode.start(on_vimenter)
5858
Menu():mount()
5959

6060
local utils = require("leetcode.utils")
61-
utils.exec_hooks("LeetEnter")
61+
utils.exec_hook("LeetEnter")
6262

6363
return true
6464
end

lua/leetcode/command/init.lua

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ function cmd.yank()
178178
api.nvim_set_current_win(q.winid)
179179
api.nvim_set_current_buf(q.bufnr)
180180

181-
local start_i, end_i = q:range()
182-
vim.cmd(("%d,%dyank"):format(start_i, end_i))
181+
local start_i, end_i, lines = q:range()
182+
vim.cmd(("%d,%dyank"):format(start_i or 1, end_i or #lines))
183183
end
184184
end
185185

@@ -341,17 +341,30 @@ function cmd.inject()
341341
local q = utils.curr_question()
342342
if not q then return end
343343

344-
local start_i, end_i = q:range(true)
345-
346344
if vim.api.nvim_buf_is_valid(q.bufnr) then
347-
local before = q:inject(true)
348-
if before then
349-
vim.api.nvim_buf_set_lines(q.bufnr, 0, start_i - 1, false, vim.split(before, "\n"))
345+
local start_i, end_i = q:range(true)
346+
347+
if start_i == nil and end_i == nil then
348+
log.error("`@leet start` and `@leet end` not found")
349+
return
350350
end
351351

352-
local after = q:inject(false)
353-
if after then
354-
vim.api.nvim_buf_set_lines(q.bufnr, end_i + 1, -1, false, vim.split(after, "\n"))
352+
if start_i == nil then
353+
log.error("`@leet start` not found")
354+
else
355+
local before = q:inject(true)
356+
if before then
357+
vim.api.nvim_buf_set_lines(q.bufnr, 0, start_i - 1, false, vim.split(before, "\n"))
358+
end
359+
end
360+
361+
if end_i == nil then
362+
log.error("`@leet start` not found")
363+
else
364+
local after = q:inject(false)
365+
if after then
366+
vim.api.nvim_buf_set_lines(q.bufnr, end_i + 1, -1, false, vim.split(after, "\n"))
367+
end
355368
end
356369
end
357370
end

lua/leetcode/utils.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function utils.get_lang_by_name(name)
7272
end
7373

7474
---@param event lc.hook
75-
function utils.exec_hooks(event, ...)
75+
function utils.exec_hook(event, ...)
7676
local fns = config.user.hooks[event]
7777
if not fns then log.error("unknown hook event: " .. event) end
7878

0 commit comments

Comments
 (0)