A Neovim plugin enabling you to solve LeetCode problems.
feat: `@leet` tags
4 files changed, +36 -15
+11-3lua/leetcode-ui/question.lua
| @@ -58,6 +58,10 @@ function Question:inject(before) | ||
| 58 | 58 | |
| 59 | 59 | local res |
| 60 | 60 | |
| 61 | + if type(inj) == "boolean" and inj == true and before then -- | |
| 62 | + inj = config.imports[self.lang] | |
| 63 | + end | |
| 64 | + | |
| 61 | 65 | if type(inj) == "table" then |
| 62 | 66 | res = table.concat(inj, "\n") |
| 63 | 67 | elseif type(inj) == "string" then |
| @@ -129,7 +133,7 @@ function Question:handle_mount() | ||
| 129 | 133 | self.console = Console(self) |
| 130 | 134 | self.info = Info(self) |
| 131 | 135 | |
| 132 | - utils.exec_hooks("LeetQuestionNew", self) | |
| 136 | + utils.exec_hook("LeetQuestionNew", self) | |
| 133 | 137 | |
| 134 | 138 | return self |
| 135 | 139 | end |
| @@ -163,7 +167,7 @@ end | ||
| 163 | 167 | ---@return integer, integer, string[] |
| 164 | 168 | function Question:range(inclusive) |
| 165 | 169 | local lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false) |
| 166 | - local start_i, end_i = 1, #lines | |
| 170 | + local start_i, end_i | |
| 167 | 171 | |
| 168 | 172 | for i, line in ipairs(lines) do |
| 169 | 173 | if line:match("@leet start") then |
| @@ -180,6 +184,10 @@ end | ||
| 180 | 184 | ---@return string |
| 181 | 185 | function Question:lines(submit) |
| 182 | 186 | local start_i, end_i, lines = self:range() |
| 187 | + | |
| 188 | + start_i = start_i or 1 | |
| 189 | + end_i = end_i or #lines | |
| 190 | + | |
| 183 | 191 | local prefix = not submit and ("\n"):rep(start_i - 1) or "" |
| 184 | 192 | return prefix .. table.concat(lines, "\n", start_i, end_i) |
| 185 | 193 | end |
| @@ -201,7 +209,7 @@ Question.change_lang = vim.schedule_wrap(function(self, lang) | ||
| 201 | 209 | |
| 202 | 210 | self.bufnr = new_bufnr |
| 203 | 211 | if bufloaded == 0 then -- |
| 204 | - utils.exec_hooks("LeetQuestionNew", self) | |
| 212 | + utils.exec_hook("LeetQuestionNew", self) | |
| 205 | 213 | end |
| 206 | 214 | else |
| 207 | 215 | log.error("Changing language failed") |
+1-1lua/leetcode.lua
| @@ -58,7 +58,7 @@ function leetcode.start(on_vimenter) | ||
| 58 | 58 | Menu():mount() |
| 59 | 59 | |
| 60 | 60 | local utils = require("leetcode.utils") |
| 61 | - utils.exec_hooks("LeetEnter") | |
| 61 | + utils.exec_hook("LeetEnter") | |
| 62 | 62 | |
| 63 | 63 | return true |
| 64 | 64 | end |
+23-10lua/leetcode/command/init.lua
| @@ -178,8 +178,8 @@ function cmd.yank() | ||
| 178 | 178 | api.nvim_set_current_win(q.winid) |
| 179 | 179 | api.nvim_set_current_buf(q.bufnr) |
| 180 | 180 | |
| 181 | - local start_i, end_i = q:range() | |
| 182 | - vim.cmd(("%d,%dyank"):format(start_i, end_i)) | |
| 181 | + local start_i, end_i, lines = q:range() | |
| 182 | + vim.cmd(("%d,%dyank"):format(start_i or 1, end_i or #lines)) | |
| 183 | 183 | end |
| 184 | 184 | end |
| 185 | 185 | |
| @@ -341,17 +341,30 @@ function cmd.inject() | ||
| 341 | 341 | local q = utils.curr_question() |
| 342 | 342 | if not q then return end |
| 343 | 343 | |
| 344 | - local start_i, end_i = q:range(true) | |
| 345 | - | |
| 346 | 344 | if vim.api.nvim_buf_is_valid(q.bufnr) then |
| 347 | - local before = q:inject(true) | |
| 348 | - if before then | |
| 349 | - vim.api.nvim_buf_set_lines(q.bufnr, 0, start_i - 1, false, vim.split(before, "\n")) | |
| 345 | + local start_i, end_i = q:range(true) | |
| 346 | + | |
| 347 | + if start_i == nil and end_i == nil then | |
| 348 | + log.error("`@leet start` and `@leet end` not found") | |
| 349 | + return | |
| 350 | 350 | end |
| 351 | 351 | |
| 352 | - local after = q:inject(false) | |
| 353 | - if after then | |
| 354 | - vim.api.nvim_buf_set_lines(q.bufnr, end_i + 1, -1, false, vim.split(after, "\n")) | |
| 352 | + if start_i == nil then | |
| 353 | + log.error("`@leet start` not found") | |
| 354 | + else | |
| 355 | + local before = q:inject(true) | |
| 356 | + if before then | |
| 357 | + vim.api.nvim_buf_set_lines(q.bufnr, 0, start_i - 1, false, vim.split(before, "\n")) | |
| 358 | + end | |
| 359 | + end | |
| 360 | + | |
| 361 | + if end_i == nil then | |
| 362 | + log.error("`@leet start` not found") | |
| 363 | + else | |
| 364 | + local after = q:inject(false) | |
| 365 | + if after then | |
| 366 | + vim.api.nvim_buf_set_lines(q.bufnr, end_i + 1, -1, false, vim.split(after, "\n")) | |
| 367 | + end | |
| 355 | 368 | end |
| 356 | 369 | end |
| 357 | 370 | end |
+1-1lua/leetcode/utils.lua
| @@ -72,7 +72,7 @@ function utils.get_lang_by_name(name) | ||
| 72 | 72 | end |
| 73 | 73 | |
| 74 | 74 | ---@param event lc.hook |
| 75 | -function utils.exec_hooks(event, ...) | |
| 75 | +function utils.exec_hook(event, ...) | |
| 76 | 76 | local fns = config.user.hooks[event] |
| 77 | 77 | if not fns then log.error("unknown hook event: " .. event) end |
| 78 | 78 |