Skip to content

Commit

Permalink
feat: non_standalone plugin (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
willothy committed Feb 28, 2024
1 parent fe51320 commit 7bd3e3d
Show file tree
Hide file tree
Showing 18 changed files with 248 additions and 30 deletions.
1 change: 1 addition & 0 deletions .stylua.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ indent_type = "Spaces"
indent_width = 4
quote_style = "ForceDouble"
call_parentheses = "Always"
# collapse_simple_statement = "FunctionOnly"
collapse_simple_statement = "Always"
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat
cache = vim.fn.stdpath("cache") .. "/leetcode",
},

---@type table<string, boolean>
plugins = {
non_standalone = false,
},

---@type boolean
logging = true,

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

---@type fun(question: lc.ui.Question)[]
["question_enter"] = {},

---@type fun()[]
["leave"] = {},
},

keys = {
Expand Down Expand Up @@ -226,6 +234,17 @@ storage = {
},
```

### plugins

[plugins list](#-plugins)

```lua
---@type table<string, boolean>
plugins = {
non_standalone = false,
},
```

### logging

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

---@type fun(question: lc.ui.Question)[]
["question_enter"] = {},

---@type fun()[]
["leave"] = {},
},
```

Expand All @@ -294,6 +316,8 @@ image_support = false,

- `menu` same as `Leet`

- `exit` close [leetcode.nvim]

- `console` opens console pop-up for currently opened question

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

## 🧩 Plugins

### Non-Standalone mode

To run [leetcode.nvim] in a non-standalone mode (i.e. not with argument or an empty Neovim session),
enable the `non_standalone` plugin in your config:

```lua
plugins = {
non_standalone = true,
}
```

You can then exit [leetcode.nvim] using `:Leet exit` command

## 🙌 Credits

- [Leetbuddy.nvim](https://github.com/Dhanus3133/Leetbuddy.nvim)
Expand Down
39 changes: 39 additions & 0 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/aee6584c-e099-4409-b114-1
cache = vim.fn.stdpath("cache") .. "/leetcode",
},

---@type table<string, boolean>
plugins = {
non_standalone = false,
},

---@type boolean
logging = true,

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

---@type fun(question: lc.ui.Question)[]
["question_enter"] = {},

---@type fun()[]
["leave"] = {},
},

