11local config = require (" leetcode.config" )
2+ local log = require (" leetcode.logger" )
23
34--- @class lc.LeetCode
45local 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
2130end
2231
2332function 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
4459end
4560
4661--- @param cfg ? lc.UserConfig
4762function 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 })
5578end
5679
0 commit comments