Skip to content

Commit

Permalink
feat: open_buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
kawre committed Feb 24, 2024
1 parent b13daa7 commit 999e537
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions lua/leetcode-ui/question.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ function Question:set_lines(code)
if not vim.api.nvim_buf_is_valid(self.bufnr) then return end

pcall(vim.cmd.undojoin)
local s_i, e_i = self:range()
local s_i, e_i, lines = self:range()
s_i = s_i or 1
e_i = e_i or #lines
code = code and code or (self:snippet(true) or "")
vim.api.nvim_buf_set_lines(self.bufnr, s_i - 1, e_i, false, vim.split(code, "\n"))
end
Expand All @@ -51,7 +53,7 @@ function Question:reset_lines()
self:set_lines(new_lines)
end

---@return string path, boolean? existed
---@return string path, boolean existed
function Question:path()
local lang = utils.get_lang(self.lang)
local alt = lang.alt and ("." .. lang.alt) or ""
Expand Down Expand Up @@ -85,6 +87,12 @@ function Question:create_buffer()
self.bufnr = vim.api.nvim_get_current_buf()
self.winid = vim.api.nvim_get_current_win()

self:open_buffer(existed, false)
end

---@param existed boolean
---@param loaded boolean
function Question:open_buffer(existed, loaded)
vim.api.nvim_set_option_value("buflisted", true, { buf = self.bufnr })

local i = self:fold_range()
Expand All @@ -95,6 +103,11 @@ function Question:create_buffer()
if existed then --
self:reset_lines()
end

if not loaded then
utils.exec_hook("question_enter", self)
self:autocmds()
end
end

---@param before boolean
Expand Down Expand Up @@ -180,14 +193,10 @@ function Question:handle_mount()

table.insert(_Lc_questions, self)

self:autocmds()

self.description = Description(self):mount()
self.console = Console(self)
self.info = Info(self)

utils.exec_hook("question_enter", self)

return self
end

Expand Down Expand Up @@ -275,22 +284,8 @@ Question.change_lang = vim.schedule_wrap(function(self, lang)
vim.api.nvim_win_set_buf(self.winid, self.bufnr)

vim.api.nvim_set_option_value("buflisted", false, { buf = old_bufnr })
vim.api.nvim_set_option_value("buflisted", true, { buf = self.bufnr })

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

if existed then --
self:reset_lines()
end

if not loaded then --
utils.exec_hook("question_enter", self)
end

self:autocmds()
self:open_buffer(existed, loaded)
end)

if not ok then
Expand Down

0 comments on commit 999e537

Please sign in to comment.