Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cf86cc4

Browse files
committed
feat: option to open leetcode.nvim inside your favorite dashboard plugin
1 parent 9e410d4 commit cf86cc4

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

lua/leetcode.lua

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
11
local config = require("leetcode.config")
2+
local log = require("leetcode.logger")
23

34
---@class lc.LeetCode
45
local leetcode = {}
56

6-
---@param arg string
7-
function leetcode.should_skip(arg)
8-
if vim.fn.argc() ~= 1 then return true end
7+
---@param on_vimenter boolean
8+
function leetcode.should_skip(on_vimenter)
9+
if on_vimenter then
10+
if vim.fn.argc() ~= 1 then return true end
911

10-
local usr_arg = vim.fn.argv()[1]
11-
if usr_arg ~= arg then return true end
12+
local usr_arg, arg = vim.fn.argv()[1], config.user.arg
13+
if usr_arg ~= arg then return true end
1214

13-
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
14-
if #lines > 1 or (#lines == 1 and lines[1]:len() > 0) then
15-
local log = require("leetcode.logger")
16-
log.warn(("Failed to initialize: `%s` is not an empty buffer"):format(usr_arg))
17-
return true
15+
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
16+
if #lines > 1 or (#lines == 1 and lines[1]:len() > 0) then
17+
log.warn(("Failed to initialize: `%s` is not an empty buffer"):format(usr_arg))
18+
return true
19+
end
20+
else
21+
for _, buf_id in pairs(vim.api.nvim_list_bufs()) do
22+
local bufinfo = vim.fn.getbufinfo(buf_id)[1]
23+
if bufinfo and (bufinfo.listed == 1 and #bufinfo.windows > 0) then --
24+
return true
25+
end
26+
end
1827
end
1928

2029
return false
2130
end
2231

2332
function leetcode.setup_cmds() require("leetcode.command").setup() end
2433

25-
---@param cfg lc.UserConfig
26-
function leetcode.start(cfg)
27-
if leetcode.should_skip(cfg.arg or config.default.arg) then return end
28-
29-
config.apply(cfg or {})
34+
---@param on_vimenter boolean
35+
function leetcode.start(on_vimenter)
36+
if leetcode.should_skip(on_vimenter) then --
37+
return false
38+
end
3039

3140
vim.api.nvim_set_current_dir(config.storage.home:absolute())
3241

@@ -39,18 +48,32 @@ function leetcode.start(cfg)
3948
local theme = require("leetcode.theme")
4049
theme.setup()
4150

51+
if not on_vimenter then --
52+
vim.cmd.enew()
53+
end
54+
4255
local Menu = require("leetcode-ui.renderer.menu")
4356
Menu():mount()
57+
58+
return true
4459
end
4560

4661
---@param cfg? lc.UserConfig
4762
function leetcode.setup(cfg)
63+
config.apply(cfg or {})
64+
65+
vim.api.nvim_create_user_command("Leet", require("leetcode.command").start_with_cmd, {
66+
bar = true,
67+
bang = true,
68+
desc = "Open leetcode.nvim",
69+
})
70+
4871
local group_id = vim.api.nvim_create_augroup("leetcode_start", { clear = true })
4972
vim.api.nvim_create_autocmd("VimEnter", {
5073
group = group_id,
5174
pattern = "*",
5275
nested = true,
53-
callback = function() leetcode.start(cfg or {}) end,
76+
callback = function() leetcode.start(true) end,
5477
})
5578
end
5679

lua/leetcode/command/init.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ function cmd.random_question(opts)
153153
Question(item):mount()
154154
end
155155

156+
function cmd.start_with_cmd()
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
164+
end
165+
156166
function cmd.menu()
157167
local ok, tabp = pcall(vim.api.nvim_win_get_tabpage, _Lc_Menu.winid)
158168
if ok then

0 commit comments

Comments
 (0)