A Neovim plugin enabling you to solve LeetCode problems.
refactor: `leetcode.open`
5 files changed, +71 -52
+1-1README.md
| @@ -442,7 +442,7 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/b7be8b95-5e2c-4153-8845-4 | ||
| 442 | 442 | ```lua |
| 443 | 443 | { |
| 444 | 444 | "kawre/leetcode.nvim", |
| 445 | - lazy = leet_arg ~= vim.fn.argv()[1], | |
| 445 | + lazy = leet_arg ~= vim.fn.argv(0, -1), | |
| 446 | 446 | opts = { arg = leet_arg }, |
| 447 | 447 | } |
| 448 | 448 | ``` |
+1-1lua/leetcode-plugins/non_standalone/init.lua
| @@ -5,7 +5,7 @@ non_standalone.opts = { | ||
| 5 | 5 | lazy = false, |
| 6 | 6 | } |
| 7 | 7 | |
| 8 | -function non_standalone.load() -- | |
| 8 | +function non_standalone.load() | |
| 9 | 9 | require("leetcode-plugins.non_standalone.leetcode") |
| 10 | 10 | end |
| 11 | 11 |
+7-35lua/leetcode-plugins/non_standalone/leetcode.lua
| @@ -1,42 +1,14 @@ | ||
| 1 | +---@diagnostic disable: invisible, duplicate-set-field | |
| 2 | + | |
| 1 | 3 | local leetcode = require("leetcode") |
| 2 | 4 | local config = require("leetcode.config") |
| 3 | 5 | |
| 4 | -local standalone = true | |
| 6 | +local is_standalone = true | |
| 5 | 7 | local prev_cwd = nil |
| 6 | 8 | |
| 7 | ----@param on_vimenter boolean | |
| 8 | -leetcode.should_skip = function(on_vimenter) | |
| 9 | - if on_vimenter then | |
| 10 | - if vim.fn.argc() ~= 1 then | |
| 11 | - return true | |
| 12 | - end | |
| 13 | - | |
| 14 | - local usr_arg, arg = vim.fn.argv()[1], config.user.arg | |
| 15 | - if usr_arg ~= arg then | |
| 16 | - return true | |
| 17 | - end | |
| 18 | - | |
| 19 | - local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true) | |
| 20 | - if #lines > 1 or (#lines == 1 and lines[1]:len() > 0) then | |
| 21 | - local log = require("leetcode.logger") | |
| 22 | - log.warn(("Failed to initialize: `%s` is not an empty buffer"):format(usr_arg)) | |
| 23 | - return true | |
| 24 | - end | |
| 25 | - else | |
| 26 | - for _, buf_id in pairs(vim.api.nvim_list_bufs()) do | |
| 27 | - local bufinfo = vim.fn.getbufinfo(buf_id)[1] | |
| 28 | - if bufinfo and (bufinfo.listed == 1 and #bufinfo.windows > 0) then | |
| 29 | - return false, true | |
| 30 | - end | |
| 31 | - end | |
| 32 | - end | |
| 33 | - | |
| 34 | - return false | |
| 35 | -end | |
| 36 | - | |
| 37 | 9 | ---@param on_vimenter boolean |
| 38 | 10 | leetcode.start = function(on_vimenter) |
| 39 | - local skip, buflisted = leetcode.should_skip(on_vimenter) | |
| 11 | + local skip, standalone = leetcode.should_skip(on_vimenter) | |
| 40 | 12 | if skip then |
| 41 | 13 | return false |
| 42 | 14 | end |
| @@ -49,14 +21,14 @@ leetcode.start = function(on_vimenter) | ||
| 49 | 21 | theme.setup() |
| 50 | 22 | |
| 51 | 23 | if not on_vimenter then |
| 52 | - if buflisted then | |
| 24 | + if not standalone then | |
| 53 | 25 | prev_cwd = vim.fn.getcwd() |
| 54 | 26 | vim.cmd.tabe() |
| 55 | 27 | else |
| 56 | 28 | vim.cmd.enew() |
| 57 | 29 | end |
| 58 | 30 | |
| 59 | - standalone = not buflisted | |
| 31 | + is_standalone = standalone ---@diagnostic disable-line: cast-local-type | |
| 60 | 32 | end |
| 61 | 33 | |
| 62 | 34 | vim.api.nvim_set_current_dir(config.storage.home:absolute()) |
| @@ -71,7 +43,7 @@ leetcode.start = function(on_vimenter) | ||
| 71 | 43 | end |
| 72 | 44 | |
| 73 | 45 | leetcode.stop = vim.schedule_wrap(function() |
| 74 | - if standalone then | |
| 46 | + if is_standalone then | |
| 75 | 47 | return vim.cmd("qa!") |
| 76 | 48 | end |
| 77 | 49 |
+59-15lua/leetcode.lua
| @@ -3,38 +3,81 @@ local config = require("leetcode.config") | ||
| 3 | 3 | ---@class lc.LeetCode |
| 4 | 4 | local leetcode = {} |
| 5 | 5 | |
| 6 | +---@private | |
| 7 | +local function log_failed_to_init() | |
| 8 | + local log = require("leetcode.logger") | |
| 9 | + log.warn("Failed to initialize: `neovim` contains listed buffers") | |
| 10 | +end | |
| 11 | + | |
| 12 | +local function log_buf_not_empty(bufname) | |
| 13 | + local log = require("leetcode.logger") | |
| 14 | + if bufname and bufname ~= "" then | |
| 15 | + log.warn(("Failed to initialize: `%s` is not an empty buffer"):format(bufname)) | |
| 16 | + else | |
| 17 | + log.warn("Failed to initialize: not an empty buffer") | |
| 18 | + end | |
| 19 | +end | |
| 20 | + | |
| 21 | +local function buf_is_empty(buf) | |
| 22 | + local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true) | |
| 23 | + return not (#lines > 1 or (#lines == 1 and lines[1]:len() > 0)) | |
| 24 | +end | |
| 25 | + | |
| 6 | 26 | ---@param on_vimenter boolean |
| 7 | 27 | --- |
| 8 | ----@return boolean | |
| 28 | +---@return boolean, boolean? (skip, standalone) | |
| 9 | 29 | function leetcode.should_skip(on_vimenter) |
| 10 | 30 | if on_vimenter then |
| 11 | - if vim.fn.argc() ~= 1 then | |
| 31 | + if vim.fn.argc(-1) ~= 1 then | |
| 12 | 32 | return true |
| 13 | 33 | end |
| 14 | 34 | |
| 15 | - local usr_arg, arg = config.user.arg, vim.fn.argv()[1] | |
| 35 | + local usr_arg, arg = config.user.arg, vim.fn.argv(0, -1) | |
| 16 | 36 | if usr_arg ~= arg then |
| 17 | 37 | return true |
| 18 | 38 | end |
| 19 | 39 | |
| 20 | - local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true) | |
| 21 | - if #lines > 1 or (#lines == 1 and lines[1]:len() > 0) then | |
| 22 | - local log = require("leetcode.logger") | |
| 23 | - log.warn(("Failed to initialize: `%s` is not an empty buffer"):format(usr_arg)) | |
| 40 | + if not buf_is_empty(0) then | |
| 41 | + log_buf_not_empty(usr_arg) | |
| 24 | 42 | return true |
| 25 | 43 | end |
| 44 | + | |
| 45 | + return false, true | |
| 26 | 46 | else |
| 27 | - for _, buf_id in pairs(vim.api.nvim_list_bufs()) do | |
| 28 | - local bufinfo = vim.fn.getbufinfo(buf_id)[1] | |
| 29 | - if bufinfo and (bufinfo.listed == 1 and #bufinfo.windows > 0) then | |
| 30 | - local log = require("leetcode.logger") | |
| 31 | - log.warn("Failed to initialize: `neovim` contains listed buffers") | |
| 47 | + local listed_bufs = vim.tbl_filter(function(info) | |
| 48 | + return info.listed == 1 | |
| 49 | + end, vim.fn.getbufinfo()) | |
| 50 | + | |
| 51 | + if #listed_bufs == 0 then | |
| 52 | + return false, true | |
| 53 | + elseif vim.fn.argc(-1) == 0 and #listed_bufs == 1 then | |
| 54 | + local buf = listed_bufs[1] | |
| 55 | + | |
| 56 | + if vim.api.nvim_get_current_buf() ~= buf.bufnr then | |
| 57 | + if config.plugins.non_standalone then | |
| 58 | + return false, false | |
| 59 | + else | |
| 60 | + log_failed_to_init() | |
| 61 | + return true | |
| 62 | + end | |
| 63 | + end | |
| 64 | + | |
| 65 | + vim.schedule(function() | |
| 66 | + if buf.changed == 1 then | |
| 67 | + vim.api.nvim_buf_delete(buf.bufnr, { force = true }) | |
| 68 | + end | |
| 69 | + end) | |
| 70 | + | |
| 71 | + return false, true | |
| 72 | + elseif #listed_bufs >= 1 then | |
| 73 | + if config.plugins.non_standalone then | |
| 74 | + return false, false | |
| 75 | + else | |
| 76 | + log_failed_to_init() | |
| 32 | 77 | return true |
| 33 | 78 | end |
| 34 | 79 | end |
| 35 | 80 | end |
| 36 | - | |
| 37 | - return false | |
| 38 | 81 | end |
| 39 | 82 | |
| 40 | 83 | function leetcode.setup_cmds() |
| @@ -43,7 +86,8 @@ end | ||
| 43 | 86 | |
| 44 | 87 | ---@param on_vimenter boolean |
| 45 | 88 | function leetcode.start(on_vimenter) |
| 46 | - if leetcode.should_skip(on_vimenter) then | |
| 89 | + local skip = leetcode.should_skip(on_vimenter) | |
| 90 | + if skip then | |
| 47 | 91 | return false |
| 48 | 92 | end |
| 49 | 93 |
+3-0lua/leetcode/config/init.lua
| @@ -21,6 +21,7 @@ local config = { | ||
| 21 | 21 | version = "1.0.1", |
| 22 | 22 | storage = {}, ---@type table<string, Path> |
| 23 | 23 | theme = {}, ---@type lc.highlights |
| 24 | + plugins = {}, | |
| 24 | 25 | |
| 25 | 26 | translator = false, |
| 26 | 27 | |
| @@ -98,6 +99,7 @@ function config.validate() | ||
| 98 | 99 | end |
| 99 | 100 | |
| 100 | 101 | function config.load_plugins() |
| 102 | + config.plugins = {} | |
| 101 | 103 | local plugins = {} |
| 102 | 104 | |
| 103 | 105 | if config.user.cn.enabled then |
| @@ -119,6 +121,7 @@ function config.load_plugins() | ||
| 119 | 121 | else |
| 120 | 122 | table.insert(lazy_plugs, plug.load) |
| 121 | 123 | end |
| 124 | + config.plugins[plugin] = true | |
| 122 | 125 | else |
| 123 | 126 | table.insert(lazy_plugs, function() |
| 124 | 127 | local log = require("leetcode.logger") |