irongit

A Neovim plugin enabling you to solve LeetCode problems.

feat: fold imports on `question_enter`

kawreauthored
parent f10ef24commit d29d9f2b5c9b7857bad91b818633905edc0e8614Browse files

6 files changed, +82 -24

+55-21lua/leetcode-ui/question.lua
@@ -51,6 +51,39 @@ function Question:create_file()
5151 return self.file:absolute()
5252 end
5353
54+---@param new_tabp? boolean
55+---@return boolean was_loaded
56+function Question:create_buffer(new_tabp)
57+ local file_name = self:create_file()
58+
59+ local buf = vim.fn.bufadd(file_name)
60+ assert(buf ~= 0, "Failed to create buffer")
61+
62+ self.bufnr = buf
63+ if vim.fn.bufloaded(self.bufnr) == 1 then
64+ return true
65+ else
66+ vim.fn.bufload(self.bufnr)
67+ end
68+
69+ local cmd
70+ if new_tabp then
71+ cmd = ("$tabe %s"):format(file_name)
72+ else
73+ cmd = ("e %s"):format(file_name)
74+ end
75+
76+ local i = self:fold_range()
77+ if i then cmd = cmd .. (" | %d,%dfold"):format(1, i) end
78+ vim.api.nvim_exec2(cmd, {})
79+
80+ self.winid = vim.api.nvim_get_current_win()
81+
82+ utils.exec_hook("question_enter", self)
83+
84+ return false
85+end
86+
5487 ---@param before boolean
5588 function Question:inject(before)
5689 local inject = config.user.injector[self.lang] or {}
@@ -113,15 +146,8 @@ end
113146 Question.unmount = vim.schedule_wrap(function(self, pre) self:_unmount(pre) end)
114147
115148 function Question:handle_mount()
116- vim.cmd("$tabe " .. self:create_file())
117-
118- -- https://github.com/kawre/leetcode.nvim/issues/14
119- if self.lang == "rust" then
120- pcall(function() require("rust-tools.standalone").start_standalone_client() end)
121- end
149+ self:create_buffer(true)
122150
123- self.bufnr = vim.api.nvim_get_current_buf()
124- self.winid = vim.api.nvim_get_current_win()
125151 table.insert(_Lc_questions, self)
126152
127153 vim.api.nvim_create_autocmd("QuitPre", {
@@ -133,8 +159,6 @@ function Question:handle_mount()
133159 self.console = Console(self)
134160 self.info = Info(self)
135161
136- utils.exec_hook("question_enter", self)
137-
138162 return self
139163 end
140164
@@ -180,6 +204,18 @@ function Question:range(inclusive)
180204 return start_i, end_i, lines
181205 end
182206
207+function Question:fold_range()
208+ local start_i, _, lines = self:range(true)
209+ if start_i == nil or start_i <= 1 then return end
210+
211+ local i = start_i - 1
212+ while lines[i] == "" do
213+ i = i - 1
214+ end
215+
216+ if 1 < i then return i end
217+end
218+
183219 ---@param submit boolean
184220 ---@return string
185221 function Question:lines(submit)
@@ -195,25 +231,23 @@ end
195231 ---@param self lc.ui.Question
196232 ---@param lang lc.lang
197233 Question.change_lang = vim.schedule_wrap(function(self, lang)
198- local old_lang = self.lang
234+ local old_lang, old_bufnr = self.lang, self.bufnr
199235 self.lang = lang
200236
201- local new_bufnr = vim.fn.bufadd(self:create_file())
202- if new_bufnr ~= 0 then
203- local bufloaded = vim.fn.bufloaded(new_bufnr)
204-
205- vim.api.nvim_win_set_buf(self.winid, new_bufnr)
206-
207- vim.api.nvim_buf_set_option(self.bufnr, "buflisted", false)
208- vim.api.nvim_buf_set_option(new_bufnr, "buflisted", true)
237+ local ok, was_loaded = pcall(Question.create_buffer, self)
238+ if ok then
239+ vim.api.nvim_buf_set_option(old_bufnr, "buflisted", false)
240+ vim.api.nvim_buf_set_option(self.bufnr, "buflisted", true)
209241
210- self.bufnr = new_bufnr
211- if bufloaded == 0 then --
242+ if was_loaded then
243+ vim.api.nvim_win_set_buf(self.winid, self.bufnr)
244+ else
212245 utils.exec_hook("question_enter", self)
213246 end
214247 else
215248 log.error("Changing language failed")
216249 self.lang = old_lang
250+ self.bufnr = old_bufnr
217251 end
218252 end)
219253
+12-0lua/leetcode/config/hooks.lua
@@ -0,0 +1,12 @@
1+---@class lc.Hooks
2+local hooks = {}
3+
4+hooks["question_enter"] = {
5+ function(q)
6+ -- https://github.com/kawre/leetcode.nvim/issues/14
7+ if q.lang ~= "rust" then return end
8+ pcall(function() require("rust-tools.standalone").start_standalone_client() end)
9+ end,
10+}
11+
12+return hooks
+1-0lua/leetcode/config/imports.lua
@@ -68,6 +68,7 @@ imports["python"] = {
6868
6969 imports["java"] = {
7070 "import java.util.*;",
71+ "import java.math.*;",
7172 }
7273
7374 return imports
+1-0lua/leetcode/config/init.lua
@@ -29,6 +29,7 @@ local config = {
2929 sessions = require("leetcode.config.sessions"),
3030 stats = require("leetcode.config.stats"),
3131 imports = require("leetcode.config.imports"),
32+ hooks = require("leetcode.config.hooks"),
3233
3334 ---@type lc.UserStatus
3435 auth = {}, ---@diagnostic disable-line
+1-1lua/leetcode/config/template.lua
@@ -102,7 +102,7 @@ local M = {
102102 ["enter"] = {},
103103
104104 ---@type fun(question: lc.ui.Question)[]
105- ["question_open"] = {},
105+ ["question_enter"] = {},
106106 },
107107
108108 keys = {
+12-2lua/leetcode/utils.lua
@@ -72,11 +72,21 @@ function utils.get_lang_by_name(name)
7272 end
7373
7474 ---@param event lc.hook
75-function utils.exec_hook(event, ...)
75+---@return fun()[]|nil
76+function utils.get_hooks(event)
77+ local defaults = config.hooks[event] or {}
7678 local fns = config.user.hooks[event]
77- if not fns then log.error("unknown hook event: " .. event) end
79+
80+ if not fns then return end
7881
7982 if type(fns) == "function" then fns = { fns } end
83+ return vim.list_extend(defaults, fns)
84+end
85+
86+---@param event lc.hook
87+function utils.exec_hook(event, ...)
88+ local fns = utils.get_hooks(event)
89+ if not fns then return log.error("unknown hook event: " .. event) end
8090
8191 for i, fn in ipairs(fns) do
8292 local ok, msg = pcall(vim.schedule_wrap(fn), ...)