irongit

A Neovim plugin enabling you to solve LeetCode problems.

fix: sync

kawreauthored
parent 461fab3commit 359ab14a2fd91edfb981111742d32ee756b2c35bBrowse files

7 files changed, +31 -39

+10-4lua/leetcode-ui/question.lua
@@ -86,20 +86,26 @@ function Question:injector(code)
8686 .. norm_inject(inject.after, false)
8787 end
8888
89-Question.unmount = vim.schedule_wrap(function(self)
89+function Question:_unmount()
9090 self.info:unmount()
9191 self.console:unmount()
9292 self.description:unmount()
9393
9494 if vim.api.nvim_buf_is_valid(self.bufnr) then
95- vim.api.nvim_buf_delete(self.bufnr, { force = true })
95+ pcall(vim.api.nvim_buf_delete, self.bufnr, { force = true })
9696 end
97+
9798 if vim.api.nvim_win_is_valid(self.winid) then --
98- vim.api.nvim_win_close(self.winid, true)
99+ pcall(vim.api.nvim_win_close, self.winid, true)
99100 end
100101
101102 _Lc_questions = vim.tbl_filter(function(q) return q.bufnr ~= self.bufnr end, _Lc_questions)
102-end)
103+
104+ self = nil
105+end
106+
107+---@param self lc.ui.Question
108+Question.unmount = vim.schedule_wrap(function(self) self:_unmount() end)
103109
104110 function Question:handle_mount()
105111 vim.cmd("$tabe " .. self:create_file())
+2-2lua/leetcode-ui/renderer/menu.lua
@@ -4,7 +4,7 @@ local config = require("leetcode.config")
44 local utils = require("leetcode-ui.utils")
55 local Renderer = require("leetcode-ui.renderer")
66
7----@class lc.ui.menu : lc.ui.Renderer
7+---@class lc.ui.Menu : lc.ui.Renderer
88 ---@field cursor lc-menu.cursor
99 ---@field maps table
1010 local Menu = Renderer:extend("LeetMenu")
@@ -150,7 +150,7 @@ function Menu:init()
150150 _Lc_Menu = self
151151 end
152152
153----@type fun(): lc.ui.menu
153+---@type fun(): lc.ui.Menu
154154 local LeetMenu = Menu
155155
156156 return LeetMenu
+9-6lua/leetcode.lua
@@ -1,10 +1,11 @@
11 local config = require("leetcode.config")
2-local log = require("leetcode.logger")
32
43 ---@class lc.LeetCode
54 local leetcode = {}
65
76 ---@param on_vimenter boolean
7+---
8+---@return boolean
89 function leetcode.should_skip(on_vimenter)
910 if on_vimenter then
1011 if vim.fn.argc() ~= 1 then return true end
@@ -14,14 +15,16 @@ function leetcode.should_skip(on_vimenter)
1415
1516 local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
1617 if #lines > 1 or (#lines == 1 and lines[1]:len() > 0) then
18+ local log = require("leetcode.logger")
1719 log.warn(("Failed to initialize: `%s` is not an empty buffer"):format(usr_arg))
1820 return true
1921 end
2022 else
2123 for _, buf_id in pairs(vim.api.nvim_list_bufs()) do
2224 local bufinfo = vim.fn.getbufinfo(buf_id)[1]
23- if bufinfo and (bufinfo.listed == 1 and #bufinfo.windows > 0) then --
24- return true
25+ if bufinfo and (bufinfo.listed == 1 and #bufinfo.windows > 0) then
26+ local log = require("leetcode.logger")
27+ log.warn("Failed to initialize: `neovim` contains listed buffers")
2528 end
2629 end
2730 end
@@ -43,9 +46,6 @@ function leetcode.start(on_vimenter)
4346
4447 leetcode.setup_cmds()
4548
46- local utils = require("leetcode.utils")
47- utils.exec_hooks("LeetEnter")
48-
4949 local theme = require("leetcode.theme")
5050 theme.setup()
5151
@@ -56,6 +56,9 @@ function leetcode.start(on_vimenter)
5656 local Menu = require("leetcode-ui.renderer.menu")
5757 Menu():mount()
5858
59+ local utils = require("leetcode.utils")
60+ utils.exec_hooks("LeetEnter")
61+
5962 return true
6063 end
6164
+1-6lua/leetcode/command/init.lua
@@ -155,12 +155,7 @@ end
155155
156156 function cmd.start_with_cmd()
157157 local leetcode = require("leetcode")
158-
159- if leetcode.start(false) then
160- cmd.menu()
161- else
162- log.warn("Failed to initialize")
163- end
158+ if leetcode.start(false) then cmd.menu() end
164159 end
165160
166161 function cmd.menu()
+6-20lua/leetcode/config/init.lua
@@ -4,7 +4,7 @@ local P = require("plenary.path")
44 ---@type lc.ui.Question[]
55 _Lc_questions = {}
66
7----@type lc.ui.menu
7+---@type lc.ui.Menu
88 _Lc_Menu = {} ---@diagnostic disable-line
99
1010 local lazy_plugs = {}
@@ -94,8 +94,13 @@ function config.load_plugins()
9494
9595 if config.user.cn.enabled then table.insert(plugins, "cn") end
9696
97+ for plugin, enabled in pairs(config.user.plugins) do
98+ if enabled then table.insert(plugins, plugin) end
99+ end
100+
97101 for _, plugin in ipairs(plugins) do
98102 local ok, plug = pcall(require, "leetcode-plugins." .. plugin)
103+
99104 if ok then
100105 if not (plug.opts or {}).lazy then
101106 plug.load()
@@ -109,23 +114,4 @@ function config.load_plugins()
109114 end
110115 end
111116
112-function config.load_high_priority_plugins()
113- local plugins = {}
114-
115- if config.user.cn.enabled then
116- config.translator = config.user.cn.translator
117- table.insert(plugins, "cn")
118- end
119-
120- for _, plugin in ipairs(plugins) do
121- local ok, plug = pcall(require, "leetcode-plugins." .. plugin)
122- if ok then
123- plug.load()
124- else
125- local log = require("leetcode.logger")
126- log.error(plug)
127- end
128- end
129-end
130-
131117 return config
+2-0lua/leetcode/config/template.lua
@@ -57,6 +57,8 @@ local M = {
5757 cache = vim.fn.stdpath("cache") .. "/leetcode",
5858 },
5959
60+ plugins = {},
61+
6062 ---@type boolean
6163 logging = true,
6264
+1-1lua/leetcode/utils.lua
@@ -77,7 +77,7 @@ function utils.exec_hooks(event, ...)
7777 if not fns then log.error("unknown hook event: " .. event) end
7878
7979 for i, fn in ipairs(fns) do
80- local ok, msg = pcall(fn, ...)
80+ local ok, msg = pcall(vim.schedule_wrap(fn), ...)
8181 if not ok then log.error(("bad hook #%d in `%s` event: %s"):format(i, event, msg)) end
8282 end
8383 end