A Neovim plugin enabling you to solve LeetCode problems.
feat(virt_text): better align
11 files changed, +116 -102
+0-7README.md
| @@ -87,9 +87,6 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat | ||
| 87 | 87 | ---@type lc.lang |
| 88 | 88 | lang = "cpp", |
| 89 | 89 | |
| 90 | - ---@type lc.sql | |
| 91 | - sql = "mysql", | |
| 92 | - | |
| 93 | 90 | ---@type string |
| 94 | 91 | directory = vim.fn.stdpath("data") .. "/leetcode/", |
| 95 | 92 | |
| @@ -153,9 +150,6 @@ Language to start your session with | ||
| 153 | 150 | ```lua |
| 154 | 151 | ---@type lc.lang |
| 155 | 152 | lang = "cpp" |
| 156 | - | |
| 157 | ----@type lc.sql_lang | |
| 158 | -sql = "mysql" | |
| 159 | 153 | ``` |
| 160 | 154 | |
| 161 | 155 | ### domain |
| @@ -284,7 +278,6 @@ return { | ||
| 284 | 278 | ## ✅ Todo |
| 285 | 279 | |
| 286 | 280 | - \[ \] CN version |
| 287 | -- \[ \] SQL support | |
| 288 | 281 | - \[x\] Statistics menu page |
| 289 | 282 | - \[ \] Docs |
| 290 | 283 | - \[x\] Hints pop-up |
+0-1lua/leetcode.lua
| @@ -38,7 +38,6 @@ function leetcode.validate() | ||
| 38 | 38 | |
| 39 | 39 | assert(vim.fn.has("nvim-0.9.0") == 1, "Neovim >= 0.9.0 required") |
| 40 | 40 | assert(utils.get_lang(config.lang), "Unsupported Language: " .. config.lang) |
| 41 | - assert(utils.get_lang(config.sql), "Unsupported SQL dialect: " .. config.sql) | |
| 42 | 41 | end |
| 43 | 42 | |
| 44 | 43 | function leetcode.start() |
+1-1lua/leetcode/api/question.lua
| @@ -31,7 +31,7 @@ end | ||
| 31 | 31 | |
| 32 | 32 | function question.random() |
| 33 | 33 | local variables = { |
| 34 | - categorySlug = "", | |
| 34 | + categorySlug = "algorithms", | |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | local query = queries.random_question() |
+0-1lua/leetcode/cache/problemlist.lua
| @@ -60,7 +60,6 @@ function problemlist.get() | ||
| 60 | 60 | local ok, problems = pcall(problemlist.parse, contents) |
| 61 | 61 | assert(ok) |
| 62 | 62 | |
| 63 | - log.debug(problems) | |
| 64 | 63 | return problems |
| 65 | 64 | end |
| 66 | 65 |
+0-2lua/leetcode/config/init.lua
| @@ -15,7 +15,6 @@ local config = { | ||
| 15 | 15 | domain = "https://leetcode.com", |
| 16 | 16 | debug = false, |
| 17 | 17 | lang = "cpp", |
| 18 | - sql = "mysql", | |
| 19 | 18 | home = {}, ---@type Path |
| 20 | 19 | |
| 21 | 20 | langs = require("leetcode.config.langs"), |
| @@ -37,7 +36,6 @@ function config.apply(cfg) | ||
| 37 | 36 | config.is_cn = config.user.domain == "cn" |
| 38 | 37 | |
| 39 | 38 | config.lang = config.user.lang |
| 40 | - config.sql = config.user.sql | |
| 41 | 39 | end |
| 42 | 40 | |
| 43 | 41 | return config |
+96-59lua/leetcode/config/langs.lua
| @@ -9,8 +9,22 @@ | ||
| 9 | 9 | |
| 10 | 10 | ---@type lc.language[] |
| 11 | 11 | return { |
| 12 | - { lang = "C++", slug = "cpp", icon = "", color = "#00599C", short = "cpp", ft = "cpp" }, | |
| 13 | - { lang = "Java", slug = "java", icon = "", color = "#E76F00", short = "java", ft = "java" }, | |
| 12 | + { | |
| 13 | + lang = "C++", | |
| 14 | + slug = "cpp", | |
| 15 | + icon = "", | |
| 16 | + color = "#00599C", | |
| 17 | + short = "cpp", | |
| 18 | + ft = "cpp", | |
| 19 | + }, | |
| 20 | + { | |
| 21 | + lang = "Java", | |
| 22 | + slug = "java", | |
| 23 | + icon = "", | |
| 24 | + color = "#E76F00", | |
| 25 | + short = "java", | |
| 26 | + ft = "java", | |
| 27 | + }, | |
| 14 | 28 | { |
| 15 | 29 | lang = "Python", |
| 16 | 30 | slug = "python", |
| @@ -27,8 +41,22 @@ return { | ||
| 27 | 41 | short = "pyth3", |
| 28 | 42 | ft = "py", |
| 29 | 43 | }, |
| 30 | - { lang = "C", slug = "c", icon = "", color = "#555555", short = "clang", ft = "c" }, | |
| 31 | - { lang = "C#", slug = "csharp", icon = "", color = "#68217A", short = "cshrp", ft = "cs" }, | |
| 44 | + { | |
| 45 | + lang = "C", | |
| 46 | + slug = "c", | |
| 47 | + icon = "", | |
| 48 | + color = "#555555", | |
| 49 | + short = "clang", | |
| 50 | + ft = "c", | |
| 51 | + }, | |
| 52 | + { | |
| 53 | + lang = "C#", | |
| 54 | + slug = "csharp", | |
| 55 | + icon = "", | |
| 56 | + color = "#68217A", | |
| 57 | + short = "cshrp", | |
| 58 | + ft = "cs", | |
| 59 | + }, | |
| 32 | 60 | { |
| 33 | 61 | lang = "JavaScript", |
| 34 | 62 | slug = "javascript", |
| @@ -45,7 +73,14 @@ return { | ||
| 45 | 73 | short = "ts", |
| 46 | 74 | ft = "ts", |
| 47 | 75 | }, |
| 48 | - { lang = "PHP", slug = "php", icon = "", color = "#777BB4", short = "php", ft = "php" }, | |
| 76 | + { | |
| 77 | + lang = "PHP", | |
| 78 | + slug = "php", | |
| 79 | + icon = "", | |
| 80 | + color = "#777BB4", | |
| 81 | + short = "php", | |
| 82 | + ft = "php", | |
| 83 | + }, | |
| 49 | 84 | { |
| 50 | 85 | lang = "Swift", |
| 51 | 86 | slug = "swift", |
| @@ -62,9 +97,30 @@ return { | ||
| 62 | 97 | short = "ktlin", |
| 63 | 98 | ft = "kt", |
| 64 | 99 | }, |
| 65 | - { lang = "Dart", slug = "dart", icon = "", color = "#1057A7", short = "dart", ft = "dart" }, | |
| 66 | - { lang = "Go", slug = "golang", icon = "", color = "#00ADD8", short = "golng", ft = "go" }, | |
| 67 | - { lang = "Ruby", slug = "ruby", icon = "", color = "#CC342D", short = "ruby", ft = "rb" }, | |
| 100 | + { | |
| 101 | + lang = "Dart", | |
| 102 | + slug = "dart", | |
| 103 | + icon = "", | |
| 104 | + color = "#1057A7", | |
| 105 | + short = "dart", | |
| 106 | + ft = "dart", | |
| 107 | + }, | |
| 108 | + { | |
| 109 | + lang = "Go", | |
| 110 | + slug = "golang", | |
| 111 | + icon = "", | |
| 112 | + color = "#00ADD8", | |
| 113 | + short = "golng", | |
| 114 | + ft = "go", | |
| 115 | + }, | |
| 116 | + { | |
| 117 | + lang = "Ruby", | |
| 118 | + slug = "ruby", | |
| 119 | + icon = "", | |
| 120 | + color = "#CC342D", | |
| 121 | + short = "ruby", | |
| 122 | + ft = "rb", | |
| 123 | + }, | |
| 68 | 124 | { |
| 69 | 125 | lang = "Scala", |
| 70 | 126 | slug = "scala", |
| @@ -73,7 +129,14 @@ return { | ||
| 73 | 129 | short = "scala", |
| 74 | 130 | ft = "scala", |
| 75 | 131 | }, |
| 76 | - { lang = "Rust", slug = "rust", icon = "", color = "#DEA584", short = "rust", ft = "rs" }, | |
| 132 | + { | |
| 133 | + lang = "Rust", | |
| 134 | + slug = "rust", | |
| 135 | + icon = "", | |
| 136 | + color = "#DEA584", | |
| 137 | + short = "rust", | |
| 138 | + ft = "rs", | |
| 139 | + }, | |
| 77 | 140 | { |
| 78 | 141 | lang = "Racket", |
| 79 | 142 | slug = "racket", |
| @@ -98,8 +161,22 @@ return { | ||
| 98 | 161 | short = "elixr", |
| 99 | 162 | ft = "ex", |
| 100 | 163 | }, |
| 101 | - { lang = "Bash", slug = "bash", icon = "", color = "#000000", short = "bash", ft = "sh" }, | |
| 102 | - { lang = "HTML", slug = "html", icon = "", color = "#E44D26", short = "html", ft = "html" }, | |
| 164 | + { | |
| 165 | + lang = "Bash", | |
| 166 | + slug = "bash", | |
| 167 | + icon = "", | |
| 168 | + color = "#000000", | |
| 169 | + short = "bash", | |
| 170 | + ft = "sh", | |
| 171 | + }, | |
| 172 | + { | |
| 173 | + lang = "HTML", | |
| 174 | + slug = "html", | |
| 175 | + icon = "", | |
| 176 | + color = "#E44D26", | |
| 177 | + short = "html", | |
| 178 | + ft = "html", | |
| 179 | + }, | |
| 103 | 180 | { |
| 104 | 181 | lang = "Python ML", |
| 105 | 182 | slug = "pythonml", |
| @@ -108,7 +185,14 @@ return { | ||
| 108 | 185 | short = "pyml", |
| 109 | 186 | ft = "py", |
| 110 | 187 | }, |
| 111 | - { lang = "React", slug = "react", icon = "⚛", color = "#61DAFB", short = "ract", ft = "jsx" }, | |
| 188 | + { | |
| 189 | + lang = "React", | |
| 190 | + slug = "react", | |
| 191 | + icon = "⚛", | |
| 192 | + color = "#61DAFB", | |
| 193 | + short = "ract", | |
| 194 | + ft = "jsx", | |
| 195 | + }, | |
| 112 | 196 | { |
| 113 | 197 | lang = "Vanilla JS", |
| 114 | 198 | slug = "vanillajs", |
| @@ -117,51 +201,4 @@ return { | ||
| 117 | 201 | short = "vjs", |
| 118 | 202 | ft = "js", |
| 119 | 203 | }, |
| 120 | - | |
| 121 | - -- sql | |
| 122 | - { | |
| 123 | - lang = "Oracle", | |
| 124 | - slug = "oraclesql", | |
| 125 | - icon = "", | |
| 126 | - color = "#ff0000", | |
| 127 | - sql = true, | |
| 128 | - short = "osql", | |
| 129 | - ft = "sql", | |
| 130 | - }, | |
| 131 | - { | |
| 132 | - lang = "PostgreSQL", | |
| 133 | - slug = "postgresql", | |
| 134 | - icon = "", | |
| 135 | - color = "#336791", | |
| 136 | - sql = true, | |
| 137 | - short = "psql", | |
| 138 | - ft = "sql", | |
| 139 | - }, | |
| 140 | - { | |
| 141 | - lang = "MySQL", | |
| 142 | - slug = "mysql", | |
| 143 | - icon = "", | |
| 144 | - color = "#f29111", | |
| 145 | - sql = true, | |
| 146 | - short = "mysql", | |
| 147 | - ft = "sql", | |
| 148 | - }, | |
| 149 | - { | |
| 150 | - lang = "MS SQL Server", | |
| 151 | - slug = "mssql", | |
| 152 | - icon = "", | |
| 153 | - color = "#ff0000", | |
| 154 | - sql = true, | |
| 155 | - short = "mssql", | |
| 156 | - ft = "sql", | |
| 157 | - }, | |
| 158 | - { | |
| 159 | - lang = "Pandas", | |
| 160 | - slug = "pythondata", | |
| 161 | - icon = "", | |
| 162 | - color = "#306998", | |
| 163 | - sql = true, | |
| 164 | - short = "pydta", | |
| 165 | - ft = "py", | |
| 166 | - }, | |
| 167 | 204 | } |
+0-10lua/leetcode/config/template.lua
| @@ -24,13 +24,6 @@ | ||
| 24 | 24 | ---| "react" |
| 25 | 25 | ---| "vanillajs" |
| 26 | 26 | |
| 27 | ----@alias lc.sql | |
| 28 | ----| "pythondata" | |
| 29 | ----| "mysql" | |
| 30 | ----| "mssql" | |
| 31 | ----| "oraclesql" | |
| 32 | ----| "postgresql" | |
| 33 | - | |
| 34 | 27 | ---@alias lc.domain |
| 35 | 28 | ---| "com" |
| 36 | 29 | ---| "cn" |
| @@ -50,9 +43,6 @@ local M = { | ||
| 50 | 43 | ---@type lc.lang |
| 51 | 44 | lang = "cpp", |
| 52 | 45 | |
| 53 | - ---@type lc.sql | |
| 54 | - sql = "mysql", | |
| 55 | - | |
| 56 | 46 | ---@type string |
| 57 | 47 | directory = vim.fn.stdpath("data") .. "/leetcode/", |
| 58 | 48 |
+2-2lua/leetcode/pickers/language.lua
| @@ -85,7 +85,7 @@ M.pick_lang = function(question, callback) | ||
| 85 | 85 | return log.warn("Language already set to: " .. snippet.t.lang) |
| 86 | 86 | end |
| 87 | 87 | |
| 88 | - config[snippet.t.sql and "sql" or "lang"] = snippet.t.slug | |
| 88 | + config.lang = snippet.t.slug | |
| 89 | 89 | actions.close(prompt_bufnr) |
| 90 | 90 | callback(snippet) |
| 91 | 91 | end) |
| @@ -116,7 +116,7 @@ M.pick = function(question) | ||
| 116 | 116 | return log.warn("Language already set to: " .. snippet.t.lang) |
| 117 | 117 | end |
| 118 | 118 | |
| 119 | - config[snippet.t.sql and "sql" or "lang"] = snippet.t.slug | |
| 119 | + config.lang = snippet.t.slug | |
| 120 | 120 | actions.close(prompt_bufnr) |
| 121 | 121 | Question:init(question.cache) |
| 122 | 122 | end) |
+11-8lua/leetcode/ui/console/testcase.lua
| @@ -77,24 +77,26 @@ function testcase:draw_extmarks() | ||
| 77 | 77 | |
| 78 | 78 | if not md.params then return end |
| 79 | 79 | |
| 80 | - local max_len = 1 | |
| 80 | + local max_lens = {} | |
| 81 | + local j, k = 1, 1 | |
| 81 | 82 | for _, line in ipairs(lines) do |
| 82 | - max_len = math.max(max_len, line:len() + 1) | |
| 83 | + if line == "" then k = k + 1 end | |
| 84 | + max_lens[k] = math.max(max_lens[k] or 1, line:len() + 1) | |
| 83 | 85 | end |
| 84 | 86 | |
| 85 | - local function get_param(idx, len) | |
| 87 | + local function get_param(idx, param_idx, len) | |
| 86 | 88 | return { |
| 87 | - { (" "):rep(max_len - len) }, | |
| 89 | + { (" "):rep(max_lens[idx] - len) }, | |
| 88 | 90 | { "", "Operator" }, |
| 89 | 91 | { " " }, |
| 90 | - { md.params[idx].name, "Comment" }, | |
| 92 | + { md.params[param_idx].name, "Comment" }, | |
| 91 | 93 | { " " }, |
| 92 | - { md.params[idx].type, "Type" }, | |
| 94 | + { md.params[param_idx].type, "Type" }, | |
| 93 | 95 | } |
| 94 | 96 | end |
| 95 | 97 | |
| 96 | - local j = 1 | |
| 97 | 98 | local invalid = false |
| 99 | + k = 1 | |
| 98 | 100 | |
| 99 | 101 | for i, line in ipairs(lines) do |
| 100 | 102 | pcall(function() |
| @@ -102,12 +104,13 @@ function testcase:draw_extmarks() | ||
| 102 | 104 | end) |
| 103 | 105 | |
| 104 | 106 | if line ~= "" then |
| 105 | - local ok, text = pcall(get_param, j, line:len()) | |
| 107 | + local ok, text = pcall(get_param, k, j, line:len()) | |
| 106 | 108 | if not ok or invalid then text = { { " invalid", "leetcode_error" } } end |
| 107 | 109 | |
| 108 | 110 | self:add_extmark(i - 1, -1, { virt_text = text }) |
| 109 | 111 | j = j + 1 |
| 110 | 112 | else |
| 113 | + k = k + 1 | |
| 111 | 114 | j = 1 |
| 112 | 115 | end |
| 113 | 116 | end |
+2-6lua/leetcode/ui/question.lua
| @@ -13,9 +13,7 @@ local Info = require("leetcode.ui.info") | ||
| 13 | 13 | ---@field bufnr integer |
| 14 | 14 | ---@field console lc.Console |
| 15 | 15 | ---@field lang string |
| 16 | ----@field sql string | |
| 17 | 16 | ---@field cache lc.Cache.Question |
| 18 | ----@field is_sql boolean | |
| 19 | 17 | local Question = {} |
| 20 | 18 | Question.__index = Question |
| 21 | 19 | |
| @@ -78,7 +76,7 @@ function Question:init(problem) | ||
| 78 | 76 | log.debug(problem) |
| 79 | 77 | log.debug("Initializing question: " .. problem.frontend_id .. ". " .. problem.title_slug) |
| 80 | 78 | |
| 81 | - local tabp = utils.detect_duplicate_question(problem.title_slug) | |
| 79 | + local tabp = utils.detect_duplicate_question(problem.title_slug, config.lang) | |
| 82 | 80 | if tabp then return pcall(vim.cmd.tabnext, tabp) end |
| 83 | 81 | |
| 84 | 82 | local q = api_question.by_title_slug(problem.title_slug) |
| @@ -86,12 +84,10 @@ function Question:init(problem) | ||
| 86 | 84 | return log.warn("Question is for premium users only") |
| 87 | 85 | end |
| 88 | 86 | |
| 89 | - local is_sql = q.meta_data.database and true or false | |
| 90 | 87 | self = setmetatable({ |
| 91 | 88 | q = q, |
| 92 | 89 | cache = problem, |
| 93 | - is_sql = is_sql, | |
| 94 | - lang = is_sql and config.sql or config.lang, | |
| 90 | + lang = config.lang, | |
| 95 | 91 | }, self) |
| 96 | 92 | |
| 97 | 93 | return self:handle_mount() |
+4-5lua/leetcode/utils.lua
| @@ -61,14 +61,13 @@ function utils.map(mode, lhs, rhs, opts) | ||
| 61 | 61 | vim.api.nvim_set_keymap(mode, lhs, rhs, options) |
| 62 | 62 | end |
| 63 | 63 | |
| 64 | -function utils.detect_duplicate_question(title_slug) | |
| 64 | +---@param title_slug string | |
| 65 | +---@param lang lc.lang | |
| 66 | +function utils.detect_duplicate_question(title_slug, lang) | |
| 65 | 67 | local tabs = utils.curr_question_tabs() |
| 66 | 68 | |
| 67 | 69 | for _, q in ipairs(tabs) do |
| 68 | - if | |
| 69 | - title_slug == q.question.q.title_slug | |
| 70 | - and (config.sql == q.question.lang or config.lang == q.question.lang) | |
| 71 | - then | |
| 70 | + if title_slug == q.question.q.title_slug and lang == q.question.lang then | |
| 72 | 71 | return q.tabpage |
| 73 | 72 | end |
| 74 | 73 | end |