From ea579dfd3bf6468cf232c5ad9120002c0e940540 Mon Sep 17 00:00:00 2001 From: kawre Date: Tue, 23 Jan 2024 18:22:15 +0100 Subject: [PATCH 01/29] feat: nonstandalone plugin --- lua/leetcode-plugins/nonstandalone.lua | 65 ++++++++++++++++++++++++++ lua/leetcode.lua | 4 +- lua/leetcode/config/init.lua | 2 +- 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 lua/leetcode-plugins/nonstandalone.lua diff --git a/lua/leetcode-plugins/nonstandalone.lua b/lua/leetcode-plugins/nonstandalone.lua new file mode 100644 index 00000000..2fe904f7 --- /dev/null +++ b/lua/leetcode-plugins/nonstandalone.lua @@ -0,0 +1,65 @@ +local leetcode = require("leetcode") +local config = require("leetcode.config") + +---@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 + 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 + + vim.api.nvim_set_current_dir(config.storage.home:absolute()) + + leetcode.setup_cmds() + + local utils = require("leetcode.utils") + utils.exec_hooks("LeetEnter") + + local theme = require("leetcode.theme") + theme.setup() + + if not on_vimenter then -- + if buflisted then + vim.cmd.tabe() + else + vim.cmd.enew() + end + end + + local Menu = require("leetcode-ui.renderer.menu") + Menu():mount() + + return true +end + +---@class lc.plugins.cn +local nonstandalone = {} + +function nonstandalone.load() end + +return nonstandalone diff --git a/lua/leetcode.lua b/lua/leetcode.lua index a2153d04..eb0e2b92 100644 --- a/lua/leetcode.lua +++ b/lua/leetcode.lua @@ -5,6 +5,8 @@ local log = require("leetcode.logger") local leetcode = {} ---@param on_vimenter boolean +--- +---@return boolean, boolean? function leetcode.should_skip(on_vimenter) if on_vimenter then if vim.fn.argc() ~= 1 then return true end @@ -21,7 +23,7 @@ function leetcode.should_skip(on_vimenter) 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 true + return true, true end end end diff --git a/lua/leetcode/config/init.lua b/lua/leetcode/config/init.lua index fe4e8802..fd516dd1 100644 --- a/lua/leetcode/config/init.lua +++ b/lua/leetcode/config/init.lua @@ -85,7 +85,7 @@ function config.validate() end function config.load_plugins() - local plugins = {} + local plugins = { "nonstandalone" } if config.user.cn.enabled then config.translator = config.user.cn.translator From 8432b7961da106078c1514c76d67fe7dac445a35 Mon Sep 17 00:00:00 2001 From: kawre Date: Tue, 23 Jan 2024 18:35:41 +0100 Subject: [PATCH 02/29] fix: cleanup --- lua/leetcode-plugins/nonstandalone.lua | 3 ++- lua/leetcode.lua | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lua/leetcode-plugins/nonstandalone.lua b/lua/leetcode-plugins/nonstandalone.lua index 2fe904f7..923d899f 100644 --- a/lua/leetcode-plugins/nonstandalone.lua +++ b/lua/leetcode-plugins/nonstandalone.lua @@ -11,6 +11,7 @@ leetcode.should_skip = function(on_vimenter) 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 @@ -57,7 +58,7 @@ leetcode.start = function(on_vimenter) return true end ----@class lc.plugins.cn +---@class lc.plugins.nonstandalone local nonstandalone = {} function nonstandalone.load() end diff --git a/lua/leetcode.lua b/lua/leetcode.lua index eb0e2b92..8df831c2 100644 --- a/lua/leetcode.lua +++ b/lua/leetcode.lua @@ -23,7 +23,7 @@ function leetcode.should_skip(on_vimenter) 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 true, true + return true end end end @@ -42,7 +42,6 @@ function leetcode.start(on_vimenter) vim.api.nvim_set_current_dir(config.storage.home:absolute()) leetcode.setup_cmds() - -- config.load_plugins() local utils = require("leetcode.utils") utils.exec_hooks("LeetEnter") From a42f5feb8dc321a2362136cc6a6b241c8deec7b4 Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Mon, 22 Jan 2024 10:49:35 -0800 Subject: [PATCH 03/29] feat: support starting plugin with Leet cmd --- README.md | 15 ++++- lua/leetcode-plugins/nonstandalone.lua | 67 ++++++++++++++++------ lua/leetcode-ui/lines/button/menu/exit.lua | 2 +- lua/leetcode-ui/renderer/menu.lua | 64 +++++++++++++++++++-- lua/leetcode.lua | 8 +-- lua/leetcode/command/init.lua | 18 +----- 6 files changed, 127 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index ea1f8bb4..bfcb720a 100644 --- a/README.md +++ b/README.md @@ -245,6 +245,8 @@ image_support = false, -- setting this to `true` will disable question descripti - `menu` same as `Leet` +- [`exit`](#exiting-leetcodenvim) closes [leetcode.nvim] + - `console` opens console pop-up for currently opened question - `info` opens a pop-up containing information about the currently opened question @@ -292,17 +294,26 @@ Leet list status= difficulty= ## 🚀 Usage This plugin is meant to be used within a **fresh** [Neovim] instance. -Meaning that to launch [leetcode.nvim] you **have** to pass +Meaning that to launch [leetcode.nvim] you can to pass [`arg`](#arg) as the _first and **only**_ [Neovim] argument ``` nvim leetcode.nvim ``` +You can also use the `:Leet` command to open [leetcode.nvim] at any time. + ### Switching between questions To switch between questions, use `Leet tabs` +### Exiting [leetcode.nvim] + +If [leetcode.nvim] was started from the cmdline with `nvim leetcode.nvim`, pressing `q` in the menu +or running `:Leet exit` will close nvim. If [leetcode.nvim] was started from a normal nvim session using +the `:Leet` command, pressing `q` in the menu or running `:Leet exit` will only close the [leetcode.nvim] +UI. + ### Sign In It is **required** to be **signed-in** to use [leetcode.nvim] @@ -329,6 +340,8 @@ return { } ``` +> Note: If you intend to use `:Leet` to open [leetcode.nvim], you can lazy load the plugin normally. + ## ✅ Todo - \[x\] CN version diff --git a/lua/leetcode-plugins/nonstandalone.lua b/lua/leetcode-plugins/nonstandalone.lua index 923d899f..e86d8c1a 100644 --- a/lua/leetcode-plugins/nonstandalone.lua +++ b/lua/leetcode-plugins/nonstandalone.lua @@ -1,6 +1,9 @@ local leetcode = require("leetcode") local config = require("leetcode.config") +local started_with_cmd = false +local did_init = false + ---@param on_vimenter boolean leetcode.should_skip = function(on_vimenter) if on_vimenter then @@ -16,12 +19,13 @@ leetcode.should_skip = function(on_vimenter) 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 + -- 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 + return false end return false @@ -29,10 +33,11 @@ end ---@param on_vimenter boolean leetcode.start = function(on_vimenter) - local skip, buflisted = leetcode.should_skip(on_vimenter) + local skip = leetcode.should_skip(on_vimenter) if skip then -- return false end + started_with_cmd = not on_vimenter vim.api.nvim_set_current_dir(config.storage.home:absolute()) @@ -44,16 +49,13 @@ leetcode.start = function(on_vimenter) local theme = require("leetcode.theme") theme.setup() - if not on_vimenter then -- - if buflisted then - vim.cmd.tabe() - else - vim.cmd.enew() - end + if did_init then + _Lc_Menu:mount() + else + local Menu = require("leetcode-ui.renderer.menu") + Menu():mount() end - - local Menu = require("leetcode-ui.renderer.menu") - Menu():mount() + did_init = true return true end @@ -61,6 +63,37 @@ end ---@class lc.plugins.nonstandalone local nonstandalone = {} -function nonstandalone.load() end +function nonstandalone.load() + local function exit() + if started_with_cmd then + for _, tab in ipairs(require("leetcode.utils").question_tabs()) do + vim.cmd.tabclose({ args = { tab.tabpage } }) + end + if _Lc_Menu:is_open() then _Lc_Menu:close() end + else + vim.cmd.quitall() + end + end + + local command = require("leetcode.command") + + -- command.exit = exit + + command.commands.exit = { exit } + + command.menu = function() leetcode.start(not started_with_cmd) end + + -- command.commands.menu = { menu } + + local MenuExitButton = require("leetcode-ui.lines.button.menu.exit") + + function MenuExitButton:init() + MenuExitButton.super.init(self, "Exit", { + icon = "󰩈", + sc = "q", + on_press = exit, + }) + end +end return nonstandalone diff --git a/lua/leetcode-ui/lines/button/menu/exit.lua b/lua/leetcode-ui/lines/button/menu/exit.lua index f0c25ec7..060e1258 100644 --- a/lua/leetcode-ui/lines/button/menu/exit.lua +++ b/lua/leetcode-ui/lines/button/menu/exit.lua @@ -8,7 +8,7 @@ function MenuExitButton:init() MenuExitButton.super.init(self, "Exit", { icon = "󰩈", sc = "q", - on_press = vim.cmd.quitall, + on_press = function() vim.cmd.quitall() end, }) end diff --git a/lua/leetcode-ui/renderer/menu.lua b/lua/leetcode-ui/renderer/menu.lua index 4caf4771..b4183027 100644 --- a/lua/leetcode-ui/renderer/menu.lua +++ b/lua/leetcode-ui/renderer/menu.lua @@ -21,6 +21,29 @@ function Menu:draw() return self end +function Menu:is_open() + return self.winid + and vim.api.nvim_win_is_valid(self.winid) + and vim.api.nvim_win_get_buf(self.winid) == self.bufnr +end + +function Menu:close() + if not self.winid or not vim.api.nvim_win_is_valid(self.winid) then return end + vim.api.nvim_buf_delete(self.bufnr, { force = true }) + self.bufnr = nil + -- close the menu window if it is not the last window + for _, win in ipairs(vim.api.nvim_tabpage_list_wins(0)) do + if + win ~= self.winid + and vim.api.nvim_win_get_config(win).relative == "" + and vim.api.nvim_win_is_valid(self.winid) + then + vim.api.nvim_win_close(self.winid, true) + break + end + end +end + ---@private function Menu:autocmds() local group_id = vim.api.nvim_create_augroup("leetcode_menu", { clear = true }) @@ -82,9 +105,6 @@ function Menu:set_page(name) end function Menu:apply_options() - vim.api.nvim_buf_set_name(self.bufnr, "") - pcall(vim.diagnostic.disable, self.bufnr) - utils.set_buf_opts(self.bufnr, { modifiable = false, buflisted = false, @@ -110,6 +130,41 @@ function Menu:apply_options() end function Menu:mount() + if self.winid and vim.api.nvim_win_is_valid(self.winid) then + vim.api.nvim_set_current_win(self.winid) + else + vim.cmd.split() + self.winid = vim.api.nvim_get_current_win() + -- Teardown all other windows (we are initializing the UI) + for _, win in ipairs(vim.api.nvim_tabpage_list_wins(0)) do + if win ~= self.winid and vim.api.nvim_win_is_valid(win) then + vim.api.nvim_win_close(win, true) + end + end + end + if self.bufnr == nil or not vim.api.nvim_buf_is_valid(self.bufnr) then + self.bufnr = vim.api.nvim_create_buf(false, true) + -- Ensure we don't keep the buffer handle around after the buffer is + -- deleted. + vim.api.nvim_create_autocmd({ + "BufUnload", + "BufDelete", + "BufWipeout", + }, { + buffer = self.bufnr, + callback = function() self.bufnr = nil end, + }) + -- Ensure we don't try to clear nonexistent maps in draw() when + -- we have just created the buffer. + self._.keymaps = {} + end + -- Ensure the correct buffer is displayed. + -- We need to set the buffer if we've just created the window, + -- and if the window was not just created then we need to ensure + -- the buffer is set in case the user has changed it. + if vim.api.nvim_win_get_buf(self.winid) ~= self.bufnr then + vim.api.nvim_win_set_buf(self.winid, self.bufnr) + end if cookie.get() then self:set_page("loading") @@ -144,9 +199,6 @@ function Menu:init() } self.maps = {} - self.bufnr = vim.api.nvim_get_current_buf() - self.winid = vim.api.nvim_get_current_win() - _Lc_Menu = self end diff --git a/lua/leetcode.lua b/lua/leetcode.lua index 8df831c2..0b9dc9fc 100644 --- a/lua/leetcode.lua +++ b/lua/leetcode.lua @@ -1,5 +1,6 @@ local config = require("leetcode.config") local log = require("leetcode.logger") +local utils = require("leetcode.utils") ---@class lc.LeetCode local leetcode = {} @@ -43,16 +44,11 @@ function leetcode.start(on_vimenter) leetcode.setup_cmds() - local utils = require("leetcode.utils") utils.exec_hooks("LeetEnter") local theme = require("leetcode.theme") theme.setup() - if not on_vimenter then -- - vim.cmd.enew() - end - local Menu = require("leetcode-ui.renderer.menu") Menu():mount() @@ -63,7 +59,7 @@ end function leetcode.setup(cfg) config.apply(cfg or {}) - vim.api.nvim_create_user_command("Leet", require("leetcode.command").start_with_cmd, { + vim.api.nvim_create_user_command("Leet", function() require("leetcode").start(false) end, { bar = true, bang = true, desc = "Open leetcode.nvim", diff --git a/lua/leetcode/command/init.lua b/lua/leetcode/command/init.lua index 3aed8cee..15d6927c 100644 --- a/lua/leetcode/command/init.lua +++ b/lua/leetcode/command/init.lua @@ -153,23 +153,9 @@ function cmd.random_question(opts) Question(item):mount() end -function cmd.start_with_cmd() - local leetcode = require("leetcode") - - if leetcode.start(false) then - cmd.menu() - else - log.warn("Failed to initialize") - end -end - function cmd.menu() - local ok, tabp = pcall(vim.api.nvim_win_get_tabpage, _Lc_Menu.winid) - if ok then - vim.api.nvim_set_current_tabpage(tabp) - else - log.error(tabp) - end + -- start the plugin or open the menu + require("leetcode").start(false) end function cmd.yank() From 69d49040ef34765d9a6031bfb4f7860bd0eb6d24 Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Mon, 22 Jan 2024 10:49:35 -0800 Subject: [PATCH 04/29] feat: support starting plugin with Leet cmd --- README.md | 15 +++++- lua/leetcode-plugins/nonstandalone.lua | 56 ++++++++++++++-------- lua/leetcode-ui/renderer/menu.lua | 64 +++++++++++++++++++++++--- lua/leetcode.lua | 8 +--- lua/leetcode/command/init.lua | 18 +------- 5 files changed, 114 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index ea1f8bb4..bfcb720a 100644 --- a/README.md +++ b/README.md @@ -245,6 +245,8 @@ image_support = false, -- setting this to `true` will disable question descripti - `menu` same as `Leet` +- [`exit`](#exiting-leetcodenvim) closes [leetcode.nvim] + - `console` opens console pop-up for currently opened question - `info` opens a pop-up containing information about the currently opened question @@ -292,17 +294,26 @@ Leet list status= difficulty= ## 🚀 Usage This plugin is meant to be used within a **fresh** [Neovim] instance. -Meaning that to launch [leetcode.nvim] you **have** to pass +Meaning that to launch [leetcode.nvim] you can to pass [`arg`](#arg) as the _first and **only**_ [Neovim] argument ``` nvim leetcode.nvim ``` +You can also use the `:Leet` command to open [leetcode.nvim] at any time. + ### Switching between questions To switch between questions, use `Leet tabs` +### Exiting [leetcode.nvim] + +If [leetcode.nvim] was started from the cmdline with `nvim leetcode.nvim`, pressing `q` in the menu +or running `:Leet exit` will close nvim. If [leetcode.nvim] was started from a normal nvim session using +the `:Leet` command, pressing `q` in the menu or running `:Leet exit` will only close the [leetcode.nvim] +UI. + ### Sign In It is **required** to be **signed-in** to use [leetcode.nvim] @@ -329,6 +340,8 @@ return { } ``` +> Note: If you intend to use `:Leet` to open [leetcode.nvim], you can lazy load the plugin normally. + ## ✅ Todo - \[x\] CN version diff --git a/lua/leetcode-plugins/nonstandalone.lua b/lua/leetcode-plugins/nonstandalone.lua index 923d899f..5764aef7 100644 --- a/lua/leetcode-plugins/nonstandalone.lua +++ b/lua/leetcode-plugins/nonstandalone.lua @@ -1,6 +1,9 @@ local leetcode = require("leetcode") local config = require("leetcode.config") +local started_with_cmd +local did_init = false + ---@param on_vimenter boolean leetcode.should_skip = function(on_vimenter) if on_vimenter then @@ -15,13 +18,6 @@ leetcode.should_skip = function(on_vimenter) 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 @@ -29,10 +25,11 @@ end ---@param on_vimenter boolean leetcode.start = function(on_vimenter) - local skip, buflisted = leetcode.should_skip(on_vimenter) + local skip = leetcode.should_skip(on_vimenter) if skip then -- return false end + if started_with_cmd == nil then started_with_cmd = not on_vimenter end vim.api.nvim_set_current_dir(config.storage.home:absolute()) @@ -44,16 +41,13 @@ leetcode.start = function(on_vimenter) local theme = require("leetcode.theme") theme.setup() - if not on_vimenter then -- - if buflisted then - vim.cmd.tabe() - else - vim.cmd.enew() - end + if did_init then + _Lc_Menu:mount() + else + local Menu = require("leetcode-ui.renderer.menu") + vim.schedule(function() Menu():mount() end) end - - local Menu = require("leetcode-ui.renderer.menu") - Menu():mount() + did_init = true return true end @@ -61,6 +55,32 @@ end ---@class lc.plugins.nonstandalone local nonstandalone = {} -function nonstandalone.load() end +function nonstandalone.load() + local function exit() + if started_with_cmd then + for _, tab in ipairs(require("leetcode.utils").question_tabs()) do + vim.cmd.tabclose({ args = { tab.tabpage } }) + end + if _Lc_Menu:is_open() then _Lc_Menu:close() end + else + vim.cmd.quitall() + end + end + local function menu() leetcode.start(not started_with_cmd) end + + local command = require("leetcode.command") + command.commands.exit = { exit } + command.commands.menu = { menu } + + local MenuExitButton = require("leetcode-ui.lines.button.menu.exit") + + function MenuExitButton:init() + MenuExitButton.super.init(self, "Exit", { + icon = "󰩈", + sc = "q", + on_press = exit, + }) + end +end return nonstandalone diff --git a/lua/leetcode-ui/renderer/menu.lua b/lua/leetcode-ui/renderer/menu.lua index 4caf4771..b4183027 100644 --- a/lua/leetcode-ui/renderer/menu.lua +++ b/lua/leetcode-ui/renderer/menu.lua @@ -21,6 +21,29 @@ function Menu:draw() return self end +function Menu:is_open() + return self.winid + and vim.api.nvim_win_is_valid(self.winid) + and vim.api.nvim_win_get_buf(self.winid) == self.bufnr +end + +function Menu:close() + if not self.winid or not vim.api.nvim_win_is_valid(self.winid) then return end + vim.api.nvim_buf_delete(self.bufnr, { force = true }) + self.bufnr = nil + -- close the menu window if it is not the last window + for _, win in ipairs(vim.api.nvim_tabpage_list_wins(0)) do + if + win ~= self.winid + and vim.api.nvim_win_get_config(win).relative == "" + and vim.api.nvim_win_is_valid(self.winid) + then + vim.api.nvim_win_close(self.winid, true) + break + end + end +end + ---@private function Menu:autocmds() local group_id = vim.api.nvim_create_augroup("leetcode_menu", { clear = true }) @@ -82,9 +105,6 @@ function Menu:set_page(name) end function Menu:apply_options() - vim.api.nvim_buf_set_name(self.bufnr, "") - pcall(vim.diagnostic.disable, self.bufnr) - utils.set_buf_opts(self.bufnr, { modifiable = false, buflisted = false, @@ -110,6 +130,41 @@ function Menu:apply_options() end function Menu:mount() + if self.winid and vim.api.nvim_win_is_valid(self.winid) then + vim.api.nvim_set_current_win(self.winid) + else + vim.cmd.split() + self.winid = vim.api.nvim_get_current_win() + -- Teardown all other windows (we are initializing the UI) + for _, win in ipairs(vim.api.nvim_tabpage_list_wins(0)) do + if win ~= self.winid and vim.api.nvim_win_is_valid(win) then + vim.api.nvim_win_close(win, true) + end + end + end + if self.bufnr == nil or not vim.api.nvim_buf_is_valid(self.bufnr) then + self.bufnr = vim.api.nvim_create_buf(false, true) + -- Ensure we don't keep the buffer handle around after the buffer is + -- deleted. + vim.api.nvim_create_autocmd({ + "BufUnload", + "BufDelete", + "BufWipeout", + }, { + buffer = self.bufnr, + callback = function() self.bufnr = nil end, + }) + -- Ensure we don't try to clear nonexistent maps in draw() when + -- we have just created the buffer. + self._.keymaps = {} + end + -- Ensure the correct buffer is displayed. + -- We need to set the buffer if we've just created the window, + -- and if the window was not just created then we need to ensure + -- the buffer is set in case the user has changed it. + if vim.api.nvim_win_get_buf(self.winid) ~= self.bufnr then + vim.api.nvim_win_set_buf(self.winid, self.bufnr) + end if cookie.get() then self:set_page("loading") @@ -144,9 +199,6 @@ function Menu:init() } self.maps = {} - self.bufnr = vim.api.nvim_get_current_buf() - self.winid = vim.api.nvim_get_current_win() - _Lc_Menu = self end diff --git a/lua/leetcode.lua b/lua/leetcode.lua index 8df831c2..0b9dc9fc 100644 --- a/lua/leetcode.lua +++ b/lua/leetcode.lua @@ -1,5 +1,6 @@ local config = require("leetcode.config") local log = require("leetcode.logger") +local utils = require("leetcode.utils") ---@class lc.LeetCode local leetcode = {} @@ -43,16 +44,11 @@ function leetcode.start(on_vimenter) leetcode.setup_cmds() - local utils = require("leetcode.utils") utils.exec_hooks("LeetEnter") local theme = require("leetcode.theme") theme.setup() - if not on_vimenter then -- - vim.cmd.enew() - end - local Menu = require("leetcode-ui.renderer.menu") Menu():mount() @@ -63,7 +59,7 @@ end function leetcode.setup(cfg) config.apply(cfg or {}) - vim.api.nvim_create_user_command("Leet", require("leetcode.command").start_with_cmd, { + vim.api.nvim_create_user_command("Leet", function() require("leetcode").start(false) end, { bar = true, bang = true, desc = "Open leetcode.nvim", diff --git a/lua/leetcode/command/init.lua b/lua/leetcode/command/init.lua index 3aed8cee..15d6927c 100644 --- a/lua/leetcode/command/init.lua +++ b/lua/leetcode/command/init.lua @@ -153,23 +153,9 @@ function cmd.random_question(opts) Question(item):mount() end -function cmd.start_with_cmd() - local leetcode = require("leetcode") - - if leetcode.start(false) then - cmd.menu() - else - log.warn("Failed to initialize") - end -end - function cmd.menu() - local ok, tabp = pcall(vim.api.nvim_win_get_tabpage, _Lc_Menu.winid) - if ok then - vim.api.nvim_set_current_tabpage(tabp) - else - log.error(tabp) - end + -- start the plugin or open the menu + require("leetcode").start(false) end function cmd.yank() From f80718393a1f4303f30eac1a700566decde395b7 Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Tue, 23 Jan 2024 12:35:24 -0800 Subject: [PATCH 05/29] restore readme.md --- README.md | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/README.md b/README.md index bfcb720a..ea1f8bb4 100644 --- a/README.md +++ b/README.md @@ -245,8 +245,6 @@ image_support = false, -- setting this to `true` will disable question descripti - `menu` same as `Leet` -- [`exit`](#exiting-leetcodenvim) closes [leetcode.nvim] - - `console` opens console pop-up for currently opened question - `info` opens a pop-up containing information about the currently opened question @@ -294,26 +292,17 @@ Leet list status= difficulty= ## 🚀 Usage This plugin is meant to be used within a **fresh** [Neovim] instance. -Meaning that to launch [leetcode.nvim] you can to pass +Meaning that to launch [leetcode.nvim] you **have** to pass [`arg`](#arg) as the _first and **only**_ [Neovim] argument ``` nvim leetcode.nvim ``` -You can also use the `:Leet` command to open [leetcode.nvim] at any time. - ### Switching between questions To switch between questions, use `Leet tabs` -### Exiting [leetcode.nvim] - -If [leetcode.nvim] was started from the cmdline with `nvim leetcode.nvim`, pressing `q` in the menu -or running `:Leet exit` will close nvim. If [leetcode.nvim] was started from a normal nvim session using -the `:Leet` command, pressing `q` in the menu or running `:Leet exit` will only close the [leetcode.nvim] -UI. - ### Sign In It is **required** to be **signed-in** to use [leetcode.nvim] @@ -340,8 +329,6 @@ return { } ``` -> Note: If you intend to use `:Leet` to open [leetcode.nvim], you can lazy load the plugin normally. - ## ✅ Todo - \[x\] CN version From 09fbcaf3fc4ec6d5720836e90698cc944f084127 Mon Sep 17 00:00:00 2001 From: kawre Date: Tue, 23 Jan 2024 23:16:45 +0100 Subject: [PATCH 06/29] fix: cleanup --- lua/leetcode/command/init.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/leetcode/command/init.lua b/lua/leetcode/command/init.lua index 15800d78..3aed8cee 100644 --- a/lua/leetcode/command/init.lua +++ b/lua/leetcode/command/init.lua @@ -167,7 +167,6 @@ function cmd.menu() local ok, tabp = pcall(vim.api.nvim_win_get_tabpage, _Lc_Menu.winid) if ok then vim.api.nvim_set_current_tabpage(tabp) - vim.api.nvim_set_current_tabpage(tabp) else log.error(tabp) end From 572356b72a347cb4e8d527dd2d8c7f71772adbe6 Mon Sep 17 00:00:00 2001 From: kawre Date: Tue, 23 Jan 2024 23:37:46 +0100 Subject: [PATCH 07/29] fix: don't change cwd when nonstandalone --- lua/leetcode-plugins/nonstandalone/leetcode.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lua/leetcode-plugins/nonstandalone/leetcode.lua b/lua/leetcode-plugins/nonstandalone/leetcode.lua index c31f178d..096e2d1c 100644 --- a/lua/leetcode-plugins/nonstandalone/leetcode.lua +++ b/lua/leetcode-plugins/nonstandalone/leetcode.lua @@ -36,8 +36,6 @@ leetcode.start = function(on_vimenter) return false end - vim.api.nvim_set_current_dir(config.storage.home:absolute()) - leetcode.setup_cmds() local utils = require("leetcode.utils") @@ -55,6 +53,9 @@ leetcode.start = function(on_vimenter) end end + --- TODO: maybe cache the previous cwd and restore it when quitting? + if standalone then vim.api.nvim_set_current_dir(config.storage.home:absolute()) end + local Menu = require("leetcode-ui.renderer.menu") Menu():mount() From 0fa8b8e8b2f1f6f83501ae1f331d0d74b722c333 Mon Sep 17 00:00:00 2001 From: kawre Date: Wed, 24 Jan 2024 00:09:50 +0100 Subject: [PATCH 08/29] fix: small fixes --- lua/leetcode-plugins/nonstandalone/init.lua | 2 +- lua/leetcode-plugins/nonstandalone/leetcode.lua | 2 ++ lua/leetcode-ui/renderer/menu.lua | 4 +--- lua/leetcode/config/template.lua | 4 ++++ lua/leetcode/utils.lua | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lua/leetcode-plugins/nonstandalone/init.lua b/lua/leetcode-plugins/nonstandalone/init.lua index 5c57fb4c..bd0f76ff 100644 --- a/lua/leetcode-plugins/nonstandalone/init.lua +++ b/lua/leetcode-plugins/nonstandalone/init.lua @@ -2,9 +2,9 @@ local nonstandalone = {} function nonstandalone.load() -- - require("leetcode-plugins.nonstandalone.command") require("leetcode-plugins.nonstandalone.exit") require("leetcode-plugins.nonstandalone.leetcode") + require("leetcode-plugins.nonstandalone.command") end return nonstandalone diff --git a/lua/leetcode-plugins/nonstandalone/leetcode.lua b/lua/leetcode-plugins/nonstandalone/leetcode.lua index 096e2d1c..08849064 100644 --- a/lua/leetcode-plugins/nonstandalone/leetcode.lua +++ b/lua/leetcode-plugins/nonstandalone/leetcode.lua @@ -73,4 +73,6 @@ leetcode.stop = function() bang = true, desc = "Open leetcode.nvim", }) + + require("leetcode.utils").exec_hooks("LeetLeave") end diff --git a/lua/leetcode-ui/renderer/menu.lua b/lua/leetcode-ui/renderer/menu.lua index 934b2d9e..19b43928 100644 --- a/lua/leetcode-ui/renderer/menu.lua +++ b/lua/leetcode-ui/renderer/menu.lua @@ -133,9 +133,7 @@ function Menu:apply_options() end function Menu:unmount() - for _, q in ipairs(_Lc_questions) do - q:unmount() - end + require("leetcode.command").q_close_all() pcall(vim.api.nvim_buf_delete, self.bufnr, { force = true }) pcall(vim.api.nvim_win_close, self.winid, true) diff --git a/lua/leetcode/config/template.lua b/lua/leetcode/config/template.lua index e525a8d7..bf19794b 100644 --- a/lua/leetcode/config/template.lua +++ b/lua/leetcode/config/template.lua @@ -26,6 +26,7 @@ ---@alias lc.hook ---| "LeetEnter" +---| "LeetLeave" ---| "LeetQuestionNew" ---@alias lc.size @@ -109,6 +110,9 @@ local M = { ---@type fun(question: lc.ui.Question)[] LeetQuestionNew = {}, + + ---@type fun()[] + LeetLeave = {}, }, keys = { diff --git a/lua/leetcode/utils.lua b/lua/leetcode/utils.lua index 46f21adb..b86fb194 100644 --- a/lua/leetcode/utils.lua +++ b/lua/leetcode/utils.lua @@ -77,7 +77,7 @@ function utils.exec_hooks(event, ...) if not fns then log.error("unknown hook event: " .. event) end for i, fn in ipairs(fns) do - local ok, msg = pcall(fn, ...) + local ok, msg = pcall(vim.schedule_wrap(fn), ...) if not ok then log.error(("bad hook #%d in `%s` event: %s"):format(i, event, msg)) end end end From 029826bd3ddd7e71da536943e4e8a00c5ee2557d Mon Sep 17 00:00:00 2001 From: kawre Date: Wed, 24 Jan 2024 16:05:56 +0100 Subject: [PATCH 09/29] feat: lazy plugs --- lua/leetcode-plugins/cn/init.lua | 6 ++++ lua/leetcode.lua | 3 +- lua/leetcode/config/init.lua | 47 ++++++++++++++++++++++++-------- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/lua/leetcode-plugins/cn/init.lua b/lua/leetcode-plugins/cn/init.lua index b0434d36..2ccd1880 100644 --- a/lua/leetcode-plugins/cn/init.lua +++ b/lua/leetcode-plugins/cn/init.lua @@ -1,8 +1,14 @@ ---@class lc.plugins.cn local cn = {} +cn.opts = { + lazy = true, +} + function cn.load() local config = require("leetcode.config") + + config.translator = config.user.cn.translator config.domain = "cn" config.is_cn = true diff --git a/lua/leetcode.lua b/lua/leetcode.lua index a2153d04..8e887b7d 100644 --- a/lua/leetcode.lua +++ b/lua/leetcode.lua @@ -37,10 +37,11 @@ function leetcode.start(on_vimenter) return false end + config.setup() + vim.api.nvim_set_current_dir(config.storage.home:absolute()) leetcode.setup_cmds() - -- config.load_plugins() local utils = require("leetcode.utils") utils.exec_hooks("LeetEnter") diff --git a/lua/leetcode/config/init.lua b/lua/leetcode/config/init.lua index fe4e8802..001e2c6d 100644 --- a/lua/leetcode/config/init.lua +++ b/lua/leetcode/config/init.lua @@ -7,7 +7,9 @@ _Lc_questions = {} ---@type lc.ui.menu _Lc_Menu = {} ---@diagnostic disable-line ----@class lc.Settings +local lazy_plugs = {} + +---@class lc.Config local config = { default = template, user = template, @@ -33,31 +35,34 @@ local config = { --- ---@param cfg lc.UserConfig Configurations to be merged. function config.apply(cfg) - cfg.storage = cfg.storage or {} + config.user = vim.tbl_deep_extend("force", config.default, cfg or {}) + config.load_plugins() +end + +function config.setup() + config.validate() -- deprecate `directory` config - if cfg.directory then + if config.user.directory then local log = require("leetcode.logger") - log.warn("Config: `directory` is deprecated. Use `storage.home` instead.") - cfg.storage.home = cfg.directory + log.warn("leetcode.nvim config: `directory` is deprecated. Use `storage.home` instead.") + config.user.storage.home = config.user.directory end - cfg.storage = vim.tbl_map(vim.fn.expand, cfg.storage) - - config.user = vim.tbl_deep_extend("force", config.default, cfg) + config.user.storage = vim.tbl_map(vim.fn.expand, config.user.storage) config.debug = config.user.debug or false ---@diagnostic disable-line config.lang = config.user.lang - config.validate() - config.storage.home = P:new(config.user.storage.home) ---@diagnostic disable-line config.storage.home:mkdir() config.storage.cache = P:new(config.user.storage.cache) ---@diagnostic disable-line config.storage.cache:mkdir() - config.load_plugins() + for _, plug_load_fn in ipairs(lazy_plugs) do + plug_load_fn() + end end function config.validate() @@ -87,6 +92,26 @@ end function config.load_plugins() local plugins = {} + if config.user.cn.enabled then table.insert(plugins, "cn") end + + for _, plugin in ipairs(plugins) do + local ok, plug = pcall(require, "leetcode-plugins." .. plugin) + if ok then + if not (plug.opts or {}).lazy then + plug.load() + else + table.insert(lazy_plugs, plug.load) + end + else + local log = require("leetcode.logger") + log.error(plug) + end + end +end + +function config.load_high_priority_plugins() + local plugins = {} + if config.user.cn.enabled then config.translator = config.user.cn.translator table.insert(plugins, "cn") From 0c12d71a7ca689d79d1a7607372a85b1d8aeb041 Mon Sep 17 00:00:00 2001 From: kawre Date: Wed, 24 Jan 2024 16:09:38 +0100 Subject: [PATCH 10/29] feat: don't lazy load nonstandalone plugin --- lua/leetcode-plugins/nonstandalone/init.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lua/leetcode-plugins/nonstandalone/init.lua b/lua/leetcode-plugins/nonstandalone/init.lua index bd0f76ff..0fd5cc54 100644 --- a/lua/leetcode-plugins/nonstandalone/init.lua +++ b/lua/leetcode-plugins/nonstandalone/init.lua @@ -1,6 +1,10 @@ ---@class lc.plugins.nonstandalone local nonstandalone = {} +nonstandalone.opts = { + lazy = false, +} + function nonstandalone.load() -- require("leetcode-plugins.nonstandalone.exit") require("leetcode-plugins.nonstandalone.leetcode") From 0acbc91e2d791356da4121417017036da676ac4e Mon Sep 17 00:00:00 2001 From: kawre Date: Wed, 24 Jan 2024 16:34:54 +0100 Subject: [PATCH 11/29] fix: cleanup --- .../nonstandalone/leetcode.lua | 8 ++++-- lua/leetcode-ui/renderer/menu.lua | 23 --------------- lua/leetcode.lua | 14 ++++++---- lua/leetcode/command/init.lua | 7 +---- lua/leetcode/config/init.lua | 28 ++++--------------- 5 files changed, 19 insertions(+), 61 deletions(-) diff --git a/lua/leetcode-plugins/nonstandalone/leetcode.lua b/lua/leetcode-plugins/nonstandalone/leetcode.lua index 08849064..2351f40c 100644 --- a/lua/leetcode-plugins/nonstandalone/leetcode.lua +++ b/lua/leetcode-plugins/nonstandalone/leetcode.lua @@ -36,10 +36,9 @@ leetcode.start = function(on_vimenter) return false end - leetcode.setup_cmds() + config.setup() - local utils = require("leetcode.utils") - utils.exec_hooks("LeetEnter") + leetcode.setup_cmds() local theme = require("leetcode.theme") theme.setup() @@ -59,6 +58,9 @@ leetcode.start = function(on_vimenter) local Menu = require("leetcode-ui.renderer.menu") Menu():mount() + local utils = require("leetcode.utils") + utils.exec_hooks("LeetEnter") + return true end diff --git a/lua/leetcode-ui/renderer/menu.lua b/lua/leetcode-ui/renderer/menu.lua index 19b43928..ad076e8d 100644 --- a/lua/leetcode-ui/renderer/menu.lua +++ b/lua/leetcode-ui/renderer/menu.lua @@ -22,29 +22,6 @@ function Menu:draw() return self end -function Menu:is_open() - return self.winid - and vim.api.nvim_win_is_valid(self.winid) - and vim.api.nvim_win_get_buf(self.winid) == self.bufnr -end - -function Menu:close() - if not self.winid or not vim.api.nvim_win_is_valid(self.winid) then return end - vim.api.nvim_buf_delete(self.bufnr, { force = true }) - self.bufnr = nil - -- close the menu window if it is not the last window - for _, win in ipairs(vim.api.nvim_tabpage_list_wins(0)) do - if - win ~= self.winid - and vim.api.nvim_win_get_config(win).relative == "" - and vim.api.nvim_win_is_valid(self.winid) - then - vim.api.nvim_win_close(self.winid, true) - break - end - end -end - ---@private function Menu:autocmds() local group_id = vim.api.nvim_create_augroup("leetcode_menu", { clear = true }) diff --git a/lua/leetcode.lua b/lua/leetcode.lua index b5e8c132..48baf7a2 100644 --- a/lua/leetcode.lua +++ b/lua/leetcode.lua @@ -1,13 +1,11 @@ local config = require("leetcode.config") -local log = require("leetcode.logger") -local utils = require("leetcode.utils") ---@class lc.LeetCode local leetcode = {} ---@param on_vimenter boolean --- ----@return boolean, boolean? +---@return boolean function leetcode.should_skip(on_vimenter) if on_vimenter then if vim.fn.argc() ~= 1 then return true end @@ -17,13 +15,16 @@ function leetcode.should_skip(on_vimenter) 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 -- + if bufinfo and (bufinfo.listed == 1 and #bufinfo.windows > 0) then + local log = require("leetcode.logger") + log.warn("Failed to initialize: `neovim` contains listed buffers") return true end end @@ -46,8 +47,6 @@ function leetcode.start(on_vimenter) leetcode.setup_cmds() - utils.exec_hooks("LeetEnter") - local theme = require("leetcode.theme") theme.setup() @@ -58,6 +57,9 @@ function leetcode.start(on_vimenter) local Menu = require("leetcode-ui.renderer.menu") Menu():mount() + local utils = require("leetcode.utils") + utils.exec_hooks("LeetEnter") + return true end diff --git a/lua/leetcode/command/init.lua b/lua/leetcode/command/init.lua index b3589de0..c0d44605 100644 --- a/lua/leetcode/command/init.lua +++ b/lua/leetcode/command/init.lua @@ -155,12 +155,7 @@ end function cmd.start_with_cmd() local leetcode = require("leetcode") - - if leetcode.start(false) then - cmd.menu() - else - log.warn("Failed to initialize") - end + if leetcode.start(false) then cmd.menu() end end function cmd.menu() diff --git a/lua/leetcode/config/init.lua b/lua/leetcode/config/init.lua index 7852fc60..185eb86e 100644 --- a/lua/leetcode/config/init.lua +++ b/lua/leetcode/config/init.lua @@ -94,8 +94,13 @@ function config.load_plugins() if config.user.cn.enabled then table.insert(plugins, "cn") end + for plugin, enabled in pairs(config.user.plugins) do + if enabled then table.insert(plugins, plugin) end + end + for _, plugin in ipairs(plugins) do local ok, plug = pcall(require, "leetcode-plugins." .. plugin) + if ok then if not (plug.opts or {}).lazy then plug.load() @@ -109,27 +114,4 @@ function config.load_plugins() end end -function config.load_high_priority_plugins() - local plugins = {} - - if config.user.cn.enabled then - config.translator = config.user.cn.translator - table.insert(plugins, "cn") - end - - for plugin, enabled in pairs(config.user.plugins) do - if enabled then table.insert(plugins, plugin) end - end - - for _, plugin in ipairs(plugins) do - local ok, plug = pcall(require, "leetcode-plugins." .. plugin) - if ok then - plug.load() - else - local log = require("leetcode.logger") - log.error(plug) - end - end -end - return config From 941cdb350e2a70871d9e21ad7f4bb4daa770c022 Mon Sep 17 00:00:00 2001 From: kawre Date: Wed, 24 Jan 2024 17:23:09 +0100 Subject: [PATCH 12/29] fix: `Leet exit` --- lua/leetcode-plugins/nonstandalone/leetcode.lua | 7 +++---- lua/leetcode-ui/question.lua | 8 +++++--- lua/leetcode-ui/renderer/menu.lua | 9 +++++++-- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lua/leetcode-plugins/nonstandalone/leetcode.lua b/lua/leetcode-plugins/nonstandalone/leetcode.lua index 2351f40c..dc7801bc 100644 --- a/lua/leetcode-plugins/nonstandalone/leetcode.lua +++ b/lua/leetcode-plugins/nonstandalone/leetcode.lua @@ -64,11 +64,10 @@ leetcode.start = function(on_vimenter) return true end -leetcode.stop = function() +leetcode.stop = vim.schedule_wrap(function() if standalone then return vim.cmd.quitall() end - local Menu = require("leetcode-ui.renderer.menu") - Menu():unmount() + _Lc_Menu:_unmount() vim.api.nvim_create_user_command("Leet", require("leetcode.command").start_with_cmd, { bar = true, @@ -77,4 +76,4 @@ leetcode.stop = function() }) require("leetcode.utils").exec_hooks("LeetLeave") -end +end) diff --git a/lua/leetcode-ui/question.lua b/lua/leetcode-ui/question.lua index bde6cc89..58c4815a 100644 --- a/lua/leetcode-ui/question.lua +++ b/lua/leetcode-ui/question.lua @@ -86,8 +86,7 @@ function Question:injector(code) .. norm_inject(inject.after, false) end ----@param self lc.ui.Question -Question.unmount = vim.schedule_wrap(function(self) +function Question:_unmount() self.info:unmount() self.console:unmount() self.description:unmount() @@ -103,7 +102,10 @@ Question.unmount = vim.schedule_wrap(function(self) _Lc_questions = vim.tbl_filter(function(q) return q.bufnr ~= self.bufnr end, _Lc_questions) self = nil -end) +end + +---@param self lc.ui.Question +Question.unmount = vim.schedule_wrap(function(self) self:_unmount() end) function Question:handle_mount() vim.cmd("$tabe " .. self:create_file()) diff --git a/lua/leetcode-ui/renderer/menu.lua b/lua/leetcode-ui/renderer/menu.lua index ad076e8d..3ddc0d19 100644 --- a/lua/leetcode-ui/renderer/menu.lua +++ b/lua/leetcode-ui/renderer/menu.lua @@ -109,8 +109,10 @@ function Menu:apply_options() }) end -function Menu:unmount() - require("leetcode.command").q_close_all() +function Menu:_unmount() + for _, tabp in ipairs(require("leetcode.utils").question_tabs()) do + tabp.question:_unmount() + end pcall(vim.api.nvim_buf_delete, self.bufnr, { force = true }) pcall(vim.api.nvim_win_close, self.winid, true) @@ -118,6 +120,9 @@ function Menu:unmount() self = nil end +---@param self lc.ui.Menu +Menu.unmount = vim.schedule_wrap(function(self) self:_unmount() end) + function Menu:mount() if cookie.get() then self:set_page("loading") From 27a4a24b776548992f839986167f7904e69d8364 Mon Sep 17 00:00:00 2001 From: kawre Date: Wed, 24 Jan 2024 18:30:49 +0100 Subject: [PATCH 13/29] docs: readme udpdate --- README.md | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index bfcb720a..10f9540e 100644 --- a/README.md +++ b/README.md @@ -293,27 +293,23 @@ Leet list status= difficulty= ## 🚀 Usage -This plugin is meant to be used within a **fresh** [Neovim] instance. -Meaning that to launch [leetcode.nvim] you can to pass -[`arg`](#arg) as the _first and **only**_ [Neovim] argument +This plugin can be initiated in two ways: + +- To start [leetcode.nvim], simply pass [`arg`](#arg) + as the _first and **only**_ [Neovim] argument ``` nvim leetcode.nvim ``` -You can also use the `:Leet` command to open [leetcode.nvim] at any time. +- _**(Experimental)**_ Alternatively, you can use `:Leet` command to open [leetcode.nvim] + within your preferred dashboard plugin. The only requirement is that [Neovim] + must not have any listed buffers open. ### Switching between questions To switch between questions, use `Leet tabs` -### Exiting [leetcode.nvim] - -If [leetcode.nvim] was started from the cmdline with `nvim leetcode.nvim`, pressing `q` in the menu -or running `:Leet exit` will close nvim. If [leetcode.nvim] was started from a normal nvim session using -the `:Leet` command, pressing `q` in the menu or running `:Leet exit` will only close the [leetcode.nvim] -UI. - ### Sign In It is **required** to be **signed-in** to use [leetcode.nvim] From fb2d959d210f819be277e078e849c8776fe76586 Mon Sep 17 00:00:00 2001 From: kawre Date: Wed, 24 Jan 2024 19:20:10 +0100 Subject: [PATCH 14/29] fix: `neovim` crashing on `Leet exit` --- README.md | 3 +++ lua/leetcode-ui/renderer/menu.lua | 6 ++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 10f9540e..911277b9 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,9 @@ hooks = { ---@type fun(question: lc.ui.Question)[] LeetQuestionNew = {}, + + ---@type fun()[] + LeetLeave = {}, }, ``` diff --git a/lua/leetcode-ui/renderer/menu.lua b/lua/leetcode-ui/renderer/menu.lua index d5ccb7fa..a7e708e9 100644 --- a/lua/leetcode-ui/renderer/menu.lua +++ b/lua/leetcode-ui/renderer/menu.lua @@ -4,7 +4,7 @@ local config = require("leetcode.config") local utils = require("leetcode-ui.utils") local Renderer = require("leetcode-ui.renderer") ----@class lc.ui.menu : lc.ui.Renderer +---@class lc.ui.Menu : lc.ui.Renderer ---@field cursor lc-menu.cursor ---@field maps table local Menu = Renderer:extend("LeetMenu") @@ -110,7 +110,9 @@ end function Menu:_unmount() for _, tabp in ipairs(require("leetcode.utils").question_tabs()) do - tabp.question:_unmount() + if vim.api.nvim_buf_is_valid(tabp.question.bufnr) then + vim.api.nvim_buf_delete(tabp.question.bufnr, { force = true }) + end end pcall(vim.api.nvim_buf_delete, self.bufnr, { force = true }) From 359595cca152cfaee614cb706405fdbe23558e64 Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Wed, 24 Jan 2024 10:37:33 -0800 Subject: [PATCH 15/29] fix: crash when unmounting menu --- lua/leetcode-ui/renderer/menu.lua | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lua/leetcode-ui/renderer/menu.lua b/lua/leetcode-ui/renderer/menu.lua index a7e708e9..d0c62a74 100644 --- a/lua/leetcode-ui/renderer/menu.lua +++ b/lua/leetcode-ui/renderer/menu.lua @@ -110,13 +110,21 @@ end function Menu:_unmount() for _, tabp in ipairs(require("leetcode.utils").question_tabs()) do + vim.api.nvim_set_current_tabpage(tabp.tabpage) -- question tabpage + vim.cmd.tabclose() if vim.api.nvim_buf_is_valid(tabp.question.bufnr) then vim.api.nvim_buf_delete(tabp.question.bufnr, { force = true }) end end - pcall(vim.api.nvim_buf_delete, self.bufnr, { force = true }) - pcall(vim.api.nvim_win_close, self.winid, true) + -- close menu tabpage + if self.bufnr and vim.api.nvim_buf_is_valid(self.bufnr) then + vim.api.nvim_buf_delete(self.bufnr, { force = true }) + end + + if self.winid and vim.api.nvim_win_is_valid(self.winid) then + vim.api.nvim_win_close(self.winid, true) + end self = nil end From b678aff7b799be6db5991e36e506c7b4a036afa8 Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Wed, 24 Jan 2024 13:32:27 -0800 Subject: [PATCH 16/29] docs: document nonstandalone plugin --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index 9277465f..71248824 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,8 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat focus_result = "L", ---@type string }, + plugins = {}, + ---@type boolean image_support = false, -- setting this to `true` will disable question description wrap } @@ -339,6 +341,24 @@ return { } ``` +### 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 "nonstandalone" plugin in your config: + +```lua +return { + "kawre/leetcode.nvim", + opts = { + plugins = { + nonstandalone = true, + } + }, +} +``` + ## 🙌 Credits - [Leetbuddy.nvim](https://github.com/Dhanus3133/Leetbuddy.nvim) From c750acaaa75b48d9712cdb13512c2b597aa8f858 Mon Sep 17 00:00:00 2001 From: kawre Date: Thu, 25 Jan 2024 18:17:29 +0100 Subject: [PATCH 17/29] fix(draw): check if buf is valid --- README.md | 7 ++++++- README.zh.md | 6 +++++- lua/leetcode-ui/question.lua | 16 +++++++++++++--- lua/leetcode-ui/renderer/init.lua | 7 +++++-- lua/leetcode-ui/renderer/result.lua | 2 +- lua/leetcode.lua | 2 +- lua/leetcode/logger/init.lua | 5 ++++- 7 files changed, 35 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2075b0dc..791226e0 100644 --- a/README.md +++ b/README.md @@ -295,6 +295,8 @@ image_support = false, - `tabs` opens a picker with all currently opened question tabs +- `yank` yanks the current question solution + - `lang` opens a picker to change the language of the current question - `run` run currently opened question @@ -368,6 +370,9 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/b7be8b95-5e2c-4153-8845-4 ### 💤 lazy loading with [lazy.nvim] +🚨 _**opting for either option makes the alternative +launch method unavailable due to lazy loading.**_ + - with [`arg`](#arg) ```lua @@ -382,7 +387,7 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/b7be8b95-5e2c-4153-8845-4 } ``` -- with `:Leet`, this will make launching with [`arg`](#arg) not work +- with `:Leet` ```lua { diff --git a/README.zh.md b/README.zh.md index 3961e1fe..90eee016 100644 --- a/README.zh.md +++ b/README.zh.md @@ -291,6 +291,8 @@ image_support = false, -- 将此设置为 `true` 将禁用问题描述的换行 - `tabs` 打开所有当前打开问题选项卡的选择器 +- `yank` 复制当前问题的解决方案 + - `lang` 打开更改当前问题语言的选择器 - `run` 运行当前打开的问题 @@ -363,6 +365,8 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/b7be8b95-5e2c-4153-8845-4 ### 💤 使用 [lazy.nvim] 进行延迟加载 +🚨 _**选择其中任一选项将由于延迟加载而使另一启动方法不可用。**_ + - 使用 [`arg`](#arg) ```lua @@ -377,7 +381,7 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/b7be8b95-5e2c-4153-8845-4 } ``` -- 使用 `:Leet`,这将导致使用 [`arg`](#arg) 启动不起作用 +- 使用 `:Leet` ```lua { diff --git a/lua/leetcode-ui/question.lua b/lua/leetcode-ui/question.lua index 58c4815a..a2cd3df8 100644 --- a/lua/leetcode-ui/question.lua +++ b/lua/leetcode-ui/question.lua @@ -31,7 +31,6 @@ function Question:get_snippet() ) end ----@private function Question:create_file() local lang = utils.get_lang(self.lang) local alt = lang.alt and ("." .. lang.alt) or "" @@ -180,17 +179,28 @@ function Question:lines() return table.concat(lines, "\n", start_i, end_i) end +---@param self lc.ui.Question ---@param lang lc.lang Question.change_lang = vim.schedule_wrap(function(self, lang) + local old_lang = self.lang self.lang = lang - self:create_file() - local new_bufnr = vim.fn.bufadd(self.file:absolute()) + local new_bufnr = vim.fn.bufadd(self:create_file()) if new_bufnr ~= 0 then + local bufloaded = vim.fn.bufloaded(new_bufnr) + vim.api.nvim_win_set_buf(self.winid, new_bufnr) + + vim.api.nvim_buf_set_option(self.bufnr, "buflisted", false) + vim.api.nvim_buf_set_option(new_bufnr, "buflisted", true) + self.bufnr = new_bufnr + if bufloaded == 0 then -- + utils.exec_hooks("LeetQuestionNew", self) + end else log.error("Changing language failed") + self.lang = old_lang end end) diff --git a/lua/leetcode-ui/renderer/init.lua b/lua/leetcode-ui/renderer/init.lua index 6f424960..faf5f8f3 100644 --- a/lua/leetcode-ui/renderer/init.lua +++ b/lua/leetcode-ui/renderer/init.lua @@ -24,6 +24,10 @@ 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() @@ -75,8 +79,7 @@ function Renderer:map(mode, key, handler, opts) -- end function Renderer:unmap(mode, key) -- - local ok, err = pcall(vim.api.nvim_buf_del_keymap, self.bufnr, mode, key) - if not ok then log.error(err) end + pcall(vim.api.nvim_buf_del_keymap, self.bufnr, mode, key) end function Renderer:clear_keymaps() diff --git a/lua/leetcode-ui/renderer/result.lua b/lua/leetcode-ui/renderer/result.lua index 7803f27e..afe7b0be 100644 --- a/lua/leetcode-ui/renderer/result.lua +++ b/lua/leetcode-ui/renderer/result.lua @@ -125,7 +125,7 @@ function ResultLayout:handle_submission_error(item) -- status code = 11 self:insert(header) self:insert(Case({ ---@diagnostic disable-line - input = item.input_formatted, + input = vim.split(item.input, "\n"), raw_input = item.last_testcase, output = item.code_output, expected = item.expected_output, diff --git a/lua/leetcode.lua b/lua/leetcode.lua index 48baf7a2..e8072d94 100644 --- a/lua/leetcode.lua +++ b/lua/leetcode.lua @@ -10,7 +10,7 @@ function leetcode.should_skip(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 + local usr_arg, arg = config.user.arg, vim.fn.argv()[1] if usr_arg ~= arg then return true end local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true) diff --git a/lua/leetcode/logger/init.lua b/lua/leetcode/logger/init.lua index ff517af2..2aeef496 100644 --- a/lua/leetcode/logger/init.lua +++ b/lua/leetcode/logger/init.lua @@ -36,7 +36,10 @@ logger.info = function(msg) logger.log(msg) end logger.warn = function(msg) logger.log(msg, lvls.WARN) end ---@param msg any -logger.error = function(msg) logger.log(msg, lvls.ERROR) end +logger.error = function(msg) + logger.log(msg, lvls.ERROR) + logger.debug(msg) +end ---@param err lc.err logger.err = function(err) From a5d7e488a4022ab3e40f935dc789a27419175a07 Mon Sep 17 00:00:00 2001 From: kawre Date: Fri, 23 Feb 2024 15:49:41 +0100 Subject: [PATCH 18/29] refactor: `leetcode.stop` --- .stylua.toml | 3 ++- lua/leetcode-plugins/nonstandalone/exit.lua | 11 ----------- lua/leetcode-plugins/nonstandalone/init.lua | 3 +-- .../nonstandalone/leetcode.lua | 18 +++++++++++++----- lua/leetcode-ui/lines/button/menu/exit.lua | 3 ++- lua/leetcode.lua | 12 +++++++++--- 6 files changed, 27 insertions(+), 23 deletions(-) delete mode 100644 lua/leetcode-plugins/nonstandalone/exit.lua diff --git a/.stylua.toml b/.stylua.toml index e9668dde..ab3403dc 100644 --- a/.stylua.toml +++ b/.stylua.toml @@ -4,4 +4,5 @@ indent_type = "Spaces" indent_width = 4 quote_style = "ForceDouble" call_parentheses = "Always" -collapse_simple_statement = "Always" +collapse_simple_statement = "FunctionOnly" +# collapse_simple_statement = "Always" diff --git a/lua/leetcode-plugins/nonstandalone/exit.lua b/lua/leetcode-plugins/nonstandalone/exit.lua deleted file mode 100644 index a83e7702..00000000 --- a/lua/leetcode-plugins/nonstandalone/exit.lua +++ /dev/null @@ -1,11 +0,0 @@ -local leetcode = require("leetcode") - -local MenuExitButton = require("leetcode-ui.lines.button.menu.exit") - -function MenuExitButton:init() - MenuExitButton.super.init(self, "Exit", { - icon = "󰩈", - sc = "q", - on_press = leetcode.stop, - }) -end diff --git a/lua/leetcode-plugins/nonstandalone/init.lua b/lua/leetcode-plugins/nonstandalone/init.lua index 0fd5cc54..1643bcbf 100644 --- a/lua/leetcode-plugins/nonstandalone/init.lua +++ b/lua/leetcode-plugins/nonstandalone/init.lua @@ -5,8 +5,7 @@ nonstandalone.opts = { lazy = false, } -function nonstandalone.load() -- - require("leetcode-plugins.nonstandalone.exit") +function nonstandalone.load() require("leetcode-plugins.nonstandalone.leetcode") require("leetcode-plugins.nonstandalone.command") end diff --git a/lua/leetcode-plugins/nonstandalone/leetcode.lua b/lua/leetcode-plugins/nonstandalone/leetcode.lua index dc7801bc..f302b4f3 100644 --- a/lua/leetcode-plugins/nonstandalone/leetcode.lua +++ b/lua/leetcode-plugins/nonstandalone/leetcode.lua @@ -6,10 +6,14 @@ local standalone = true ---@param on_vimenter boolean leetcode.should_skip = function(on_vimenter) if on_vimenter then - if vim.fn.argc() ~= 1 then return true end + 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 + 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 @@ -53,7 +57,9 @@ leetcode.start = function(on_vimenter) end --- TODO: maybe cache the previous cwd and restore it when quitting? - if standalone then vim.api.nvim_set_current_dir(config.storage.home:absolute()) end + if standalone then + vim.api.nvim_set_current_dir(config.storage.home:absolute()) + end local Menu = require("leetcode-ui.renderer.menu") Menu():mount() @@ -65,9 +71,11 @@ leetcode.start = function(on_vimenter) end leetcode.stop = vim.schedule_wrap(function() - if standalone then return vim.cmd.quitall() end + if standalone then + return vim.cmd("qa!") + end - _Lc_Menu:_unmount() + _Lc_menu:_unmount() vim.api.nvim_create_user_command("Leet", require("leetcode.command").start_with_cmd, { bar = true, diff --git a/lua/leetcode-ui/lines/button/menu/exit.lua b/lua/leetcode-ui/lines/button/menu/exit.lua index 060e1258..566dfb04 100644 --- a/lua/leetcode-ui/lines/button/menu/exit.lua +++ b/lua/leetcode-ui/lines/button/menu/exit.lua @@ -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") @@ -8,7 +9,7 @@ function MenuExitButton:init() MenuExitButton.super.init(self, "Exit", { icon = "󰩈", sc = "q", - on_press = function() vim.cmd.quitall() end, + on_press = leetcode.stop, }) end diff --git a/lua/leetcode.lua b/lua/leetcode.lua index e8072d94..32b9fc20 100644 --- a/lua/leetcode.lua +++ b/lua/leetcode.lua @@ -8,10 +8,14 @@ local leetcode = {} ---@return boolean function leetcode.should_skip(on_vimenter) if on_vimenter then - if vim.fn.argc() ~= 1 then return true end + if vim.fn.argc() ~= 1 then + return true + end local usr_arg, arg = config.user.arg, vim.fn.argv()[1] - if usr_arg ~= arg then return true end + 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 @@ -37,7 +41,7 @@ function leetcode.setup_cmds() require("leetcode.command").setup() end ---@param on_vimenter boolean function leetcode.start(on_vimenter) - if leetcode.should_skip(on_vimenter) then -- + if leetcode.should_skip(on_vimenter) then return false end @@ -63,6 +67,8 @@ function leetcode.start(on_vimenter) return true end +function leetcode.stop() vim.cmd("qa!") end + ---@param cfg? lc.UserConfig function leetcode.setup(cfg) config.apply(cfg or {}) From 12a54fd24ee6961290a425b4c9054e2e1cc12ff1 Mon Sep 17 00:00:00 2001 From: kawre Date: Fri, 23 Feb 2024 16:04:11 +0100 Subject: [PATCH 19/29] docs(readme): `plugin` section --- README.md | 30 ++++++++++++++++++++---------- lua/leetcode/config/template.lua | 3 ++- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index da40a564..5a18aa30 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,11 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat cache = vim.fn.stdpath("cache") .. "/leetcode", }, + ---@type table + plugins = { + nonstandalone = false, + }, + ---@type boolean logging = true, @@ -149,7 +154,6 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat focus_result = "L", ---@type string }, - plugins = {}, ---@type boolean image_support = false, @@ -228,6 +232,17 @@ storage = { }, ``` +### plugins + +[plugins list](#-plugins) + +```lua +---@type table +plugins = { + nonstandalone = false, +}, +``` + ### logging Whether to log [leetcode.nvim] status notifications @@ -414,21 +429,16 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/b7be8b95-5e2c-4153-8845-4 } ``` -### Plugins +## 🧩 Plugins -#### Non-Standalone mode +### Non-Standalone mode To run [leetcode.nvim] in a non-standalone mode (i.e. not with argument or an empty Neovim session), enable the "nonstandalone" plugin in your config: ```lua -return { - "kawre/leetcode.nvim", - opts = { - plugins = { - nonstandalone = true, - } - }, +plugins = { + nonstandalone = true, } ``` diff --git a/lua/leetcode/config/template.lua b/lua/leetcode/config/template.lua index 7d4f17ab..c1d4dcb2 100644 --- a/lua/leetcode/config/template.lua +++ b/lua/leetcode/config/template.lua @@ -58,8 +58,9 @@ local M = { cache = vim.fn.stdpath("cache") .. "/leetcode", }, + ---@type table plugins = { - nonstandalone = false, ---@type boolean + nonstandalone = false, }, ---@type boolean From 7d52bdbb960365c37b442b73e0dd70383c7d870e Mon Sep 17 00:00:00 2001 From: kawre Date: Fri, 23 Feb 2024 17:07:02 +0100 Subject: [PATCH 20/29] feat: change `_Lc_state` --- .../nonstandalone/leetcode.lua | 2 +- lua/leetcode-ui/lines/calendar.lua | 18 ++- lua/leetcode-ui/lines/solved.lua | 6 +- lua/leetcode-ui/question.lua | 15 ++- lua/leetcode-ui/renderer/init.lua | 46 +++++-- lua/leetcode-ui/renderer/menu.lua | 24 ++-- lua/leetcode/command/init.lua | 118 +++++++++++++----- lua/leetcode/config/init.lua | 21 ++-- lua/leetcode/config/stats.lua | 21 ++-- lua/leetcode/utils.lua | 22 +++- 10 files changed, 204 insertions(+), 89 deletions(-) diff --git a/lua/leetcode-plugins/nonstandalone/leetcode.lua b/lua/leetcode-plugins/nonstandalone/leetcode.lua index f302b4f3..3aea4be9 100644 --- a/lua/leetcode-plugins/nonstandalone/leetcode.lua +++ b/lua/leetcode-plugins/nonstandalone/leetcode.lua @@ -75,7 +75,7 @@ leetcode.stop = vim.schedule_wrap(function() return vim.cmd("qa!") end - _Lc_menu:_unmount() + _Lc_state.menu:_unmount() vim.api.nvim_create_user_command("Leet", require("leetcode.command").start_with_cmd, { bar = true, diff --git a/lua/leetcode-ui/lines/calendar.lua b/lua/leetcode-ui/lines/calendar.lua index 86ee7345..7720d701 100644 --- a/lua/leetcode-ui/lines/calendar.lua +++ b/lua/leetcode-ui/lines/calendar.lua @@ -18,7 +18,9 @@ local Calendar = Lines:extend("LeetCalendar") local function get_days_in_month(month, year) local days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } - if (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) then days_in_month[2] = 29 end + if (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) then + days_in_month[2] = 29 + end return days_in_month[month] end @@ -57,7 +59,9 @@ function Calendar:handle_res(res) hour = 1, isdst = false, }) - if self.threshold <= os.time(time) then self.threshold = self.threshold + 24 * 60 * 60 end + if self.threshold <= os.time(time) then + self.threshold = self.threshold + 24 * 60 * 60 + end self.last_year_sub_count = 0 self.month_lens = {} @@ -83,7 +87,7 @@ function Calendar:handle_res(res) self:append(line):endl() end - _Lc_menu:draw() + _Lc_state.menu:draw() end function Calendar:handle_submissions() @@ -161,7 +165,9 @@ local function square_hl(count, max_count) end function Calendar:handle_weekdays() - if self.curr_time > self.threshold then return end + if self.curr_time > self.threshold then + return + end local curr = os.date("*t", self.curr_time) local count = self:get_submission(curr) or 0 @@ -174,7 +180,9 @@ end function Calendar:fetch() statistics.calendar(function(res, err) - if err then return log.err(err) end + if err then + return log.err(err) + end self:handle_res(res) end) end diff --git a/lua/leetcode-ui/lines/solved.lua b/lua/leetcode-ui/lines/solved.lua index 17c6571c..4f293978 100644 --- a/lua/leetcode-ui/lines/solved.lua +++ b/lua/leetcode-ui/lines/solved.lua @@ -76,7 +76,7 @@ function Solved:handle_res(res) self:endl() end - _Lc_menu:draw() + _Lc_state.menu:draw() end function Solved:update() @@ -93,7 +93,9 @@ end function Solved:fetch() statistics.solved(function(res, err) - if err then return log.err(err) end + if err then + return log.err(err) + end self:handle_res(res) end) end diff --git a/lua/leetcode-ui/question.lua b/lua/leetcode-ui/question.lua index 220fc640..e20ec71f 100644 --- a/lua/leetcode-ui/question.lua +++ b/lua/leetcode-ui/question.lua @@ -22,7 +22,9 @@ local Question = Object("LeetQuestion") function Question:get_snippet(raw) local snippets = self.q.code_snippets ~= vim.NIL and self.q.code_snippets or {} local snip = vim.tbl_filter(function(snip) return snip.lang_slug == self.lang end, snippets)[1] - if not snip then return end + if not snip then + return + end local code = snip.code:gsub("\r\n", "\n") return raw and code or self:injector(code) @@ -96,7 +98,10 @@ function Question:_unmount(pre) vim.api.nvim_win_close(self.winid, true) 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 @@ -115,7 +120,7 @@ function Question:handle_mount() self.bufnr = vim.api.nvim_get_current_buf() self.winid = vim.api.nvim_get_current_win() - table.insert(_Lc_questions, self) + table.insert(_Lc_state.questions, self) vim.api.nvim_create_autocmd("QuitPre", { buffer = self.bufnr, @@ -133,7 +138,9 @@ end function Question:mount() local tabp = utils.detect_duplicate_question(self.cache.title_slug, config.lang) - if tabp then return pcall(vim.api.nvim_set_current_tabpage, tabp) end + if tabp then + return pcall(vim.api.nvim_set_current_tabpage, tabp) + end local q = api_question.by_title_slug(self.cache.title_slug) if not q or q.is_paid_only and not config.auth.is_premium then diff --git a/lua/leetcode-ui/renderer/init.lua b/lua/leetcode-ui/renderer/init.lua index faf5f8f3..5de5e5d8 100644 --- a/lua/leetcode-ui/renderer/init.lua +++ b/lua/leetcode-ui/renderer/init.lua @@ -24,7 +24,7 @@ function Renderer:draw(component) self.bufnr = component.bufnr self.winid = component.winid - if not vim.api.nvim_buf_is_valid(self.bufnr) then -- + if not vim.api.nvim_buf_is_valid(self.bufnr) then return end @@ -41,7 +41,9 @@ function Renderer:draw(component) Renderer.super.draw(self, self, self._.opts) end) - if c_ok then pcall(vim.api.nvim_win_set_cursor, self.winid, c) end + if c_ok then + pcall(vim.api.nvim_win_set_cursor, self.winid, c) + end end ---@private @@ -49,17 +51,27 @@ end ---@param fn function function Renderer:modifiable(fn) local bufnr = self.bufnr - if not (bufnr and vim.api.nvim_buf_is_valid(bufnr)) then return end + if not (bufnr and vim.api.nvim_buf_is_valid(bufnr)) then + return + end local modi = vim.api.nvim_buf_get_option(bufnr, "modifiable") - if not modi then vim.api.nvim_buf_set_option(bufnr, "modifiable", true) end + if not modi then + vim.api.nvim_buf_set_option(bufnr, "modifiable", true) + end fn() - if not modi then vim.api.nvim_buf_set_option(bufnr, "modifiable", false) end + if not modi then + vim.api.nvim_buf_set_option(bufnr, "modifiable", false) + end end function Renderer:map(mode, key, handler, opts) -- - if not self.bufnr then return end - if type(key) == "number" then key = tostring(key) end + if not self.bufnr then + return + end + if type(key) == "number" then + key = tostring(key) + end if type(key) == "table" then for _, k in ipairs(key) do @@ -72,7 +84,9 @@ function Renderer:map(mode, key, handler, opts) -- local clearable = options.clearable options.clearable = nil - if clearable then self._.keymaps[key] = mode end + if clearable then + self._.keymaps[key] = mode + end vim.keymap.set(mode, key, handler, options) vim.keymap.set(mode, key, handler, options) end @@ -112,15 +126,23 @@ function Renderer:handle_press(line_idx) vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", true, false, true), "n", true) end - if not self.bufnr or not self.winid then feedenter() end - if not line_idx and not vim.api.nvim_win_is_valid(self.winid) then return feedenter() end + if not self.bufnr or not self.winid then + feedenter() + end + if not line_idx and not vim.api.nvim_win_is_valid(self.winid) then + return feedenter() + end line_idx = line_idx or vim.api.nvim_win_get_cursor(self.winid)[1] - if not self._.buttons[line_idx] then return feedenter() end + if not self._.buttons[line_idx] then + return feedenter() + end local ok, err = pcall(function() self._.buttons[line_idx]:press() end) - if not ok then log.error(err) end + if not ok then + log.error(err) + end end ---@param button lc.ui.Button diff --git a/lua/leetcode-ui/renderer/menu.lua b/lua/leetcode-ui/renderer/menu.lua index e29244eb..25c63195 100644 --- a/lua/leetcode-ui/renderer/menu.lua +++ b/lua/leetcode-ui/renderer/menu.lua @@ -12,7 +12,9 @@ local Menu = Renderer:extend("LeetMenu") local function tbl_keys(t) local keys = vim.tbl_keys(t) - if vim.tbl_isempty(keys) then return end + if vim.tbl_isempty(keys) then + return + end table.sort(keys) return keys end @@ -50,13 +52,17 @@ function Menu:autocmds() end function Menu:cursor_move() - if not self.winid or not api.nvim_win_is_valid(self.winid) then return end + if not self.winid or not api.nvim_win_is_valid(self.winid) then + return + end local curr = api.nvim_win_get_cursor(self.winid) local prev = self.cursor.prev local keys = tbl_keys(self._.buttons) - if not keys then return end + if not keys then + return + end local function find_nearest(l, r) while l < r do @@ -138,13 +144,7 @@ function Menu:apply_options() end function Menu:_unmount() - for _, tabp in ipairs(require("leetcode.utils").question_tabs()) do - vim.api.nvim_set_current_tabpage(tabp.tabpage) -- question tabpage - vim.cmd.tabclose() - if vim.api.nvim_buf_is_valid(tabp.question.bufnr) then - vim.api.nvim_buf_delete(tabp.question.bufnr, { force = true }) - end - end + require("leetcode.command").q_close_all() -- close menu tabpage if self.bufnr and vim.api.nvim_buf_is_valid(self.bufnr) then @@ -154,8 +154,6 @@ function Menu:_unmount() if self.winid and vim.api.nvim_win_is_valid(self.winid) then vim.api.nvim_win_close(self.winid, true) end - - self = nil end ---@param self lc.ui.Menu @@ -219,7 +217,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 diff --git a/lua/leetcode/command/init.lua b/lua/leetcode/command/init.lua index 4b571d49..4a8b9d33 100644 --- a/lua/leetcode/command/init.lua +++ b/lua/leetcode/command/init.lua @@ -105,7 +105,9 @@ cmd.expire = vim.schedule_wrap(function() cmd.cookie_prompt(function(success) if success then - if api.nvim_tabpage_is_valid(tabp) then api.nvim_set_current_tabpage(tabp) end + if api.nvim_tabpage_is_valid(tabp) then + api.nvim_set_current_tabpage(tabp) + end log.info("Successful re-login") else cmd.delete_cookie() @@ -125,7 +127,9 @@ function cmd.qot() local Question = require("leetcode-ui.question") problems.question_of_today(function(qot, err) - if err then return log.err(err) end + if err then + return log.err(err) + end local problemlist = require("leetcode.cache.problemlist") Question(problemlist.get_by_title_slug(qot.title_slug)):mount() end) @@ -137,7 +141,9 @@ function cmd.random_question(opts) local problems = require("leetcode.cache.problemlist") local question = require("leetcode.api.question") - if opts and opts.difficulty then opts.difficulty = opts.difficulty[1]:upper() end + if opts and opts.difficulty then + opts.difficulty = opts.difficulty[1]:upper() + end if opts and opts.status then opts.status = ({ ac = "AC", @@ -147,7 +153,9 @@ function cmd.random_question(opts) end local q, err = question.random(opts) - if err then return log.err(err) end + if err then + return log.err(err) + end local item = problems.get_by_title_slug(q.title_slug) or {} local Question = require("leetcode-ui.question") @@ -156,23 +164,27 @@ end function cmd.start_with_cmd() local leetcode = require("leetcode") - if leetcode.start(false) then cmd.menu() end + if leetcode.start(false) then + cmd.menu() + end end function cmd.menu() - local ok, tabp = pcall(api.nvim_win_get_tabpage, _Lc_menu.winid) + local ok, tabp = pcall(api.nvim_win_get_tabpage, _Lc_state.menu.winid) if ok then api.nvim_set_current_tabpage(tabp) else - _Lc_menu:remount() + _Lc_state.menu:remount() end end function cmd.yank() local utils = require("leetcode.utils") local q = utils.curr_question() - if not q then return end + if not q then + return + end if api.nvim_buf_is_valid(q.bufnr) and api.nvim_win_is_valid(q.winid) then api.nvim_set_current_win(q.winid) @@ -184,7 +196,7 @@ function cmd.yank() end ---@param page lc-menu.page -function cmd.set_menu_page(page) _Lc_menu:set_page(page) end +function cmd.set_menu_page(page) _Lc_state.menu:set_page(page) end function cmd.start_user_session() -- cmd.set_menu_page("menu") @@ -196,31 +208,41 @@ function cmd.question_tabs() require("leetcode.pickers.question-tabs").pick() en function cmd.change_lang() local utils = require("leetcode.utils") local q = utils.curr_question() - if q then require("leetcode.pickers.language").pick(q) end + if q then + require("leetcode.pickers.language").pick(q) + end end function cmd.desc_toggle() local utils = require("leetcode.utils") local q = utils.curr_question() - if q then q.description:toggle() end + if q then + q.description:toggle() + end end function cmd.desc_toggle_stats() local utils = require("leetcode.utils") local q = utils.curr_question() - if q then q.description:toggle_stats() end + if q then + q.description:toggle_stats() + end end function cmd.console() local utils = require("leetcode.utils") local q = utils.curr_question() - if q then q.console:toggle() end + if q then + q.console:toggle() + end end function cmd.info() local utils = require("leetcode.utils") local q = utils.curr_question() - if q then q.info:toggle() end + if q then + q.info:toggle() + end end function cmd.hints() @@ -232,18 +254,24 @@ function cmd.q_run() local utils = require("leetcode.utils") utils.auth_guard() local q = utils.curr_question() - if q then q.console:run() end + if q then + q.console:run() + end end function cmd.q_submit() local utils = require("leetcode.utils") utils.auth_guard() local q = utils.curr_question() - if q then q.console:run(true) end + if q then + q.console:run(true) + end end function cmd.ui_skills() - if config.is_cn then return end + if config.is_cn then + return + end local skills = require("leetcode-ui.popup.skills") skills:show() end @@ -279,7 +307,9 @@ function cmd.reset() local utils = require("leetcode.utils") utils.auth_guard() local q = utils.curr_question() - if not q then return end + if not q then + return + end local snip = q:get_snippet(true) utils.set_question_lines(q, snip) @@ -289,7 +319,9 @@ function cmd.last_submit() local utils = require("leetcode.utils") utils.auth_guard() local q = utils.curr_question() - if not q then return end + if not q then + return + end local question_api = require("leetcode.api.question") question_api.latest_submission(q.q.id, q.lang, function(res, err) -- @@ -315,7 +347,9 @@ function cmd.restore() local utils = require("leetcode.utils") utils.auth_guard() local q = utils.curr_question() - if not q then return end + if not q then + return + end if (q.winid and api.nvim_win_is_valid(q.winid)) @@ -344,7 +378,9 @@ function cmd.get_session_by_name(name) local sessions = config.sessions.all name = name:lower() - if name == config.sessions.default then name = "" end + if name == config.sessions.default then + name = "" + end return vim.tbl_filter(function(s) return s.name:lower() == name end, sessions)[1] end @@ -352,11 +388,15 @@ function cmd.change_session(opts) local name = opts.name[1] or config.sessions.default local session = cmd.get_session_by_name(name) - if not session then return log.error("Session not found") end + if not session then + return log.error("Session not found") + end local stats_api = require("leetcode.api.statistics") stats_api.change_session(session.id, function(_, err) - if err then return log.err(err) end + if err then + return log.err(err) + end log.info(("Session changed to `%s`"):format(name)) config.stats.update() end) @@ -364,11 +404,15 @@ end function cmd.create_session(opts) local name = opts.name[1] - if not name then return log.error("Session name not provided") end + if not name then + return log.error("Session name not provided") + end local stats_api = require("leetcode.api.statistics") stats_api.create_session(name, function(_, err) - if err then return log.err(err) end + if err then + return log.err(err) + end log.info(("session `%s` created"):format(name)) end) end @@ -384,12 +428,16 @@ end ---@return string[], string[] function cmd.parse(args) local parts = vim.split(vim.trim(args), "%s+") - if args:sub(-1) == " " then parts[#parts + 1] = "" end + if args:sub(-1) == " " then + parts[#parts + 1] = "" + end local options = {} for _, part in ipairs(parts) do local opt = part:match("(.-)=.-") - if opt then table.insert(options, opt) end + if opt then + table.insert(options, opt) + end end return parts, options @@ -398,9 +446,15 @@ end ---@param tbl table local function cmds_keys(tbl) return vim.tbl_filter(function(key) - if type(key) ~= "string" then return false end - if key:sub(1, 1) == "_" then return false end - if tbl[key]._private then return false end + if type(key) ~= "string" then + return false + end + if key:sub(1, 1) == "_" then + return false + end + if tbl[key]._private then + return false + end return true end, vim.tbl_keys(tbl)) @@ -421,7 +475,9 @@ end --- ---@return string[] function cmd.rec_complete(args, options, cmds) - if not cmds or vim.tbl_isempty(args) then return {} end + if not cmds or vim.tbl_isempty(args) then + return {} + end if not cmds._args and cmds[args[1]] then return cmd.rec_complete(args, options, cmds[table.remove(args, 1)]) diff --git a/lua/leetcode/config/init.lua b/lua/leetcode/config/init.lua index f13f7033..59dd7194 100644 --- a/lua/leetcode/config/init.lua +++ b/lua/leetcode/config/init.lua @@ -1,11 +1,10 @@ local template = require("leetcode.config.template") local P = require("plenary.path") ----@type lc.ui.Question[] -_Lc_questions = {} - ----@type lc.ui.Menu -_Lc_menu = {} ---@diagnostic disable-line +_Lc_state = { + menu = nil, ---@type lc.ui.Menu + questions = {}, ---@type lc.ui.Question[] +} local lazy_plugs = {} @@ -79,7 +78,9 @@ function config.validate() local matches = {} for _, slug in ipairs(lang_slugs) do local percent = slug:match(config.lang) or config.lang:match(slug) - if percent then table.insert(matches, slug) end + if percent then + table.insert(matches, slug) + end end if not vim.tbl_isempty(matches) then @@ -94,10 +95,14 @@ end function config.load_plugins() local plugins = {} - if config.user.cn.enabled then table.insert(plugins, "cn") end + if config.user.cn.enabled then + table.insert(plugins, "cn") + end for plugin, enabled in pairs(config.user.plugins) do - if enabled then table.insert(plugins, plugin) end + if enabled then + table.insert(plugins, plugin) + end end for _, plugin in ipairs(plugins) do diff --git a/lua/leetcode/config/stats.lua b/lua/leetcode/config/stats.lua index 4f141c66..4f58e01f 100644 --- a/lua/leetcode/config/stats.lua +++ b/lua/leetcode/config/stats.lua @@ -9,12 +9,14 @@ function Stats.update_streak() local log = require("leetcode.logger") stats_api.streak(function(res, err) - if err then return log.err(err) end + if err then + return log.err(err) + end Stats.daily.streak = res.streakCount Stats.daily.today_completed = res.todayCompleted - if _Lc_menu then _Lc_menu:draw() end + _Lc_state.menu:draw() end) end @@ -25,12 +27,17 @@ function Stats.update_sessions() Stats.progress = {} stats_api.sessions(function(_, err) - if err then return log.err(err) end - if _Lc_menu then _Lc_menu:draw() end + if err then + return log.err(err) + end + + _Lc_state.menu:draw() end) stats_api.session_progress(function(res, err) - if err then return log.err(err) end + if err then + return log.err(err) + end Stats.progress = {} @@ -39,7 +46,7 @@ function Stats.update_sessions() Stats.progress[p.difficulty:lower()] = p end - if _Lc_menu then _Lc_menu:draw() end + _Lc_state.menu:draw() end) end @@ -47,7 +54,7 @@ function Stats.update() Stats.update_streak() Stats.update_sessions() - if _Lc_menu then _Lc_menu:draw() end + _Lc_state.menu:draw() end return Stats diff --git a/lua/leetcode/utils.lua b/lua/leetcode/utils.lua index 6e366cc2..b31ef06f 100644 --- a/lua/leetcode/utils.lua +++ b/lua/leetcode/utils.lua @@ -31,9 +31,11 @@ end function utils.question_tabs() local questions = {} - for _, q in ipairs(_Lc_questions) do + for _, q in ipairs(_Lc_state.questions) do local tabp = utils.question_tabp(q) - if tabp then table.insert(questions, { tabpage = tabp, question = q }) end + if tabp then + table.insert(questions, { tabpage = tabp, question = q }) + end end return questions @@ -43,7 +45,9 @@ end ---@return integer|nil function utils.question_tabp(q) local ok, tabp = pcall(vim.api.nvim_win_get_tabpage, q.winid) - if ok then return tabp end + if ok then + return tabp + end end ---@return lc.ui.Question @@ -74,11 +78,15 @@ end ---@param event lc.hook function utils.exec_hooks(event, ...) local fns = config.user.hooks[event] - if not fns then log.error("unknown hook event: " .. event) end + if not fns then + log.error("unknown hook event: " .. event) + end for i, fn in ipairs(fns) do local ok, msg = pcall(vim.schedule_wrap(fn), ...) - if not ok then log.error(("bad hook #%d in `%s` event: %s"):format(i, event, msg)) end + if not ok then + log.error(("bad hook #%d in `%s` event: %s"):format(i, event, msg)) + end end end @@ -104,7 +112,9 @@ function utils.norm_ins(str) end function utils.set_question_lines(q, code) - if not vim.api.nvim_buf_is_valid(q.bufnr) then return end + if not vim.api.nvim_buf_is_valid(q.bufnr) then + return + end local s_i, e_i = q:range() vim.api.nvim_buf_set_lines(q.bufnr, s_i - 1, e_i, false, vim.split(code, "\n")) From 7286a7f53a4f7fd44f3ace59318bd1aaeafac7cb Mon Sep 17 00:00:00 2001 From: kawre Date: Fri, 23 Feb 2024 17:22:31 +0100 Subject: [PATCH 21/29] feat: restore cwd on `Leet exit` --- .stylua.toml | 4 +- .../nonstandalone/leetcode.lua | 27 ++--- lua/leetcode-ui/lines/calendar.lua | 16 +-- lua/leetcode-ui/lines/solved.lua | 4 +- lua/leetcode-ui/question.lua | 8 +- lua/leetcode-ui/renderer/init.lua | 48 ++------ lua/leetcode-ui/renderer/menu.lua | 12 +- lua/leetcode.lua | 12 +- lua/leetcode/command/init.lua | 112 +++++------------- lua/leetcode/config/init.lua | 12 +- lua/leetcode/config/stats.lua | 12 +- lua/leetcode/utils.lua | 20 +--- 12 files changed, 79 insertions(+), 208 deletions(-) diff --git a/.stylua.toml b/.stylua.toml index ab3403dc..1af79332 100644 --- a/.stylua.toml +++ b/.stylua.toml @@ -4,5 +4,5 @@ indent_type = "Spaces" indent_width = 4 quote_style = "ForceDouble" call_parentheses = "Always" -collapse_simple_statement = "FunctionOnly" -# collapse_simple_statement = "Always" +# collapse_simple_statement = "FunctionOnly" +collapse_simple_statement = "Always" diff --git a/lua/leetcode-plugins/nonstandalone/leetcode.lua b/lua/leetcode-plugins/nonstandalone/leetcode.lua index 3aea4be9..3441442b 100644 --- a/lua/leetcode-plugins/nonstandalone/leetcode.lua +++ b/lua/leetcode-plugins/nonstandalone/leetcode.lua @@ -2,18 +2,15 @@ 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 + 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 + 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 @@ -49,18 +46,17 @@ leetcode.start = function(on_vimenter) if not on_vimenter then -- if buflisted then - standalone = false + prev_cwd = vim.fn.getcwd() vim.cmd.tabe() else vim.cmd.enew() end - end - --- TODO: maybe cache the previous cwd and restore it when quitting? - if standalone then - vim.api.nvim_set_current_dir(config.storage.home:absolute()) + standalone = not buflisted end + vim.api.nvim_set_current_dir(config.storage.home:absolute()) + local Menu = require("leetcode-ui.renderer.menu") Menu():mount() @@ -71,9 +67,7 @@ leetcode.start = function(on_vimenter) end leetcode.stop = vim.schedule_wrap(function() - if standalone then - return vim.cmd("qa!") - end + if standalone then return vim.cmd("qa!") end _Lc_state.menu:_unmount() @@ -84,4 +78,9 @@ leetcode.stop = vim.schedule_wrap(function() }) require("leetcode.utils").exec_hooks("LeetLeave") + + if prev_cwd then + vim.api.nvim_set_current_dir(prev_cwd) + prev_cwd = nil + end end) diff --git a/lua/leetcode-ui/lines/calendar.lua b/lua/leetcode-ui/lines/calendar.lua index 7720d701..cb482114 100644 --- a/lua/leetcode-ui/lines/calendar.lua +++ b/lua/leetcode-ui/lines/calendar.lua @@ -18,9 +18,7 @@ local Calendar = Lines:extend("LeetCalendar") local function get_days_in_month(month, year) local days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } - if (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) then - days_in_month[2] = 29 - end + if (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) then days_in_month[2] = 29 end return days_in_month[month] end @@ -59,9 +57,7 @@ function Calendar:handle_res(res) hour = 1, isdst = false, }) - if self.threshold <= os.time(time) then - self.threshold = self.threshold + 24 * 60 * 60 - end + if self.threshold <= os.time(time) then self.threshold = self.threshold + 24 * 60 * 60 end self.last_year_sub_count = 0 self.month_lens = {} @@ -165,9 +161,7 @@ local function square_hl(count, max_count) end function Calendar:handle_weekdays() - if self.curr_time > self.threshold then - return - end + if self.curr_time > self.threshold then return end local curr = os.date("*t", self.curr_time) local count = self:get_submission(curr) or 0 @@ -180,9 +174,7 @@ end function Calendar:fetch() statistics.calendar(function(res, err) - if err then - return log.err(err) - end + if err then return log.err(err) end self:handle_res(res) end) end diff --git a/lua/leetcode-ui/lines/solved.lua b/lua/leetcode-ui/lines/solved.lua index 4f293978..70f9eb53 100644 --- a/lua/leetcode-ui/lines/solved.lua +++ b/lua/leetcode-ui/lines/solved.lua @@ -93,9 +93,7 @@ end function Solved:fetch() statistics.solved(function(res, err) - if err then - return log.err(err) - end + if err then return log.err(err) end self:handle_res(res) end) end diff --git a/lua/leetcode-ui/question.lua b/lua/leetcode-ui/question.lua index e20ec71f..04f1998a 100644 --- a/lua/leetcode-ui/question.lua +++ b/lua/leetcode-ui/question.lua @@ -22,9 +22,7 @@ local Question = Object("LeetQuestion") function Question:get_snippet(raw) local snippets = self.q.code_snippets ~= vim.NIL and self.q.code_snippets or {} local snip = vim.tbl_filter(function(snip) return snip.lang_slug == self.lang end, snippets)[1] - if not snip then - return - end + if not snip then return end local code = snip.code:gsub("\r\n", "\n") return raw and code or self:injector(code) @@ -138,9 +136,7 @@ end function Question:mount() local tabp = utils.detect_duplicate_question(self.cache.title_slug, config.lang) - if tabp then - return pcall(vim.api.nvim_set_current_tabpage, tabp) - end + if tabp then return pcall(vim.api.nvim_set_current_tabpage, tabp) end local q = api_question.by_title_slug(self.cache.title_slug) if not q or q.is_paid_only and not config.auth.is_premium then diff --git a/lua/leetcode-ui/renderer/init.lua b/lua/leetcode-ui/renderer/init.lua index 5de5e5d8..e5ce85aa 100644 --- a/lua/leetcode-ui/renderer/init.lua +++ b/lua/leetcode-ui/renderer/init.lua @@ -24,9 +24,7 @@ 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 + if not vim.api.nvim_buf_is_valid(self.bufnr) then return end self:map("n", keys.confirm, function() self:handle_press() end) @@ -41,9 +39,7 @@ function Renderer:draw(component) Renderer.super.draw(self, self, self._.opts) end) - if c_ok then - pcall(vim.api.nvim_win_set_cursor, self.winid, c) - end + if c_ok then pcall(vim.api.nvim_win_set_cursor, self.winid, c) end end ---@private @@ -51,27 +47,17 @@ end ---@param fn function function Renderer:modifiable(fn) local bufnr = self.bufnr - if not (bufnr and vim.api.nvim_buf_is_valid(bufnr)) then - return - end + if not (bufnr and vim.api.nvim_buf_is_valid(bufnr)) then return end local modi = vim.api.nvim_buf_get_option(bufnr, "modifiable") - if not modi then - vim.api.nvim_buf_set_option(bufnr, "modifiable", true) - end + if not modi then vim.api.nvim_buf_set_option(bufnr, "modifiable", true) end fn() - if not modi then - vim.api.nvim_buf_set_option(bufnr, "modifiable", false) - end + if not modi then vim.api.nvim_buf_set_option(bufnr, "modifiable", false) end end function Renderer:map(mode, key, handler, opts) -- - if not self.bufnr then - return - end - if type(key) == "number" then - key = tostring(key) - end + if not self.bufnr then return end + if type(key) == "number" then key = tostring(key) end if type(key) == "table" then for _, k in ipairs(key) do @@ -84,9 +70,7 @@ function Renderer:map(mode, key, handler, opts) -- local clearable = options.clearable options.clearable = nil - if clearable then - self._.keymaps[key] = mode - end + if clearable then self._.keymaps[key] = mode end vim.keymap.set(mode, key, handler, options) vim.keymap.set(mode, key, handler, options) end @@ -126,23 +110,15 @@ function Renderer:handle_press(line_idx) vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", true, false, true), "n", true) end - if not self.bufnr or not self.winid then - feedenter() - end - if not line_idx and not vim.api.nvim_win_is_valid(self.winid) then - return feedenter() - end + if not self.bufnr or not self.winid then feedenter() end + if not line_idx and not vim.api.nvim_win_is_valid(self.winid) then return feedenter() end line_idx = line_idx or vim.api.nvim_win_get_cursor(self.winid)[1] - if not self._.buttons[line_idx] then - return feedenter() - end + if not self._.buttons[line_idx] then return feedenter() end local ok, err = pcall(function() self._.buttons[line_idx]:press() end) - if not ok then - log.error(err) - end + if not ok then log.error(err) end end ---@param button lc.ui.Button diff --git a/lua/leetcode-ui/renderer/menu.lua b/lua/leetcode-ui/renderer/menu.lua index 25c63195..eedd4e51 100644 --- a/lua/leetcode-ui/renderer/menu.lua +++ b/lua/leetcode-ui/renderer/menu.lua @@ -12,9 +12,7 @@ local Menu = Renderer:extend("LeetMenu") local function tbl_keys(t) local keys = vim.tbl_keys(t) - if vim.tbl_isempty(keys) then - return - end + if vim.tbl_isempty(keys) then return end table.sort(keys) return keys end @@ -52,17 +50,13 @@ function Menu:autocmds() end function Menu:cursor_move() - if not self.winid or not api.nvim_win_is_valid(self.winid) then - return - end + if not self.winid or not api.nvim_win_is_valid(self.winid) then return end local curr = api.nvim_win_get_cursor(self.winid) local prev = self.cursor.prev local keys = tbl_keys(self._.buttons) - if not keys then - return - end + if not keys then return end local function find_nearest(l, r) while l < r do diff --git a/lua/leetcode.lua b/lua/leetcode.lua index 32b9fc20..44d35f9f 100644 --- a/lua/leetcode.lua +++ b/lua/leetcode.lua @@ -8,14 +8,10 @@ local leetcode = {} ---@return boolean function leetcode.should_skip(on_vimenter) if on_vimenter then - if vim.fn.argc() ~= 1 then - return true - end + if vim.fn.argc() ~= 1 then return true end local usr_arg, arg = config.user.arg, vim.fn.argv()[1] - if usr_arg ~= arg then - return true - end + 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 @@ -41,9 +37,7 @@ function leetcode.setup_cmds() require("leetcode.command").setup() end ---@param on_vimenter boolean function leetcode.start(on_vimenter) - if leetcode.should_skip(on_vimenter) then - return false - end + if leetcode.should_skip(on_vimenter) then return false end config.setup() diff --git a/lua/leetcode/command/init.lua b/lua/leetcode/command/init.lua index 4a8b9d33..f627a161 100644 --- a/lua/leetcode/command/init.lua +++ b/lua/leetcode/command/init.lua @@ -105,9 +105,7 @@ cmd.expire = vim.schedule_wrap(function() cmd.cookie_prompt(function(success) if success then - if api.nvim_tabpage_is_valid(tabp) then - api.nvim_set_current_tabpage(tabp) - end + if api.nvim_tabpage_is_valid(tabp) then api.nvim_set_current_tabpage(tabp) end log.info("Successful re-login") else cmd.delete_cookie() @@ -127,9 +125,7 @@ function cmd.qot() local Question = require("leetcode-ui.question") problems.question_of_today(function(qot, err) - if err then - return log.err(err) - end + if err then return log.err(err) end local problemlist = require("leetcode.cache.problemlist") Question(problemlist.get_by_title_slug(qot.title_slug)):mount() end) @@ -141,9 +137,7 @@ function cmd.random_question(opts) local problems = require("leetcode.cache.problemlist") local question = require("leetcode.api.question") - if opts and opts.difficulty then - opts.difficulty = opts.difficulty[1]:upper() - end + if opts and opts.difficulty then opts.difficulty = opts.difficulty[1]:upper() end if opts and opts.status then opts.status = ({ ac = "AC", @@ -153,9 +147,7 @@ function cmd.random_question(opts) end local q, err = question.random(opts) - if err then - return log.err(err) - end + if err then return log.err(err) end local item = problems.get_by_title_slug(q.title_slug) or {} local Question = require("leetcode-ui.question") @@ -164,9 +156,7 @@ end function cmd.start_with_cmd() local leetcode = require("leetcode") - if leetcode.start(false) then - cmd.menu() - end + if leetcode.start(false) then cmd.menu() end end function cmd.menu() @@ -182,9 +172,7 @@ end function cmd.yank() local utils = require("leetcode.utils") local q = utils.curr_question() - if not q then - return - end + if not q then return end if api.nvim_buf_is_valid(q.bufnr) and api.nvim_win_is_valid(q.winid) then api.nvim_set_current_win(q.winid) @@ -208,41 +196,31 @@ function cmd.question_tabs() require("leetcode.pickers.question-tabs").pick() en function cmd.change_lang() local utils = require("leetcode.utils") local q = utils.curr_question() - if q then - require("leetcode.pickers.language").pick(q) - end + if q then require("leetcode.pickers.language").pick(q) end end function cmd.desc_toggle() local utils = require("leetcode.utils") local q = utils.curr_question() - if q then - q.description:toggle() - end + if q then q.description:toggle() end end function cmd.desc_toggle_stats() local utils = require("leetcode.utils") local q = utils.curr_question() - if q then - q.description:toggle_stats() - end + if q then q.description:toggle_stats() end end function cmd.console() local utils = require("leetcode.utils") local q = utils.curr_question() - if q then - q.console:toggle() - end + if q then q.console:toggle() end end function cmd.info() local utils = require("leetcode.utils") local q = utils.curr_question() - if q then - q.info:toggle() - end + if q then q.info:toggle() end end function cmd.hints() @@ -254,24 +232,18 @@ function cmd.q_run() local utils = require("leetcode.utils") utils.auth_guard() local q = utils.curr_question() - if q then - q.console:run() - end + if q then q.console:run() end end function cmd.q_submit() local utils = require("leetcode.utils") utils.auth_guard() local q = utils.curr_question() - if q then - q.console:run(true) - end + if q then q.console:run(true) end end function cmd.ui_skills() - if config.is_cn then - return - end + if config.is_cn then return end local skills = require("leetcode-ui.popup.skills") skills:show() end @@ -307,9 +279,7 @@ function cmd.reset() local utils = require("leetcode.utils") utils.auth_guard() local q = utils.curr_question() - if not q then - return - end + if not q then return end local snip = q:get_snippet(true) utils.set_question_lines(q, snip) @@ -319,9 +289,7 @@ function cmd.last_submit() local utils = require("leetcode.utils") utils.auth_guard() local q = utils.curr_question() - if not q then - return - end + if not q then return end local question_api = require("leetcode.api.question") question_api.latest_submission(q.q.id, q.lang, function(res, err) -- @@ -347,9 +315,7 @@ function cmd.restore() local utils = require("leetcode.utils") utils.auth_guard() local q = utils.curr_question() - if not q then - return - end + if not q then return end if (q.winid and api.nvim_win_is_valid(q.winid)) @@ -378,9 +344,7 @@ function cmd.get_session_by_name(name) local sessions = config.sessions.all name = name:lower() - if name == config.sessions.default then - name = "" - end + if name == config.sessions.default then name = "" end return vim.tbl_filter(function(s) return s.name:lower() == name end, sessions)[1] end @@ -388,15 +352,11 @@ function cmd.change_session(opts) local name = opts.name[1] or config.sessions.default local session = cmd.get_session_by_name(name) - if not session then - return log.error("Session not found") - end + if not session then return log.error("Session not found") end local stats_api = require("leetcode.api.statistics") stats_api.change_session(session.id, function(_, err) - if err then - return log.err(err) - end + if err then return log.err(err) end log.info(("Session changed to `%s`"):format(name)) config.stats.update() end) @@ -404,15 +364,11 @@ end function cmd.create_session(opts) local name = opts.name[1] - if not name then - return log.error("Session name not provided") - end + if not name then return log.error("Session name not provided") end local stats_api = require("leetcode.api.statistics") stats_api.create_session(name, function(_, err) - if err then - return log.err(err) - end + if err then return log.err(err) end log.info(("session `%s` created"):format(name)) end) end @@ -428,16 +384,12 @@ end ---@return string[], string[] function cmd.parse(args) local parts = vim.split(vim.trim(args), "%s+") - if args:sub(-1) == " " then - parts[#parts + 1] = "" - end + if args:sub(-1) == " " then parts[#parts + 1] = "" end local options = {} for _, part in ipairs(parts) do local opt = part:match("(.-)=.-") - if opt then - table.insert(options, opt) - end + if opt then table.insert(options, opt) end end return parts, options @@ -446,15 +398,9 @@ end ---@param tbl table local function cmds_keys(tbl) return vim.tbl_filter(function(key) - if type(key) ~= "string" then - return false - end - if key:sub(1, 1) == "_" then - return false - end - if tbl[key]._private then - return false - end + if type(key) ~= "string" then return false end + if key:sub(1, 1) == "_" then return false end + if tbl[key]._private then return false end return true end, vim.tbl_keys(tbl)) @@ -475,9 +421,7 @@ end --- ---@return string[] function cmd.rec_complete(args, options, cmds) - if not cmds or vim.tbl_isempty(args) then - return {} - end + if not cmds or vim.tbl_isempty(args) then return {} end if not cmds._args and cmds[args[1]] then return cmd.rec_complete(args, options, cmds[table.remove(args, 1)]) diff --git a/lua/leetcode/config/init.lua b/lua/leetcode/config/init.lua index 59dd7194..4a850bf9 100644 --- a/lua/leetcode/config/init.lua +++ b/lua/leetcode/config/init.lua @@ -78,9 +78,7 @@ function config.validate() local matches = {} for _, slug in ipairs(lang_slugs) do local percent = slug:match(config.lang) or config.lang:match(slug) - if percent then - table.insert(matches, slug) - end + if percent then table.insert(matches, slug) end end if not vim.tbl_isempty(matches) then @@ -95,14 +93,10 @@ end function config.load_plugins() local plugins = {} - if config.user.cn.enabled then - table.insert(plugins, "cn") - end + if config.user.cn.enabled then table.insert(plugins, "cn") end for plugin, enabled in pairs(config.user.plugins) do - if enabled then - table.insert(plugins, plugin) - end + if enabled then table.insert(plugins, plugin) end end for _, plugin in ipairs(plugins) do diff --git a/lua/leetcode/config/stats.lua b/lua/leetcode/config/stats.lua index 4f58e01f..0a6aca1e 100644 --- a/lua/leetcode/config/stats.lua +++ b/lua/leetcode/config/stats.lua @@ -9,9 +9,7 @@ function Stats.update_streak() local log = require("leetcode.logger") stats_api.streak(function(res, err) - if err then - return log.err(err) - end + if err then return log.err(err) end Stats.daily.streak = res.streakCount Stats.daily.today_completed = res.todayCompleted @@ -27,17 +25,13 @@ function Stats.update_sessions() Stats.progress = {} stats_api.sessions(function(_, err) - if err then - return log.err(err) - end + if err then return log.err(err) end _Lc_state.menu:draw() end) stats_api.session_progress(function(res, err) - if err then - return log.err(err) - end + if err then return log.err(err) end Stats.progress = {} diff --git a/lua/leetcode/utils.lua b/lua/leetcode/utils.lua index b31ef06f..161b8342 100644 --- a/lua/leetcode/utils.lua +++ b/lua/leetcode/utils.lua @@ -33,9 +33,7 @@ function utils.question_tabs() for _, q in ipairs(_Lc_state.questions) do local tabp = utils.question_tabp(q) - if tabp then - table.insert(questions, { tabpage = tabp, question = q }) - end + if tabp then table.insert(questions, { tabpage = tabp, question = q }) end end return questions @@ -45,9 +43,7 @@ end ---@return integer|nil function utils.question_tabp(q) local ok, tabp = pcall(vim.api.nvim_win_get_tabpage, q.winid) - if ok then - return tabp - end + if ok then return tabp end end ---@return lc.ui.Question @@ -78,15 +74,11 @@ end ---@param event lc.hook function utils.exec_hooks(event, ...) local fns = config.user.hooks[event] - if not fns then - log.error("unknown hook event: " .. event) - end + if not fns then log.error("unknown hook event: " .. event) end for i, fn in ipairs(fns) do local ok, msg = pcall(vim.schedule_wrap(fn), ...) - if not ok then - log.error(("bad hook #%d in `%s` event: %s"):format(i, event, msg)) - end + if not ok then log.error(("bad hook #%d in `%s` event: %s"):format(i, event, msg)) end end end @@ -112,9 +104,7 @@ function utils.norm_ins(str) end function utils.set_question_lines(q, code) - if not vim.api.nvim_buf_is_valid(q.bufnr) then - return - end + if not vim.api.nvim_buf_is_valid(q.bufnr) then return end local s_i, e_i = q:range() vim.api.nvim_buf_set_lines(q.bufnr, s_i - 1, e_i, false, vim.split(code, "\n")) From dc623d2d6c13a80e1834f0824c482938f92abfca Mon Sep 17 00:00:00 2001 From: kawre Date: Fri, 23 Feb 2024 17:33:56 +0100 Subject: [PATCH 22/29] docs(readme): cn plugins section --- README.md | 2 +- README.zh.md | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5a18aa30..06f3b065 100644 --- a/README.md +++ b/README.md @@ -434,7 +434,7 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/b7be8b95-5e2c-4153-8845-4 ### Non-Standalone mode To run [leetcode.nvim] in a non-standalone mode (i.e. not with argument or an empty Neovim session), -enable the "nonstandalone" plugin in your config: +enable the `nonstandalone` plugin in your config: ```lua plugins = { diff --git a/README.zh.md b/README.zh.md index 7f002dfe..33f1faaf 100644 --- a/README.zh.md +++ b/README.zh.md @@ -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 + plugins = { + nonstandalone = false, + }, + ---@type boolean logging = true, @@ -228,6 +233,17 @@ storage = { }, ``` +### plugins + +[插件列表](#-plugins) + +```lua +---@type table +plugins = { + nonstandalone = false, +}, +``` + ### logging 是否记录 [leetcode.nvim] 状态通知 @@ -408,6 +424,19 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/b7be8b95-5e2c-4153-8845-4 } ``` +## 🧩 Plugins + +### Non-Standalone mode + +要在非独立模式下运行 [leetcode.nvim](即不带参数或在空的 Neovim 会话中运行), +请在您的配置中启用 `nonstandalone` 插件: + +```lua +plugins = { + nonstandalone = true, +} +``` + ## 🙌 鸣谢 - [Leetbuddy.nvim](https://github.com/Dhanus3133/Leetbuddy.nvim) From 97d4e596370aa01b7c0ae3d4c813ef8a4548378e Mon Sep 17 00:00:00 2001 From: kawre Date: Fri, 23 Feb 2024 17:48:39 +0100 Subject: [PATCH 23/29] refactor: `Leet exit` --- lua/leetcode-plugins/nonstandalone/command.lua | 4 ---- lua/leetcode-plugins/nonstandalone/init.lua | 3 +-- lua/leetcode/command/init.lua | 6 ++++++ 3 files changed, 7 insertions(+), 6 deletions(-) delete mode 100644 lua/leetcode-plugins/nonstandalone/command.lua diff --git a/lua/leetcode-plugins/nonstandalone/command.lua b/lua/leetcode-plugins/nonstandalone/command.lua deleted file mode 100644 index 450a3829..00000000 --- a/lua/leetcode-plugins/nonstandalone/command.lua +++ /dev/null @@ -1,4 +0,0 @@ -local command = require("leetcode.command") -local leetcode = require("leetcode") - -command.commands.exit = { leetcode.stop } diff --git a/lua/leetcode-plugins/nonstandalone/init.lua b/lua/leetcode-plugins/nonstandalone/init.lua index 1643bcbf..12904955 100644 --- a/lua/leetcode-plugins/nonstandalone/init.lua +++ b/lua/leetcode-plugins/nonstandalone/init.lua @@ -5,9 +5,8 @@ nonstandalone.opts = { lazy = false, } -function nonstandalone.load() +function nonstandalone.load() -- require("leetcode-plugins.nonstandalone.leetcode") - require("leetcode-plugins.nonstandalone.command") end return nonstandalone diff --git a/lua/leetcode/command/init.lua b/lua/leetcode/command/init.lua index f627a161..bcf2bd1c 100644 --- a/lua/leetcode/command/init.lua +++ b/lua/leetcode/command/init.lua @@ -99,6 +99,11 @@ cmd.q_close_all = vim.schedule_wrap(function() end end) +function cmd.exit() + local leetcode = require("leetcode") + leetcode.stop() +end + cmd.expire = vim.schedule_wrap(function() local tabp = api.nvim_get_current_tabpage() cmd.menu() @@ -492,6 +497,7 @@ cmd.commands = { cmd.menu, menu = { cmd.menu }, + exit = { cmd.exit }, console = { cmd.console }, info = { cmd.info }, hints = { cmd.hints }, From d0407cfb7a8a4e6e7aa5158c62e21b8a0e81c806 Mon Sep 17 00:00:00 2001 From: kawre Date: Fri, 23 Feb 2024 17:59:56 +0100 Subject: [PATCH 24/29] refactor!: rename to `non_standalone` --- README.md | 8 ++++---- README.zh.md | 8 ++++---- lua/leetcode-plugins/non_standalone/init.lua | 12 ++++++++++++ .../{nonstandalone => non_standalone}/leetcode.lua | 0 lua/leetcode-plugins/nonstandalone/init.lua | 12 ------------ lua/leetcode/config/template.lua | 2 +- 6 files changed, 21 insertions(+), 21 deletions(-) create mode 100644 lua/leetcode-plugins/non_standalone/init.lua rename lua/leetcode-plugins/{nonstandalone => non_standalone}/leetcode.lua (100%) delete mode 100644 lua/leetcode-plugins/nonstandalone/init.lua diff --git a/README.md b/README.md index 06f3b065..47fd1a72 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat ---@type table plugins = { - nonstandalone = false, + non_standalone = false, }, ---@type boolean @@ -239,7 +239,7 @@ storage = { ```lua ---@type table plugins = { - nonstandalone = false, + non_standalone = false, }, ``` @@ -434,11 +434,11 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/b7be8b95-5e2c-4153-8845-4 ### Non-Standalone mode To run [leetcode.nvim] in a non-standalone mode (i.e. not with argument or an empty Neovim session), -enable the `nonstandalone` plugin in your config: +enable the `non_standalone` plugin in your config: ```lua plugins = { - nonstandalone = true, + non_standalone = true, } ``` diff --git a/README.zh.md b/README.zh.md index 33f1faaf..26b704bc 100644 --- a/README.zh.md +++ b/README.zh.md @@ -97,7 +97,7 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/aee6584c-e099-4409-b114-1 ---@type table plugins = { - nonstandalone = false, + non_standalone = false, }, ---@type boolean @@ -240,7 +240,7 @@ storage = { ```lua ---@type table plugins = { - nonstandalone = false, + non_standalone = false, }, ``` @@ -429,11 +429,11 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/b7be8b95-5e2c-4153-8845-4 ### Non-Standalone mode 要在非独立模式下运行 [leetcode.nvim](即不带参数或在空的 Neovim 会话中运行), -请在您的配置中启用 `nonstandalone` 插件: +请在您的配置中启用 `non_standalone` 插件: ```lua plugins = { - nonstandalone = true, + non_standalone = true, } ``` diff --git a/lua/leetcode-plugins/non_standalone/init.lua b/lua/leetcode-plugins/non_standalone/init.lua new file mode 100644 index 00000000..709b7c80 --- /dev/null +++ b/lua/leetcode-plugins/non_standalone/init.lua @@ -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 diff --git a/lua/leetcode-plugins/nonstandalone/leetcode.lua b/lua/leetcode-plugins/non_standalone/leetcode.lua similarity index 100% rename from lua/leetcode-plugins/nonstandalone/leetcode.lua rename to lua/leetcode-plugins/non_standalone/leetcode.lua diff --git a/lua/leetcode-plugins/nonstandalone/init.lua b/lua/leetcode-plugins/nonstandalone/init.lua deleted file mode 100644 index 12904955..00000000 --- a/lua/leetcode-plugins/nonstandalone/init.lua +++ /dev/null @@ -1,12 +0,0 @@ ----@class lc.plugins.nonstandalone -local nonstandalone = {} - -nonstandalone.opts = { - lazy = false, -} - -function nonstandalone.load() -- - require("leetcode-plugins.nonstandalone.leetcode") -end - -return nonstandalone diff --git a/lua/leetcode/config/template.lua b/lua/leetcode/config/template.lua index c1d4dcb2..930c86ec 100644 --- a/lua/leetcode/config/template.lua +++ b/lua/leetcode/config/template.lua @@ -60,7 +60,7 @@ local M = { ---@type table plugins = { - nonstandalone = false, + non_standalone = false, }, ---@type boolean From 24353ab99dc8fc97e507fcdb6d4e7949a8b1a83a Mon Sep 17 00:00:00 2001 From: kawre Date: Mon, 26 Feb 2024 22:42:17 +0100 Subject: [PATCH 25/29] fix: hooks --- lua/leetcode-plugins/non_standalone/leetcode.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lua/leetcode-plugins/non_standalone/leetcode.lua b/lua/leetcode-plugins/non_standalone/leetcode.lua index 3441442b..ee98714e 100644 --- a/lua/leetcode-plugins/non_standalone/leetcode.lua +++ b/lua/leetcode-plugins/non_standalone/leetcode.lua @@ -61,7 +61,7 @@ leetcode.start = function(on_vimenter) Menu():mount() local utils = require("leetcode.utils") - utils.exec_hooks("LeetEnter") + utils.exec_hooks("enter") return true end @@ -77,7 +77,8 @@ leetcode.stop = vim.schedule_wrap(function() desc = "Open leetcode.nvim", }) - require("leetcode.utils").exec_hooks("LeetLeave") + local utils = require("leetcode.utils") + utils.exec_hooks("leave") if prev_cwd then vim.api.nvim_set_current_dir(prev_cwd) From fa4da78fbef3199404d1e4e0437bc25bdc78698e Mon Sep 17 00:00:00 2001 From: kawre Date: Mon, 26 Feb 2024 22:54:47 +0100 Subject: [PATCH 26/29] fix: crash on `Leet exit` --- lua/leetcode-ui/renderer/menu.lua | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lua/leetcode-ui/renderer/menu.lua b/lua/leetcode-ui/renderer/menu.lua index eedd4e51..0de10562 100644 --- a/lua/leetcode-ui/renderer/menu.lua +++ b/lua/leetcode-ui/renderer/menu.lua @@ -140,10 +140,9 @@ end function Menu:_unmount() require("leetcode.command").q_close_all() - -- close menu tabpage - if self.bufnr and vim.api.nvim_buf_is_valid(self.bufnr) then - vim.api.nvim_buf_delete(self.bufnr, { force = true }) - end + -- if self.bufnr and vim.api.nvim_buf_is_valid(self.bufnr) then + -- vim.api.nvim_buf_delete(self.bufnr, { force = true }) + -- end if self.winid and vim.api.nvim_win_is_valid(self.winid) then vim.api.nvim_win_close(self.winid, true) From 45dafd4f7df69df6f7ab775840fd25dacdbbb889 Mon Sep 17 00:00:00 2001 From: kawre Date: Mon, 26 Feb 2024 22:57:42 +0100 Subject: [PATCH 27/29] fix: change buf on `Leet menu` --- lua/leetcode/command/init.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lua/leetcode/command/init.lua b/lua/leetcode/command/init.lua index 008cd44c..20d3eb69 100644 --- a/lua/leetcode/command/init.lua +++ b/lua/leetcode/command/init.lua @@ -165,10 +165,12 @@ function cmd.start_with_cmd() end function cmd.menu() - local ok, tabp = pcall(api.nvim_win_get_tabpage, _Lc_state.menu.winid) + local winid, bufnr = _Lc_state.menu.winid, _Lc_state.menu.bufnr + local ok, tabp = pcall(api.nvim_win_get_tabpage, winid) if ok then api.nvim_set_current_tabpage(tabp) + api.nvim_win_set_buf(winid, bufnr) else _Lc_state.menu:remount() end From ab866732751554fe36e17d4fbbeb8257c7321d0c Mon Sep 17 00:00:00 2001 From: kawre Date: Wed, 28 Feb 2024 22:12:05 +0100 Subject: [PATCH 28/29] docs: sync --- README.md | 8 ++++++-- README.zh.md | 10 ++++++++++ lua/leetcode-ui/renderer/menu.lua | 10 ++++++---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0d9e141a..5171bd4d 100644 --- a/README.md +++ b/README.md @@ -142,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 = { @@ -154,7 +157,6 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat focus_result = "L", ---@type string }, - ---@type boolean image_support = false, } @@ -314,7 +316,7 @@ image_support = false, - `menu` same as `Leet` -- [`exit`](#exiting-leetcodenvim) closes [leetcode.nvim] +- `exit` close [leetcode.nvim] - `console` opens console pop-up for currently opened question @@ -454,6 +456,8 @@ plugins = { } ``` +You can then exit [leetcode.nvim] using `:Leet exit` command + ## 🙌 Credits - [Leetbuddy.nvim](https://github.com/Dhanus3133/Leetbuddy.nvim) diff --git a/README.zh.md b/README.zh.md index 7eadfec9..6cc7a336 100644 --- a/README.zh.md +++ b/README.zh.md @@ -144,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 = { @@ -287,6 +290,9 @@ hooks = { ---@type fun(question: lc.ui.Question)[] ["question_enter"] = {}, + + ---@type fun()[] + ["leave"] = {}, }, ``` @@ -309,6 +315,8 @@ image_support = false, -- 将此设置为 `true` 将禁用问题描述的换行 - `menu` 与 `Leet` 相同 +- `exit` 关闭 [leetcode.nvim] + - `console` 打开当前打开问题的控制台弹出窗口 - `info` 打开包含当前打开问题信息的弹出窗口 @@ -446,6 +454,8 @@ plugins = { } ``` +你可以使用 `:Leet exit` 命令退出 [leetcode.nvim] + ## 🙌 鸣谢 - [Leetbuddy.nvim](https://github.com/Dhanus3133/Leetbuddy.nvim) diff --git a/lua/leetcode-ui/renderer/menu.lua b/lua/leetcode-ui/renderer/menu.lua index 0de10562..3db3b792 100644 --- a/lua/leetcode-ui/renderer/menu.lua +++ b/lua/leetcode-ui/renderer/menu.lua @@ -140,13 +140,15 @@ end function Menu:_unmount() require("leetcode.command").q_close_all() - -- if self.bufnr and vim.api.nvim_buf_is_valid(self.bufnr) then - -- vim.api.nvim_buf_delete(self.bufnr, { force = true }) - -- end - 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 ---@param self lc.ui.Menu From 317da16b3ceff994646308f0cf44659d7340e66f Mon Sep 17 00:00:00 2001 From: kawre Date: Wed, 28 Feb 2024 22:23:35 +0100 Subject: [PATCH 29/29] refactor: check if `vim.v.dying` --- lua/leetcode-plugins/non_standalone/leetcode.lua | 2 +- lua/leetcode-ui/question.lua | 6 +++--- lua/leetcode-ui/renderer/menu.lua | 9 +++++---- lua/leetcode/command/init.lua | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lua/leetcode-plugins/non_standalone/leetcode.lua b/lua/leetcode-plugins/non_standalone/leetcode.lua index ee98714e..2c9df97c 100644 --- a/lua/leetcode-plugins/non_standalone/leetcode.lua +++ b/lua/leetcode-plugins/non_standalone/leetcode.lua @@ -69,7 +69,7 @@ end leetcode.stop = vim.schedule_wrap(function() if standalone then return vim.cmd("qa!") end - _Lc_state.menu:_unmount() + _Lc_state.menu:unmount() vim.api.nvim_create_user_command("Leet", require("leetcode.command").start_with_cmd, { bar = true, diff --git a/lua/leetcode-ui/question.lua b/lua/leetcode-ui/question.lua index 99739ac6..44ab426d 100644 --- a/lua/leetcode-ui/question.lua +++ b/lua/leetcode-ui/question.lua @@ -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 @@ -175,7 +175,7 @@ function Question:unmount() 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 @@ -184,7 +184,7 @@ 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 diff --git a/lua/leetcode-ui/renderer/menu.lua b/lua/leetcode-ui/renderer/menu.lua index ae170df4..784fbf08 100644 --- a/lua/leetcode-ui/renderer/menu.lua +++ b/lua/leetcode-ui/renderer/menu.lua @@ -137,7 +137,11 @@ function Menu:apply_options() }) end -function Menu:_unmount() +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 @@ -151,9 +155,6 @@ function Menu:_unmount() end) end ----@param self lc.ui.Menu -Menu.unmount = vim.schedule_wrap(function(self) self:_unmount() end) - function Menu:remount() if self.winid and api.nvim_win_is_valid(self.winid) then -- api.nvim_win_close(self.winid, true) diff --git a/lua/leetcode/command/init.lua b/lua/leetcode/command/init.lua index 2973d7d5..f8628b2e 100644 --- a/lua/leetcode/command/init.lua +++ b/lua/leetcode/command/init.lua @@ -95,7 +95,7 @@ cmd.q_close_all = function() local qs = utils.question_tabs() for _, tabp in ipairs(qs) do - tabp.question:_unmount() + tabp.question:unmount() end end