A Neovim plugin enabling you to solve LeetCode problems.
refactor(stylua): disable `collapse_simple_statement`
65 files changed, +971 -385
+0-2.stylua.toml
| @@ -4,5 +4,3 @@ indent_type = "Spaces" | ||
| 4 | 4 | indent_width = 4 |
| 5 | 5 | quote_style = "ForceDouble" |
| 6 | 6 | call_parentheses = "Always" |
| 7 | -# collapse_simple_statement = "FunctionOnly" | |
| 8 | -collapse_simple_statement = "Always" |
+12-4lua/leetcode-plugins/cn/api.lua
| @@ -17,7 +17,9 @@ statistics.solved = function(cb) ---@diagnostic disable-line | ||
| 17 | 17 | utils.query(query, variables, { |
| 18 | 18 | endpoint = urls.solved, |
| 19 | 19 | callback = function(res, err) |
| 20 | - if err then return cb(nil, err) end | |
| 20 | + if err then | |
| 21 | + return cb(nil, err) | |
| 22 | + end | |
| 21 | 23 | |
| 22 | 24 | local data = res.data |
| 23 | 25 | local submit_stats = data["submit_stats"] |
| @@ -38,7 +40,9 @@ statistics.session_progress = function(cb) | ||
| 38 | 40 | |
| 39 | 41 | utils.query(query, variables, { |
| 40 | 42 | callback = function(res, err) |
| 41 | - if err then return cb(nil, err) end | |
| 43 | + if err then | |
| 44 | + return cb(nil, err) | |
| 45 | + end | |
| 42 | 46 | |
| 43 | 47 | local data = res.data |
| 44 | 48 | local session_progress = data["userProfileUserQuestionProgress"]["numAcceptedQuestions"] |
| @@ -57,7 +61,9 @@ statistics.calendar = function(cb) ---@diagnostic disable-line | ||
| 57 | 61 | utils.query(query, variables, { |
| 58 | 62 | endpoint = urls.calendar, |
| 59 | 63 | callback = function(res, err) |
| 60 | - if err then return cb(nil, err) end | |
| 64 | + if err then | |
| 65 | + return cb(nil, err) | |
| 66 | + end | |
| 61 | 67 | |
| 62 | 68 | local data = res.data |
| 63 | 69 | local calendar = data["calendar"] |
| @@ -83,7 +89,9 @@ statistics.languages = function(cb) ---@diagnostic disable-line | ||
| 83 | 89 | utils.query(query, variables, { |
| 84 | 90 | endpoint = urls.languages, |
| 85 | 91 | callback = function(res, err) |
| 86 | - if err then return cb(nil, err) end | |
| 92 | + if err then | |
| 93 | + return cb(nil, err) | |
| 94 | + end | |
| 87 | 95 | |
| 88 | 96 | local data = res.data |
| 89 | 97 | local lang_prob_count = data["languageProblemCount"] |
+3-1lua/leetcode-plugins/cn/utils.lua
| @@ -3,7 +3,9 @@ local log = require("leetcode.logger") | ||
| 3 | 3 | local cn_utils = {} |
| 4 | 4 | |
| 5 | 5 | ---@param str string |
| 6 | -local function capitalizeFirst(str) return str:lower():gsub("^%l", string.upper) end | |
| 6 | +local function capitalizeFirst(str) | |
| 7 | + return str:lower():gsub("^%l", string.upper) | |
| 8 | +end | |
| 7 | 9 | |
| 8 | 10 | ---@return table |
| 9 | 11 | function cn_utils.calc_question_count(stats) |
+12-6lua/leetcode-plugins/non_standalone/leetcode.lua
| @@ -7,10 +7,14 @@ local prev_cwd = nil | ||
| 7 | 7 | ---@param on_vimenter boolean |
| 8 | 8 | leetcode.should_skip = function(on_vimenter) |
| 9 | 9 | if on_vimenter then |
| 10 | - if vim.fn.argc() ~= 1 then return true end | |
| 10 | + if vim.fn.argc() ~= 1 then | |
| 11 | + return true | |
| 12 | + end | |
| 11 | 13 | |
| 12 | 14 | local usr_arg, arg = vim.fn.argv()[1], config.user.arg |
| 13 | - if usr_arg ~= arg then return true end | |
| 15 | + if usr_arg ~= arg then | |
| 16 | + return true | |
| 17 | + end | |
| 14 | 18 | |
| 15 | 19 | local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true) |
| 16 | 20 | if #lines > 1 or (#lines == 1 and lines[1]:len() > 0) then |
| @@ -21,7 +25,7 @@ leetcode.should_skip = function(on_vimenter) | ||
| 21 | 25 | else |
| 22 | 26 | for _, buf_id in pairs(vim.api.nvim_list_bufs()) do |
| 23 | 27 | local bufinfo = vim.fn.getbufinfo(buf_id)[1] |
| 24 | - if bufinfo and (bufinfo.listed == 1 and #bufinfo.windows > 0) then -- | |
| 28 | + if bufinfo and (bufinfo.listed == 1 and #bufinfo.windows > 0) then | |
| 25 | 29 | return false, true |
| 26 | 30 | end |
| 27 | 31 | end |
| @@ -33,7 +37,7 @@ end | ||
| 33 | 37 | ---@param on_vimenter boolean |
| 34 | 38 | leetcode.start = function(on_vimenter) |
| 35 | 39 | local skip, buflisted = leetcode.should_skip(on_vimenter) |
| 36 | - if skip then -- | |
| 40 | + if skip then | |
| 37 | 41 | return false |
| 38 | 42 | end |
| 39 | 43 | |
| @@ -44,7 +48,7 @@ leetcode.start = function(on_vimenter) | ||
| 44 | 48 | local theme = require("leetcode.theme") |
| 45 | 49 | theme.setup() |
| 46 | 50 | |
| 47 | - if not on_vimenter then -- | |
| 51 | + if not on_vimenter then | |
| 48 | 52 | if buflisted then |
| 49 | 53 | prev_cwd = vim.fn.getcwd() |
| 50 | 54 | vim.cmd.tabe() |
| @@ -67,7 +71,9 @@ leetcode.start = function(on_vimenter) | ||
| 67 | 71 | end |
| 68 | 72 | |
| 69 | 73 | leetcode.stop = vim.schedule_wrap(function() |
| 70 | - if standalone then return vim.cmd("qa!") end | |
| 74 | + if standalone then | |
| 75 | + return vim.cmd("qa!") | |
| 76 | + end | |
| 71 | 77 | |
| 72 | 78 | _Lc_state.menu:unmount() |
| 73 | 79 |
+9-3lua/leetcode-ui/group/cases.lua
| @@ -20,13 +20,17 @@ function Cases:make_nav() | ||
| 20 | 20 | local nav = Lines({}, { padding = { top = 1 } }) |
| 21 | 21 | |
| 22 | 22 | for i, case in ipairs(self.cases) do |
| 23 | - self.console.result:map("n", i, function() self:change(i) end, { clearable = true }) | |
| 23 | + self.console.result:map("n", i, function() | |
| 24 | + self:change(i) | |
| 25 | + end, { clearable = true }) | |
| 24 | 26 | |
| 25 | 27 | local hl = self:nav_case_hl(case, i) |
| 26 | 28 | local msg = (" Case (%d) "):format(i) |
| 27 | 29 | |
| 28 | 30 | nav:append(msg, hl) |
| 29 | - if i ~= #self.cases then nav:append(" ") end | |
| 31 | + if i ~= #self.cases then | |
| 32 | + nav:append(" ") | |
| 33 | + end | |
| 30 | 34 | end |
| 31 | 35 | |
| 32 | 36 | return nav |
| @@ -43,7 +47,9 @@ end | ||
| 43 | 47 | |
| 44 | 48 | ---@param idx integer |
| 45 | 49 | function Cases:change(idx) |
| 46 | - if not self.cases[idx] or idx == self.idx then return end | |
| 50 | + if not self.cases[idx] or idx == self.idx then | |
| 51 | + return | |
| 52 | + end | |
| 47 | 53 | |
| 48 | 54 | self.idx = idx |
| 49 | 55 | self.console.result:draw() |
+16-6lua/leetcode-ui/group/init.lua
| @@ -36,7 +36,9 @@ function Group:contents() | ||
| 36 | 36 | local items = utils.shallowcopy(self._.items) |
| 37 | 37 | |
| 38 | 38 | local contents = Group.super.contents(self) |
| 39 | - if not vim.tbl_isempty(contents) then table.insert(items, Lines(contents)) end | |
| 39 | + if not vim.tbl_isempty(contents) then | |
| 40 | + table.insert(items, Lines(contents)) | |
| 41 | + end | |
| 40 | 42 | |
| 41 | 43 | return items |
| 42 | 44 | end |
| @@ -51,27 +53,35 @@ function Group:draw(layout, opts) | ||
| 51 | 53 | local toppad = padding and padding.top |
| 52 | 54 | local botpad = padding and padding.bot |
| 53 | 55 | |
| 54 | - if toppad then Pad(toppad):draw(layout) end | |
| 56 | + if toppad then | |
| 57 | + Pad(toppad):draw(layout) | |
| 58 | + end | |
| 55 | 59 | |
| 56 | 60 | local items = self:contents() |
| 57 | 61 | for i, item in ipairs(items) do |
| 58 | 62 | item:draw(layout, options:get()) |
| 59 | - if i ~= #items and spacing then Pad(spacing):draw(layout) end | |
| 63 | + if i ~= #items and spacing then | |
| 64 | + Pad(spacing):draw(layout) | |
| 65 | + end | |
| 60 | 66 | end |
| 61 | 67 | |
| 62 | - if botpad then Pad(botpad):draw(layout) end | |
| 68 | + if botpad then | |
| 69 | + Pad(botpad):draw(layout) | |
| 70 | + end | |
| 63 | 71 | end |
| 64 | 72 | |
| 65 | 73 | ---@param item lc.ui.Lines |
| 66 | 74 | function Group:insert(item) |
| 67 | - if not vim.tbl_isempty(Group.super.contents(self)) then self:endgrp() end | |
| 75 | + if not vim.tbl_isempty(Group.super.contents(self)) then | |
| 76 | + self:endgrp() | |
| 77 | + end | |
| 68 | 78 | table.insert(self._.items, item) |
| 69 | 79 | |
| 70 | 80 | return self |
| 71 | 81 | end |
| 72 | 82 | |
| 73 | 83 | function Group:append(content, highlight) |
| 74 | - if type(content) == "table" and O.is_instance(content, Group) then -- | |
| 84 | + if type(content) == "table" and O.is_instance(content, Group) then | |
| 75 | 85 | local items = content:contents() |
| 76 | 86 | |
| 77 | 87 | for _, item in ipairs(items) do |
+3-1lua/leetcode-ui/group/page/cache.lua
| @@ -18,7 +18,9 @@ page:insert(Title({ "Menu" }, "Cache")) | ||
| 18 | 18 | local update_btn = Button("Update", { |
| 19 | 19 | icon = "", |
| 20 | 20 | sc = "u", |
| 21 | - on_press = function() cmd.cache_update() end, | |
| 21 | + on_press = function() | |
| 22 | + cmd.cache_update() | |
| 23 | + end, | |
| 22 | 24 | }) |
| 23 | 25 | |
| 24 | 26 | local back_btn = BackButton("menu") |
+3-1lua/leetcode-ui/group/page/stats.lua
| @@ -38,7 +38,9 @@ local skills = Button("Skills", { | ||
| 38 | 38 | sc = "s", |
| 39 | 39 | on_press = cmd.ui_skills, |
| 40 | 40 | }) |
| 41 | -if not config.is_cn then buttons:insert(skills) end | |
| 41 | +if not config.is_cn then | |
| 42 | + buttons:insert(skills) | |
| 43 | +end | |
| 42 | 44 | |
| 43 | 45 | local languages = Button("Languages", { |
| 44 | 46 | icon = "", |
+1-1lua/leetcode-ui/group/pre/init.lua
| @@ -23,7 +23,7 @@ end | ||
| 23 | 23 | function Pre:init(title, item) |
| 24 | 24 | Pre.super.init(self, {}, { spacing = 1, position = "left" }) |
| 25 | 25 | |
| 26 | - if title then -- | |
| 26 | + if title then | |
| 27 | 27 | self:insert(title) |
| 28 | 28 | end |
| 29 | 29 |
+6-2lua/leetcode-ui/group/pre/input.lua
| @@ -13,8 +13,12 @@ function Input:init(title, input, params) -- | ||
| 13 | 13 | local group = Group({}, { spacing = 1 }) |
| 14 | 14 | |
| 15 | 15 | for i, case in ipairs(input) do |
| 16 | - local ok, param = pcall(function() return params[i].name end) | |
| 17 | - if ok then group:append(param .. " =", "leetcode_normal"):endl() end | |
| 16 | + local ok, param = pcall(function() | |
| 17 | + return params[i].name | |
| 18 | + end) | |
| 19 | + if ok then | |
| 20 | + group:append(param .. " =", "leetcode_normal"):endl() | |
| 21 | + end | |
| 18 | 22 | group:append(case):endgrp() |
| 19 | 23 | end |
| 20 | 24 |
+6-2lua/leetcode-ui/group/tag/a.lua
| @@ -6,11 +6,15 @@ local log = require("leetcode.logger") | ||
| 6 | 6 | local A = Tag:extend("LeetTagA") |
| 7 | 7 | |
| 8 | 8 | ---@param url string |
| 9 | -local function norm_url(url) return url:lower():gsub("/$", ""):gsub("#.-$", "") end | |
| 9 | +local function norm_url(url) | |
| 10 | + return url:lower():gsub("/$", ""):gsub("#.-$", "") | |
| 11 | +end | |
| 10 | 12 | |
| 11 | 13 | ---@param u1 string |
| 12 | 14 | ---@param u2 string |
| 13 | -local function is_same_url(u1, u2) return norm_url(u1) == norm_url(u2) end | |
| 15 | +local function is_same_url(u1, u2) | |
| 16 | + return norm_url(u1) == norm_url(u2) | |
| 17 | +end | |
| 14 | 18 | |
| 15 | 19 | function A:contents() |
| 16 | 20 | local Group = require("leetcode-ui.group") |
+20-8lua/leetcode-ui/group/tag/init.lua
| @@ -51,7 +51,9 @@ function Tag.normalize(text) | ||
| 51 | 51 | :gsub("\n", "&lcnl;") |
| 52 | 52 | :gsub("\t", "&lctab;") |
| 53 | 53 | :gsub("%s", " ") |
| 54 | - :gsub("<[^>]*>", function(match) return match:gsub(" ", " ") end) | |
| 54 | + :gsub("<[^>]*>", function(match) | |
| 55 | + return match:gsub(" ", " ") | |
| 56 | + end) | |
| 55 | 57 | -- :gsub("<a[^>]*>(.-)</a>", function(match) return match:gsub("&#?%w+;", utils.entity) end) |
| 56 | 58 | |
| 57 | 59 | log.debug(text) |
| @@ -71,7 +73,9 @@ function Tag:add_indent(item, text) | ||
| 71 | 73 | end |
| 72 | 74 | end |
| 73 | 75 | |
| 74 | -function Tag:get_text(node) return ts.get_node_text(node, self.text) end | |
| 76 | +function Tag:get_text(node) | |
| 77 | + return ts.get_node_text(node, self.text) | |
| 78 | +end | |
| 75 | 79 | |
| 76 | 80 | ---@param node TSNode |
| 77 | 81 | --- |
| @@ -95,7 +99,9 @@ end | ||
| 95 | 99 | -- 1206 |
| 96 | 100 | ---@param node TSNode |
| 97 | 101 | function Tag:get_el_data(node) |
| 98 | - if node:type() ~= "element" then return {} end | |
| 102 | + if node:type() ~= "element" then | |
| 103 | + return {} | |
| 104 | + end | |
| 99 | 105 | |
| 100 | 106 | local start_tag |
| 101 | 107 | for child in node:iter_children() do |
| @@ -107,7 +113,9 @@ function Tag:get_el_data(node) | ||
| 107 | 113 | end |
| 108 | 114 | end |
| 109 | 115 | |
| 110 | - if not start_tag then return {} end | |
| 116 | + if not start_tag then | |
| 117 | + return {} | |
| 118 | + end | |
| 111 | 119 | |
| 112 | 120 | local tag, attrs = nil, {} |
| 113 | 121 | for child in start_tag:iter_children() do |
| @@ -147,7 +155,9 @@ function Tag:parse_node() -- | ||
| 147 | 155 | end |
| 148 | 156 | |
| 149 | 157 | function Tag.trim(lines) -- |
| 150 | - if not lines or vim.tbl_isempty(lines) then return {} end | |
| 158 | + if not lines or vim.tbl_isempty(lines) then | |
| 159 | + return {} | |
| 160 | + end | |
| 151 | 161 | |
| 152 | 162 | while not vim.tbl_isempty(lines) and lines[1]:content() == "" do |
| 153 | 163 | table.remove(lines, 1) |
| @@ -160,7 +170,9 @@ function Tag.trim(lines) -- | ||
| 160 | 170 | return lines |
| 161 | 171 | end |
| 162 | 172 | |
| 163 | -local function req_tag(str) return require("leetcode-ui.group.tag." .. str) end | |
| 173 | +local function req_tag(str) | |
| 174 | + return require("leetcode-ui.group.tag." .. str) | |
| 175 | +end | |
| 164 | 176 | |
| 165 | 177 | function Tag:contents() |
| 166 | 178 | local items = Tag.super.contents(self) |
| @@ -224,13 +236,13 @@ end | ||
| 224 | 236 | local LeetTag = Tag |
| 225 | 237 | |
| 226 | 238 | ---@param text string |
| 227 | -function Tag.static:parse(text) -- | |
| 239 | +function Tag.static:parse(text) | |
| 228 | 240 | ---@type string |
| 229 | 241 | local normalized = Normalizer:norm(text) |
| 230 | 242 | |
| 231 | 243 | local ok, parser = pcall(ts.get_string_parser, normalized, "html") |
| 232 | 244 | |
| 233 | - if not ok then -- | |
| 245 | + if not ok then | |
| 234 | 246 | local Plain = require("leetcode.parser.plain") |
| 235 | 247 | return Plain:parse(text) |
| 236 | 248 | end |
+12-4lua/leetcode-ui/layout/console.lua
| @@ -44,10 +44,18 @@ function ConsoleLayout:mount() | ||
| 44 | 44 | ConsoleLayout.super.mount(self) |
| 45 | 45 | |
| 46 | 46 | self:set_keymaps({ |
| 47 | - [keys.reset_testcases] = function() self.testcase:reset() end, | |
| 48 | - [keys.use_testcase] = function() self:use_testcase() end, | |
| 49 | - [keys.focus_testcases] = function() self.testcase:focus() end, | |
| 50 | - [keys.focus_result] = function() self.result:focus() end, | |
| 47 | + [keys.reset_testcases] = function() | |
| 48 | + self.testcase:reset() | |
| 49 | + end, | |
| 50 | + [keys.use_testcase] = function() | |
| 51 | + self:use_testcase() | |
| 52 | + end, | |
| 53 | + [keys.focus_testcases] = function() | |
| 54 | + self.testcase:focus() | |
| 55 | + end, | |
| 56 | + [keys.focus_result] = function() | |
| 57 | + self.result:focus() | |
| 58 | + end, | |
| 51 | 59 | }) |
| 52 | 60 | end |
| 53 | 61 |
+3-1lua/leetcode-ui/layout/init.lua
| @@ -17,7 +17,9 @@ function Layout:show() | ||
| 17 | 17 | end |
| 18 | 18 | |
| 19 | 19 | function Layout:hide() |
| 20 | - if not self.visible then return end | |
| 20 | + if not self.visible then | |
| 21 | + return | |
| 22 | + end | |
| 21 | 23 | Layout.super.hide(self) |
| 22 | 24 | self.visible = false |
| 23 | 25 | end |
+7-3lua/leetcode-ui/line/init.lua
| @@ -7,10 +7,14 @@ local log = require("leetcode.logger") | ||
| 7 | 7 | ---@class lc.ui.Line : NuiLine |
| 8 | 8 | local Line = NuiLine:extend("LeetLine") |
| 9 | 9 | |
| 10 | -function Line:contents() return self._texts end | |
| 10 | +function Line:contents() | |
| 11 | + return self._texts | |
| 12 | +end | |
| 11 | 13 | |
| 12 | 14 | function Line:longest() |
| 13 | - if self.class.name == "LeetLine" then return vim.api.nvim_strwidth(self:content()) end | |
| 15 | + if self.class.name == "LeetLine" then | |
| 16 | + return vim.api.nvim_strwidth(self:content()) | |
| 17 | + end | |
| 14 | 18 | |
| 15 | 19 | local max_len = 0 |
| 16 | 20 | for _, item in pairs(self:contents()) do |
| @@ -29,7 +33,7 @@ function Line:draw(layout, opts) | ||
| 29 | 33 | local pad = options:get_padding() |
| 30 | 34 | |
| 31 | 35 | if pad then |
| 32 | - if pad.left then -- | |
| 36 | + if pad.left then | |
| 33 | 37 | table.insert(texts, 1, NuiText((" "):rep(pad.left))) |
| 34 | 38 | elseif pad.right then |
| 35 | 39 | table.insert(texts, NuiText((" "):rep(pad.right))) |
+3-1lua/leetcode-ui/lines/button/init.lua
| @@ -24,7 +24,9 @@ function Button:draw(renderer, opts) | ||
| 24 | 24 | Button.super.draw(self, renderer, opts) |
| 25 | 25 | end |
| 26 | 26 | |
| 27 | -function Button:press() self._.opts.on_press() end | |
| 27 | +function Button:press() | |
| 28 | + self._.opts.on_press() | |
| 29 | +end | |
| 28 | 30 | |
| 29 | 31 | ---@param lines lc.ui.Line[] |
| 30 | 32 | ---@param opts lc.ui.Button.opts |
+12-4lua/leetcode-ui/lines/calendar.lua
| @@ -18,7 +18,9 @@ local Calendar = Lines:extend("LeetCalendar") | ||
| 18 | 18 | |
| 19 | 19 | local function get_days_in_month(month, year) |
| 20 | 20 | local days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } |
| 21 | - if (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) then days_in_month[2] = 29 end | |
| 21 | + if (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) then | |
| 22 | + days_in_month[2] = 29 | |
| 23 | + end | |
| 22 | 24 | return days_in_month[month] |
| 23 | 25 | end |
| 24 | 26 | |
| @@ -57,7 +59,9 @@ function Calendar:handle_res(res) | ||
| 57 | 59 | hour = 1, |
| 58 | 60 | isdst = false, |
| 59 | 61 | }) |
| 60 | - if self.threshold <= os.time(time) then self.threshold = self.threshold + 24 * 60 * 60 end | |
| 62 | + if self.threshold <= os.time(time) then | |
| 63 | + self.threshold = self.threshold + 24 * 60 * 60 | |
| 64 | + end | |
| 61 | 65 | |
| 62 | 66 | self.last_year_sub_count = 0 |
| 63 | 67 | self.month_lens = {} |
| @@ -161,7 +165,9 @@ local function square_hl(count, max_count) | ||
| 161 | 165 | end |
| 162 | 166 | |
| 163 | 167 | function Calendar:handle_weekdays() |
| 164 | - if self.curr_time > self.threshold then return end | |
| 168 | + if self.curr_time > self.threshold then | |
| 169 | + return | |
| 170 | + end | |
| 165 | 171 | |
| 166 | 172 | local curr = os.date("*t", self.curr_time) |
| 167 | 173 | local count = self:get_submission(curr) or 0 |
| @@ -174,7 +180,9 @@ end | ||
| 174 | 180 | |
| 175 | 181 | function Calendar:fetch() |
| 176 | 182 | statistics.calendar(function(res, err) |
| 177 | - if err then return log.err(err) end | |
| 183 | + if err then | |
| 184 | + return log.err(err) | |
| 185 | + end | |
| 178 | 186 | self:handle_res(res) |
| 179 | 187 | end) |
| 180 | 188 | end |
+16-6lua/leetcode-ui/lines/init.lua
| @@ -14,7 +14,9 @@ function Lines:contents() | ||
| 14 | 14 | local lines = utils.shallowcopy(self._.lines) |
| 15 | 15 | |
| 16 | 16 | local contents = Lines.super.contents(self) |
| 17 | - if not vim.tbl_isempty(contents) then table.insert(lines, Line(contents)) end | |
| 17 | + if not vim.tbl_isempty(contents) then | |
| 18 | + table.insert(lines, Line(contents)) | |
| 19 | + end | |
| 18 | 20 | |
| 19 | 21 | return lines |
| 20 | 22 | end |
| @@ -57,10 +59,14 @@ function Lines:draw(layout, opts) | ||
| 57 | 59 | options:set({ padding = { left = leftpad } }) |
| 58 | 60 | |
| 59 | 61 | local toppad = padding and padding.top |
| 60 | - if toppad then lines = vim.list_extend(create_pad(toppad), lines) end | |
| 62 | + if toppad then | |
| 63 | + lines = vim.list_extend(create_pad(toppad), lines) | |
| 64 | + end | |
| 61 | 65 | |
| 62 | 66 | local botpad = padding and padding.bot |
| 63 | - if botpad then lines = vim.list_extend(lines, create_pad(botpad)) end | |
| 67 | + if botpad then | |
| 68 | + lines = vim.list_extend(lines, create_pad(botpad)) | |
| 69 | + end | |
| 64 | 70 | |
| 65 | 71 | for _, line in pairs(lines) do |
| 66 | 72 | line:draw(layout, options:get()) |
| @@ -68,12 +74,14 @@ function Lines:draw(layout, opts) | ||
| 68 | 74 | end |
| 69 | 75 | |
| 70 | 76 | function Lines:append(content, highlight) |
| 71 | - if type(content) == "table" and O.is_instance(content, Lines) then -- | |
| 77 | + if type(content) == "table" and O.is_instance(content, Lines) then | |
| 72 | 78 | local lines = content:contents() |
| 73 | 79 | |
| 74 | 80 | for i, line in ipairs(lines) do |
| 75 | 81 | Lines.super.append(self, line) |
| 76 | - if i ~= #lines then self:endl() end | |
| 82 | + if i ~= #lines then | |
| 83 | + self:endl() | |
| 84 | + end | |
| 77 | 85 | end |
| 78 | 86 | else |
| 79 | 87 | Lines.super.append(self, content, highlight) |
| @@ -90,7 +98,9 @@ function Lines:clear() | ||
| 90 | 98 | end |
| 91 | 99 | |
| 92 | 100 | function Lines:insert(item) -- |
| 93 | - if not vim.tbl_isempty(Lines.super.contents(self)) then self:endl() end | |
| 101 | + if not vim.tbl_isempty(Lines.super.contents(self)) then | |
| 102 | + self:endl() | |
| 103 | + end | |
| 94 | 104 | table.insert(self._.lines, item) |
| 95 | 105 | |
| 96 | 106 | return self |
+6-5lua/leetcode-ui/lines/solved.lua
| @@ -34,10 +34,9 @@ function Solved:handle_res(res) | ||
| 34 | 34 | |
| 35 | 35 | local max_count_len = 0 |
| 36 | 36 | for _, stat in ipairs(res.submit_stats.acSubmissionNum) do |
| 37 | - local total_count = vim.tbl_filter( | |
| 38 | - function(c) return c.difficulty == stat.difficulty end, | |
| 39 | - res.questions_count | |
| 40 | - )[1].count | |
| 37 | + local total_count = vim.tbl_filter(function(c) | |
| 38 | + return c.difficulty == stat.difficulty | |
| 39 | + end, res.questions_count)[1].count | |
| 41 | 40 | |
| 42 | 41 | local solved_line = Line() |
| 43 | 42 | solved_line:append(tostring(stat.count)) |
| @@ -93,7 +92,9 @@ end | ||
| 93 | 92 | |
| 94 | 93 | function Solved:fetch() |
| 95 | 94 | statistics.solved(function(res, err) |
| 96 | - if err then return log.err(err) end | |
| 95 | + if err then | |
| 96 | + return log.err(err) | |
| 97 | + end | |
| 97 | 98 | self:handle_res(res) |
| 98 | 99 | end) |
| 99 | 100 | end |
+6-2lua/leetcode-ui/opts.lua
| @@ -30,9 +30,13 @@ function Opts:merge(opts) -- | ||
| 30 | 30 | return self |
| 31 | 31 | end |
| 32 | 32 | |
| 33 | -function Opts:set(opts) self.opts = vim.tbl_deep_extend("force", self.opts, opts) end | |
| 33 | +function Opts:set(opts) | |
| 34 | + self.opts = vim.tbl_deep_extend("force", self.opts, opts) | |
| 35 | +end | |
| 34 | 36 | |
| 35 | -function Opts:get() return self.opts end | |
| 37 | +function Opts:get() | |
| 38 | + return self.opts | |
| 39 | +end | |
| 36 | 40 | |
| 37 | 41 | function Opts:init(opts) -- |
| 38 | 42 | self.opts = vim.tbl_deep_extend("force", { |
+3-1lua/leetcode-ui/popup/console/init.lua
| @@ -8,7 +8,9 @@ local ConsolePopup = Popup:extend("LeetConsolePopup") | ||
| 8 | 8 | ConsolePopup.handle_leave = vim.schedule_wrap(function(self) |
| 9 | 9 | local curr_bufnr = vim.api.nvim_get_current_buf() |
| 10 | 10 | for _, p in pairs(self.console.popups) do |
| 11 | - if p.bufnr == curr_bufnr then return end | |
| 11 | + if p.bufnr == curr_bufnr then | |
| 12 | + return | |
| 13 | + end | |
| 12 | 14 | end |
| 13 | 15 | self.console:hide() |
| 14 | 16 | end) |
+6-2lua/leetcode-ui/popup/console/result.lua
| @@ -16,12 +16,16 @@ function ResultPopup:handle(item) | ||
| 16 | 16 | self.border:set_highlight(item._.hl) |
| 17 | 17 | self.renderer:handle_res(item) |
| 18 | 18 | |
| 19 | - if item.last_testcase then self.last_testcase = item.last_testcase end | |
| 19 | + if item.last_testcase then | |
| 20 | + self.last_testcase = item.last_testcase | |
| 21 | + end | |
| 20 | 22 | |
| 21 | 23 | if item._.submission then |
| 22 | 24 | local status = item.status_code == 10 and "ac" or "notac" |
| 23 | 25 | problemlist.change_status(self.console.question.q.title_slug, status) |
| 24 | - if status == "ac" then config.stats.update_streak() end | |
| 26 | + if status == "ac" then | |
| 27 | + config.stats.update_streak() | |
| 28 | + end | |
| 25 | 29 | end |
| 26 | 30 | |
| 27 | 31 | self:draw() |
+30-13lua/leetcode-ui/popup/console/testcase.lua
| @@ -12,12 +12,16 @@ local Testcase = ConsolePopup:extend("LeetTestcasePopup") | ||
| 12 | 12 | |
| 13 | 13 | function Testcase:content() |
| 14 | 14 | local lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false) |
| 15 | - lines = vim.tbl_filter(function(line) return line ~= "" end, lines) | |
| 15 | + lines = vim.tbl_filter(function(line) | |
| 16 | + return line ~= "" | |
| 17 | + end, lines) | |
| 16 | 18 | return table.concat(lines, "\n") |
| 17 | 19 | end |
| 18 | 20 | |
| 19 | 21 | function Testcase:snapshot(id, data) -- |
| 20 | - if not data.test_case then return end | |
| 22 | + if not data.test_case then | |
| 23 | + return | |
| 24 | + end | |
| 21 | 25 | self.testcases[id] = data.test_case |
| 22 | 26 | end |
| 23 | 27 | |
| @@ -29,7 +33,9 @@ function Testcase:by_id(id) -- | ||
| 29 | 33 | for case in vim.gsplit(self.testcases[id] or "", "\n") do |
| 30 | 34 | local j = math.floor(i / n) + 1 |
| 31 | 35 | |
| 32 | - if not testcases[j] then testcases[j] = {} end | |
| 36 | + if not testcases[j] then | |
| 37 | + testcases[j] = {} | |
| 38 | + end | |
| 33 | 39 | table.insert(testcases[j], case) |
| 34 | 40 | |
| 35 | 41 | i = i + 1 |
| @@ -45,7 +51,9 @@ function Testcase:populate() | ||
| 45 | 51 | self.testcase_len = #vim.split(t_list[1] or "", "\n") |
| 46 | 52 | |
| 47 | 53 | for i, case in ipairs(t_list) do |
| 48 | - if i ~= 1 then table.insert(lines, "") end | |
| 54 | + if i ~= 1 then | |
| 55 | + table.insert(lines, "") | |
| 56 | + end | |
| 49 | 57 | for s in vim.gsplit(case, "\n", { trimempty = true }) do |
| 50 | 58 | table.insert(lines, s) |
| 51 | 59 | end |
| @@ -56,13 +64,14 @@ function Testcase:populate() | ||
| 56 | 64 | end |
| 57 | 65 | |
| 58 | 66 | function Testcase:clear_extmarks() |
| 59 | - if not config.user.console.testcase.virt_text then return end | |
| 67 | + if not config.user.console.testcase.virt_text then | |
| 68 | + return | |
| 69 | + end | |
| 60 | 70 | local ns = vim.api.nvim_create_namespace("leetcode_extmarks") |
| 61 | 71 | |
| 62 | - self.extmarks = vim.tbl_filter( | |
| 63 | - function(extmark) return not vim.api.nvim_buf_del_extmark(self.bufnr, ns, extmark) end, | |
| 64 | - self.extmarks | |
| 65 | - ) | |
| 72 | + self.extmarks = vim.tbl_filter(function(extmark) | |
| 73 | + return not vim.api.nvim_buf_del_extmark(self.bufnr, ns, extmark) | |
| 74 | + end, self.extmarks) | |
| 66 | 75 | end |
| 67 | 76 | |
| 68 | 77 | ---@param line integer |
| @@ -75,7 +84,9 @@ function Testcase:add_extmark(line, col, opts) | ||
| 75 | 84 | end |
| 76 | 85 | |
| 77 | 86 | function Testcase:draw_extmarks() |
| 78 | - if not config.user.console.testcase.virt_text then return end | |
| 87 | + if not config.user.console.testcase.virt_text then | |
| 88 | + return | |
| 89 | + end | |
| 79 | 90 | |
| 80 | 91 | self:clear_extmarks() |
| 81 | 92 | local bufnr = self.bufnr |
| @@ -83,7 +94,9 @@ function Testcase:draw_extmarks() | ||
| 83 | 94 | local md = self.console.question.q.meta_data |
| 84 | 95 | local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) |
| 85 | 96 | |
| 86 | - if not md.params then return end | |
| 97 | + if not md.params then | |
| 98 | + return | |
| 99 | + end | |
| 87 | 100 | |
| 88 | 101 | local j = 1 |
| 89 | 102 | local pad = (" "):rep(2) |
| @@ -102,7 +115,9 @@ function Testcase:draw_extmarks() | ||
| 102 | 115 | |
| 103 | 116 | for i, line in ipairs(lines) do |
| 104 | 117 | pcall(function() |
| 105 | - if lines[i - 1] == "" and lines[i] == "" then invalid = true end | |
| 118 | + if lines[i - 1] == "" and lines[i] == "" then | |
| 119 | + invalid = true | |
| 120 | + end | |
| 106 | 121 | end) |
| 107 | 122 | |
| 108 | 123 | if line ~= "" then |
| @@ -147,7 +162,9 @@ function Testcase:autocmds() | ||
| 147 | 162 | "TextChangedI", |
| 148 | 163 | "TextChangedP", |
| 149 | 164 | "InsertLeave", |
| 150 | - }, function() self:draw_extmarks() end) | |
| 165 | + }, function() | |
| 166 | + self:draw_extmarks() | |
| 167 | + end) | |
| 151 | 168 | end |
| 152 | 169 | |
| 153 | 170 | function Testcase:mount() |
+9-3lua/leetcode-ui/popup/info.lua
| @@ -92,7 +92,9 @@ function Info:populate() | ||
| 92 | 92 | if type(node.text) == "string" then |
| 93 | 93 | line:append(" ") |
| 94 | 94 | local txt = Parser:parse(node.text) |
| 95 | - if txt:lines()[1] then line:append(txt:lines()[1]) end | |
| 95 | + if txt:lines()[1] then | |
| 96 | + line:append(txt:lines()[1]) | |
| 97 | + end | |
| 96 | 98 | else |
| 97 | 99 | line:append(node.text) |
| 98 | 100 | end |
| @@ -106,9 +108,13 @@ function Info:populate() | ||
| 106 | 108 | |
| 107 | 109 | self:map("n", keys.confirm, function() |
| 108 | 110 | local node = tree:get_node() |
| 109 | - if not node then return end | |
| 111 | + if not node then | |
| 112 | + return | |
| 113 | + end | |
| 110 | 114 | |
| 111 | - if node.text and node.text.press then node.text:press() end | |
| 115 | + if node.text and node.text.press then | |
| 116 | + node.text:press() | |
| 117 | + end | |
| 112 | 118 | |
| 113 | 119 | if not node:is_expanded() then |
| 114 | 120 | node:expand() |
+21-7lua/leetcode-ui/popup/init.lua
| @@ -31,7 +31,9 @@ function Popup:_buf_destory() | ||
| 31 | 31 | end |
| 32 | 32 | |
| 33 | 33 | function Popup:focus() |
| 34 | - if not vim.api.nvim_win_is_valid(self.winid) then return end | |
| 34 | + if not vim.api.nvim_win_is_valid(self.winid) then | |
| 35 | + return | |
| 36 | + end | |
| 35 | 37 | vim.api.nvim_set_current_win(self.winid) |
| 36 | 38 | end |
| 37 | 39 | |
| @@ -56,19 +58,27 @@ function Popup:unmount() | ||
| 56 | 58 | self.visible = false |
| 57 | 59 | end |
| 58 | 60 | |
| 59 | -function Popup:map(...) self.renderer:map(...) end | |
| 61 | +function Popup:map(...) | |
| 62 | + self.renderer:map(...) | |
| 63 | +end | |
| 60 | 64 | |
| 61 | 65 | function Popup:mount() |
| 62 | 66 | Popup.super.mount(self) |
| 63 | 67 | |
| 64 | 68 | self.visible = true |
| 65 | 69 | |
| 66 | - self:on({ "BufLeave", "WinLeave" }, function() self:handle_leave() end) | |
| 67 | - self:map("n", keys.toggle, function() self:hide() end) | |
| 70 | + self:on({ "BufLeave", "WinLeave" }, function() | |
| 71 | + self:handle_leave() | |
| 72 | + end) | |
| 73 | + self:map("n", keys.toggle, function() | |
| 74 | + self:hide() | |
| 75 | + end) | |
| 68 | 76 | end |
| 69 | 77 | |
| 70 | 78 | function Popup:hide() |
| 71 | - if not self.visible then return end | |
| 79 | + if not self.visible then | |
| 80 | + return | |
| 81 | + end | |
| 72 | 82 | Popup.super.hide(self) |
| 73 | 83 | |
| 74 | 84 | self.visible = false |
| @@ -82,9 +92,13 @@ function Popup:toggle() | ||
| 82 | 92 | end |
| 83 | 93 | end |
| 84 | 94 | |
| 85 | -function Popup:handle_leave() self:hide() end | |
| 95 | +function Popup:handle_leave() | |
| 96 | + self:hide() | |
| 97 | +end | |
| 86 | 98 | |
| 87 | -function Popup:draw() self.renderer:draw(self) end | |
| 99 | +function Popup:draw() | |
| 100 | + self.renderer:draw(self) | |
| 101 | +end | |
| 88 | 102 | |
| 89 | 103 | function Popup:update_renderer() |
| 90 | 104 | self.renderer.bufnr = self.bufnr |
+6-2lua/leetcode-ui/popup/languages.lua
| @@ -42,9 +42,13 @@ end | ||
| 42 | 42 | ---@param res lc.Languages.Res |
| 43 | 43 | function Languages:populate(res) |
| 44 | 44 | local group = Group({}, { spacing = 1 }) |
| 45 | - if res == vim.NIL then return end | |
| 45 | + if res == vim.NIL then | |
| 46 | + return | |
| 47 | + end | |
| 46 | 48 | |
| 47 | - table.sort(res, function(a, b) return a.problems_solved > b.problems_solved end) | |
| 49 | + table.sort(res, function(a, b) | |
| 50 | + return a.problems_solved > b.problems_solved | |
| 51 | + end) | |
| 48 | 52 | for _, lang in ipairs(res) do |
| 49 | 53 | group:insert(self:handle(lang)) |
| 50 | 54 | end |
+3-1lua/leetcode-ui/popup/skills.lua
| @@ -16,7 +16,9 @@ local Skills = Popup:extend("LeetSkills") | ||
| 16 | 16 | function Skills:handle(name, skills) |
| 17 | 17 | local lines = Lines() |
| 18 | 18 | |
| 19 | - table.sort(skills, function(a, b) return a.problems_solved > b.problems_solved end) | |
| 19 | + table.sort(skills, function(a, b) | |
| 20 | + return a.problems_solved > b.problems_solved | |
| 21 | + end) | |
| 20 | 22 | lines:append(config.icons.square, utils.diff_to_hl(name)) |
| 21 | 23 | lines:append(" " .. name) |
| 22 | 24 |
+45-22lua/leetcode-ui/question.lua
| @@ -22,8 +22,12 @@ local Question = Object("LeetQuestion") | ||
| 22 | 22 | ---@param raw? boolean |
| 23 | 23 | function Question:snippet(raw) |
| 24 | 24 | local snippets = self.q.code_snippets ~= vim.NIL and self.q.code_snippets or {} |
| 25 | - local snip = vim.tbl_filter(function(snip) return snip.lang_slug == self.lang end, snippets)[1] | |
| 26 | - if not snip then return end | |
| 25 | + local snip = vim.tbl_filter(function(snip) | |
| 26 | + return snip.lang_slug == self.lang | |
| 27 | + end, snippets)[1] | |
| 28 | + if not snip then | |
| 29 | + return | |
| 30 | + end | |
| 27 | 31 | |
| 28 | 32 | local code = snip.code:gsub("\r\n", "\n") |
| 29 | 33 | return raw and code or self:injector(code) |
| @@ -31,7 +35,9 @@ end | ||
| 31 | 35 | |
| 32 | 36 | ---@param code? string |
| 33 | 37 | function Question:set_lines(code) |
| 34 | - if not vim.api.nvim_buf_is_valid(self.bufnr) then return end | |
| 38 | + if not vim.api.nvim_buf_is_valid(self.bufnr) then | |
| 39 | + return | |
| 40 | + end | |
| 35 | 41 | |
| 36 | 42 | pcall(vim.cmd.undojoin) |
| 37 | 43 | local s_i, e_i, lines = self:range() |
| @@ -42,7 +48,9 @@ function Question:set_lines(code) | ||
| 42 | 48 | end |
| 43 | 49 | |
| 44 | 50 | function Question:reset_lines() |
| 45 | - if not self.reset then return end | |
| 51 | + if not self.reset then | |
| 52 | + return | |
| 53 | + end | |
| 46 | 54 | |
| 47 | 55 | local new_lines = self:snippet(true) or "" |
| 48 | 56 | |
| @@ -63,7 +71,7 @@ function Question:path() | ||
| 63 | 71 | ("%s.%s-%s.%s"):format(self.q.frontend_id, self.q.title_slug, lang.slug, lang.ft) |
| 64 | 72 | self.file = config.storage.home:joinpath(fn_legacy) |
| 65 | 73 | |
| 66 | - if self.file:exists() then -- | |
| 74 | + if self.file:exists() then | |
| 67 | 75 | return self.file:absolute(), true |
| 68 | 76 | end |
| 69 | 77 | |
| @@ -71,7 +79,7 @@ function Question:path() | ||
| 71 | 79 | self.file = config.storage.home:joinpath(fn) |
| 72 | 80 | local existed = self.file:exists() |
| 73 | 81 | |
| 74 | - if not existed then -- | |
| 82 | + if not existed then | |
| 75 | 83 | self.file:write(self:snippet(), "w") |
| 76 | 84 | end |
| 77 | 85 | |
| @@ -94,11 +102,11 @@ function Question:open_buffer(existed) | ||
| 94 | 102 | vim.api.nvim_set_option_value("buflisted", true, { buf = self.bufnr }) |
| 95 | 103 | |
| 96 | 104 | local i = self:fold_range() |
| 97 | - if i then -- | |
| 105 | + if i then | |
| 98 | 106 | pcall(vim.cmd, ("%d,%dfold"):format(1, i)) |
| 99 | 107 | end |
| 100 | 108 | |
| 101 | - if existed then -- | |
| 109 | + if existed then | |
| 102 | 110 | self:reset_lines() |
| 103 | 111 | end |
| 104 | 112 | end |
| @@ -110,7 +118,7 @@ function Question:inject(before) | ||
| 110 | 118 | |
| 111 | 119 | local res |
| 112 | 120 | |
| 113 | - if type(inj) == "boolean" and inj == true and before then -- | |
| 121 | + if type(inj) == "boolean" and inj == true and before then | |
| 114 | 122 | inj = config.imports[self.lang] |
| 115 | 123 | end |
| 116 | 124 | |
| @@ -138,16 +146,20 @@ function Question:injector(code) | ||
| 138 | 146 | } |
| 139 | 147 | |
| 140 | 148 | local before = self:inject(true) |
| 141 | - if before then table.insert(parts, 1, before) end | |
| 149 | + if before then | |
| 150 | + table.insert(parts, 1, before) | |
| 151 | + end | |
| 142 | 152 | |
| 143 | 153 | local after = self:inject(false) |
| 144 | - if after then table.insert(parts, after) end | |
| 154 | + if after then | |
| 155 | + table.insert(parts, after) | |
| 156 | + end | |
| 145 | 157 | |
| 146 | 158 | return table.concat(parts, "\n") |
| 147 | 159 | end |
| 148 | 160 | |
| 149 | 161 | function Question:_unmount() |
| 150 | - if vim.v.dying ~= 0 then -- | |
| 162 | + if vim.v.dying ~= 0 then | |
| 151 | 163 | return |
| 152 | 164 | end |
| 153 | 165 | |
| @@ -160,17 +172,18 @@ function Question:_unmount() | ||
| 160 | 172 | vim.api.nvim_buf_delete(self.bufnr, { force = true, unload = false }) |
| 161 | 173 | end |
| 162 | 174 | |
| 163 | - _Lc_state.questions = vim.tbl_filter( | |
| 164 | - function(q) return q.bufnr ~= self.bufnr end, | |
| 165 | - _Lc_state.questions | |
| 166 | - ) | |
| 175 | + _Lc_state.questions = vim.tbl_filter(function(q) | |
| 176 | + return q.bufnr ~= self.bufnr | |
| 177 | + end, _Lc_state.questions) | |
| 167 | 178 | |
| 168 | 179 | self = nil |
| 169 | 180 | end) |
| 170 | 181 | end |
| 171 | 182 | |
| 172 | 183 | function Question:unmount() |
| 173 | - if vim.api.nvim_win_is_valid(self.winid) then vim.api.nvim_win_close(self.winid, true) end | |
| 184 | + if vim.api.nvim_win_is_valid(self.winid) then | |
| 185 | + vim.api.nvim_win_close(self.winid, true) | |
| 186 | + end | |
| 174 | 187 | end |
| 175 | 188 | |
| 176 | 189 | local group = vim.api.nvim_create_augroup("leetcode_questions", { clear = true }) |
| @@ -178,7 +191,9 @@ function Question:autocmds() | ||
| 178 | 191 | vim.api.nvim_create_autocmd("WinClosed", { |
| 179 | 192 | group = group, |
| 180 | 193 | pattern = tostring(self.winid), |
| 181 | - callback = function() self:_unmount() end, | |
| 194 | + callback = function() | |
| 195 | + self:_unmount() | |
| 196 | + end, | |
| 182 | 197 | }) |
| 183 | 198 | end |
| 184 | 199 | |
| @@ -199,7 +214,9 @@ end | ||
| 199 | 214 | |
| 200 | 215 | function Question:mount() |
| 201 | 216 | local tabp = utils.detect_duplicate_question(self.cache.title_slug, config.lang) |
| 202 | - if tabp then return pcall(vim.api.nvim_set_current_tabpage, tabp) end | |
| 217 | + if tabp then | |
| 218 | + return pcall(vim.api.nvim_set_current_tabpage, tabp) | |
| 219 | + end | |
| 203 | 220 | |
| 204 | 221 | local q = api_question.by_title_slug(self.cache.title_slug) |
| 205 | 222 | if not q or q.is_paid_only and not config.auth.is_premium then |
| @@ -241,14 +258,18 @@ end | ||
| 241 | 258 | |
| 242 | 259 | function Question:fold_range() |
| 243 | 260 | local start_i, _, lines = self:range(true) |
| 244 | - if start_i == nil or start_i <= 1 then return end | |
| 261 | + if start_i == nil or start_i <= 1 then | |
| 262 | + return | |
| 263 | + end | |
| 245 | 264 | |
| 246 | 265 | local i = start_i - 1 |
| 247 | 266 | while lines[i] == "" do |
| 248 | 267 | i = i - 1 |
| 249 | 268 | end |
| 250 | 269 | |
| 251 | - if 1 < i then return i end | |
| 270 | + if 1 < i then | |
| 271 | + return i | |
| 272 | + end | |
| 252 | 273 | end |
| 253 | 274 | |
| 254 | 275 | ---@param submit boolean |
| @@ -281,7 +302,9 @@ Question.change_lang = vim.schedule_wrap(function(self, lang) | ||
| 281 | 302 | vim.api.nvim_set_option_value("buflisted", false, { buf = old_bufnr }) |
| 282 | 303 | self:open_buffer(existed) |
| 283 | 304 | |
| 284 | - if not loaded then utils.exec_hooks("question_enter", self) end | |
| 305 | + if not loaded then | |
| 306 | + utils.exec_hooks("question_enter", self) | |
| 307 | + end | |
| 285 | 308 | end) |
| 286 | 309 | |
| 287 | 310 | if not ok then |
+48-16lua/leetcode-ui/renderer/init.lua
| @@ -24,9 +24,13 @@ function Renderer:draw(component) | ||
| 24 | 24 | self.bufnr = component.bufnr |
| 25 | 25 | self.winid = component.winid |
| 26 | 26 | |
| 27 | - if not vim.api.nvim_buf_is_valid(self.bufnr) then return end | |
| 27 | + if not vim.api.nvim_buf_is_valid(self.bufnr) then | |
| 28 | + return | |
| 29 | + end | |
| 28 | 30 | |
| 29 | - self:map("n", keys.confirm, function() self:handle_press() end) | |
| 31 | + self:map("n", keys.confirm, function() | |
| 32 | + self:handle_press() | |
| 33 | + end) | |
| 30 | 34 | |
| 31 | 35 | self:clear_keymaps() |
| 32 | 36 | self._.buttons = {} |
| @@ -39,7 +43,9 @@ function Renderer:draw(component) | ||
| 39 | 43 | |
| 40 | 44 | Renderer.super.draw(self, self, self._.opts) |
| 41 | 45 | end) |
| 42 | - if c_ok then pcall(vim.api.nvim_win_set_cursor, self.winid, c) end | |
| 46 | + if c_ok then | |
| 47 | + pcall(vim.api.nvim_win_set_cursor, self.winid, c) | |
| 48 | + end | |
| 43 | 49 | end |
| 44 | 50 | |
| 45 | 51 | ---@private |
| @@ -47,17 +53,27 @@ end | ||
| 47 | 53 | ---@param fn function |
| 48 | 54 | function Renderer:modifiable(fn) |
| 49 | 55 | local bufnr = self.bufnr |
| 50 | - if not (bufnr and vim.api.nvim_buf_is_valid(bufnr)) then return end | |
| 56 | + if not (bufnr and vim.api.nvim_buf_is_valid(bufnr)) then | |
| 57 | + return | |
| 58 | + end | |
| 51 | 59 | |
| 52 | 60 | local modi = vim.api.nvim_buf_get_option(bufnr, "modifiable") |
| 53 | - if not modi then vim.api.nvim_buf_set_option(bufnr, "modifiable", true) end | |
| 61 | + if not modi then | |
| 62 | + vim.api.nvim_buf_set_option(bufnr, "modifiable", true) | |
| 63 | + end | |
| 54 | 64 | fn() |
| 55 | - if not modi then vim.api.nvim_buf_set_option(bufnr, "modifiable", false) end | |
| 65 | + if not modi then | |
| 66 | + vim.api.nvim_buf_set_option(bufnr, "modifiable", false) | |
| 67 | + end | |
| 56 | 68 | end |
| 57 | 69 | |
| 58 | 70 | function Renderer:map(mode, key, handler, opts) -- |
| 59 | - if not self.bufnr then return end | |
| 60 | - if type(key) == "number" then key = tostring(key) end | |
| 71 | + if not self.bufnr then | |
| 72 | + return | |
| 73 | + end | |
| 74 | + if type(key) == "number" then | |
| 75 | + key = tostring(key) | |
| 76 | + end | |
| 61 | 77 | |
| 62 | 78 | if type(key) == "table" then |
| 63 | 79 | for _, k in ipairs(key) do |
| @@ -70,7 +86,9 @@ function Renderer:map(mode, key, handler, opts) -- | ||
| 70 | 86 | local clearable = options.clearable |
| 71 | 87 | options.clearable = nil |
| 72 | 88 | |
| 73 | - if clearable then self._.keymaps[key] = mode end | |
| 89 | + if clearable then | |
| 90 | + self._.keymaps[key] = mode | |
| 91 | + end | |
| 74 | 92 | vim.keymap.set(mode, key, handler, options) |
| 75 | 93 | vim.keymap.set(mode, key, handler, options) |
| 76 | 94 | end |
| @@ -94,7 +112,9 @@ function Renderer:clear() | ||
| 94 | 112 | self._.line_idx = 1 |
| 95 | 113 | self._.buttons = {} |
| 96 | 114 | self:clear_keymaps() |
| 97 | - self:modifiable(function() vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, {}) end) | |
| 115 | + self:modifiable(function() | |
| 116 | + vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, {}) | |
| 117 | + end) | |
| 98 | 118 | end |
| 99 | 119 | |
| 100 | 120 | ---@param val? integer Optional value to increment line index by |
| @@ -110,15 +130,25 @@ function Renderer:handle_press(line_idx) | ||
| 110 | 130 | vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<cr>", true, false, true), "n", true) |
| 111 | 131 | end |
| 112 | 132 | |
| 113 | - if not self.bufnr or not self.winid then feedenter() end | |
| 114 | - if not line_idx and not vim.api.nvim_win_is_valid(self.winid) then return feedenter() end | |
| 133 | + if not self.bufnr or not self.winid then | |
| 134 | + feedenter() | |
| 135 | + end | |
| 136 | + if not line_idx and not vim.api.nvim_win_is_valid(self.winid) then | |
| 137 | + return feedenter() | |
| 138 | + end | |
| 115 | 139 | |
| 116 | 140 | line_idx = line_idx or vim.api.nvim_win_get_cursor(self.winid)[1] |
| 117 | 141 | |
| 118 | - if not self._.buttons[line_idx] then return feedenter() end | |
| 142 | + if not self._.buttons[line_idx] then | |
| 143 | + return feedenter() | |
| 144 | + end | |
| 119 | 145 | |
| 120 | - local ok, err = pcall(function() self._.buttons[line_idx]:press() end) | |
| 121 | - if not ok then log.error(err) end | |
| 146 | + local ok, err = pcall(function() | |
| 147 | + self._.buttons[line_idx]:press() | |
| 148 | + end) | |
| 149 | + if not ok then | |
| 150 | + log.error(err) | |
| 151 | + end | |
| 122 | 152 | end |
| 123 | 153 | |
| 124 | 154 | ---@param button lc.ui.Button |
| @@ -131,7 +161,9 @@ function Renderer:apply_button(button) -- | ||
| 131 | 161 | end |
| 132 | 162 | |
| 133 | 163 | ---@param layout lc.ui.Renderer |
| 134 | -function Renderer:set(layout) self._.items = layout._.items end | |
| 164 | +function Renderer:set(layout) | |
| 165 | + self._.items = layout._.items | |
| 166 | +end | |
| 135 | 167 | |
| 136 | 168 | ---@param components lc.ui.Lines[] |
| 137 | 169 | ---@param opts? lc.ui.opts |
+9-3lua/leetcode-ui/renderer/result.lua
| @@ -54,7 +54,9 @@ function ResultLayout:handle_accepted(item) | ||
| 54 | 54 | |
| 55 | 55 | local lang = utils.get_lang_by_name(item.pretty_lang) |
| 56 | 56 | local lang_text = { item.pretty_lang, "Structure" } |
| 57 | - if lang then lang_text = { lang.icon .. " " .. lang.lang, lang.hl or "Structure" } end | |
| 57 | + if lang then | |
| 58 | + lang_text = { lang.icon .. " " .. lang.lang, lang.hl or "Structure" } | |
| 59 | + end | |
| 58 | 60 | |
| 59 | 61 | if config.translator then |
| 60 | 62 | runtime |
| @@ -108,7 +110,9 @@ end | ||
| 108 | 110 | --- |
| 109 | 111 | ---@param item lc.runtime |
| 110 | 112 | function ResultLayout:handle_runtime(item) -- status code = 10 |
| 111 | - if item._.submission then return self:handle_accepted(item) end | |
| 113 | + if item._.submission then | |
| 114 | + return self:handle_accepted(item) | |
| 115 | + end | |
| 112 | 116 | |
| 113 | 117 | local header = Header(item) |
| 114 | 118 | self:insert(header) |
| @@ -249,7 +253,9 @@ function ResultLayout:handle_res(item) | ||
| 249 | 253 | end, |
| 250 | 254 | |
| 251 | 255 | -- unknown |
| 252 | - ["unknown"] = function() log.error("unknown runner status code: " .. item.status_code) end, | |
| 256 | + ["unknown"] = function() | |
| 257 | + log.error("unknown runner status code: " .. item.status_code) | |
| 258 | + end, | |
| 253 | 259 | } |
| 254 | 260 | |
| 255 | 261 | local handler = handlers[item.status_code] |
+15-5lua/leetcode-ui/split/description.lua
| @@ -23,7 +23,9 @@ function Description:autocmds() | ||
| 23 | 23 | vim.api.nvim_create_autocmd("WinResized", { |
| 24 | 24 | group = group_id, |
| 25 | 25 | buffer = self.bufnr, |
| 26 | - callback = function() self:draw() end, | |
| 26 | + callback = function() | |
| 27 | + self:draw() | |
| 28 | + end, | |
| 27 | 29 | }) |
| 28 | 30 | end |
| 29 | 31 | |
| @@ -71,7 +73,9 @@ function Description:draw() | ||
| 71 | 73 | end |
| 72 | 74 | |
| 73 | 75 | function Description:draw_imgs() |
| 74 | - if not img_sup then return end | |
| 76 | + if not img_sup then | |
| 77 | + return | |
| 78 | + end | |
| 75 | 79 | |
| 76 | 80 | local lines = vim.api.nvim_buf_get_lines(self.bufnr, 1, -1, false) |
| 77 | 81 | for i, line in ipairs(lines) do |
| @@ -86,7 +90,9 @@ function Description:draw_imgs() | ||
| 86 | 90 | window = self.winid, |
| 87 | 91 | with_virtual_padding = true, |
| 88 | 92 | }, function(image) |
| 89 | - if not image then return end | |
| 93 | + if not image then | |
| 94 | + return | |
| 95 | + end | |
| 90 | 96 | |
| 91 | 97 | self.images[link] = image |
| 92 | 98 | image:render({ y = i + 1 }) |
| @@ -119,7 +125,9 @@ function Description:populate() | ||
| 119 | 125 | |
| 120 | 126 | header:append(q.frontend_id .. ". ", "leetcode_normal") |
| 121 | 127 | header:append(utils.translate(q.title, q.translated_title)) |
| 122 | - if q.is_paid_only then header:append(" " .. t("Premium"), "leetcode_medium") end | |
| 128 | + if q.is_paid_only then | |
| 129 | + header:append(" " .. t("Premium"), "leetcode_medium") | |
| 130 | + end | |
| 123 | 131 | header:endgrp() |
| 124 | 132 | |
| 125 | 133 | local show_stats = self.show_stats |
| @@ -141,7 +149,9 @@ function Description:populate() | ||
| 141 | 149 | header:append(likes .. " ", "leetcode_alt") |
| 142 | 150 | |
| 143 | 151 | local dislikes = show_stats and q.dislikes or "___" |
| 144 | - if not config.is_cn then header:append((" %s "):format(dislikes), "leetcode_alt") end | |
| 152 | + if not config.is_cn then | |
| 153 | + header:append((" %s "):format(dislikes), "leetcode_alt") | |
| 154 | + end | |
| 145 | 155 | |
| 146 | 156 | header:append((" %s "):format(config.icons.bar)) |
| 147 | 157 |
+15-5lua/leetcode-ui/split/init.lua
| @@ -49,7 +49,9 @@ function Split:show() | ||
| 49 | 49 | end |
| 50 | 50 | |
| 51 | 51 | function Split:hide() |
| 52 | - if not self.visible then return end | |
| 52 | + if not self.visible then | |
| 53 | + return | |
| 54 | + end | |
| 53 | 55 | Split.super.hide(self) |
| 54 | 56 | |
| 55 | 57 | self.visible = false |
| @@ -60,10 +62,14 @@ function Split:mount() | ||
| 60 | 62 | |
| 61 | 63 | self.visible = true |
| 62 | 64 | |
| 63 | - self:map("n", keys.toggle, function() self:toggle() end) | |
| 65 | + self:map("n", keys.toggle, function() | |
| 66 | + self:toggle() | |
| 67 | + end) | |
| 64 | 68 | end |
| 65 | 69 | |
| 66 | -function Split:map(...) self.renderer:map(...) end | |
| 70 | +function Split:map(...) | |
| 71 | + self.renderer:map(...) | |
| 72 | +end | |
| 67 | 73 | |
| 68 | 74 | function Split:unmount() |
| 69 | 75 | Split.super.unmount(self) |
| @@ -71,9 +77,13 @@ function Split:unmount() | ||
| 71 | 77 | self.visible = false |
| 72 | 78 | end |
| 73 | 79 | |
| 74 | -function Split:draw() self.renderer:draw(self) end | |
| 80 | +function Split:draw() | |
| 81 | + self.renderer:draw(self) | |
| 82 | +end | |
| 75 | 83 | |
| 76 | -function Split:clear() self.renderer:clear() end | |
| 84 | +function Split:clear() | |
| 85 | + self.renderer:clear() | |
| 86 | +end | |
| 77 | 87 | |
| 78 | 88 | function Split:update_renderer() |
| 79 | 89 | self.renderer.bufnr = self.bufnr |
+21-7lua/leetcode-ui/utils.lua
| @@ -17,7 +17,9 @@ end | ||
| 17 | 17 | |
| 18 | 18 | ---@param item lc.ui.Lines |
| 19 | 19 | function utils.longest_line(item) |
| 20 | - if item.class.name == "LeetLine" then return vim.api.nvim_strwidth(item:content()) end | |
| 20 | + if item.class.name == "LeetLine" then | |
| 21 | + return vim.api.nvim_strwidth(item:content()) | |
| 22 | + end | |
| 21 | 23 | |
| 22 | 24 | local max_len = 0 |
| 23 | 25 | for _, line in pairs(item:contents()) do |
| @@ -47,14 +49,18 @@ end | ||
| 47 | 49 | |
| 48 | 50 | ---@param status string |
| 49 | 51 | function utils.status_to_hl(status) |
| 50 | - if not status then return end | |
| 52 | + if not status then | |
| 53 | + return | |
| 54 | + end | |
| 51 | 55 | |
| 52 | 56 | return table.unpack(config.icons.hl.status[status]) |
| 53 | 57 | end |
| 54 | 58 | |
| 55 | 59 | ---@param layout lc.ui.Renderer |
| 56 | 60 | function utils.win_width(layout) |
| 57 | - if not vim.api.nvim_win_is_valid(layout.winid) then return 0 end | |
| 61 | + if not vim.api.nvim_win_is_valid(layout.winid) then | |
| 62 | + return 0 | |
| 63 | + end | |
| 58 | 64 | return vim.api.nvim_win_get_width(layout.winid) |
| 59 | 65 | end |
| 60 | 66 | |
| @@ -98,21 +104,29 @@ function utils.get_padding(lines, layout) | ||
| 98 | 104 | end |
| 99 | 105 | |
| 100 | 106 | function utils.set_buf_opts(bufnr, options) |
| 101 | - if not vim.api.nvim_buf_is_valid(bufnr) then return end | |
| 107 | + if not vim.api.nvim_buf_is_valid(bufnr) then | |
| 108 | + return | |
| 109 | + end | |
| 102 | 110 | |
| 103 | 111 | for opt, value in pairs(options) do |
| 104 | 112 | local ok, err = pcall(vim.api.nvim_set_option_value, opt, value, { buf = bufnr }) |
| 105 | - if not ok then log.error(err) end | |
| 113 | + if not ok then | |
| 114 | + log.error(err) | |
| 115 | + end | |
| 106 | 116 | end |
| 107 | 117 | end |
| 108 | 118 | |
| 109 | 119 | function utils.set_win_opts(winid, options) |
| 110 | - if not vim.api.nvim_win_is_valid(winid) then return end | |
| 120 | + if not vim.api.nvim_win_is_valid(winid) then | |
| 121 | + return | |
| 122 | + end | |
| 111 | 123 | |
| 112 | 124 | for opt, value in pairs(options) do |
| 113 | 125 | local ok, err = |
| 114 | 126 | pcall(vim.api.nvim_set_option_value, opt, value, { win = winid, scope = "local" }) |
| 115 | - if not ok then log.error(err) end | |
| 127 | + if not ok then | |
| 128 | + log.error(err) | |
| 129 | + end | |
| 116 | 130 | end |
| 117 | 131 | end |
| 118 | 132 |
+19-7lua/leetcode.lua
| @@ -8,10 +8,14 @@ local leetcode = {} | ||
| 8 | 8 | ---@return boolean |
| 9 | 9 | function leetcode.should_skip(on_vimenter) |
| 10 | 10 | if on_vimenter then |
| 11 | - if vim.fn.argc() ~= 1 then return true end | |
| 11 | + if vim.fn.argc() ~= 1 then | |
| 12 | + return true | |
| 13 | + end | |
| 12 | 14 | |
| 13 | 15 | local usr_arg, arg = config.user.arg, vim.fn.argv()[1] |
| 14 | - if usr_arg ~= arg then return true end | |
| 16 | + if usr_arg ~= arg then | |
| 17 | + return true | |
| 18 | + end | |
| 15 | 19 | |
| 16 | 20 | local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true) |
| 17 | 21 | if #lines > 1 or (#lines == 1 and lines[1]:len() > 0) then |
| @@ -33,11 +37,15 @@ function leetcode.should_skip(on_vimenter) | ||
| 33 | 37 | return false |
| 34 | 38 | end |
| 35 | 39 | |
| 36 | -function leetcode.setup_cmds() require("leetcode.command").setup() end | |
| 40 | +function leetcode.setup_cmds() | |
| 41 | + require("leetcode.command").setup() | |
| 42 | +end | |
| 37 | 43 | |
| 38 | 44 | ---@param on_vimenter boolean |
| 39 | 45 | function leetcode.start(on_vimenter) |
| 40 | - if leetcode.should_skip(on_vimenter) then return false end | |
| 46 | + if leetcode.should_skip(on_vimenter) then | |
| 47 | + return false | |
| 48 | + end | |
| 41 | 49 | |
| 42 | 50 | config.setup() |
| 43 | 51 | |
| @@ -48,7 +56,7 @@ function leetcode.start(on_vimenter) | ||
| 48 | 56 | local theme = require("leetcode.theme") |
| 49 | 57 | theme.setup() |
| 50 | 58 | |
| 51 | - if not on_vimenter then -- | |
| 59 | + if not on_vimenter then | |
| 52 | 60 | vim.cmd.enew() |
| 53 | 61 | end |
| 54 | 62 | |
| @@ -61,7 +69,9 @@ function leetcode.start(on_vimenter) | ||
| 61 | 69 | return true |
| 62 | 70 | end |
| 63 | 71 | |
| 64 | -function leetcode.stop() vim.cmd("qa!") end | |
| 72 | +function leetcode.stop() | |
| 73 | + vim.cmd("qa!") | |
| 74 | +end | |
| 65 | 75 | |
| 66 | 76 | ---@param cfg? lc.UserConfig |
| 67 | 77 | function leetcode.setup(cfg) |
| @@ -78,7 +88,9 @@ function leetcode.setup(cfg) | ||
| 78 | 88 | group = group_id, |
| 79 | 89 | pattern = "*", |
| 80 | 90 | nested = true, |
| 81 | - callback = function() leetcode.start(true) end, | |
| 91 | + callback = function() | |
| 92 | + leetcode.start(true) | |
| 93 | + end, | |
| 82 | 94 | }) |
| 83 | 95 | end |
| 84 | 96 |
+6-2lua/leetcode/api/auth.lua
| @@ -13,7 +13,9 @@ function Auth.user(cb) | ||
| 13 | 13 | |
| 14 | 14 | if cb then |
| 15 | 15 | utils.query(query, {}, { |
| 16 | - callback = function(res, err) cb(Auth.handle(res, err)) end, | |
| 16 | + callback = function(res, err) | |
| 17 | + cb(Auth.handle(res, err)) | |
| 18 | + end, | |
| 17 | 19 | endpoint = urls.auth, |
| 18 | 20 | }) |
| 19 | 21 | else |
| @@ -26,7 +28,9 @@ end | ||
| 26 | 28 | ---@private |
| 27 | 29 | ---@return lc.UserStatus, lc.err |
| 28 | 30 | function Auth.handle(res, err) |
| 29 | - if err then return res, err end | |
| 31 | + if err then | |
| 32 | + return res, err | |
| 33 | + end | |
| 30 | 34 | |
| 31 | 35 | local auth = res.data.userStatus |
| 32 | 36 | err = {} |
+39-13lua/leetcode/api/problems.lua
| @@ -17,33 +17,45 @@ function Problems.all(cb, noti) | ||
| 17 | 17 | local endpoint = urls.problems:format("algorithms") |
| 18 | 18 | |
| 19 | 19 | local spinner |
| 20 | - if noti then spinner = Spinner:init("updating problemlist cache...", "points") end | |
| 20 | + if noti then | |
| 21 | + spinner = Spinner:init("updating problemlist cache...", "points") | |
| 22 | + end | |
| 21 | 23 | |
| 22 | 24 | if cb then |
| 23 | 25 | utils.get(endpoint, { |
| 24 | 26 | callback = function(res, err) |
| 25 | 27 | if err then |
| 26 | - if spinner then spinner:stop(err.msg, false) end | |
| 28 | + if spinner then | |
| 29 | + spinner:stop(err.msg, false) | |
| 30 | + end | |
| 27 | 31 | return cb(nil, err) |
| 28 | 32 | end |
| 29 | 33 | |
| 30 | 34 | local problems = utils.normalize_problems(res.stat_status_pairs) |
| 31 | 35 | |
| 32 | 36 | if config.is_cn then |
| 33 | - if spinner then spinner:update("fetching title translations") end | |
| 37 | + if spinner then | |
| 38 | + spinner:update("fetching title translations") | |
| 39 | + end | |
| 34 | 40 | Problems.translated_titles(function(titles, terr) |
| 35 | 41 | if terr then |
| 36 | - if spinner then spinner:stop(terr.msg, false) end | |
| 42 | + if spinner then | |
| 43 | + spinner:stop(terr.msg, false) | |
| 44 | + end | |
| 37 | 45 | return cb(nil, terr) |
| 38 | 46 | end |
| 39 | 47 | |
| 40 | 48 | problems = utils.translate_titles(problems, titles) |
| 41 | - if spinner then spinner:stop("cache updated") end | |
| 49 | + if spinner then | |
| 50 | + spinner:stop("cache updated") | |
| 51 | + end | |
| 42 | 52 | |
| 43 | 53 | cb(problems) |
| 44 | 54 | end) |
| 45 | 55 | else |
| 46 | - if spinner then spinner:stop("cache updated") end | |
| 56 | + if spinner then | |
| 57 | + spinner:stop("cache updated") | |
| 58 | + end | |
| 47 | 59 | |
| 48 | 60 | cb(problems) |
| 49 | 61 | end |
| @@ -52,7 +64,9 @@ function Problems.all(cb, noti) | ||
| 52 | 64 | else |
| 53 | 65 | local res, err = utils.get(endpoint) |
| 54 | 66 | if err then |
| 55 | - if spinner then spinner:stop(err.msg, false) end | |
| 67 | + if spinner then | |
| 68 | + spinner:stop(err.msg, false) | |
| 69 | + end | |
| 56 | 70 | return nil, err |
| 57 | 71 | end |
| 58 | 72 | |
| @@ -61,14 +75,20 @@ function Problems.all(cb, noti) | ||
| 61 | 75 | if config.is_cn then |
| 62 | 76 | local titles, terr = Problems.translated_titles() |
| 63 | 77 | if terr then |
| 64 | - if spinner then spinner:stop(terr.msg, false) end | |
| 78 | + if spinner then | |
| 79 | + spinner:stop(terr.msg, false) | |
| 80 | + end | |
| 65 | 81 | return nil, terr |
| 66 | 82 | end |
| 67 | 83 | |
| 68 | - if spinner then spinner:stop("problems cache updated") end | |
| 84 | + if spinner then | |
| 85 | + spinner:stop("problems cache updated") | |
| 86 | + end | |
| 69 | 87 | return utils.translate_titles(problems, titles) |
| 70 | 88 | else |
| 71 | - if spinner then spinner:stop("problems cache updated") end | |
| 89 | + if spinner then | |
| 90 | + spinner:stop("problems cache updated") | |
| 91 | + end | |
| 72 | 92 | return problems |
| 73 | 93 | end |
| 74 | 94 | end |
| @@ -79,7 +99,9 @@ function Problems.question_of_today(cb) | ||
| 79 | 99 | |
| 80 | 100 | utils.query(query, {}, { |
| 81 | 101 | callback = function(res, err) |
| 82 | - if err then return cb(nil, err) end | |
| 102 | + if err then | |
| 103 | + return cb(nil, err) | |
| 104 | + end | |
| 83 | 105 | |
| 84 | 106 | local tday_record = res.data["todayRecord"] |
| 85 | 107 | local question = config.is_cn and tday_record[1].question or tday_record.question |
| @@ -94,13 +116,17 @@ function Problems.translated_titles(cb) | ||
| 94 | 116 | if cb then |
| 95 | 117 | utils.query(query, {}, { |
| 96 | 118 | callback = function(res, err) |
| 97 | - if err then return cb(nil, err) end | |
| 119 | + if err then | |
| 120 | + return cb(nil, err) | |
| 121 | + end | |
| 98 | 122 | cb(res.data.translations) |
| 99 | 123 | end, |
| 100 | 124 | }) |
| 101 | 125 | else |
| 102 | 126 | local res, err = utils.query(query, {}) |
| 103 | - if err then return nil, err end | |
| 127 | + if err then | |
| 128 | + return nil, err | |
| 129 | + end | |
| 104 | 130 | return res.data.translations |
| 105 | 131 | end |
| 106 | 132 | end |
+12-4lua/leetcode/api/question.lua
| @@ -20,12 +20,16 @@ function question.by_title_slug(title_slug) | ||
| 20 | 20 | local query = queries.question |
| 21 | 21 | |
| 22 | 22 | local res, err = utils.query(query, variables) |
| 23 | - if not res or err then return log.err(err) end | |
| 23 | + if not res or err then | |
| 24 | + return log.err(err) | |
| 25 | + end | |
| 24 | 26 | |
| 25 | 27 | local q = res.data.question |
| 26 | 28 | q.meta_data = select(2, pcall(utils.decode, q.meta_data)) |
| 27 | 29 | q.stats = select(2, pcall(utils.decode, q.stats)) |
| 28 | - if type(q.similar) == "string" then q.similar = utils.normalize_similar_cn(q.similar) end | |
| 30 | + if type(q.similar) == "string" then | |
| 31 | + q.similar = utils.normalize_similar_cn(q.similar) | |
| 32 | + end | |
| 29 | 33 | |
| 30 | 34 | return q |
| 31 | 35 | end |
| @@ -41,14 +45,18 @@ function question.random(filters) | ||
| 41 | 45 | |
| 42 | 46 | local config = require("leetcode.config") |
| 43 | 47 | local res, err = utils.query(query, variables) |
| 44 | - if err then return nil, err end | |
| 48 | + if err then | |
| 49 | + return nil, err | |
| 50 | + end | |
| 45 | 51 | |
| 46 | 52 | local q = res.data.randomQuestion |
| 47 | 53 | |
| 48 | 54 | if q == vim.NIL then |
| 49 | 55 | local msg = "Random question fetch responded with `null`" |
| 50 | 56 | |
| 51 | - if filters then msg = msg .. ".\n\nMaybe invalid filters?\n" .. vim.inspect(filters) end | |
| 57 | + if filters then | |
| 58 | + msg = msg .. ".\n\nMaybe invalid filters?\n" .. vim.inspect(filters) | |
| 59 | + end | |
| 52 | 60 | |
| 53 | 61 | return nil, { msg = msg, lvl = vim.log.levels.ERROR } |
| 54 | 62 | end |
+27-9lua/leetcode/api/statistics.lua
| @@ -17,7 +17,9 @@ function statistics.calendar(cb) | ||
| 17 | 17 | utils.query(query, variables, { |
| 18 | 18 | endpoint = urls.calendar, |
| 19 | 19 | callback = function(res, err) |
| 20 | - if err then return cb(nil, err) end | |
| 20 | + if err then | |
| 21 | + return cb(nil, err) | |
| 22 | + end | |
| 21 | 23 | |
| 22 | 24 | local data = res.data |
| 23 | 25 | local calendar = data["matchedUser"]["calendar"] |
| @@ -42,7 +44,9 @@ function statistics.session_progress(cb) | ||
| 42 | 44 | |
| 43 | 45 | utils.query(query, variables, { |
| 44 | 46 | callback = function(res, err) |
| 45 | - if err then return cb(nil, err) end | |
| 47 | + if err then | |
| 48 | + return cb(nil, err) | |
| 49 | + end | |
| 46 | 50 | |
| 47 | 51 | local data = res.data |
| 48 | 52 | local session_progress = data["matchedUser"]["submitStats"]["acSubmissionNum"] |
| @@ -62,7 +66,9 @@ function statistics.solved(cb) | ||
| 62 | 66 | utils.query(query, variables, { |
| 63 | 67 | endpoint = urls.solved, |
| 64 | 68 | callback = function(res, err) |
| 65 | - if err then return cb(nil, err) end | |
| 69 | + if err then | |
| 70 | + return cb(nil, err) | |
| 71 | + end | |
| 66 | 72 | |
| 67 | 73 | local data = res.data |
| 68 | 74 | local questions_count = data["allQuestionsCount"] |
| @@ -87,7 +93,9 @@ function statistics.skills(cb) | ||
| 87 | 93 | utils.query(query, variables, { |
| 88 | 94 | endpoint = urls.skills, |
| 89 | 95 | callback = function(res, err) |
| 90 | - if err then return cb(nil, err) end | |
| 96 | + if err then | |
| 97 | + return cb(nil, err) | |
| 98 | + end | |
| 91 | 99 | local data = res.data |
| 92 | 100 | local tag_problems_counts = data["matchedUser"]["tag_problems_counts"] |
| 93 | 101 | cb(tag_problems_counts) |
| @@ -106,7 +114,9 @@ function statistics.languages(cb) | ||
| 106 | 114 | utils.query(query, variables, { |
| 107 | 115 | endpoint = urls.languages, |
| 108 | 116 | callback = function(res, err) |
| 109 | - if err then return cb(nil, err) end | |
| 117 | + if err then | |
| 118 | + return cb(nil, err) | |
| 119 | + end | |
| 110 | 120 | local data = res.data |
| 111 | 121 | local lang_prob_count = data["matchedUser"]["languageProblemCount"] |
| 112 | 122 | cb(lang_prob_count) |
| @@ -122,7 +132,9 @@ function statistics.streak(cb) | ||
| 122 | 132 | utils.query(query, variables, { |
| 123 | 133 | endpoint = urls.streak_counter, |
| 124 | 134 | callback = function(res, err) |
| 125 | - if err then return cb(nil, err) end | |
| 135 | + if err then | |
| 136 | + return cb(nil, err) | |
| 137 | + end | |
| 126 | 138 | local data = res.data |
| 127 | 139 | local streak = data["streakCounter"] |
| 128 | 140 | cb(streak) |
| @@ -137,7 +149,9 @@ function statistics.sessions(cb) | ||
| 137 | 149 | utils.post(url, { |
| 138 | 150 | body = vim.empty_dict(), |
| 139 | 151 | callback = function(res, err) |
| 140 | - if err then return cb(nil, err) end | |
| 152 | + if err then | |
| 153 | + return cb(nil, err) | |
| 154 | + end | |
| 141 | 155 | config.sessions.update(res.sessions) |
| 142 | 156 | cb(res.sessions) |
| 143 | 157 | end, |
| @@ -155,7 +169,9 @@ function statistics.change_session(id, cb) | ||
| 155 | 169 | utils.put(url, { |
| 156 | 170 | body = body, |
| 157 | 171 | callback = function(res, err) |
| 158 | - if err then return cb(nil, err) end | |
| 172 | + if err then | |
| 173 | + return cb(nil, err) | |
| 174 | + end | |
| 159 | 175 | config.sessions.update(res.sessions) |
| 160 | 176 | cb(res.sessions) |
| 161 | 177 | end, |
| @@ -173,7 +189,9 @@ function statistics.create_session(name, cb) | ||
| 173 | 189 | utils.put(url, { |
| 174 | 190 | body = body, |
| 175 | 191 | callback = function(res, err) |
| 176 | - if err then return cb(nil, err) end | |
| 192 | + if err then | |
| 193 | + return cb(nil, err) | |
| 194 | + end | |
| 177 | 195 | config.sessions.update(res.sessions) |
| 178 | 196 | cb(res.sessions) |
| 179 | 197 | end, |
+49-41lua/leetcode/api/utils.lua
| @@ -63,10 +63,14 @@ function utils.curl(method, params) | ||
| 63 | 63 | }, params or {}) |
| 64 | 64 | local url = ("https://leetcode.%s%s"):format(config.domain, params.endpoint) |
| 65 | 65 | |
| 66 | - if type(params.body) == "table" then params.body = vim.json.encode(params.body) end | |
| 66 | + if type(params.body) == "table" then | |
| 67 | + params.body = vim.json.encode(params.body) | |
| 68 | + end | |
| 67 | 69 | |
| 68 | 70 | local tries = params.retry |
| 69 | - local function should_retry(err) return err and err.status >= 500 and tries > 0 end | |
| 71 | + local function should_retry(err) | |
| 72 | + return err and err.status >= 500 and tries > 0 | |
| 73 | + end | |
| 70 | 74 | |
| 71 | 75 | if params.callback then |
| 72 | 76 | local cb = vim.schedule_wrap(params.callback) |
| @@ -112,7 +116,9 @@ function utils.handle_res(out) | ||
| 112 | 116 | local ok, msg = pcall(function() |
| 113 | 117 | local dec = utils.decode(out.body) |
| 114 | 118 | |
| 115 | - if dec.error then return dec.error end | |
| 119 | + if dec.error then | |
| 120 | + return dec.error | |
| 121 | + end | |
| 116 | 122 | |
| 117 | 123 | local tbl = {} |
| 118 | 124 | for _, e in ipairs(dec.errors) do |
| @@ -139,7 +145,9 @@ end | ||
| 139 | 145 | |
| 140 | 146 | ---@param err lc.err |
| 141 | 147 | function utils.check_err(err) |
| 142 | - if not err then return end | |
| 148 | + if not err then | |
| 149 | + return | |
| 150 | + end | |
| 143 | 151 | |
| 144 | 152 | if err.status then |
| 145 | 153 | if err.status == 401 or err.status == 403 then |
| @@ -161,25 +169,26 @@ end | ||
| 161 | 169 | function utils.normalize_similar_cn(s) |
| 162 | 170 | s = select(2, pcall(utils.decode, s)) |
| 163 | 171 | |
| 164 | - return vim.tbl_map( | |
| 165 | - function(sq) | |
| 166 | - return { | |
| 167 | - title = sq.title, | |
| 168 | - translated_title = sq.translatedTitle, | |
| 169 | - paid_only = sq.isPaidOnly, | |
| 170 | - title_slug = sq.titleSlug, | |
| 171 | - difficulty = sq.difficulty, | |
| 172 | - } | |
| 173 | - end, | |
| 174 | - s | |
| 175 | - ) | |
| 172 | + return vim.tbl_map(function(sq) | |
| 173 | + return { | |
| 174 | + title = sq.title, | |
| 175 | + translated_title = sq.translatedTitle, | |
| 176 | + paid_only = sq.isPaidOnly, | |
| 177 | + title_slug = sq.titleSlug, | |
| 178 | + difficulty = sq.difficulty, | |
| 179 | + } | |
| 180 | + end, s) | |
| 176 | 181 | end |
| 177 | 182 | |
| 178 | -function utils.lvl_to_name(lvl) return ({ "Easy", "Medium", "Hard" })[lvl] end | |
| 183 | +function utils.lvl_to_name(lvl) | |
| 184 | + return ({ "Easy", "Medium", "Hard" })[lvl] | |
| 185 | +end | |
| 179 | 186 | |
| 180 | 187 | ---@return lc.cache.Question[] |
| 181 | 188 | function utils.normalize_problems(problems) |
| 182 | - problems = vim.tbl_filter(function(p) return not p.stat.question__hide end, problems) | |
| 189 | + problems = vim.tbl_filter(function(p) | |
| 190 | + return not p.stat.question__hide | |
| 191 | + end, problems) | |
| 183 | 192 | |
| 184 | 193 | local comp = function(a, b) |
| 185 | 194 | local a_fid = a.stat.frontend_question_id |
| @@ -200,28 +209,25 @@ function utils.normalize_problems(problems) | ||
| 200 | 209 | end |
| 201 | 210 | table.sort(problems, comp) |
| 202 | 211 | |
| 203 | - return vim.tbl_map( | |
| 204 | - function(p) | |
| 205 | - return { | |
| 206 | - status = p.status, | |
| 207 | - id = p.stat.question_id, | |
| 208 | - frontend_id = p.stat.frontend_question_id, | |
| 209 | - title = p.stat.question__title, | |
| 210 | - title_cn = "", | |
| 211 | - title_slug = p.stat.question__title_slug, | |
| 212 | - link = ("https://leetcode.%s/problems/%s/"):format( | |
| 213 | - config.domain, | |
| 214 | - p.stat.question__title_slug | |
| 215 | - ), | |
| 216 | - paid_only = p.paid_only, | |
| 217 | - ac_rate = p.stat.total_acs * 100 / math.max(p.stat.total_submitted, 1), | |
| 218 | - difficulty = utils.lvl_to_name(p.difficulty.level), | |
| 219 | - starred = p.is_favor, | |
| 220 | - topic_tags = {}, | |
| 221 | - } | |
| 222 | - end, | |
| 223 | - problems | |
| 224 | - ) | |
| 212 | + return vim.tbl_map(function(p) | |
| 213 | + return { | |
| 214 | + status = p.status, | |
| 215 | + id = p.stat.question_id, | |
| 216 | + frontend_id = p.stat.frontend_question_id, | |
| 217 | + title = p.stat.question__title, | |
| 218 | + title_cn = "", | |
| 219 | + title_slug = p.stat.question__title_slug, | |
| 220 | + link = ("https://leetcode.%s/problems/%s/"):format( | |
| 221 | + config.domain, | |
| 222 | + p.stat.question__title_slug | |
| 223 | + ), | |
| 224 | + paid_only = p.paid_only, | |
| 225 | + ac_rate = p.stat.total_acs * 100 / math.max(p.stat.total_submitted, 1), | |
| 226 | + difficulty = utils.lvl_to_name(p.difficulty.level), | |
| 227 | + starred = p.is_favor, | |
| 228 | + topic_tags = {}, | |
| 229 | + } | |
| 230 | + end, problems) | |
| 225 | 231 | end |
| 226 | 232 | |
| 227 | 233 | ---@param problems lc.cache.Question[] |
| @@ -234,7 +240,9 @@ function utils.translate_titles(problems, titles) | ||
| 234 | 240 | |
| 235 | 241 | return vim.tbl_map(function(p) |
| 236 | 242 | local title = map[tostring(p.id)] |
| 237 | - if title then p.title_cn = title end | |
| 243 | + if title then | |
| 244 | + p.title_cn = title | |
| 245 | + end | |
| 238 | 246 | return p |
| 239 | 247 | end, problems) |
| 240 | 248 | end |
+21-7lua/leetcode/cache/cookie.lua
| @@ -20,30 +20,40 @@ local Cookie = {} | ||
| 20 | 20 | ---@return string|nil |
| 21 | 21 | function Cookie.set(str) |
| 22 | 22 | local _, cerr = Cookie.parse(str) |
| 23 | - if cerr then return cerr end | |
| 23 | + if cerr then | |
| 24 | + return cerr | |
| 25 | + end | |
| 24 | 26 | |
| 25 | 27 | file:write(str, "w") |
| 26 | 28 | local auth_api = require("leetcode.api.auth") |
| 27 | 29 | local _, aerr = auth_api.user() |
| 28 | 30 | |
| 29 | - if aerr then return aerr.msg end | |
| 31 | + if aerr then | |
| 32 | + return aerr.msg | |
| 33 | + end | |
| 30 | 34 | end |
| 31 | 35 | |
| 32 | 36 | ---@return boolean |
| 33 | 37 | function Cookie.delete() |
| 34 | - if not file:exists() then return false end | |
| 38 | + if not file:exists() then | |
| 39 | + return false | |
| 40 | + end | |
| 35 | 41 | return pcall(path.rm, file) |
| 36 | 42 | end |
| 37 | 43 | |
| 38 | 44 | ---@return lc.cache.Cookie | nil |
| 39 | 45 | function Cookie.get() |
| 40 | - if not file:exists() then return end | |
| 46 | + if not file:exists() then | |
| 47 | + return | |
| 48 | + end | |
| 41 | 49 | |
| 42 | 50 | local fstats = file:_stat() |
| 43 | 51 | local ftime = fstats.mtime.sec |
| 44 | 52 | |
| 45 | 53 | local hcookie = hist[ftime] |
| 46 | - if hcookie then return hcookie end | |
| 54 | + if hcookie then | |
| 55 | + return hcookie | |
| 56 | + end | |
| 47 | 57 | |
| 48 | 58 | local contents = file:read() |
| 49 | 59 | if not contents or type(contents) ~= "string" then |
| @@ -66,10 +76,14 @@ end | ||
| 66 | 76 | ---@return lc.cache.Cookie|nil, string|nil |
| 67 | 77 | function Cookie.parse(str) |
| 68 | 78 | local csrf = str:match("csrftoken=([^;]+)") |
| 69 | - if not csrf or csrf == "" then return nil, "Bad csrf token format" end | |
| 79 | + if not csrf or csrf == "" then | |
| 80 | + return nil, "Bad csrf token format" | |
| 81 | + end | |
| 70 | 82 | |
| 71 | 83 | local ls = str:match("LEETCODE_SESSION=([^;]+)") |
| 72 | - if not ls or ls == "" then return nil, "Bad leetcode session token format" end | |
| 84 | + if not ls or ls == "" then | |
| 85 | + return nil, "Bad leetcode session token format" | |
| 86 | + end | |
| 73 | 87 | |
| 74 | 88 | return { csrftoken = csrf, leetcode_session = ls, str = str } |
| 75 | 89 | end |
+3-1lua/leetcode/cache/init.lua
| @@ -3,6 +3,8 @@ local Problemlist = require("leetcode.cache.problemlist") | ||
| 3 | 3 | ---@class lc.Cache |
| 4 | 4 | local cache = {} |
| 5 | 5 | |
| 6 | -function cache.update() Problemlist.update() end | |
| 6 | +function cache.update() | |
| 7 | + Problemlist.update() | |
| 8 | +end | |
| 7 | 9 | |
| 8 | 10 | return cache |
+33-11lua/leetcode/cache/problemlist.lua
| @@ -28,17 +28,25 @@ local hist = nil | ||
| 28 | 28 | local Problemlist = {} |
| 29 | 29 | |
| 30 | 30 | ---@return lc.cache.Question[] |
| 31 | -function Problemlist.get() return Problemlist.read().data end | |
| 31 | +function Problemlist.get() | |
| 32 | + return Problemlist.read().data | |
| 33 | +end | |
| 32 | 34 | |
| 33 | 35 | ---@return lc.cache.payload |
| 34 | 36 | function Problemlist.read() |
| 35 | - if not file:exists() then return Problemlist.populate() end | |
| 37 | + if not file:exists() then | |
| 38 | + return Problemlist.populate() | |
| 39 | + end | |
| 36 | 40 | |
| 37 | 41 | local time = os.time() |
| 38 | - if hist and (time - hist.at) <= math.min(60, interval) then return hist.payload end | |
| 42 | + if hist and (time - hist.at) <= math.min(60, interval) then | |
| 43 | + return hist.payload | |
| 44 | + end | |
| 39 | 45 | |
| 40 | 46 | local contents = file:read() |
| 41 | - if not contents or type(contents) ~= "string" then return Problemlist.populate() end | |
| 47 | + if not contents or type(contents) ~= "string" then | |
| 48 | + return Problemlist.populate() | |
| 49 | + end | |
| 42 | 50 | |
| 43 | 51 | local cached = Problemlist.parse(contents) |
| 44 | 52 | |
| @@ -47,7 +55,9 @@ function Problemlist.read() | ||
| 47 | 55 | end |
| 48 | 56 | |
| 49 | 57 | hist = { at = time, payload = cached } |
| 50 | - if (time - cached.updated_at) > interval then Problemlist.update() end | |
| 58 | + if (time - cached.updated_at) > interval then | |
| 59 | + Problemlist.update() | |
| 60 | + end | |
| 51 | 61 | |
| 52 | 62 | return cached |
| 53 | 63 | end |
| @@ -67,7 +77,9 @@ end | ||
| 67 | 77 | |
| 68 | 78 | function Problemlist.update() |
| 69 | 79 | problems_api.all(function(res, err) |
| 70 | - if not err then Problemlist.write({ data = res }) end | |
| 80 | + if not err then | |
| 81 | + Problemlist.write({ data = res }) | |
| 82 | + end | |
| 71 | 83 | end, true) |
| 72 | 84 | end |
| 73 | 85 | |
| @@ -75,7 +87,9 @@ end | ||
| 75 | 87 | function Problemlist.get_by_title_slug(title_slug) |
| 76 | 88 | local problems = Problemlist.get() |
| 77 | 89 | |
| 78 | - local problem = vim.tbl_filter(function(e) return e.title_slug == title_slug end, problems)[1] | |
| 90 | + local problem = vim.tbl_filter(function(e) | |
| 91 | + return e.title_slug == title_slug | |
| 92 | + end, problems)[1] | |
| 79 | 93 | |
| 80 | 94 | assert(problem, ("Problem `%s` not found. Try updating cache?"):format(title_slug)) |
| 81 | 95 | return problem |
| @@ -89,7 +103,9 @@ function Problemlist.write(payload) | ||
| 89 | 103 | username = config.auth.name, |
| 90 | 104 | }, payload) |
| 91 | 105 | |
| 92 | - if not payload.data then payload.data = Problemlist.get() end | |
| 106 | + if not payload.data then | |
| 107 | + payload.data = Problemlist.get() | |
| 108 | + end | |
| 93 | 109 | |
| 94 | 110 | file:write(vim.json.encode(payload), "w") |
| 95 | 111 | hist = { at = os.time(), payload = payload } |
| @@ -100,7 +116,9 @@ end | ||
| 100 | 116 | ---@param str string |
| 101 | 117 | --- |
| 102 | 118 | ---@return lc.cache.payload |
| 103 | -function Problemlist.parse(str) return vim.json.decode(str) end | |
| 119 | +function Problemlist.parse(str) | |
| 120 | + return vim.json.decode(str) | |
| 121 | +end | |
| 104 | 122 | |
| 105 | 123 | ---@param title_slug string |
| 106 | 124 | ---@param status "ac" | "notac" |
| @@ -108,7 +126,9 @@ Problemlist.change_status = vim.schedule_wrap(function(title_slug, status) | ||
| 108 | 126 | local cached = Problemlist.read() |
| 109 | 127 | |
| 110 | 128 | cached.data = vim.tbl_map(function(p) |
| 111 | - if p.title_slug == title_slug then p.status = status end | |
| 129 | + if p.title_slug == title_slug then | |
| 130 | + p.status = status | |
| 131 | + end | |
| 112 | 132 | return p |
| 113 | 133 | end, cached.data) |
| 114 | 134 | |
| @@ -116,7 +136,9 @@ Problemlist.change_status = vim.schedule_wrap(function(title_slug, status) | ||
| 116 | 136 | end) |
| 117 | 137 | |
| 118 | 138 | function Problemlist.delete() |
| 119 | - if not file:exists() then return false end | |
| 139 | + if not file:exists() then | |
| 140 | + return false | |
| 141 | + end | |
| 120 | 142 | return pcall(path.rm, file) |
| 121 | 143 | end |
| 122 | 144 |
+120-51lua/leetcode/command/init.lua
| @@ -72,8 +72,12 @@ function cmd.cookie_prompt(cb) | ||
| 72 | 72 | input:mount() |
| 73 | 73 | |
| 74 | 74 | local keys = config.user.keys |
| 75 | - input:map("n", keys.toggle, function() input:unmount() end) | |
| 76 | - input:on(event.BufLeave, function() input:unmount() end) | |
| 75 | + input:map("n", keys.toggle, function() | |
| 76 | + input:unmount() | |
| 77 | + end) | |
| 78 | + input:on(event.BufLeave, function() | |
| 79 | + input:unmount() | |
| 80 | + end) | |
| 77 | 81 | end |
| 78 | 82 | |
| 79 | 83 | function cmd.sign_out() |
| @@ -110,7 +114,9 @@ cmd.expire = vim.schedule_wrap(function() | ||
| 110 | 114 | |
| 111 | 115 | cmd.cookie_prompt(function(success) |
| 112 | 116 | if success then |
| 113 | - if api.nvim_tabpage_is_valid(tabp) then api.nvim_set_current_tabpage(tabp) end | |
| 117 | + if api.nvim_tabpage_is_valid(tabp) then | |
| 118 | + api.nvim_set_current_tabpage(tabp) | |
| 119 | + end | |
| 114 | 120 | log.info("Successful re-login") |
| 115 | 121 | else |
| 116 | 122 | cmd.delete_cookie() |
| @@ -130,7 +136,9 @@ function cmd.qot() | ||
| 130 | 136 | local Question = require("leetcode-ui.question") |
| 131 | 137 | |
| 132 | 138 | problems.question_of_today(function(qot, err) |
| 133 | - if err then return log.err(err) end | |
| 139 | + if err then | |
| 140 | + return log.err(err) | |
| 141 | + end | |
| 134 | 142 | local problemlist = require("leetcode.cache.problemlist") |
| 135 | 143 | Question(problemlist.get_by_title_slug(qot.title_slug), true):mount() |
| 136 | 144 | end) |
| @@ -142,7 +150,9 @@ function cmd.random_question(opts) | ||
| 142 | 150 | local problems = require("leetcode.cache.problemlist") |
| 143 | 151 | local question = require("leetcode.api.question") |
| 144 | 152 | |
| 145 | - if opts and opts.difficulty then opts.difficulty = opts.difficulty[1]:upper() end | |
| 153 | + if opts and opts.difficulty then | |
| 154 | + opts.difficulty = opts.difficulty[1]:upper() | |
| 155 | + end | |
| 146 | 156 | if opts and opts.status then |
| 147 | 157 | opts.status = ({ |
| 148 | 158 | ac = "AC", |
| @@ -152,7 +162,9 @@ function cmd.random_question(opts) | ||
| 152 | 162 | end |
| 153 | 163 | |
| 154 | 164 | local q, err = question.random(opts) |
| 155 | - if err then return log.err(err) end | |
| 165 | + if err then | |
| 166 | + return log.err(err) | |
| 167 | + end | |
| 156 | 168 | |
| 157 | 169 | local item = problems.get_by_title_slug(q.title_slug) or {} |
| 158 | 170 | local Question = require("leetcode-ui.question") |
| @@ -161,7 +173,9 @@ end | ||
| 161 | 173 | |
| 162 | 174 | function cmd.start_with_cmd() |
| 163 | 175 | local leetcode = require("leetcode") |
| 164 | - if leetcode.start(false) then cmd.menu() end | |
| 176 | + if leetcode.start(false) then | |
| 177 | + cmd.menu() | |
| 178 | + end | |
| 165 | 179 | end |
| 166 | 180 | |
| 167 | 181 | function cmd.menu() |
| @@ -179,7 +193,9 @@ end | ||
| 179 | 193 | function cmd.yank() |
| 180 | 194 | local utils = require("leetcode.utils") |
| 181 | 195 | local q = utils.curr_question() |
| 182 | - if not q then return end | |
| 196 | + if not q then | |
| 197 | + return | |
| 198 | + end | |
| 183 | 199 | |
| 184 | 200 | if api.nvim_buf_is_valid(q.bufnr) and api.nvim_win_is_valid(q.winid) then |
| 185 | 201 | api.nvim_set_current_win(q.winid) |
| @@ -191,43 +207,57 @@ function cmd.yank() | ||
| 191 | 207 | end |
| 192 | 208 | |
| 193 | 209 | ---@param page lc-menu.page |
| 194 | -function cmd.set_menu_page(page) _Lc_state.menu:set_page(page) end | |
| 210 | +function cmd.set_menu_page(page) | |
| 211 | + _Lc_state.menu:set_page(page) | |
| 212 | +end | |
| 195 | 213 | |
| 196 | 214 | function cmd.start_user_session() -- |
| 197 | 215 | cmd.set_menu_page("menu") |
| 198 | 216 | config.stats.update() |
| 199 | 217 | end |
| 200 | 218 | |
| 201 | -function cmd.question_tabs() require("leetcode.pickers.question-tabs").pick() end | |
| 219 | +function cmd.question_tabs() | |
| 220 | + require("leetcode.pickers.question-tabs").pick() | |
| 221 | +end | |
| 202 | 222 | |
| 203 | 223 | function cmd.change_lang() |
| 204 | 224 | local utils = require("leetcode.utils") |
| 205 | 225 | local q = utils.curr_question() |
| 206 | - if q then require("leetcode.pickers.language").pick(q) end | |
| 226 | + if q then | |
| 227 | + require("leetcode.pickers.language").pick(q) | |
| 228 | + end | |
| 207 | 229 | end |
| 208 | 230 | |
| 209 | 231 | function cmd.desc_toggle() |
| 210 | 232 | local utils = require("leetcode.utils") |
| 211 | 233 | local q = utils.curr_question() |
| 212 | - if q then q.description:toggle() end | |
| 234 | + if q then | |
| 235 | + q.description:toggle() | |
| 236 | + end | |
| 213 | 237 | end |
| 214 | 238 | |
| 215 | 239 | function cmd.desc_toggle_stats() |
| 216 | 240 | local utils = require("leetcode.utils") |
| 217 | 241 | local q = utils.curr_question() |
| 218 | - if q then q.description:toggle_stats() end | |
| 242 | + if q then | |
| 243 | + q.description:toggle_stats() | |
| 244 | + end | |
| 219 | 245 | end |
| 220 | 246 | |
| 221 | 247 | function cmd.console() |
| 222 | 248 | local utils = require("leetcode.utils") |
| 223 | 249 | local q = utils.curr_question() |
| 224 | - if q then q.console:toggle() end | |
| 250 | + if q then | |
| 251 | + q.console:toggle() | |
| 252 | + end | |
| 225 | 253 | end |
| 226 | 254 | |
| 227 | 255 | function cmd.info() |
| 228 | 256 | local utils = require("leetcode.utils") |
| 229 | 257 | local q = utils.curr_question() |
| 230 | - if q then q.info:toggle() end | |
| 258 | + if q then | |
| 259 | + q.info:toggle() | |
| 260 | + end | |
| 231 | 261 | end |
| 232 | 262 | |
| 233 | 263 | function cmd.hints() |
| @@ -239,18 +269,24 @@ function cmd.q_run() | ||
| 239 | 269 | local utils = require("leetcode.utils") |
| 240 | 270 | utils.auth_guard() |
| 241 | 271 | local q = utils.curr_question() |
| 242 | - if q then q.console:run() end | |
| 272 | + if q then | |
| 273 | + q.console:run() | |
| 274 | + end | |
| 243 | 275 | end |
| 244 | 276 | |
| 245 | 277 | function cmd.q_submit() |
| 246 | 278 | local utils = require("leetcode.utils") |
| 247 | 279 | utils.auth_guard() |
| 248 | 280 | local q = utils.curr_question() |
| 249 | - if q then q.console:run(true) end | |
| 281 | + if q then | |
| 282 | + q.console:run(true) | |
| 283 | + end | |
| 250 | 284 | end |
| 251 | 285 | |
| 252 | 286 | function cmd.ui_skills() |
| 253 | - if config.is_cn then return end | |
| 287 | + if config.is_cn then | |
| 288 | + return | |
| 289 | + end | |
| 254 | 290 | local skills = require("leetcode-ui.popup.skills") |
| 255 | 291 | skills:show() |
| 256 | 292 | end |
| @@ -286,7 +322,9 @@ function cmd.reset() | ||
| 286 | 322 | local utils = require("leetcode.utils") |
| 287 | 323 | utils.auth_guard() |
| 288 | 324 | local q = utils.curr_question() |
| 289 | - if not q then return end | |
| 325 | + if not q then | |
| 326 | + return | |
| 327 | + end | |
| 290 | 328 | |
| 291 | 329 | q:set_lines() |
| 292 | 330 | end |
| @@ -295,7 +333,9 @@ function cmd.last_submit() | ||
| 295 | 333 | local utils = require("leetcode.utils") |
| 296 | 334 | utils.auth_guard() |
| 297 | 335 | local q = utils.curr_question() |
| 298 | - if not q then return end | |
| 336 | + if not q then | |
| 337 | + return | |
| 338 | + end | |
| 299 | 339 | |
| 300 | 340 | local question_api = require("leetcode.api.question") |
| 301 | 341 | question_api.latest_submission(q.q.id, q.lang, function(res, err) -- |
| @@ -321,7 +361,9 @@ function cmd.restore() | ||
| 321 | 361 | local utils = require("leetcode.utils") |
| 322 | 362 | utils.auth_guard() |
| 323 | 363 | local q = utils.curr_question() |
| 324 | - if not q then return end | |
| 364 | + if not q then | |
| 365 | + return | |
| 366 | + end | |
| 325 | 367 | |
| 326 | 368 | if |
| 327 | 369 | (q.winid and api.nvim_win_is_valid(q.winid)) |
| @@ -345,7 +387,9 @@ function cmd.inject() | ||
| 345 | 387 | local utils = require("leetcode.utils") |
| 346 | 388 | utils.auth_guard() |
| 347 | 389 | local q = utils.curr_question() |
| 348 | - if not q then return end | |
| 390 | + if not q then | |
| 391 | + return | |
| 392 | + end | |
| 349 | 393 | |
| 350 | 394 | if api.nvim_buf_is_valid(q.bufnr) then |
| 351 | 395 | local start_i, end_i = q:range(true) |
| @@ -377,26 +421,36 @@ end | ||
| 377 | 421 | |
| 378 | 422 | function cmd.get_active_session() |
| 379 | 423 | local sessions = config.sessions.all |
| 380 | - return vim.tbl_filter(function(s) return s.is_active end, sessions)[1] | |
| 424 | + return vim.tbl_filter(function(s) | |
| 425 | + return s.is_active | |
| 426 | + end, sessions)[1] | |
| 381 | 427 | end |
| 382 | 428 | |
| 383 | 429 | function cmd.get_session_by_name(name) |
| 384 | 430 | local sessions = config.sessions.all |
| 385 | 431 | |
| 386 | 432 | name = name:lower() |
| 387 | - if name == config.sessions.default then name = "" end | |
| 388 | - return vim.tbl_filter(function(s) return s.name:lower() == name end, sessions)[1] | |
| 433 | + if name == config.sessions.default then | |
| 434 | + name = "" | |
| 435 | + end | |
| 436 | + return vim.tbl_filter(function(s) | |
| 437 | + return s.name:lower() == name | |
| 438 | + end, sessions)[1] | |
| 389 | 439 | end |
| 390 | 440 | |
| 391 | 441 | function cmd.change_session(opts) |
| 392 | 442 | local name = opts.name[1] or config.sessions.default |
| 393 | 443 | |
| 394 | 444 | local session = cmd.get_session_by_name(name) |
| 395 | - if not session then return log.error("Session not found") end | |
| 445 | + if not session then | |
| 446 | + return log.error("Session not found") | |
| 447 | + end | |
| 396 | 448 | |
| 397 | 449 | local stats_api = require("leetcode.api.statistics") |
| 398 | 450 | stats_api.change_session(session.id, function(_, err) |
| 399 | - if err then return log.err(err) end | |
| 451 | + if err then | |
| 452 | + return log.err(err) | |
| 453 | + end | |
| 400 | 454 | log.info(("Session changed to `%s`"):format(name)) |
| 401 | 455 | config.stats.update() |
| 402 | 456 | end) |
| @@ -404,16 +458,22 @@ end | ||
| 404 | 458 | |
| 405 | 459 | function cmd.create_session(opts) |
| 406 | 460 | local name = opts.name[1] |
| 407 | - if not name then return log.error("Session name not provided") end | |
| 461 | + if not name then | |
| 462 | + return log.error("Session name not provided") | |
| 463 | + end | |
| 408 | 464 | |
| 409 | 465 | local stats_api = require("leetcode.api.statistics") |
| 410 | 466 | stats_api.create_session(name, function(_, err) |
| 411 | - if err then return log.err(err) end | |
| 467 | + if err then | |
| 468 | + return log.err(err) | |
| 469 | + end | |
| 412 | 470 | log.info(("session `%s` created"):format(name)) |
| 413 | 471 | end) |
| 414 | 472 | end |
| 415 | 473 | |
| 416 | -function cmd.update_sessions() config.stats.update_sessions() end | |
| 474 | +function cmd.update_sessions() | |
| 475 | + config.stats.update_sessions() | |
| 476 | +end | |
| 417 | 477 | |
| 418 | 478 | function cmd.fix() |
| 419 | 479 | require("leetcode.cache.cookie").delete() |
| @@ -424,12 +484,16 @@ end | ||
| 424 | 484 | ---@return string[], string[] |
| 425 | 485 | function cmd.parse(args) |
| 426 | 486 | local parts = vim.split(vim.trim(args), "%s+") |
| 427 | - if args:sub(-1) == " " then parts[#parts + 1] = "" end | |
| 487 | + if args:sub(-1) == " " then | |
| 488 | + parts[#parts + 1] = "" | |
| 489 | + end | |
| 428 | 490 | |
| 429 | 491 | local options = {} |
| 430 | 492 | for _, part in ipairs(parts) do |
| 431 | 493 | local opt = part:match("(.-)=.-") |
| 432 | - if opt then table.insert(options, opt) end | |
| 494 | + if opt then | |
| 495 | + table.insert(options, opt) | |
| 496 | + end | |
| 433 | 497 | end |
| 434 | 498 | |
| 435 | 499 | return parts, options |
| @@ -438,9 +502,15 @@ end | ||
| 438 | 502 | ---@param tbl table |
| 439 | 503 | local function cmds_keys(tbl) |
| 440 | 504 | return vim.tbl_filter(function(key) |
| 441 | - if type(key) ~= "string" then return false end | |
| 442 | - if key:sub(1, 1) == "_" then return false end | |
| 443 | - if tbl[key]._private then return false end | |
| 505 | + if type(key) ~= "string" then | |
| 506 | + return false | |
| 507 | + end | |
| 508 | + if key:sub(1, 1) == "_" then | |
| 509 | + return false | |
| 510 | + end | |
| 511 | + if tbl[key]._private then | |
| 512 | + return false | |
| 513 | + end | |
| 444 | 514 | |
| 445 | 515 | return true |
| 446 | 516 | end, vim.tbl_keys(tbl)) |
| @@ -461,7 +531,9 @@ end | ||
| 461 | 531 | --- |
| 462 | 532 | ---@return string[] |
| 463 | 533 | function cmd.rec_complete(args, options, cmds) |
| 464 | - if not cmds or vim.tbl_isempty(args) then return {} end | |
| 534 | + if not cmds or vim.tbl_isempty(args) then | |
| 535 | + return {} | |
| 536 | + end | |
| 465 | 537 | |
| 466 | 538 | if not cmds._args and cmds[args[1]] then |
| 467 | 539 | return cmd.rec_complete(args, options, cmds[table.remove(args, 1)]) |
| @@ -470,29 +542,26 @@ function cmd.rec_complete(args, options, cmds) | ||
| 470 | 542 | local txt, keys = args[#args], cmds_keys(cmds) |
| 471 | 543 | if cmds._args then |
| 472 | 544 | local option_keys = cmds_keys(cmds._args) |
| 473 | - option_keys = vim.tbl_filter( | |
| 474 | - function(key) return not vim.tbl_contains(options, key) end, | |
| 475 | - option_keys | |
| 476 | - ) | |
| 477 | - option_keys = vim.tbl_map(function(key) return ("%s="):format(key) end, option_keys) | |
| 545 | + option_keys = vim.tbl_filter(function(key) | |
| 546 | + return not vim.tbl_contains(options, key) | |
| 547 | + end, option_keys) | |
| 548 | + option_keys = vim.tbl_map(function(key) | |
| 549 | + return ("%s="):format(key) | |
| 550 | + end, option_keys) | |
| 478 | 551 | keys = vim.tbl_extend("force", keys, option_keys) |
| 479 | 552 | |
| 480 | 553 | local s = vim.split(txt, "=") |
| 481 | 554 | if s[2] and cmds._args[s[1]] then |
| 482 | 555 | local vals = vim.split(s[2], ",") |
| 483 | - return vim.tbl_filter( | |
| 484 | - function(key) | |
| 485 | - return not vim.tbl_contains(vals, key) and key:find(vals[#vals], 1, true) == 1 | |
| 486 | - end, | |
| 487 | - cmds._args[s[1]] | |
| 488 | - ) | |
| 556 | + return vim.tbl_filter(function(key) | |
| 557 | + return not vim.tbl_contains(vals, key) and key:find(vals[#vals], 1, true) == 1 | |
| 558 | + end, cmds._args[s[1]]) | |
| 489 | 559 | end |
| 490 | 560 | end |
| 491 | 561 | |
| 492 | - return vim.tbl_filter( | |
| 493 | - function(key) return not vim.tbl_contains(args, key) and key:find(txt, 1, true) == 1 end, | |
| 494 | - keys | |
| 495 | - ) | |
| 562 | + return vim.tbl_filter(function(key) | |
| 563 | + return not vim.tbl_contains(args, key) and key:find(txt, 1, true) == 1 | |
| 564 | + end, keys) | |
| 496 | 565 | end |
| 497 | 566 | |
| 498 | 567 | function cmd.exec(args) |
+6-2lua/leetcode/config/hooks.lua
| @@ -4,8 +4,12 @@ local hooks = {} | ||
| 4 | 4 | hooks["question_enter"] = { |
| 5 | 5 | vim.schedule_wrap(function(q) |
| 6 | 6 | -- https://github.com/kawre/leetcode.nvim/issues/14 |
| 7 | - if q.lang ~= "rust" then return end | |
| 8 | - pcall(function() require("rust-tools.standalone").start_standalone_client() end) | |
| 7 | + if q.lang ~= "rust" then | |
| 8 | + return | |
| 9 | + end | |
| 10 | + pcall(function() | |
| 11 | + require("rust-tools.standalone").start_standalone_client() | |
| 12 | + end) | |
| 9 | 13 | end), |
| 10 | 14 | } |
| 11 | 15 |
+13-5lua/leetcode/config/init.lua
| @@ -73,14 +73,18 @@ function config.validate() | ||
| 73 | 73 | |
| 74 | 74 | assert(vim.fn.has("nvim-0.9.0") == 1, "Neovim >= 0.9.0 required") |
| 75 | 75 | |
| 76 | - if not utils.get_lang(config.lang) then -- | |
| 76 | + if not utils.get_lang(config.lang) then | |
| 77 | 77 | ---@type lc.lang[] |
| 78 | - local lang_slugs = vim.tbl_map(function(lang) return lang.slug end, config.langs) | |
| 78 | + local lang_slugs = vim.tbl_map(function(lang) | |
| 79 | + return lang.slug | |
| 80 | + end, config.langs) | |
| 79 | 81 | |
| 80 | 82 | local matches = {} |
| 81 | 83 | for _, slug in ipairs(lang_slugs) do |
| 82 | 84 | local percent = slug:match(config.lang) or config.lang:match(slug) |
| 83 | - if percent then table.insert(matches, slug) end | |
| 85 | + if percent then | |
| 86 | + table.insert(matches, slug) | |
| 87 | + end | |
| 84 | 88 | end |
| 85 | 89 | |
| 86 | 90 | if not vim.tbl_isempty(matches) then |
| @@ -95,10 +99,14 @@ end | ||
| 95 | 99 | function config.load_plugins() |
| 96 | 100 | local plugins = {} |
| 97 | 101 | |
| 98 | - if config.user.cn.enabled then table.insert(plugins, "cn") end | |
| 102 | + if config.user.cn.enabled then | |
| 103 | + table.insert(plugins, "cn") | |
| 104 | + end | |
| 99 | 105 | |
| 100 | 106 | for plugin, enabled in pairs(config.user.plugins) do |
| 101 | - if enabled then table.insert(plugins, plugin) end | |
| 107 | + if enabled then | |
| 108 | + table.insert(plugins, plugin) | |
| 109 | + end | |
| 102 | 110 | end |
| 103 | 111 | |
| 104 | 112 | for _, plugin in ipairs(plugins) do |
+9-3lua/leetcode/config/stats.lua
| @@ -9,7 +9,9 @@ function Stats.update_streak() | ||
| 9 | 9 | local log = require("leetcode.logger") |
| 10 | 10 | |
| 11 | 11 | stats_api.streak(function(res, err) |
| 12 | - if err then return log.err(err) end | |
| 12 | + if err then | |
| 13 | + return log.err(err) | |
| 14 | + end | |
| 13 | 15 | |
| 14 | 16 | Stats.daily.streak = res.streakCount |
| 15 | 17 | Stats.daily.today_completed = res.todayCompleted |
| @@ -25,13 +27,17 @@ function Stats.update_sessions() | ||
| 25 | 27 | Stats.progress = {} |
| 26 | 28 | |
| 27 | 29 | stats_api.sessions(function(_, err) |
| 28 | - if err then return log.err(err) end | |
| 30 | + if err then | |
| 31 | + return log.err(err) | |
| 32 | + end | |
| 29 | 33 | |
| 30 | 34 | _Lc_state.menu:draw() |
| 31 | 35 | end) |
| 32 | 36 | |
| 33 | 37 | stats_api.session_progress(function(res, err) |
| 34 | - if err then return log.err(err) end | |
| 38 | + if err then | |
| 39 | + return log.err(err) | |
| 40 | + end | |
| 35 | 41 | |
| 36 | 42 | Stats.progress = {} |
| 37 | 43 |
+24-8lua/leetcode/logger/init.lua
| @@ -1,5 +1,7 @@ | ||
| 1 | 1 | local n_ok, notify = pcall(require, "notify") |
| 2 | -if n_ok then vim.notify = notify end | |
| 2 | +if n_ok then | |
| 3 | + vim.notify = notify | |
| 4 | +end | |
| 3 | 5 | |
| 4 | 6 | local config = require("leetcode.config") |
| 5 | 7 | local t = require("leetcode.translator") |
| @@ -8,7 +10,9 @@ local lvls = vim.log.levels | ||
| 8 | 10 | ---@class lc.Logger |
| 9 | 11 | local logger = {} |
| 10 | 12 | |
| 11 | -local function normalize(msg) return type(msg) == "string" and t(msg) or vim.inspect(msg) end | |
| 13 | +local function normalize(msg) | |
| 14 | + return type(msg) == "string" and t(msg) or vim.inspect(msg) | |
| 15 | +end | |
| 12 | 16 | |
| 13 | 17 | -- ---@private |
| 14 | 18 | -- ---@param msg any |
| @@ -18,22 +22,30 @@ local function normalize(msg) return type(msg) == "string" and t(msg) or vim.ins | ||
| 18 | 22 | -- end) |
| 19 | 23 | |
| 20 | 24 | function logger.log(msg, lvl) |
| 21 | - if not config.user.logging then return end | |
| 25 | + if not config.user.logging then | |
| 26 | + return | |
| 27 | + end | |
| 22 | 28 | |
| 23 | 29 | local title = config.name |
| 24 | 30 | lvl = lvl or lvls.INFO |
| 25 | 31 | msg = normalize(msg) |
| 26 | 32 | |
| 27 | - if lvl == lvls.DEBUG then msg = debug.traceback(msg .. "\n") end | |
| 33 | + if lvl == lvls.DEBUG then | |
| 34 | + msg = debug.traceback(msg .. "\n") | |
| 35 | + end | |
| 28 | 36 | |
| 29 | 37 | vim.notify(msg, lvl, { title = title }) |
| 30 | 38 | end |
| 31 | 39 | |
| 32 | 40 | ---@param msg any |
| 33 | -logger.info = function(msg) logger.log(msg) end | |
| 41 | +logger.info = function(msg) | |
| 42 | + logger.log(msg) | |
| 43 | +end | |
| 34 | 44 | |
| 35 | 45 | ---@param msg any |
| 36 | -logger.warn = function(msg) logger.log(msg, lvls.WARN) end | |
| 46 | +logger.warn = function(msg) | |
| 47 | + logger.log(msg, lvls.WARN) | |
| 48 | +end | |
| 37 | 49 | |
| 38 | 50 | ---@param msg any |
| 39 | 51 | logger.error = function(msg) |
| @@ -43,7 +55,9 @@ end | ||
| 43 | 55 | |
| 44 | 56 | ---@param err lc.err |
| 45 | 57 | logger.err = function(err) |
| 46 | - if not err then return logger.error("error") end | |
| 58 | + if not err then | |
| 59 | + return logger.error("error") | |
| 60 | + end | |
| 47 | 61 | |
| 48 | 62 | local msg = err.msg or "" |
| 49 | 63 | local lvl = err.lvl or lvls.ERROR |
| @@ -55,7 +69,9 @@ end | ||
| 55 | 69 | ---@param show? boolean |
| 56 | 70 | ---@return any |
| 57 | 71 | logger.debug = function(msg, show) |
| 58 | - if not config.debug then return msg end | |
| 72 | + if not config.debug then | |
| 73 | + return msg | |
| 74 | + end | |
| 59 | 75 | |
| 60 | 76 | local lvl = (show == nil or not show) and lvls.DEBUG or lvls.ERROR |
| 61 | 77 | logger.log(msg, lvl) |
+18-6lua/leetcode/logger/spinner/init.lua
| @@ -42,7 +42,9 @@ local spinners = { | ||
| 42 | 42 | ---@private |
| 43 | 43 | function spinner:spin() |
| 44 | 44 | local stype = self.spinner |
| 45 | - if not stype then return end | |
| 45 | + if not stype then | |
| 46 | + return | |
| 47 | + end | |
| 46 | 48 | |
| 47 | 49 | self:set(nil, nil, { |
| 48 | 50 | icon = stype.frames[self.index + 1], |
| @@ -51,7 +53,9 @@ function spinner:spin() | ||
| 51 | 53 | self.index = (self.index + 1) % #stype.frames |
| 52 | 54 | |
| 53 | 55 | local fps = math.floor(1000 / stype.fps) |
| 54 | - vim.defer_fn(function() self:spin() end, fps) | |
| 56 | + vim.defer_fn(function() | |
| 57 | + self:spin() | |
| 58 | + end, fps) | |
| 55 | 59 | end |
| 56 | 60 | |
| 57 | 61 | ---@private |
| @@ -60,9 +64,13 @@ end | ||
| 60 | 64 | ---@param lvl? integer |
| 61 | 65 | ---@param opts? table |
| 62 | 66 | function spinner:set(msg, lvl, opts) |
| 63 | - if not self.spinner then return end | |
| 67 | + if not self.spinner then | |
| 68 | + return | |
| 69 | + end | |
| 64 | 70 | |
| 65 | - if msg then self:update(msg) end | |
| 71 | + if msg then | |
| 72 | + self:update(msg) | |
| 73 | + end | |
| 66 | 74 | lvl = lvl or vim.log.levels.INFO |
| 67 | 75 | |
| 68 | 76 | opts = vim.tbl_deep_extend("force", { |
| @@ -76,10 +84,14 @@ function spinner:set(msg, lvl, opts) | ||
| 76 | 84 | end |
| 77 | 85 | |
| 78 | 86 | ---@param spinner_type lc.spinner_types |
| 79 | -function spinner:change(spinner_type) self.spinner = spinners[spinner_type] end | |
| 87 | +function spinner:change(spinner_type) | |
| 88 | + self.spinner = spinners[spinner_type] | |
| 89 | +end | |
| 80 | 90 | |
| 81 | 91 | ---@param msg any |
| 82 | -function spinner:update(msg) self.msg = t(tostring(msg)) end | |
| 92 | +function spinner:update(msg) | |
| 93 | + self.msg = t(tostring(msg)) | |
| 94 | +end | |
| 83 | 95 | |
| 84 | 96 | function spinner:start() |
| 85 | 97 | self:spin() |
+6-2lua/leetcode/parser/normalizer.lua
| @@ -64,7 +64,9 @@ function Normalizer:entities() | ||
| 64 | 64 | :gsub("\n", "&lcnl;") |
| 65 | 65 | :gsub("\t", "&lctab;") |
| 66 | 66 | :gsub("%s", " ") |
| 67 | - :gsub("<[^>]+>", function(match) return match:gsub(" ", " ") end) | |
| 67 | + :gsub("<[^>]+>", function(match) | |
| 68 | + return match:gsub(" ", " ") | |
| 69 | + end) | |
| 68 | 70 | end |
| 69 | 71 | |
| 70 | 72 | ---@param text string |
| @@ -84,6 +86,8 @@ end | ||
| 84 | 86 | local LeetNormalizer = Normalizer |
| 85 | 87 | |
| 86 | 88 | ---@param text string |
| 87 | -function Normalizer:norm(text) return LeetNormalizer(text).text end | |
| 89 | +function Normalizer:norm(text) | |
| 90 | + return LeetNormalizer(text).text | |
| 91 | +end | |
| 88 | 92 | |
| 89 | 93 | return LeetNormalizer |
+6-2lua/leetcode/parser/plain.lua
| @@ -7,7 +7,9 @@ local u = require("leetcode.parser.utils") | ||
| 7 | 7 | ---@field text string |
| 8 | 8 | local Plain = Group:extend("LeetParserPlain") |
| 9 | 9 | |
| 10 | -function Plain.normalize(text) return text:gsub("<[^>]+>", "") end | |
| 10 | +function Plain.normalize(text) | |
| 11 | + return text:gsub("<[^>]+>", "") | |
| 12 | +end | |
| 11 | 13 | |
| 12 | 14 | ---@param text string |
| 13 | 15 | function Plain:init(text) |
| @@ -24,6 +26,8 @@ end | ||
| 24 | 26 | local LeetParserPlain = Plain |
| 25 | 27 | |
| 26 | 28 | ---@param text string |
| 27 | -function Plain.static:parse(text) return LeetParserPlain(text) end | |
| 29 | +function Plain.static:parse(text) | |
| 30 | + return LeetParserPlain(text) | |
| 31 | +end | |
| 28 | 32 | |
| 29 | 33 | return LeetParserPlain |
+6-2lua/leetcode/parser/utils.lua
| @@ -99,10 +99,14 @@ function utils.hl(tag) -- | ||
| 99 | 99 | local tag_names = {} |
| 100 | 100 | |
| 101 | 101 | for _, v in ipairs(tag.tags) do |
| 102 | - if v.name then table.insert(tag_names, v.name) end | |
| 102 | + if v.name then | |
| 103 | + table.insert(tag_names, v.name) | |
| 104 | + end | |
| 103 | 105 | end |
| 104 | 106 | |
| 105 | - if tag.name then table.insert(tag_names, tag.name) end | |
| 107 | + if tag.name then | |
| 108 | + table.insert(tag_names, tag.name) | |
| 109 | + end | |
| 106 | 110 | |
| 107 | 111 | return theme.get_dynamic(tag_names) |
| 108 | 112 | end |
+12-4lua/leetcode/pickers/language.lua
| @@ -17,7 +17,9 @@ local M = {} | ||
| 17 | 17 | ---@param snippet lc.QuestionCodeSnippet |
| 18 | 18 | --- |
| 19 | 19 | ---@return string |
| 20 | -local function lang_formatter(snippet) return string.format("%s %s", snippet.t.lang, snippet.t.slug) end | |
| 20 | +local function lang_formatter(snippet) | |
| 21 | + return string.format("%s %s", snippet.t.lang, snippet.t.slug) | |
| 22 | +end | |
| 21 | 23 | |
| 22 | 24 | ---@param snippet lc.QuestionCodeSnippet |
| 23 | 25 | local function dislay_icon(snippet) |
| @@ -55,7 +57,9 @@ end | ||
| 55 | 57 | local function entry_maker(entry) |
| 56 | 58 | ---@type lc.language |
| 57 | 59 | entry.t = utils.get_lang(entry.lang_slug) |
| 58 | - if not entry.t then return end | |
| 60 | + if not entry.t then | |
| 61 | + return | |
| 62 | + end | |
| 59 | 63 | |
| 60 | 64 | return { |
| 61 | 65 | value = entry, |
| @@ -79,7 +83,9 @@ M.pick_lang = function(question, callback) | ||
| 79 | 83 | attach_mappings = function(prompt_bufnr, map) |
| 80 | 84 | actions.select_default:replace(function() |
| 81 | 85 | local selection = action_state.get_selected_entry() |
| 82 | - if not selection then return end | |
| 86 | + if not selection then | |
| 87 | + return | |
| 88 | + end | |
| 83 | 89 | |
| 84 | 90 | local snippet = selection.value |
| 85 | 91 | if question.lang == snippet.t.slug then |
| @@ -112,7 +118,9 @@ M.pick = function(question) | ||
| 112 | 118 | attach_mappings = function(prompt_bufnr, map) |
| 113 | 119 | actions.select_default:replace(function() |
| 114 | 120 | local selection = action_state.get_selected_entry() |
| 115 | - if not selection then return end | |
| 121 | + if not selection then | |
| 122 | + return | |
| 123 | + end | |
| 116 | 124 | |
| 117 | 125 | local snippet = selection.value |
| 118 | 126 | if question.lang == snippet.t.slug then |
+13-5lua/leetcode/pickers/question-tabs.lua
| @@ -22,14 +22,18 @@ end | ||
| 22 | 22 | |
| 23 | 23 | local function display_current(entry) |
| 24 | 24 | local tabp = vim.api.nvim_get_current_tabpage() |
| 25 | - if tabp ~= entry.tabpage then return unpack({ "", "" }) end | |
| 25 | + if tabp ~= entry.tabpage then | |
| 26 | + return unpack({ "", "" }) | |
| 27 | + end | |
| 26 | 28 | |
| 27 | 29 | return { icons.caret.right, "leetcode_ref" } |
| 28 | 30 | end |
| 29 | 31 | |
| 30 | 32 | local function display_difficulty(q) |
| 31 | 33 | local lang = utils.get_lang(q.lang) |
| 32 | - if not lang then return {} end | |
| 34 | + if not lang then | |
| 35 | + return {} | |
| 36 | + end | |
| 33 | 37 | return { lang.icon, "leetcode_lang_" .. lang.slug } |
| 34 | 38 | end |
| 35 | 39 | |
| @@ -78,7 +82,7 @@ return { | ||
| 78 | 82 | pick = function() |
| 79 | 83 | local tabs = utils.question_tabs() |
| 80 | 84 | |
| 81 | - if vim.tbl_isempty(tabs) then -- | |
| 85 | + if vim.tbl_isempty(tabs) then | |
| 82 | 86 | return log.warn("No questions opened") |
| 83 | 87 | end |
| 84 | 88 | |
| @@ -101,10 +105,14 @@ return { | ||
| 101 | 105 | actions.close(prompt_bufnr) |
| 102 | 106 | local selection = action_state.get_selected_entry() |
| 103 | 107 | |
| 104 | - if not selection then return end | |
| 108 | + if not selection then | |
| 109 | + return | |
| 110 | + end | |
| 105 | 111 | local ok, err = |
| 106 | 112 | pcall(vim.api.nvim_set_current_tabpage, selection.value.tabpage) |
| 107 | - if not ok then log.error(err) end | |
| 113 | + if not ok then | |
| 114 | + log.error(err) | |
| 115 | + end | |
| 108 | 116 | end) |
| 109 | 117 | return true |
| 110 | 118 | end, |
+16-6lua/leetcode/pickers/question.lua
| @@ -38,7 +38,9 @@ local function display_user_status(question) | ||
| 38 | 38 | return config.auth.is_premium and config.icons.hl.unlock or config.icons.hl.lock |
| 39 | 39 | end |
| 40 | 40 | |
| 41 | - if question.status == vim.NIL then return { "" } end | |
| 41 | + if question.status == vim.NIL then | |
| 42 | + return { "" } | |
| 43 | + end | |
| 42 | 44 | return config.icons.hl.status[question.status] or { "" } |
| 43 | 45 | end |
| 44 | 46 | |
| @@ -94,7 +96,9 @@ local theme = require("telescope.themes").get_dropdown({ | ||
| 94 | 96 | --- |
| 95 | 97 | ---@return lc.cache.Question[] |
| 96 | 98 | local function filter_questions(questions, opts) |
| 97 | - if vim.tbl_isempty(opts or {}) then return questions end | |
| 99 | + if vim.tbl_isempty(opts or {}) then | |
| 100 | + return questions | |
| 101 | + end | |
| 98 | 102 | |
| 99 | 103 | ---@param q lc.cache.Question |
| 100 | 104 | return vim.tbl_filter(function(q) |
| @@ -104,7 +108,7 @@ local function filter_questions(questions, opts) | ||
| 104 | 108 | end |
| 105 | 109 | |
| 106 | 110 | local status_flag = true |
| 107 | - if opts.status and not vim.tbl_contains(opts.status, q.status) then -- | |
| 111 | + if opts.status and not vim.tbl_contains(opts.status, q.status) then | |
| 108 | 112 | status_flag = false |
| 109 | 113 | end |
| 110 | 114 | |
| @@ -126,7 +130,9 @@ return { | ||
| 126 | 130 | attach_mappings = function(prompt_bufnr, map) |
| 127 | 131 | local function mount_question(reset) |
| 128 | 132 | local selection = action_state.get_selected_entry() |
| 129 | - if not selection then return end | |
| 133 | + if not selection then | |
| 134 | + return | |
| 135 | + end | |
| 130 | 136 | |
| 131 | 137 | local q = selection.value |
| 132 | 138 | if q.paid_only and not config.auth.is_premium then |
| @@ -137,8 +143,12 @@ return { | ||
| 137 | 143 | Question(q, reset):mount() |
| 138 | 144 | end |
| 139 | 145 | |
| 140 | - actions.select_default:replace(function() mount_question() end) | |
| 141 | - map({ "n", "i" }, "<C-Enter>", function() mount_question(true) end) | |
| 146 | + actions.select_default:replace(function() | |
| 147 | + mount_question() | |
| 148 | + end) | |
| 149 | + map({ "n", "i" }, "<C-Enter>", function() | |
| 150 | + mount_question(true) | |
| 151 | + end) | |
| 142 | 152 | |
| 143 | 153 | return true |
| 144 | 154 | end, |
+15-5lua/leetcode/runner/init.lua
| @@ -17,7 +17,9 @@ Runner.running = false | ||
| 17 | 17 | ---@param self lc.Runner |
| 18 | 18 | ---@param submit boolean |
| 19 | 19 | Runner.run = vim.schedule_wrap(function(self, submit) |
| 20 | - if Runner.running then return log.warn("Runner is busy") end | |
| 20 | + if Runner.running then | |
| 21 | + return log.warn("Runner is busy") | |
| 22 | + end | |
| 21 | 23 | |
| 22 | 24 | local ok, err = pcall(Runner.handle, self, submit) |
| 23 | 25 | if not ok then |
| @@ -26,7 +28,9 @@ Runner.run = vim.schedule_wrap(function(self, submit) | ||
| 26 | 28 | end |
| 27 | 29 | end) |
| 28 | 30 | |
| 29 | -Runner.stop = function() Runner.running = false end | |
| 31 | +Runner.stop = function() | |
| 32 | + Runner.running = false | |
| 33 | +end | |
| 30 | 34 | |
| 31 | 35 | function Runner:handle(submit) |
| 32 | 36 | Runner.running = true |
| @@ -41,7 +45,9 @@ function Runner:handle(submit) | ||
| 41 | 45 | |
| 42 | 46 | local judge = Judge:init() |
| 43 | 47 | local function callback(item, state, err) |
| 44 | - if err or item then self:stop() end | |
| 48 | + if err or item then | |
| 49 | + self:stop() | |
| 50 | + end | |
| 45 | 51 | |
| 46 | 52 | if item then |
| 47 | 53 | judge:stop(item.status_msg, item._.success) |
| @@ -51,7 +57,9 @@ function Runner:handle(submit) | ||
| 51 | 57 | judge:stop(err.msg or "Something went wrong", false) |
| 52 | 58 | end |
| 53 | 59 | |
| 54 | - if item then question.console.result:handle(item) end | |
| 60 | + if item then | |
| 61 | + question.console.result:handle(item) | |
| 62 | + end | |
| 55 | 63 | end |
| 56 | 64 | |
| 57 | 65 | leetbody:write(vim.json.encode(body), "w") |
| @@ -59,6 +67,8 @@ function Runner:handle(submit) | ||
| 59 | 67 | end |
| 60 | 68 | |
| 61 | 69 | ---@param question lc.ui.Question |
| 62 | -function Runner:init(question) return setmetatable({ question = question }, self) end | |
| 70 | +function Runner:init(question) | |
| 71 | + return setmetatable({ question = question }, self) | |
| 72 | +end | |
| 63 | 73 | |
| 64 | 74 | return Runner |
+3-1lua/leetcode/runner/utils.lua
| @@ -1,6 +1,8 @@ | ||
| 1 | 1 | local utils = {} |
| 2 | 2 | |
| 3 | 3 | ---@param lines string[] |
| 4 | -function utils.to_typed_code(lines) return table.concat(lines, "\n") end | |
| 4 | +function utils.to_typed_code(lines) | |
| 5 | + return table.concat(lines, "\n") | |
| 6 | +end | |
| 5 | 7 | |
| 6 | 8 | return utils |
+6-2lua/leetcode/theme/default.lua
| @@ -8,8 +8,12 @@ local function hl(name) | ||
| 8 | 8 | local highlight = vim.api.nvim_get_hl(0, { name = name, link = false }) |
| 9 | 9 | setmetatable(highlight, { |
| 10 | 10 | __index = function(self, key) |
| 11 | - if key == "bg" then return self.background end | |
| 12 | - if key == "fg" then return self.foreground end | |
| 11 | + if key == "bg" then | |
| 12 | + return self.background | |
| 13 | + end | |
| 14 | + if key == "fg" then | |
| 15 | + return self.foreground | |
| 16 | + end | |
| 13 | 17 | |
| 14 | 18 | return nil |
| 15 | 19 | end, |
+21-7lua/leetcode/theme/init.lua
| @@ -45,8 +45,12 @@ function theme.load_devicons() | ||
| 45 | 45 | vim.tbl_map(function(l) |
| 46 | 46 | local icon, color = devicons.get_icon_color(l.ft) |
| 47 | 47 | |
| 48 | - if icon then l.icon = icon end | |
| 49 | - if color then l.color = color end | |
| 48 | + if icon then | |
| 49 | + l.icon = icon | |
| 50 | + end | |
| 51 | + if color then | |
| 52 | + l.color = color | |
| 53 | + end | |
| 50 | 54 | |
| 51 | 55 | return l |
| 52 | 56 | end, config.langs) |
| @@ -60,7 +64,9 @@ function theme.load() | ||
| 60 | 64 | vim.api.nvim_set_hl(0, key, t) |
| 61 | 65 | end |
| 62 | 66 | |
| 63 | - if devicons_ok then theme.load_devicons() end | |
| 67 | + if devicons_ok then | |
| 68 | + theme.load_devicons() | |
| 69 | + end | |
| 64 | 70 | |
| 65 | 71 | ---@param lang lc.language |
| 66 | 72 | vim.tbl_map(function(lang) |
| @@ -82,10 +88,14 @@ end | ||
| 82 | 88 | |
| 83 | 89 | ---@param tags string[] |
| 84 | 90 | function theme.get_dynamic(tags) |
| 85 | - if vim.tbl_isempty(tags) then return "leetcode_normal" end | |
| 91 | + if vim.tbl_isempty(tags) then | |
| 92 | + return "leetcode_normal" | |
| 93 | + end | |
| 86 | 94 | |
| 87 | 95 | local name = "leetcode_dyn_" .. table.concat(tags, "_") |
| 88 | - if dynamic_hls[name] then return name end | |
| 96 | + if dynamic_hls[name] then | |
| 97 | + return name | |
| 98 | + end | |
| 89 | 99 | |
| 90 | 100 | return theme.create_dynamic(name, tags) |
| 91 | 101 | end |
| @@ -99,11 +109,15 @@ function theme.create_dynamic(name, tags, defaults) | ||
| 99 | 109 | local tbl = defaults["normal"] |
| 100 | 110 | for _, tag in ipairs(tags) do |
| 101 | 111 | local hl = highlights[tag] |
| 102 | - if hl then tbl = vim.tbl_extend("force", tbl, defaults[hl]) end | |
| 112 | + if hl then | |
| 113 | + tbl = vim.tbl_extend("force", tbl, defaults[hl]) | |
| 114 | + end | |
| 103 | 115 | end |
| 104 | 116 | |
| 105 | 117 | if tbl.italic or tbl.bold then |
| 106 | - if tbl.fg == defaults["normal"].fg then tbl.fg = defaults[""].fg end | |
| 118 | + if tbl.fg == defaults["normal"].fg then | |
| 119 | + tbl.fg = defaults[""].fg | |
| 120 | + end | |
| 107 | 121 | end |
| 108 | 122 | |
| 109 | 123 | if pcall(vim.api.nvim_set_hl, 0, name, tbl) then |
+6-2lua/leetcode/translator.lua
| @@ -94,9 +94,13 @@ local translate = { | ||
| 94 | 94 | |
| 95 | 95 | ---@param text string |
| 96 | 96 | local function t(text) |
| 97 | - local keys = vim.tbl_map(function(key) return key:lower() end, vim.tbl_keys(translate)) | |
| 97 | + local keys = vim.tbl_map(function(key) | |
| 98 | + return key:lower() | |
| 99 | + end, vim.tbl_keys(translate)) | |
| 98 | 100 | |
| 99 | - if not vim.tbl_contains(keys, text:lower()) then return text end | |
| 101 | + if not vim.tbl_contains(keys, text:lower()) then | |
| 102 | + return text | |
| 103 | + end | |
| 100 | 104 | return translate[text:lower()] |
| 101 | 105 | end |
| 102 | 106 |
+33-11lua/leetcode/utils.lua
| @@ -13,7 +13,9 @@ function utils.deprecate_usr_cmd(name, new) | ||
| 13 | 13 | end |
| 14 | 14 | |
| 15 | 15 | ---@param fn string |
| 16 | -function utils.cmd(fn) return ("<cmd>lua require('leetcode.command').%s()<cr>"):format(fn) end | |
| 16 | +function utils.cmd(fn) | |
| 17 | + return ("<cmd>lua require('leetcode.command').%s()<cr>"):format(fn) | |
| 18 | +end | |
| 17 | 19 | |
| 18 | 20 | ---@param title_slug string |
| 19 | 21 | ---@param lang lc.lang |
| @@ -33,7 +35,9 @@ function utils.question_tabs() | ||
| 33 | 35 | |
| 34 | 36 | for _, q in ipairs(_Lc_state.questions) do |
| 35 | 37 | local tabp = utils.question_tabp(q) |
| 36 | - if tabp then table.insert(questions, { tabpage = tabp, question = q }) end | |
| 38 | + if tabp then | |
| 39 | + table.insert(questions, { tabpage = tabp, question = q }) | |
| 40 | + end | |
| 37 | 41 | end |
| 38 | 42 | |
| 39 | 43 | return questions |
| @@ -43,7 +47,9 @@ end | ||
| 43 | 47 | ---@return integer|nil |
| 44 | 48 | function utils.question_tabp(q) |
| 45 | 49 | local ok, tabp = pcall(vim.api.nvim_win_get_tabpage, q.winid) |
| 46 | - if ok then return tabp end | |
| 50 | + if ok then | |
| 51 | + return tabp | |
| 52 | + end | |
| 47 | 53 | end |
| 48 | 54 | |
| 49 | 55 | ---@return lc.ui.Question |
| @@ -51,7 +57,9 @@ function utils.curr_question() | ||
| 51 | 57 | local tabp = vim.api.nvim_get_current_tabpage() |
| 52 | 58 | local tabs = utils.question_tabs() |
| 53 | 59 | |
| 54 | - local tab = vim.tbl_filter(function(tab) return tab.tabpage == tabp end, tabs)[1] or {} | |
| 60 | + local tab = vim.tbl_filter(function(tab) | |
| 61 | + return tab.tabpage == tabp | |
| 62 | + end, tabs)[1] or {} | |
| 55 | 63 | if tab.question then |
| 56 | 64 | return tab.question, tabp |
| 57 | 65 | else |
| @@ -62,22 +70,30 @@ end | ||
| 62 | 70 | ---@return lc.language |
| 63 | 71 | function utils.get_lang(slug) |
| 64 | 72 | ---@param l lc.language |
| 65 | - return vim.tbl_filter(function(l) return l.slug == slug end, config.langs)[1] | |
| 73 | + return vim.tbl_filter(function(l) | |
| 74 | + return l.slug == slug | |
| 75 | + end, config.langs)[1] | |
| 66 | 76 | end |
| 67 | 77 | |
| 68 | 78 | ---@return lc.language |
| 69 | 79 | function utils.get_lang_by_name(name) |
| 70 | 80 | ---@param l lc.language |
| 71 | - return vim.tbl_filter(function(l) return l.lang == name end, config.langs)[1] | |
| 81 | + return vim.tbl_filter(function(l) | |
| 82 | + return l.lang == name | |
| 83 | + end, config.langs)[1] | |
| 72 | 84 | end |
| 73 | 85 | |
| 74 | 86 | ---@param event lc.hook |
| 75 | 87 | ---@return fun()[]|nil |
| 76 | 88 | function utils.get_hooks(event) |
| 77 | 89 | local fns = config.user.hooks[event] |
| 78 | - if not fns then return end | |
| 90 | + if not fns then | |
| 91 | + return | |
| 92 | + end | |
| 79 | 93 | |
| 80 | - if type(fns) == "function" then fns = { fns } end | |
| 94 | + if type(fns) == "function" then | |
| 95 | + fns = { fns } | |
| 96 | + end | |
| 81 | 97 | |
| 82 | 98 | return vim.list_extend(fns, config.hooks[event] or {}) |
| 83 | 99 | end |
| @@ -85,11 +101,15 @@ end | ||
| 85 | 101 | ---@param event lc.hook |
| 86 | 102 | function utils.exec_hooks(event, ...) |
| 87 | 103 | local fns = utils.get_hooks(event) |
| 88 | - if not fns then return log.error("unknown hook event: " .. event) end | |
| 104 | + if not fns then | |
| 105 | + return log.error("unknown hook event: " .. event) | |
| 106 | + end | |
| 89 | 107 | |
| 90 | 108 | for i, fn in ipairs(fns) do |
| 91 | 109 | local ok, msg = pcall(fn, ...) |
| 92 | - if not ok then log.error(("bad hook #%d in `%s` event: %s"):format(i, event, msg)) end | |
| 110 | + if not ok then | |
| 111 | + log.error(("bad hook #%d in `%s` event: %s"):format(i, event, msg)) | |
| 112 | + end | |
| 93 | 113 | end |
| 94 | 114 | end |
| 95 | 115 | |
| @@ -107,7 +127,9 @@ function utils.translate(content, translated_content) | ||
| 107 | 127 | end |
| 108 | 128 | end |
| 109 | 129 | |
| 110 | -function utils.auth_guard() assert(config.auth.is_signed_in, "User not logged-in") end | |
| 130 | +function utils.auth_guard() | |
| 131 | + assert(config.auth.is_signed_in, "User not logged-in") | |
| 132 | +end | |
| 111 | 133 | |
| 112 | 134 | function utils.norm_ins(str) |
| 113 | 135 | local ins = vim.inspect(str) |