@@ -3,38 +3,81 @@ local config = require("leetcode.config")
3
3
--- @class lc.LeetCode
4
4
local leetcode = {}
5
5
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
+
6
26
--- @param on_vimenter boolean
7
27
---
8
- --- @return boolean
28
+ --- @return boolean , boolean ? ( skip , standalone )
9
29
function leetcode .should_skip (on_vimenter )
10
30
if on_vimenter then
11
- if vim .fn .argc () ~= 1 then
31
+ if vim .fn .argc (- 1 ) ~= 1 then
12
32
return true
13
33
end
14
34
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 )
16
36
if usr_arg ~= arg then
17
37
return true
18
38
end
19
39
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 )
24
42
return true
25
43
end
44
+
45
+ return false , true
26
46
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 ()
32
77
return true
33
78
end
34
79
end
35
80
end
36
-
37
- return false
38
81
end
39
82
40
83
function leetcode .setup_cmds ()
43
86
44
87
--- @param on_vimenter boolean
45
88
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
47
91
return false
48
92
end
49
93
0 commit comments