Skip to content

Commit

Permalink
feat: fold imports on question_enter
Browse files Browse the repository at this point in the history
  • Loading branch information
kawre committed Feb 24, 2024
1 parent f10ef24 commit d29d9f2
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 24 deletions.
76 changes: 55 additions & 21 deletions lua/leetcode-ui/question.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,39 @@ function Question:create_file()
return self.file:absolute()
end

---@param new_tabp? boolean
---@return boolean was_loaded
function Question:create_buffer(new_tabp)
local file_name = self:create_file()

local buf = vim.fn.bufadd(file_name)
assert(buf ~= 0, "Failed to create buffer")

self.bufnr = buf
if vim.fn.bufloaded(self.bufnr) == 1 then
return true
else
vim.fn.bufload(self.bufnr)
end

local cmd
if new_tabp then
cmd = ("$tabe %s"):format(file_name)
else
cmd = ("e %s"):format(file_name)
end

local i = self:fold_range()
if i then cmd = cmd .. (" | %d,%dfold"):format(1, i) end
vim.api.nvim_exec2(cmd, {})

self.winid = vim.api.nvim_get_current_win()

utils.exec_hook("question_enter", self)

return false
end

---@param before boolean
function Question:inject(before)
local inject = config.user.injector[self.lang] or {}
Expand Down Expand Up @@ -113,15 +146,8 @@ end
Question.unmount = vim.schedule_wrap(function(self, pre) self:_unmount(pre) end)

function Question:handle_mount()
vim.cmd("$tabe " .. self:create_file())

-- https://github.com/kawre/leetcode.nvim/issues/14
if self.lang == "rust" then
pcall(function() require("rust-tools.standalone").start_standalone_client() end)
end
self:create_buffer(true)

self.bufnr = vim.api.nvim_get_current_buf()
self.winid = vim.api.nvim_get_current_win()
table.insert(_Lc_questions, self)

vim.api.nvim_create_autocmd("QuitPre", {
Expand All @@ -133,8 +159,6 @@ function Question:handle_mount()
self.console = Console(self)
self.info = Info(self)

utils.exec_hook("question_enter", self)

return self
end

Expand Down Expand Up @@ -180,6 +204,18 @@ function Question:range(inclusive)
return start_i, end_i, lines
end

function Question:fold_range()
local start_i, _, lines = self:range(true)
if start_i == nil or start_i <= 1 then return end

local i = start_i - 1
while lines[i] == "" do
i = i - 1
end

if 1 < i then return i end
end

---@param submit boolean
---@return string
function Question:lines(submit)
Expand All @@ -195,25 +231,23 @@ end
---@param self lc.ui.Question
---@param lang lc.lang
Question.change_lang = vim.schedule_wrap(function(self, lang)
local old_lang = self.lang
local old_lang, old_bufnr = self.lang, self.bufnr
self.lang = lang

local new_bufnr = vim.fn.bufadd(self:create_file())
if new_bufnr ~= 0 then
local bufloaded = vim.fn.bufloaded(new_bufnr)

vim.api.nvim_win_set_buf(self.winid, new_bufnr)

vim.api.nvim_buf_set_option(self.bufnr, "buflisted", false)
vim.api.nvim_buf_set_option(new_bufnr, "buflisted", true)
local ok, was_loaded = pcall(Question.create_buffer, self)
if ok then
vim.api.nvim_buf_set_option(old_bufnr, "buflisted", false)
vim.api.nvim_buf_set_option(self.bufnr, "buflisted", true)

self.bufnr = new_bufnr
if bufloaded == 0 then --
if was_loaded then
vim.api.nvim_win_set_buf(self.winid, self.bufnr)
else
utils.exec_hook("question_enter", self)
end
else
log.error("Changing language failed")
self.lang = old_lang
self.bufnr = old_bufnr
end
end)

Expand Down
12 changes: 12 additions & 0 deletions lua/leetcode/config/hooks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---@class lc.Hooks
local hooks = {}

hooks["question_enter"] = {
function(q)
-- https://github.com/kawre/leetcode.nvim/issues/14
if q.lang ~= "rust" then return end
pcall(function() require("rust-tools.standalone").start_standalone_client() end)
end,
}

return hooks
1 change: 1 addition & 0 deletions lua/leetcode/config/imports.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ imports["python"] = {

imports["java"] = {
"import java.util.*;",
"import java.math.*;",
}

return imports
1 change: 1 addition & 0 deletions lua/leetcode/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ local config = {
sessions = require("leetcode.config.sessions"),
stats = require("leetcode.config.stats"),
imports = require("leetcode.config.imports"),
hooks = require("leetcode.config.hooks"),

---@type lc.UserStatus
auth = {}, ---@diagnostic disable-line
Expand Down
2 changes: 1 addition & 1 deletion lua/leetcode/config/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ local M = {
["enter"] = {},

---@type fun(question: lc.ui.Question)[]
["question_open"] = {},
["question_enter"] = {},
},

keys = {
Expand Down
14 changes: 12 additions & 2 deletions lua/leetcode/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,21 @@ function utils.get_lang_by_name(name)
end

---@param event lc.hook
function utils.exec_hook(event, ...)
---@return fun()[]|nil
function utils.get_hooks(event)
local defaults = config.hooks[event] or {}
local fns = config.user.hooks[event]
if not fns then log.error("unknown hook event: " .. event) end

if not fns then return end

if type(fns) == "function" then fns = { fns } end
return vim.list_extend(defaults, fns)
end

---@param event lc.hook
function utils.exec_hook(event, ...)
local fns = utils.get_hooks(event)
if not fns then return log.error("unknown hook event: " .. event) end

for i, fn in ipairs(fns) do
local ok, msg = pcall(vim.schedule_wrap(fn), ...)
Expand Down

0 comments on commit d29d9f2

Please sign in to comment.