@@ -3,38 +3,81 @@ local config = require("leetcode.config")
33--- @class lc.LeetCode
44local 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 )
929function 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
3881end
3982
4083function leetcode .setup_cmds ()
4386
4487--- @param on_vimenter boolean
4588function 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
0 commit comments