keys = {
Expand Down Expand Up @@ -228,6 +236,17 @@ storage = {
},
```

### plugins

[插件列表](#-plugins)

```lua
---@type table<string, boolean>
plugins = {
non_standalone = false,
},
```

### logging

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

---@type fun(question: lc.ui.Question)[]
["question_enter"] = {},

---@type fun()[]
["leave"] = {},
},
```

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

- `menu``Leet` 相同

- `exit` 关闭 [leetcode.nvim]

- `console` 打开当前打开问题的控制台弹出窗口

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

## 🧩 Plugins

### Non-Standalone mode

要在非独立模式下运行 [leetcode.nvim](即不带参数或在空的 Neovim 会话中运行),
请在您的配置中启用 `non_standalone` 插件:

```lua
plugins = {
non_standalone = true,
}
```

你可以使用 `:Leet exit` 命令退出 [leetcode.nvim]

## 🙌 鸣谢

- [Leetbuddy.nvim](https://github.com/Dhanus3133/Leetbuddy.nvim)
Expand Down
12 changes: 12 additions & 0 deletions lua/leetcode-plugins/non_standalone/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---@class lc.plugins.non_standalone
local non_standalone = {}

non_standalone.opts = {
lazy = false,
}

function non_standalone.load() --
require("leetcode-plugins.non_standalone.leetcode")
end

return non_standalone
87 changes: 87 additions & 0 deletions lua/leetcode-plugins/non_standalone/leetcode.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
local leetcode = require("leetcode")
local config = require("leetcode.config")

local standalone = true
local prev_cwd = nil

---@param on_vimenter boolean
leetcode.should_skip = function(on_vimenter)
if on_vimenter then
if vim.fn.argc() ~= 1 then return true end

local usr_arg, arg = vim.fn.argv()[1], config.user.arg
if usr_arg ~= arg then return true end

local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
if #lines > 1 or (#lines == 1 and lines[1]:len() > 0) then
local log = require("leetcode.logger")
log.warn(("Failed to initialize: `%s` is not an empty buffer"):format(usr_arg))
return true
end
else
for _, buf_id in pairs(vim.api.nvim_list_bufs()) do
local bufinfo = vim.fn.getbufinfo(buf_id)[1]
if bufinfo and (bufinfo.listed == 1 and #bufinfo.windows > 0) then --
return false, true
end
end
end

return false
end

---@param on_vimenter boolean
leetcode.start = function(on_vimenter)
local skip, buflisted = leetcode.should_skip(on_vimenter)
if skip then --
return false
end

config.setup()

leetcode.setup_cmds()

local theme = require("leetcode.theme")
theme.setup()

if not on_vimenter then --
if buflisted then
prev_cwd = vim.fn.getcwd()
vim.cmd.tabe()
else
vim.cmd.enew()
end

standalone = not buflisted
end

vim.api.nvim_set_current_dir(config.storage.home:absolute())

local Menu = require("leetcode-ui.renderer.menu")
Menu():mount()

local utils = require("leetcode.utils")
utils.exec_hooks("enter")

return true
end

leetcode.stop = vim.schedule_wrap(function()
if standalone then return vim.cmd("qa!") end

_Lc_state.menu:unmount()

vim.api.nvim_create_user_command("Leet", require("leetcode.command").start_with_cmd, {
bar = true,
bang = true,
desc = "Open leetcode.nvim",
})

local utils = require("leetcode.utils")
utils.exec_hooks("leave")

if prev_cwd then
vim.api.nvim_set_current_dir(prev_cwd)
prev_cwd = nil
end
end)
3 changes: 2 additions & 1 deletion lua/leetcode-ui/lines/button/menu/exit.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local MenuButton = require("leetcode-ui.lines.button.menu")
local leetcode = require("leetcode")

---@class lc.ui.Button.Menu.Exit : lc.ui.Button.Menu
local MenuExitButton = MenuButton:extend("LeetMenuExitButton")
Expand All @@ -8,7 +9,7 @@ function MenuExitButton:init()
MenuExitButton.super.init(self, "Exit", {
icon = "󰩈",
sc = "q",
on_press = function() vim.cmd("qa!") end,
on_press = leetcode.stop,
})
end

Expand Down
2 changes: 1 addition & 1 deletion lua/leetcode-ui/lines/calendar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function Calendar:handle_res(res)
self:append(line):endl()
end

_Lc_menu:draw()
_Lc_state.menu:draw()
end

function Calendar:handle_submissions()
Expand Down
2 changes: 1 addition & 1 deletion lua/leetcode-ui/lines/solved.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function Solved:handle_res(res)
self:endl()
end

_Lc_menu:draw()
_Lc_state.menu:draw()
end

function Solved:update()
Expand Down
15 changes: 8 additions & 7 deletions lua/leetcode-ui/question.lua
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function Question:injector(code)
return table.concat(parts, "\n")
end

function Question:unmount()
function Question:_unmount()
if vim.v.dying ~= 0 then --
return
end
Expand All @@ -166,15 +166,16 @@ function Question:unmount()
vim.api.nvim_buf_delete(self.bufnr, { force = true, unload = false })
end

_Lc_questions = vim.tbl_filter(function(q) --
return q.bufnr ~= self.bufnr
end, _Lc_questions)
_Lc_state.questions = vim.tbl_filter(
function(q) return q.bufnr ~= self.bufnr end,
_Lc_state.questions
)

self = nil
end)
end

function Question:_unmount()
function Question:unmount()
if vim.api.nvim_win_is_valid(self.winid) then vim.api.nvim_win_close(self.winid, true) end
end

Expand All @@ -183,14 +184,14 @@ function Question:autocmds()
vim.api.nvim_create_autocmd("WinClosed", {
group = group,
pattern = tostring(self.winid),
callback = function() self:unmount() end,
callback = function() self:_unmount() end,
})
end

function Question:handle_mount()
self:create_buffer()

table.insert(_Lc_questions, self)
table.insert(_Lc_state.questions, self)

self.description = Description(self):mount()
self.console = Console(self)
Expand Down
2 changes: 2 additions & 0 deletions lua/leetcode-ui/renderer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ function Renderer:draw(component)
self.bufnr = component.bufnr
self.winid = component.winid

if not vim.api.nvim_buf_is_valid(self.bufnr) then return end

self:map("n", keys.confirm, function() self:handle_press() end)

self:clear_keymaps()
Expand Down
20 changes: 19 additions & 1 deletion lua/leetcode-ui/renderer/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,24 @@ function Menu:apply_options()
})
end

function Menu:unmount()
if vim.v.dying ~= 0 then --
return
end

require("leetcode.command").q_close_all()

if self.winid and vim.api.nvim_win_is_valid(self.winid) then
vim.api.nvim_win_close(self.winid, true)
end

vim.schedule(function()
if self.bufnr and vim.api.nvim_buf_is_valid(self.bufnr) then
vim.api.nvim_buf_delete(self.bufnr, { force = true })
end
end)
end

function Menu:remount()
if self.winid and api.nvim_win_is_valid(self.winid) then --
api.nvim_win_close(self.winid, true)
Expand Down Expand Up @@ -195,7 +213,7 @@ function Menu:init()
self.bufnr = api.nvim_get_current_buf()
self.winid = api.nvim_get_current_win()

_Lc_menu = self
_Lc_state.menu = self
end

---@type fun(): lc.ui.Menu
Expand Down
Loading

0 comments on commit 7bd3e3d

Please sign in to comment.