irongit

A Neovim plugin enabling you to solve LeetCode problems.

feat(parser): new line trimmer

kawreauthored
parent 0489d6dcommit 2f9b199e5493c556569ecca5ad08c924706110eaBrowse files

6 files changed, +29 -22

+7-9lua/leetcode/logger/init.lua
@@ -7,38 +7,36 @@ local lvls = vim.log.levels
77 ---@class lc.Logger
88 local logger = {}
99
10----@alias msg string | number | boolean | table | nil
11-
1210 local function normalize(msg) return type(msg) == "string" and msg or vim.inspect(msg) end
1311
14----@param msg msg
12+---@param msg any
1513 ---@param lvl? integer
1614 ---@return any
1715 logger.log = function(msg, lvl)
1816 if not config.user.logging then return msg end
19- vim.notify(normalize(msg), lvl or lvls.INFO, { title = config.name })
2017
18+ vim.notify(normalize(msg), lvl or lvls.INFO, { title = config.name })
2119 return msg
2220 end
2321
24----@param msg msg
22+---@param msg any
2523 ---@return any
2624 logger.info = function(msg) return logger.log(msg) end
2725
28----@param msg msg
26+---@param msg any
2927 ---@return any
3028 logger.warn = function(msg) return logger.log(msg, lvls.WARN) end
3129
32----@param msg msg
30+---@param msg any
3331 ---@return any
3432 logger.error = function(msg) return logger.log(msg, lvls.ERROR) end
3533
36----@param msg msg
34+---@param msg any
3735 ---@return any
3836 logger.debug = function(msg)
3937 if not config.debug then return msg end
4038
41- logger.log(debug.traceback(normalize(msg)), lvls.DEBUG)
39+ logger.log(debug.traceback(normalize(msg) .. "\n"), lvls.DEBUG)
4240 return msg
4341 end
4442
+1-1lua/leetcode/logger/spinner.lua
@@ -67,7 +67,7 @@ end
6767 ---@param spinner_type lc.spinner_types
6868 function spinner:change(spinner_type) self.spinner = spinners[spinner_type] end
6969
70----@param msg msg
70+---@param msg any
7171 function spinner:update(msg) self.msg = tostring(msg) end
7272
7373 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-
81 local Text = require("leetcode-ui.component.text")
92 local utils = require("leetcode.parser.utils")
3+local log = require("leetcode.logger")
104
115 local NuiText = require("nui.text")
126 local NuiLine = require("nui.line")
@@ -253,6 +247,7 @@ local function normalize_html(str)
253247 "<p><strong[^>]*>(Constraints:)</strong></p>(\n*)",
254248 "\n\n<constraints> %1</constraints>\n\n"
255249 )
250+ :gsub("(<code[^>]*>)(.-)(</code>)", "%1&lccode;%2&lccode;%3")
256251 :gsub("<pre>\n*(.-)\n*</pre>", "<pre>\n%1</pre>")
257252 :gsub("\n*<p>&nbsp;</p>\n*", "&lcpad;")
258253 :gsub("\n", "&lcnl;")
@@ -275,10 +270,20 @@ function Parser:plain_parser()
275270 end
276271 end
277272
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+
278285 ---@return lc-ui.Text
279286 function Parser:parse()
280- self.ts = vim.treesitter
281-
282287 local norm = normalize_html(self.str)
283288 local ok, parser = pcall(self.ts.get_string_parser, norm, self.lang)
284289 if ok then
@@ -289,14 +294,17 @@ function Parser:parse()
289294 self:plain_parser()
290295 end
291296
292- return self.text
297+ return self:trim()
293298 end
294299
295300 ---@param str string
296301 ---@param lang string
297302 function Parser:init(str, lang)
303+ log.debug(str)
304+
298305 local obj = setmetatable({
299306 str = str,
307+ ts = vim.treesitter,
300308 lang = lang,
301309 text = Text:init(),
302310 line = NuiLine(),
+1-0lua/leetcode/parser/utils.lua
@@ -33,6 +33,7 @@ local entities = {
3333 ["&lcnl;"] = "",
3434 ["&lctab;"] = "\t",
3535 ["&lcend;"] = "",
36+ ["&lccode;"] = "`",
3637 }
3738
3839 local highlights = {
+1-1lua/leetcode/theme/default.lua
@@ -34,7 +34,7 @@ return function()
3434
3535 normal = { fg = hl("FloatTitle").fg },
3636 alt = { fg = hl("Comment").fg },
37- code = { fg = hl("Type").fg },
37+ code = { fg = hl("Type").fg, bg = hl("Normal").bg },
3838 example = { fg = hl("DiagnosticHint").fg },
3939 constraints = { fg = hl("DiagnosticInfo").fg },
4040 indent = { fg = hl("Comment").fg },
+1-1lua/leetcode/ui/description.lua
@@ -114,7 +114,7 @@ function description:populate()
114114 )
115115 if not vim.tbl_isempty(q.hints) then
116116 statsline:append(" | ")
117- statsline:append("Hints", "leetcode_alt")
117+ statsline:append("󰛨 Hints", "leetcode_alt")
118118 end
119119
120120 local titlecomp = Text:init({