A Neovim plugin enabling you to solve LeetCode problems.
| 1 | local config = require("leetcode.config") |
| 2 | local log = require("leetcode.logger") |
| 3 | |
| 4 | ---@class lc.Utils |
| 5 | local utils = {} |
| 6 | |
| 7 | |
| 8 | vim.api.nvim_create_user_command(name, function() |
| 9 | local cmd = require("leetcode.command") |
| 10 | cmd.deprecate(name, new) |
| 11 | pcall(vim.cmd, new) ---@diagnostic disable-line |
| 12 | end, {}) |
| 13 | end |
| 14 | |
| 15 | ---@param fn string |
| 16 | |
| 17 | return ("<cmd>lua require('leetcode.command').%s()<cr>"):format(fn) |
| 18 | end |
| 19 | |
| 20 | ---@param title_slug string |
| 21 | ---@param lang lc.lang |
| 22 | |
| 23 | local tabs = utils.question_tabs() |
| 24 | |
| 25 | for _, q in ipairs(tabs) do |
| 26 | if title_slug == q.question.q.title_slug and lang == q.question.lang then |
| 27 | return q.tabpage |
| 28 | end |
| 29 | end |
| 30 | end |
| 31 | |
| 32 | ---@return { tabpage: integer, question: lc.ui.Question }[] |
| 33 | |
| 34 | local questions = {} |
| 35 | |
| 36 | for _, q in ipairs(_Lc_state.questions) do |
| 37 | local tabp = utils.question_tabp(q) |
| 38 | if tabp then |
| 39 | table.insert(questions, { tabpage = tabp, question = q }) |
| 40 | end |
| 41 | end |
| 42 | |
| 43 | return questions |
| 44 | end |
| 45 | |
| 46 | ---@param q lc.ui.Question |
| 47 | ---@return integer|nil |
| 48 | |
| 49 | local ok, tabp = pcall(vim.api.nvim_win_get_tabpage, q.winid) |
| 50 | if ok then |
| 51 | return tabp |
| 52 | end |
| 53 | end |
| 54 | |
| 55 | ---@return lc.ui.Question |
| 56 | |
| 57 | local tabp = vim.api.nvim_get_current_tabpage() |
| 58 | local tabs = utils.question_tabs() |
| 59 | |
| 60 | local tab = vim.tbl_filter(function(tab) |
| 61 | return tab.tabpage == tabp |
| 62 | end, tabs)[1] or {} |
| 63 | if tab.question then |
| 64 | return tab.question, tabp |
| 65 | else |
| 66 | log.error("No current question found") |
| 67 | end |
| 68 | end |
| 69 | |
| 70 | ---@return lc.language |
| 71 | |
| 72 | ---@param l lc.language |
| 73 | return vim.tbl_filter(function(l) |
| 74 | return l.slug == slug |
| 75 | end, config.langs)[1] |
| 76 | end |
| 77 | |
| 78 | ---@return lc.language |
| 79 | |
| 80 | ---@param l lc.language |
| 81 | return vim.tbl_filter(function(l) |
| 82 | return l.lang == name |
| 83 | end, config.langs)[1] |
| 84 | end |
| 85 | |
| 86 | ---@param event lc.hook |
| 87 | ---@return fun()[]|nil |
| 88 | |
| 89 | local fns = config.user.hooks[event] |
| 90 | if not fns then |
| 91 | return |
| 92 | end |
| 93 | |
| 94 | if type(fns) == "function" then |
| 95 | fns = { fns } |
| 96 | end |
| 97 | |
| 98 | return vim.list_extend(fns, config.hooks[event] or {}) |
| 99 | end |
| 100 | |
| 101 | ---@param event lc.hook |
| 102 | |
| 103 | local fns = utils.get_hooks(event) |
| 104 | if not fns then |
| 105 | return log.error("unknown hook event: " .. event) |
| 106 | end |
| 107 | |
| 108 | for i, fn in ipairs(fns) do |
| 109 | local ok, msg = pcall(fn, ...) |
| 110 | if not ok then |
| 111 | log.error(("bad hook #%d in `%s` event: %s"):format(i, event, msg)) |
| 112 | end |
| 113 | end |
| 114 | end |
| 115 | |
| 116 | ---@param content string |
| 117 | ---@param translated_content string |
| 118 | |
| 119 | if config.is_cn then |
| 120 | if config.user.cn.translate_problems then |
| 121 | return translated_content or content |
| 122 | else |
| 123 | return content or translated_content |
| 124 | end |
| 125 | else |
| 126 | return content |
| 127 | end |
| 128 | end |
| 129 | |
| 130 | |
| 131 | if not config.auth.is_signed_in then |
| 132 | error("User not logged-in", 0) |
| 133 | end |
| 134 | end |
| 135 | |
| 136 | |
| 137 | local ins = vim.inspect(str) |
| 138 | return ins:sub(2, #ins - 1) |
| 139 | end |
| 140 | |
| 141 | |
| 142 | with = with or function() end |
| 143 | without = without or function() end |
| 144 | return (vim.fn.has("nvim-" .. v) == 1 and with or without)() |
| 145 | end |
| 146 | |
| 147 | ---@param name lc.editor.section |
| 148 | ---@param start? boolean |
| 149 | |
| 150 | if name == "code" then |
| 151 | return ("@leet %s"):format(start and "start" or "end") |
| 152 | end |
| 153 | return ("@leet %s %s"):format(name, start and "start" or "end") |
| 154 | end |
| 155 | |
| 156 | return utils |