A Neovim plugin enabling you to solve LeetCode problems.
feat(parser): new line trimmer
6 files changed, +29 -22
+7-9lua/leetcode/logger/init.lua
| @@ -7,38 +7,36 @@ local lvls = vim.log.levels | ||
| 7 | 7 | ---@class lc.Logger |
| 8 | 8 | local logger = {} |
| 9 | 9 | |
| 10 | ----@alias msg string | number | boolean | table | nil | |
| 11 | - | |
| 12 | 10 | local function normalize(msg) return type(msg) == "string" and msg or vim.inspect(msg) end |
| 13 | 11 | |
| 14 | ----@param msg msg | |
| 12 | +---@param msg any | |
| 15 | 13 | ---@param lvl? integer |
| 16 | 14 | ---@return any |
| 17 | 15 | logger.log = function(msg, lvl) |
| 18 | 16 | if not config.user.logging then return msg end |
| 19 | - vim.notify(normalize(msg), lvl or lvls.INFO, { title = config.name }) | |
| 20 | 17 | |
| 18 | + vim.notify(normalize(msg), lvl or lvls.INFO, { title = config.name }) | |
| 21 | 19 | return msg |
| 22 | 20 | end |
| 23 | 21 | |
| 24 | ----@param msg msg | |
| 22 | +---@param msg any | |
| 25 | 23 | ---@return any |
| 26 | 24 | logger.info = function(msg) return logger.log(msg) end |
| 27 | 25 | |
| 28 | ----@param msg msg | |
| 26 | +---@param msg any | |
| 29 | 27 | ---@return any |
| 30 | 28 | logger.warn = function(msg) return logger.log(msg, lvls.WARN) end |
| 31 | 29 | |
| 32 | ----@param msg msg | |
| 30 | +---@param msg any | |
| 33 | 31 | ---@return any |
| 34 | 32 | logger.error = function(msg) return logger.log(msg, lvls.ERROR) end |
| 35 | 33 | |
| 36 | ----@param msg msg | |
| 34 | +---@param msg any | |
| 37 | 35 | ---@return any |
| 38 | 36 | logger.debug = function(msg) |
| 39 | 37 | if not config.debug then return msg end |
| 40 | 38 | |
| 41 | - logger.log(debug.traceback(normalize(msg)), lvls.DEBUG) | |
| 39 | + logger.log(debug.traceback(normalize(msg) .. "\n"), lvls.DEBUG) | |
| 42 | 40 | return msg |
| 43 | 41 | end |
| 44 | 42 |
+1-1lua/leetcode/logger/spinner.lua
| @@ -67,7 +67,7 @@ end | ||
| 67 | 67 | ---@param spinner_type lc.spinner_types |
| 68 | 68 | function spinner:change(spinner_type) self.spinner = spinners[spinner_type] end |
| 69 | 69 | |
| 70 | ----@param msg msg | |
| 70 | +---@param msg any | |
| 71 | 71 | function spinner:update(msg) self.msg = tostring(msg) end |
| 72 | 72 | |
| 73 | 73 | function spinner:start() |
+18-10lua/leetcode/parser/init.lua
| @@ -1,12 +1,6 @@ | ||
| 1 | ----TODO: <blockquote> | |
| 2 | ----TODO: <a> parser | |
| 3 | ----TODO: <img> parser | |
| 4 | ----TODO: <u> parser | |
| 5 | ----TODO: question duplication | |
| 6 | ----TODO: Q2066 | |
| 7 | - | |
| 8 | 1 | local Text = require("leetcode-ui.component.text") |
| 9 | 2 | local utils = require("leetcode.parser.utils") |
| 3 | +local log = require("leetcode.logger") | |
| 10 | 4 | |
| 11 | 5 | local NuiText = require("nui.text") |
| 12 | 6 | local NuiLine = require("nui.line") |
| @@ -253,6 +247,7 @@ local function normalize_html(str) | ||
| 253 | 247 | "<p><strong[^>]*>(Constraints:)</strong></p>(\n*)", |
| 254 | 248 | "\n\n<constraints> %1</constraints>\n\n" |
| 255 | 249 | ) |
| 250 | + :gsub("(<code[^>]*>)(.-)(</code>)", "%1&lccode;%2&lccode;%3") | |
| 256 | 251 | :gsub("<pre>\n*(.-)\n*</pre>", "<pre>\n%1</pre>") |
| 257 | 252 | :gsub("\n*<p> </p>\n*", "&lcpad;") |
| 258 | 253 | :gsub("\n", "&lcnl;") |
| @@ -275,10 +270,20 @@ function Parser:plain_parser() | ||
| 275 | 270 | end |
| 276 | 271 | end |
| 277 | 272 | |
| 273 | +-- Trim excessive lines | |
| 274 | +function Parser:trim() | |
| 275 | + local lines = self.text.lines | |
| 276 | + for i = #lines, 1, -1 do | |
| 277 | + if lines[i]:content() ~= "" then break end | |
| 278 | + table.remove(lines, i) | |
| 279 | + end | |
| 280 | + self.text.lines = lines | |
| 281 | + | |
| 282 | + return self.text | |
| 283 | +end | |
| 284 | + | |
| 278 | 285 | ---@return lc-ui.Text |
| 279 | 286 | function Parser:parse() |
| 280 | - self.ts = vim.treesitter | |
| 281 | - | |
| 282 | 287 | local norm = normalize_html(self.str) |
| 283 | 288 | local ok, parser = pcall(self.ts.get_string_parser, norm, self.lang) |
| 284 | 289 | if ok then |
| @@ -289,14 +294,17 @@ function Parser:parse() | ||
| 289 | 294 | self:plain_parser() |
| 290 | 295 | end |
| 291 | 296 | |
| 292 | - return self.text | |
| 297 | + return self:trim() | |
| 293 | 298 | end |
| 294 | 299 | |
| 295 | 300 | ---@param str string |
| 296 | 301 | ---@param lang string |
| 297 | 302 | function Parser:init(str, lang) |
| 303 | + log.debug(str) | |
| 304 | + | |
| 298 | 305 | local obj = setmetatable({ |
| 299 | 306 | str = str, |
| 307 | + ts = vim.treesitter, | |
| 300 | 308 | lang = lang, |
| 301 | 309 | text = Text:init(), |
| 302 | 310 | line = NuiLine(), |
+1-0lua/leetcode/parser/utils.lua
| @@ -33,6 +33,7 @@ local entities = { | ||
| 33 | 33 | ["&lcnl;"] = "", |
| 34 | 34 | ["&lctab;"] = "\t", |
| 35 | 35 | ["&lcend;"] = "", |
| 36 | + ["&lccode;"] = "`", | |
| 36 | 37 | } |
| 37 | 38 | |
| 38 | 39 | local highlights = { |
+1-1lua/leetcode/theme/default.lua
| @@ -34,7 +34,7 @@ return function() | ||
| 34 | 34 | |
| 35 | 35 | normal = { fg = hl("FloatTitle").fg }, |
| 36 | 36 | alt = { fg = hl("Comment").fg }, |
| 37 | - code = { fg = hl("Type").fg }, | |
| 37 | + code = { fg = hl("Type").fg, bg = hl("Normal").bg }, | |
| 38 | 38 | example = { fg = hl("DiagnosticHint").fg }, |
| 39 | 39 | constraints = { fg = hl("DiagnosticInfo").fg }, |
| 40 | 40 | indent = { fg = hl("Comment").fg }, |
+1-1lua/leetcode/ui/description.lua
| @@ -114,7 +114,7 @@ function description:populate() | ||
| 114 | 114 | ) |
| 115 | 115 | if not vim.tbl_isempty(q.hints) then |
| 116 | 116 | statsline:append(" | ") |
| 117 | - statsline:append("Hints", "leetcode_alt") | |
| 117 | + statsline:append(" Hints", "leetcode_alt") | |
| 118 | 118 | end |
| 119 | 119 | |
| 120 | 120 | local titlecomp = Text:init({ |