irongit

A Neovim plugin enabling you to solve LeetCode problems.

refactor: `leetcode.open`

kawreauthored
parent 5d8b30fcommit 98136532e61f6eb6a51b8130a8bbf73ccdee3d51Browse files

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
442442 ```lua
443443 {
444444 "kawre/leetcode.nvim",
445- lazy = leet_arg ~= vim.fn.argv()[1],
445+ lazy = leet_arg ~= vim.fn.argv(0, -1),
446446 opts = { arg = leet_arg },
447447 }
448448 ```
+1-1lua/leetcode-plugins/non_standalone/init.lua
@@ -5,7 +5,7 @@ non_standalone.opts = {
55 lazy = false,
66 }
77
8-function non_standalone.load() --
8+function non_standalone.load()
99 require("leetcode-plugins.non_standalone.leetcode")
1010 end
1111
+7-35lua/leetcode-plugins/non_standalone/leetcode.lua
@@ -1,42 +1,14 @@
1+---@diagnostic disable: invisible, duplicate-set-field
2+
13 local leetcode = require("leetcode")
24 local config = require("leetcode.config")
35
4-local standalone = true
6+local is_standalone = true
57 local prev_cwd = nil
68
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-
379 ---@param on_vimenter boolean
3810 leetcode.start = function(on_vimenter)
39- local skip, buflisted = leetcode.should_skip(on_vimenter)
11+ local skip, standalone = leetcode.should_skip(on_vimenter)
4012 if skip then
4113 return false
4214 end
@@ -49,14 +21,14 @@ leetcode.start = function(on_vimenter)
4921 theme.setup()
5022
5123 if not on_vimenter then
52- if buflisted then
24+ if not standalone then
5325 prev_cwd = vim.fn.getcwd()
5426 vim.cmd.tabe()
5527 else
5628 vim.cmd.enew()
5729 end
5830
59- standalone = not buflisted
31+ is_standalone = standalone ---@diagnostic disable-line: cast-local-type
6032 end
6133
6234 vim.api.nvim_set_current_dir(config.storage.home:absolute())
@@ -71,7 +43,7 @@ leetcode.start = function(on_vimenter)
7143 end
7244
7345 leetcode.stop = vim.schedule_wrap(function()
74- if standalone then
46+ if is_standalone then
7547 return vim.cmd("qa!")
7648 end
7749
+59-15lua/leetcode.lua
@@ -3,38 +3,81 @@ local config = require("leetcode.config")
33 ---@class lc.LeetCode
44 local leetcode = {}
55
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+
626 ---@param on_vimenter boolean
727 ---
8----@return boolean
28+---@return boolean, boolean? (skip, standalone)
929 function leetcode.should_skip(on_vimenter)
1030 if on_vimenter then
11- if vim.fn.argc() ~= 1 then
31+ if vim.fn.argc(-1) ~= 1 then
1232 return true
1333 end
1434
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)
1636 if usr_arg ~= arg then
1737 return true
1838 end
1939
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)
2442 return true
2543 end
44+
45+ return false, true
2646 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()
3277 return true
3378 end
3479 end
3580 end
36-
37- return false
3881 end
3982
4083 function leetcode.setup_cmds()
@@ -43,7 +86,8 @@ end
4386
4487 ---@param on_vimenter boolean
4588 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
4791 return false
4892 end
4993
+3-0lua/leetcode/config/init.lua
@@ -21,6 +21,7 @@ local config = {
2121 version = "1.0.1",
2222 storage = {}, ---@type table<string, Path>
2323 theme = {}, ---@type lc.highlights
24+ plugins = {},
2425
2526 translator = false,
2627
@@ -98,6 +99,7 @@ function config.validate()
9899 end
99100
100101 function config.load_plugins()
102+ config.plugins = {}
101103 local plugins = {}
102104
103105 if config.user.cn.enabled then
@@ -119,6 +121,7 @@ function config.load_plugins()
119121 else
120122 table.insert(lazy_plugs, plug.load)
121123 end
124+ config.plugins[plugin] = true
122125 else
123126 table.insert(lazy_plugs, function()
124127 local log = require("leetcode.logger")