1
1
local config = require (" leetcode.config" )
2
+ local log = require (" leetcode.logger" )
2
3
3
4
--- @class lc.LeetCode
4
5
local leetcode = {}
5
6
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
9
11
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
12
14
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
18
27
end
19
28
20
29
return false
21
30
end
22
31
23
32
function leetcode .setup_cmds () require (" leetcode.command" ).setup () end
24
33
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
30
39
31
40
vim .api .nvim_set_current_dir (config .storage .home :absolute ())
32
41
@@ -39,18 +48,32 @@ function leetcode.start(cfg)
39
48
local theme = require (" leetcode.theme" )
40
49
theme .setup ()
41
50
51
+ if not on_vimenter then --
52
+ vim .cmd .enew ()
53
+ end
54
+
42
55
local Menu = require (" leetcode-ui.renderer.menu" )
43
56
Menu ():mount ()
57
+
58
+ return true
44
59
end
45
60
46
61
--- @param cfg ? lc.UserConfig
47
62
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
+
48
71
local group_id = vim .api .nvim_create_augroup (" leetcode_start" , { clear = true })
49
72
vim .api .nvim_create_autocmd (" VimEnter" , {
50
73
group = group_id ,
51
74
pattern = " *" ,
52
75
nested = true ,
53
- callback = function () leetcode .start (cfg or {} ) end ,
76
+ callback = function () leetcode .start (true ) end ,
54
77
})
55
78
end
56
79
0 commit comments