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

Commit 7bd3e3d

Browse files
authored
feat: non_standalone plugin (kawre#51)
1 parent fe51320 commit 7bd3e3d

File tree

18 files changed

+248
-30
lines changed

18 files changed

+248
-30
lines changed

.stylua.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ indent_type = "Spaces"
44
indent_width = 4
55
quote_style = "ForceDouble"
66
call_parentheses = "Always"
7+
# collapse_simple_statement = "FunctionOnly"
78
collapse_simple_statement = "Always"

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat
9393
cache = vim.fn.stdpath("cache") .. "/leetcode",
9494
},
9595

96+
---@type table<string, boolean>
97+
plugins = {
98+
non_standalone = false,
99+
},
100+
96101
---@type boolean
97102
logging = true,
98103

@@ -137,6 +142,9 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat
137142

138143
---@type fun(question: lc.ui.Question)[]
139144
["question_enter"] = {},
145+
146+
---@type fun()[]
147+
["leave"] = {},
140148
},
141149

142150
keys = {
@@ -226,6 +234,17 @@ storage = {
226234
},
227235
```
228236

237+
### plugins
238+
239+
[plugins list](#-plugins)
240+
241+
```lua
242+
---@type table<string, boolean>
243+
plugins = {
244+
non_standalone = false,
245+
},
246+
```
247+
229248
### logging
230249

231250
Whether to log [leetcode.nvim] status notifications
@@ -272,6 +291,9 @@ hooks = {
272291

273292
---@type fun(question: lc.ui.Question)[]
274293
["question_enter"] = {},
294+
295+
---@type fun()[]
296+
["leave"] = {},
275297
},
276298
```
277299

@@ -294,6 +316,8 @@ image_support = false,
294316

295317
- `menu` same as `Leet`
296318

319+
- `exit` close [leetcode.nvim]
320+
297321
- `console` opens console pop-up for currently opened question
298322

299323
- `info` opens a pop-up containing information about the currently opened question
@@ -419,6 +443,21 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/b7be8b95-5e2c-4153-8845-4
419443
}
420444
```
421445

446+
## 🧩 Plugins
447+
448+
### Non-Standalone mode
449+
450+
To run [leetcode.nvim] in a non-standalone mode (i.e. not with argument or an empty Neovim session),
451+
enable the `non_standalone` plugin in your config:
452+
453+
```lua
454+
plugins = {
455+
non_standalone = true,
456+
}
457+
```
458+
459+
You can then exit [leetcode.nvim] using `:Leet exit` command
460+
422461
## 🙌 Credits
423462

424463
- [Leetbuddy.nvim](https://github.com/Dhanus3133/Leetbuddy.nvim)

README.zh.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/aee6584c-e099-4409-b114-1
9595
cache = vim.fn.stdpath("cache") .. "/leetcode",
9696
},
9797

98+
---@type table<string, boolean>
99+
plugins = {
100+
non_standalone = false,
101+
},
102+
98103
---@type boolean
99104
logging = true,
100105

@@ -139,6 +144,9 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/aee6584c-e099-4409-b114-1
139144

140145
---@type fun(question: lc.ui.Question)[]
141146
["question_enter"] = {},
147+
148+
---@type fun()[]
149+
["leave"] = {},
142150
},
143151

144152
keys = {
@@ -228,6 +236,17 @@ storage = {
228236
},
229237
```
230238

