A Neovim plugin enabling you to solve LeetCode problems.
fix: cleanup
3 files changed, +25 -35
+17-8lua/leetcode/parser/html.lua
| @@ -112,7 +112,8 @@ function Html:handle_list(tags) | ||
| 112 | 112 | local li_type = get_list_type() |
| 113 | 113 | local li_count = vim.fn.count(tags, "li") |
| 114 | 114 | local leftpad = string.rep("\t", li_count) |
| 115 | - local li_icons = { "", "", "" } | |
| 115 | + -- local li_icons = { "", "", "" } | |
| 116 | + local li_icons = { "*", "-", "+" } | |
| 116 | 117 | |
| 117 | 118 | local text |
| 118 | 119 | if li_type == "ul" then |
| @@ -152,7 +153,6 @@ function Html:handle_link(text, tag_data) | ||
| 152 | 153 | |
| 153 | 154 | link = href.value or "" |
| 154 | 155 | elseif tag == "img" then |
| 155 | - log.info(tag_data.attrs) | |
| 156 | 156 | local alt = vim.tbl_filter(function(attr) |
| 157 | 157 | if attr.name == "alt" then return attr end |
| 158 | 158 | end, tag_data.attrs)[1] or {} |
| @@ -248,28 +248,38 @@ end | ||
| 248 | 248 | function Html:normalize() |
| 249 | 249 | log.debug(self.str) |
| 250 | 250 | |
| 251 | - self.str = self.str | |
| 251 | + self.str = self | |
| 252 | + .str | |
| 252 | 253 | :gsub("", "") |
| 253 | 254 | :gsub("\r\n", "\n") |
| 254 | 255 | :gsub("\t*<(/?li)>", "<%1>") |
| 255 | 256 | :gsub("\t*<(/?ul)>", "<%1>") |
| 256 | 257 | :gsub("\t*<(/?ol)>", "<%1>") |
| 258 | + -- :gsub( | |
| 259 | + -- "\t*<(li[^>]*)>(.-)\t*</(li)>", | |
| 260 | + -- "<%1>%2</%3>\n" | |
| 261 | + -- ) | |
| 262 | + -- :gsub("\t*<(ul[^>]*)>(.-)\t*</(ul)>", "<%1>%2</%3>\n") | |
| 263 | + -- :gsub("\t*<(ol[^>]*)>(.-)\t*</(ol)>", "<%1>%2</%3>\n") | |
| 257 | 264 | :gsub( |
| 258 | - "<p><strong[^>]*>(Example%s*%d+:)</strong></p>(\n*)", | |
| 265 | + "<p><strong[^>]*>(Example%s*%d*:)%s*</strong></p>\n*", | |
| 259 | 266 | "\n\n<example> %1</example>\n\n" |
| 260 | 267 | ) |
| 261 | 268 | :gsub( |
| 262 | - "<p><strong[^>]*>(Constraints:)</strong></p>(\n*)", | |
| 269 | + "<p><strong[^>]*>(Constraints:)%s*</strong></p>\n*", | |
| 263 | 270 | "\n\n<constraints> %1</constraints>\n\n" |
| 264 | 271 | ) |
| 265 | - :gsub("<img([^>]*)/>\n*", "<img%1>img</img>") | |
| 272 | + :gsub("<(img[^>]*)/>\n*", "<%1>img</img>") | |
| 266 | 273 | :gsub("<pre>\n*(.-)\n*</pre>", "<pre>\n%1</pre>") |
| 267 | 274 | :gsub("\n*<p> </p>\n*", "&lcpad;") |
| 268 | 275 | :gsub("\n", "&lcnl;") |
| 269 | 276 | :gsub("\t", "&lctab;") |
| 270 | 277 | :gsub("%s", " ") |
| 271 | 278 | :gsub("<[^>]*>", function(match) return match:gsub(" ", " ") end) |
| 272 | - :gsub("<a[^>]*>(.-)</a>", function(match) return match:gsub(" ", " ") end) .. "&lcend;" | |
| 279 | + :gsub("<a[^>]*>(.-)</a>", function(match) | |
| 280 | + match = match:gsub("&#?%w+;", utils.entity) | |
| 281 | + return match:gsub(" ", " ") | |
| 282 | + end) .. "&lcend;" | |
| 273 | 283 | end |
| 274 | 284 | |
| 275 | 285 | -- Trim excessive lines |
| @@ -296,7 +306,6 @@ function Html:parse(html) | ||
| 296 | 306 | newline_count = 0, |
| 297 | 307 | ol_count = {}, |
| 298 | 308 | }, self) |
| 299 | - log.debug("siema") | |
| 300 | 309 | |
| 301 | 310 | self:normalize() |
| 302 | 311 | local ok, parser = pcall(self.ts.get_string_parser, self.str, "html") |
+1-1lua/leetcode/theme/default.lua
| @@ -41,7 +41,7 @@ M.get = function() | ||
| 41 | 41 | constraints = { fg = hl("DiagnosticInfo").fg }, |
| 42 | 42 | indent = { fg = hl("Comment").fg }, |
| 43 | 43 | link = { fg = hl("Function").fg, underline = true }, |
| 44 | - list = { fg = hl("Tag").fg }, | |
| 44 | + list = { fg = hl("Statement").fg }, | |
| 45 | 45 | } |
| 46 | 46 | end |
| 47 | 47 |
+7-26lua/leetcode/ui/question.lua
| @@ -32,21 +32,21 @@ end | ||
| 32 | 32 | |
| 33 | 33 | function Question:get_snippet() |
| 34 | 34 | local snippets = self.q.code_snippets ~= vim.NIL and self.q.code_snippets or {} |
| 35 | - return vim.tbl_filter(function(snip) return snip.lang_slug == self.lang end, snippets)[1] or {} | |
| 35 | + return vim.tbl_filter(function(snip) return snip.lang_slug == self.lang end, snippets)[1] | |
| 36 | 36 | end |
| 37 | 37 | |
| 38 | 38 | ---@private |
| 39 | 39 | function Question:create_file() |
| 40 | 40 | local lang = utils.get_lang(is_sql(self.q) and config.sql or config.lang) |
| 41 | - local suffix = lang.sql and "-" .. lang.short or "" | |
| 41 | + local suffix = lang.sql and string.format("-%s", lang.short) or "" | |
| 42 | 42 | local fn = string.format("%s.%s%s.%s", self.q.frontend_id, self.q.title_slug, suffix, lang.ft) |
| 43 | 43 | |
| 44 | 44 | self.file = config.home:joinpath(fn) |
| 45 | + if not self.file:exists() then self.file:write(self:get_snippet().code, "w") end | |
| 45 | 46 | end |
| 46 | 47 | |
| 47 | 48 | function Question:mount() |
| 48 | 49 | self:create_file() |
| 49 | - if not self.file:exists() then self.file:write(self:get_snippet().code, "w") end | |
| 50 | 50 | |
| 51 | 51 | vim.api.nvim_set_current_dir(config.home:absolute()) |
| 52 | 52 | vim.cmd("$tabe " .. self.file:absolute()) |
| @@ -59,30 +59,9 @@ function Question:mount() | ||
| 59 | 59 | self.console = Console:init(self) |
| 60 | 60 | self.hints = Hints:init(self) |
| 61 | 61 | |
| 62 | - self:autocmds() | |
| 63 | 62 | return self |
| 64 | 63 | end |
| 65 | 64 | |
| 66 | -function Question:autocmds() | |
| 67 | - -- local group_id = vim.api.nvim_create_augroup("leetcode_questions", { clear = true }) | |
| 68 | - | |
| 69 | - -- local q_group_id = vim.api.nvim_create_augroup("leetcode_question", {}) | |
| 70 | - -- vim.api.nvim_create_autocmd("BufWinEnter", { | |
| 71 | - -- group = q_group_id, | |
| 72 | - -- buffer = self.bufnr, | |
| 73 | - -- callback = function() | |
| 74 | - -- log.info("wtf") | |
| 75 | - -- self.description.split:show() | |
| 76 | - -- end, | |
| 77 | - -- }) | |
| 78 | - -- | |
| 79 | - -- vim.api.nvim_create_autocmd("BufWinLeave", { | |
| 80 | - -- group = q_group_id, | |
| 81 | - -- buffer = self.bufnr, | |
| 82 | - -- callback = function() self.description.split:hide() end, | |
| 83 | - -- }) | |
| 84 | -end | |
| 85 | - | |
| 86 | 65 | function Question:handle_mount() |
| 87 | 66 | if self:get_snippet() then |
| 88 | 67 | self:mount() |
| @@ -100,6 +79,8 @@ end | ||
| 100 | 79 | |
| 101 | 80 | ---@param problem lc.Cache.Question |
| 102 | 81 | function Question:init(problem) |
| 82 | + log.debug("Initializing question: " .. problem.frontend_id .. ". " .. problem.title_slug) | |
| 83 | + | |
| 103 | 84 | local tabp = utils.detect_duplicate_question(problem.title_slug) |
| 104 | 85 | if tabp then return pcall(vim.cmd.tabnext, tabp) end |
| 105 | 86 | |
| @@ -110,13 +91,13 @@ function Question:init(problem) | ||
| 110 | 91 | |
| 111 | 92 | local lang = utils.get_lang(is_sql(q) and config.sql or config.lang) |
| 112 | 93 | |
| 113 | - local obj = setmetatable({ | |
| 94 | + self = setmetatable({ | |
| 114 | 95 | q = q, |
| 115 | 96 | lang = lang.slug, |
| 116 | 97 | cache = problem, |
| 117 | 98 | }, self) |
| 118 | 99 | |
| 119 | - return obj:handle_mount() | |
| 100 | + return self:handle_mount() | |
| 120 | 101 | end |
| 121 | 102 | |
| 122 | 103 | return Question |