A Neovim plugin enabling you to solve LeetCode problems.
fix: sync
7 files changed, +31 -39
+10-4lua/leetcode-ui/question.lua
| @@ -86,20 +86,26 @@ function Question:injector(code) | ||
| 86 | 86 | .. norm_inject(inject.after, false) |
| 87 | 87 | end |
| 88 | 88 | |
| 89 | -Question.unmount = vim.schedule_wrap(function(self) | |
| 89 | +function Question:_unmount() | |
| 90 | 90 | self.info:unmount() |
| 91 | 91 | self.console:unmount() |
| 92 | 92 | self.description:unmount() |
| 93 | 93 | |
| 94 | 94 | 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 }) | |
| 96 | 96 | end |
| 97 | + | |
| 97 | 98 | 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) | |
| 99 | 100 | end |
| 100 | 101 | |
| 101 | 102 | _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) | |
| 103 | 109 | |
| 104 | 110 | function Question:handle_mount() |
| 105 | 111 | vim.cmd("$tabe " .. self:create_file()) |
+9-6lua/leetcode.lua
| @@ -1,10 +1,11 @@ | ||
| 1 | 1 | local config = require("leetcode.config") |
| 2 | -local log = require("leetcode.logger") | |
| 3 | 2 | |
| 4 | 3 | ---@class lc.LeetCode |
| 5 | 4 | local leetcode = {} |
| 6 | 5 | |
| 7 | 6 | ---@param on_vimenter boolean |
| 7 | +--- | |
| 8 | +---@return boolean | |
| 8 | 9 | function leetcode.should_skip(on_vimenter) |
| 9 | 10 | if on_vimenter then |
| 10 | 11 | if vim.fn.argc() ~= 1 then return true end |
| @@ -14,14 +15,16 @@ function leetcode.should_skip(on_vimenter) | ||
| 14 | 15 | |
| 15 | 16 | local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true) |
| 16 | 17 | if #lines > 1 or (#lines == 1 and lines[1]:len() > 0) then |
| 18 | + local log = require("leetcode.logger") | |
| 17 | 19 | log.warn(("Failed to initialize: `%s` is not an empty buffer"):format(usr_arg)) |
| 18 | 20 | return true |
| 19 | 21 | end |
| 20 | 22 | else |
| 21 | 23 | for _, buf_id in pairs(vim.api.nvim_list_bufs()) do |
| 22 | 24 | 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") | |
| 25 | 28 | end |
| 26 | 29 | end |
| 27 | 30 | end |
| @@ -43,9 +46,6 @@ function leetcode.start(on_vimenter) | ||
| 43 | 46 | |
| 44 | 47 | leetcode.setup_cmds() |
| 45 | 48 | |
| 46 | - local utils = require("leetcode.utils") | |
| 47 | - utils.exec_hooks("LeetEnter") | |
| 48 | - | |
| 49 | 49 | local theme = require("leetcode.theme") |
| 50 | 50 | theme.setup() |
| 51 | 51 | |
| @@ -56,6 +56,9 @@ function leetcode.start(on_vimenter) | ||
| 56 | 56 | local Menu = require("leetcode-ui.renderer.menu") |
| 57 | 57 | Menu():mount() |
| 58 | 58 | |
| 59 | + local utils = require("leetcode.utils") | |
| 60 | + utils.exec_hooks("LeetEnter") | |
| 61 | + | |
| 59 | 62 | return true |
| 60 | 63 | end |
| 61 | 64 |
+1-6lua/leetcode/command/init.lua
| @@ -155,12 +155,7 @@ end | ||
| 155 | 155 | |
| 156 | 156 | function cmd.start_with_cmd() |
| 157 | 157 | 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 | |
| 164 | 159 | end |
| 165 | 160 | |
| 166 | 161 | function cmd.menu() |
+6-20lua/leetcode/config/init.lua
| @@ -4,7 +4,7 @@ local P = require("plenary.path") | ||
| 4 | 4 | ---@type lc.ui.Question[] |
| 5 | 5 | _Lc_questions = {} |
| 6 | 6 | |
| 7 | ----@type lc.ui.menu | |
| 7 | +---@type lc.ui.Menu | |
| 8 | 8 | _Lc_Menu = {} ---@diagnostic disable-line |
| 9 | 9 | |
| 10 | 10 | local lazy_plugs = {} |
| @@ -94,8 +94,13 @@ function config.load_plugins() | ||
| 94 | 94 | |
| 95 | 95 | if config.user.cn.enabled then table.insert(plugins, "cn") end |
| 96 | 96 | |
| 97 | + for plugin, enabled in pairs(config.user.plugins) do | |
| 98 | + if enabled then table.insert(plugins, plugin) end | |
| 99 | + end | |
| 100 | + | |
| 97 | 101 | for _, plugin in ipairs(plugins) do |
| 98 | 102 | local ok, plug = pcall(require, "leetcode-plugins." .. plugin) |
| 103 | + | |
| 99 | 104 | if ok then |
| 100 | 105 | if not (plug.opts or {}).lazy then |
| 101 | 106 | plug.load() |
| @@ -109,23 +114,4 @@ function config.load_plugins() | ||
| 109 | 114 | end |
| 110 | 115 | end |
| 111 | 116 | |
| 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 | - | |
| 131 | 117 | return config |
+2-0lua/leetcode/config/template.lua
| @@ -57,6 +57,8 @@ local M = { | ||
| 57 | 57 | cache = vim.fn.stdpath("cache") .. "/leetcode", |
| 58 | 58 | }, |
| 59 | 59 | |
| 60 | + plugins = {}, | |
| 61 | + | |
| 60 | 62 | ---@type boolean |
| 61 | 63 | logging = true, |
| 62 | 64 |
+1-1lua/leetcode/utils.lua
| @@ -77,7 +77,7 @@ function utils.exec_hooks(event, ...) | ||
| 77 | 77 | if not fns then log.error("unknown hook event: " .. event) end |
| 78 | 78 | |
| 79 | 79 | for i, fn in ipairs(fns) do |
| 80 | - local ok, msg = pcall(fn, ...) | |
| 80 | + local ok, msg = pcall(vim.schedule_wrap(fn), ...) | |
| 81 | 81 | if not ok then log.error(("bad hook #%d in `%s` event: %s"):format(i, event, msg)) end |
| 82 | 82 | end |
| 83 | 83 | end |