239+
### plugins
240+
241+
[插件列表](#-plugins)
242+
243+
```lua
244+
---@type table<string, boolean>
245+
plugins = {
246+
non_standalone = false,
247+
},
248+
```
249+
231250
### logging
232251

233252
是否记录 [leetcode.nvim] 状态通知
@@ -271,6 +290,9 @@ hooks = {
271290

272291
---@type fun(question: lc.ui.Question)[]
273292
["question_enter"] = {},
293+
294+
---@type fun()[]
295+
["leave"] = {},
274296
},
275297
```
276298

@@ -293,6 +315,8 @@ image_support = false, -- 将此设置为 `true` 将禁用问题描述的换行
293315

294316
- `menu``Leet` 相同
295317

318+
- `exit` 关闭 [leetcode.nvim]
319+
296320
- `console` 打开当前打开问题的控制台弹出窗口
297321

298322
- `info` 打开包含当前打开问题信息的弹出窗口
@@ -417,6 +441,21 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/b7be8b95-5e2c-4153-8845-4
417441
}
418442
```
419443

444+
## 🧩 Plugins
445+
446+
### Non-Standalone mode
447+
448+
要在非独立模式下运行 [leetcode.nvim](即不带参数或在空的 Neovim 会话中运行),
449+
请在您的配置中启用 `non_standalone` 插件:
450+
451+
```lua
452+
plugins = {
453+
non_standalone = true,
454+
}
455+
```
456+
457+
你可以使用 `:Leet exit` 命令退出 [leetcode.nvim]
458+
420459
## 🙌 鸣谢
421460

422461
- [Leetbuddy.nvim](https://github.com/Dhanus3133/Leetbuddy.nvim)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---@class lc.plugins.non_standalone
2+
local non_standalone = {}
3+
4+
non_standalone.opts = {
5+
lazy = false,
6+
}
7+
8+
function non_standalone.load() --
9+
require("leetcode-plugins.non_standalone.leetcode")
10+
end
11+
12+
return non_standalone
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
local leetcode = require("leetcode")
2+
local config = require("leetcode.config")
3+
4+
local standalone = true
5+
local prev_cwd = nil
6+
7+
---@param on_vimenter boolean
8+
leetcode.should_skip = function(on_vimenter)
9+
if on_vimenter then
10+
if vim.fn.argc() ~= 1 then return true end
11+
12+
local usr_arg, arg = vim.fn.argv()[1], config.user.arg
13+
if usr_arg ~= arg then return true end
14+
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+
local log = require("leetcode.logger")
18+
log.warn(("Failed to initialize: `%s` is not an empty buffer"):format(usr_arg))
19+
return true
20+
end
21+
else
22+
for _, buf_id in pairs(vim.api.nvim_list_bufs()) do
23+
local bufinfo = vim.fn.getbufinfo(buf_id)[1]
24+
if bufinfo and (bufinfo.listed == 1 and #bufinfo.windows > 0) then --
25+
return false, true
26+
end
27+
end
28+
end
29+
30+
return false
31+
end
32+
33+
---@param on_vimenter boolean
34+
leetcode.start = function(on_vimenter)
35+
local skip, buflisted = leetcode.should_skip(on_vimenter)
36+
if skip then --
37+
return false
38+
end
39+
40+
config.setup()
41+
42+
leetcode.setup_cmds()
43+
44+
local theme = require("leetcode.theme")
45+
theme.setup()
46+
47+
if not on_vimenter then --
48+
if buflisted then
49+
prev_cwd = vim.fn.getcwd()
50+
vim.cmd.tabe()
51+
else
52+
vim.cmd.enew()
53+
end
54+
55+
standalone = not buflisted
56+
end
57+
58+
vim.api.nvim_set_current_dir(config.storage.home:absolute())
59+
60+
local Menu = require("leetcode-ui.renderer.menu")
61+
Menu():mount()
62+
63+
local utils = require("leetcode.utils")
64+
utils.exec_hooks("enter")
65+
66+
return true
67+
end
68+
69+
leetcode.stop = vim.schedule_wrap(function()
70+
if standalone then return vim.cmd("qa!") end
71+
72+
_Lc_state.menu:unmount()
73+
74+
vim.api.nvim_create_user_command("Leet", require("leetcode.command").start_with_cmd, {
75+
bar = true,
76+
bang = true,
77+
desc = "Open leetcode.nvim",
78+
})
79+
80+
local utils = require("leetcode.utils")
81+
utils.exec_hooks("leave")
82+
83+
if prev_cwd then
84+
vim.api.nvim_set_current_dir(prev_cwd)
85+
prev_cwd = nil
86+
end
87+
end)

lua/leetcode-ui/lines/button/menu/exit.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local MenuButton = require("leetcode-ui.lines.button.menu")
2+
local leetcode = require("leetcode")
23

34
---@class lc.ui.Button.Menu.Exit : lc.ui.Button.Menu
45
local MenuExitButton = MenuButton:extend("LeetMenuExitButton")
@@ -8,7 +9,7 @@ function MenuExitButton:init()
89
MenuExitButton.super.init(self, "Exit", {
910
icon = "󰩈",
1011
sc = "q",
11-
on_press = function() vim.cmd("qa!") end,
12+
on_press = leetcode.stop,
1213
})
1314
end
1415

lua/leetcode-ui/lines/calendar.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function Calendar:handle_res(res)
8383
self:append(line):endl()
8484
end
8585

86-
_Lc_menu:draw()
86+
_Lc_state.menu:draw()
8787
end
8888

8989
function Calendar:handle_submissions()

lua/leetcode-ui/lines/solved.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function Solved:handle_res(res)
7676
self:endl()
7777
end
7878

79-
_Lc_menu:draw()
79+
_Lc_state.menu:draw()
8080
end
8181

8282
function Solved:update()

lua/leetcode-ui/question.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ function Question:injector(code)
152152
return table.concat(parts, "\n")
153153
end
154154

155-
function Question:unmount()
155+
function Question:_unmount()
156156
if vim.v.dying ~= 0 then --
157157
return
158158
end
@@ -166,15 +166,16 @@ function Question:unmount()
166166
vim.api.nvim_buf_delete(self.bufnr, { force = true, unload = false })
167167
end
168168

169-
_Lc_questions = vim.tbl_filter(function(q) --
170-
return q.bufnr ~= self.bufnr
171-
end, _Lc_questions)
169+
_Lc_state.questions = vim.tbl_filter(
170+
function(q) return q.bufnr ~= self.bufnr end,
171+
_Lc_state.questions
172+
)
172173

173174
self = nil
174175
end)
175176
end
176177

177-
function Question:_unmount()
178+
function Question:unmount()
178179
if vim.api.nvim_win_is_valid(self.winid) then vim.api.nvim_win_close(self.winid, true) end
179180
end
180181

@@ -183,14 +184,14 @@ function Question:autocmds()
183184
vim.api.nvim_create_autocmd("WinClosed", {
184185
group = group,
185186
pattern = tostring(self.winid),
186-
callback = function() self:unmount() end,
187+
callback = function() self:_unmount() end,
187188
})
188189
end
189190

190191
function Question:handle_mount()
191192
self:create_buffer()
192193

193-
table.insert(_Lc_questions, self)
194+
table.insert(_Lc_state.questions, self)
194195

195196
self.description = Description(self):mount()
196197
self.console = Console(self)

lua/leetcode-ui/renderer/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ function Renderer:draw(component)
2424
self.bufnr = component.bufnr
2525
self.winid = component.winid
2626

27+
if not vim.api.nvim_buf_is_valid(self.bufnr) then return end
28+
2729
self:map("n", keys.confirm, function() self:handle_press() end)
2830

2931
self:clear_keymaps()

lua/leetcode-ui/renderer/menu.lua

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,24 @@ function Menu:apply_options()
137137
})
138138
end
139139

140+
function Menu:unmount()
141+
if vim.v.dying ~= 0 then --
142+
return
143+
end
144+
145+
require("leetcode.command").q_close_all()
146+
147+
if self.winid and vim.api.nvim_win_is_valid(self.winid) then
148+
vim.api.nvim_win_close(self.winid, true)
149+
end
150+
151+
vim.schedule(function()
152+
if self.bufnr and vim.api.nvim_buf_is_valid(self.bufnr) then
153+
vim.api.nvim_buf_delete(self.bufnr, { force = true })
154+
end
155+
end)
156+
end
157+
140158
function Menu:remount()
141159
if self.winid and api.nvim_win_is_valid(self.winid) then --
142160
api.nvim_win_close(self.winid, true)
@@ -195,7 +213,7 @@ function Menu:init()
195213
self.bufnr = api.nvim_get_current_buf()
196214
self.winid = api.nvim_get_current_win()
197215

198-
_Lc_menu = self
216+
_Lc_state.menu = self
199217
end
200218

201219
---@type fun(): lc.ui.Menu

0 commit comments

Comments
 (0)