Skip to content

Commit

Permalink
feat: @leet tags
Browse files Browse the repository at this point in the history
  • Loading branch information
kawre committed Feb 24, 2024
1 parent 3a2e852 commit 910bff9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
14 changes: 11 additions & 3 deletions lua/leetcode-ui/question.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ function Question:inject(before)

local res

if type(inj) == "boolean" and inj == true and before then --
inj = config.imports[self.lang]
end

if type(inj) == "table" then
res = table.concat(inj, "\n")
elseif type(inj) == "string" then
Expand Down Expand Up @@ -129,7 +133,7 @@ function Question:handle_mount()
self.console = Console(self)
self.info = Info(self)

utils.exec_hooks("LeetQuestionNew", self)
utils.exec_hook("LeetQuestionNew", self)

return self
end
Expand Down Expand Up @@ -163,7 +167,7 @@ end
---@return integer, integer, string[]
function Question:range(inclusive)
local lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false)
local start_i, end_i = 1, #lines
local start_i, end_i

for i, line in ipairs(lines) do
if line:match("@leet start") then
Expand All @@ -180,6 +184,10 @@ end
---@return string
function Question:lines(submit)
local start_i, end_i, lines = self:range()

start_i = start_i or 1
end_i = end_i or #lines

local prefix = not submit and ("\n"):rep(start_i - 1) or ""
return prefix .. table.concat(lines, "\n", start_i, end_i)
end
Expand All @@ -201,7 +209,7 @@ Question.change_lang = vim.schedule_wrap(function(self, lang)

self.bufnr = new_bufnr
if bufloaded == 0 then --
utils.exec_hooks("LeetQuestionNew", self)
utils.exec_hook("LeetQuestionNew", self)
end
else
log.error("Changing language failed")
Expand Down
2 changes: 1 addition & 1 deletion lua/leetcode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function leetcode.start(on_vimenter)
Menu():mount()

local utils = require("leetcode.utils")
utils.exec_hooks("LeetEnter")
utils.exec_hook("LeetEnter")

return true
end
Expand Down
33 changes: 23 additions & 10 deletions lua/leetcode/command/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ function cmd.yank()
api.nvim_set_current_win(q.winid)
api.nvim_set_current_buf(q.bufnr)

local start_i, end_i = q:range()
vim.cmd(("%d,%dyank"):format(start_i, end_i))
local start_i, end_i, lines = q:range()
vim.cmd(("%d,%dyank"):format(start_i or 1, end_i or #lines))
end
end

Expand Down Expand Up @@ -341,17 +341,30 @@ function cmd.inject()
local q = utils.curr_question()
if not q then return end

local start_i, end_i = q:range(true)

if vim.api.nvim_buf_is_valid(q.bufnr) then
local before = q:inject(true)
if before then
vim.api.nvim_buf_set_lines(q.bufnr, 0, start_i - 1, false, vim.split(before, "\n"))
local start_i, end_i = q:range(true)

if start_i == nil and end_i == nil then
log.error("`@leet start` and `@leet end` not found")
return
end

local after = q:inject(false)
if after then
vim.api.nvim_buf_set_lines(q.bufnr, end_i + 1, -1, false, vim.split(after, "\n"))
if start_i == nil then
log.error("`@leet start` not found")
else
local before = q:inject(true)
if before then
vim.api.nvim_buf_set_lines(q.bufnr, 0, start_i - 1, false, vim.split(before, "\n"))
end
end

if end_i == nil then
log.error("`@leet start` not found")
else
local after = q:inject(false)
if after then
vim.api.nvim_buf_set_lines(q.bufnr, end_i + 1, -1, false, vim.split(after, "\n"))
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lua/leetcode/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function utils.get_lang_by_name(name)
end

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

Expand Down

0 comments on commit 910bff9

Please sign in to comment.