Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support function type injector #154

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lua/leetcode-ui/question.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,19 @@ function Question:open_buffer(existed)
ui_utils.buf_set_opts(self.bufnr, { buflisted = true })
ui_utils.win_set_buf(self.winid, self.bufnr, true)

local i = self:fold_range()
if i then
pcall(vim.cmd, ("%d,%dfold"):format(1, i))
end
-- local i = self:fold_range()
-- if i then
-- pcall(vim.cmd, ("%d,%dfold"):format(1, i))
-- end

if existed and self.cache.status == "ac" then
self:reset_lines()
end
end

---@param before boolean
function Question:inject(before)
---@param code string
function Question:inject(before, code)
local inject = config.user.injector[self.lang] or {}
local inj = before and inject.before or inject.after

Expand All @@ -120,6 +121,10 @@ function Question:inject(before)
inj = config.imports[self.lang]
end

if type(inj) == "function" then
inj = inj(code)
end

if type(inj) == "table" then
res = table.concat(inj, "\n")
elseif type(inj) == "string" then
Expand All @@ -143,12 +148,12 @@ function Question:injector(code)
("%s @leet end"):format(lang.comment),
}

local before = self:inject(true)
local before = self:inject(true, code)
if before then
table.insert(parts, 1, before)
end

local after = self:inject(false)
local after = self:inject(false, code)
if after then
table.insert(parts, after)
end
Expand Down