irongit

A Neovim plugin enabling you to solve LeetCode problems.

feat(virt_text): better align

kawreauthored
parent 4e142cacommit 4ef4e080a00291646a868f0b3b4f3b0a143df206Browse files

11 files changed, +116 -102

+0-7README.md
@@ -87,9 +87,6 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat
8787 ---@type lc.lang
8888 lang = "cpp",
8989
90- ---@type lc.sql
91- sql = "mysql",
92-
9390 ---@type string
9491 directory = vim.fn.stdpath("data") .. "/leetcode/",
9592
@@ -153,9 +150,6 @@ Language to start your session with
153150 ```lua
154151 ---@type lc.lang
155152 lang = "cpp"
156-
157----@type lc.sql_lang
158-sql = "mysql"
159153 ```
160154
161155 ### domain
@@ -284,7 +278,6 @@ return {
284278 ## ✅ Todo
285279
286280 - \[ \] CN version
287-- \[ \] SQL support
288281 - \[x\] Statistics menu page
289282 - \[ \] Docs
290283 - \[x\] Hints pop-up
+0-1lua/leetcode.lua
@@ -38,7 +38,6 @@ function leetcode.validate()
3838
3939 assert(vim.fn.has("nvim-0.9.0") == 1, "Neovim >= 0.9.0 required")
4040 assert(utils.get_lang(config.lang), "Unsupported Language: " .. config.lang)
41- assert(utils.get_lang(config.sql), "Unsupported SQL dialect: " .. config.sql)
4241 end
4342
4443 function leetcode.start()
+1-1lua/leetcode/api/question.lua
@@ -31,7 +31,7 @@ end
3131
3232 function question.random()
3333 local variables = {
34- categorySlug = "",
34+ categorySlug = "algorithms",
3535 }
3636
3737 local query = queries.random_question()
+0-1lua/leetcode/cache/problemlist.lua
@@ -60,7 +60,6 @@ function problemlist.get()
6060 local ok, problems = pcall(problemlist.parse, contents)
6161 assert(ok)
6262
63- log.debug(problems)
6463 return problems
6564 end
6665
+0-2lua/leetcode/config/init.lua
@@ -15,7 +15,6 @@ local config = {
1515 domain = "https://leetcode.com",
1616 debug = false,
1717 lang = "cpp",
18- sql = "mysql",
1918 home = {}, ---@type Path
2019
2120 langs = require("leetcode.config.langs"),
@@ -37,7 +36,6 @@ function config.apply(cfg)
3736 config.is_cn = config.user.domain == "cn"
3837
3938 config.lang = config.user.lang
40- config.sql = config.user.sql
4139 end
4240
4341 return config
+96-59lua/leetcode/config/langs.lua
@@ -9,8 +9,22 @@
99
1010 ---@type lc.language[]
1111 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+ },
1428 {
1529 lang = "Python",
1630 slug = "python",
@@ -27,8 +41,22 @@ return {
2741 short = "pyth3",
2842 ft = "py",
2943 },
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+ },
3260 {
3361 lang = "JavaScript",
3462 slug = "javascript",
@@ -45,7 +73,14 @@ return {
4573 short = "ts",
4674 ft = "ts",
4775 },
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+ },
4984 {
5085 lang = "Swift",
5186 slug = "swift",
@@ -62,9 +97,30 @@ return {
6297 short = "ktlin",
6398 ft = "kt",
6499 },
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+ },
68124 {
69125 lang = "Scala",
70126 slug = "scala",
@@ -73,7 +129,14 @@ return {
73129 short = "scala",
74130 ft = "scala",
75131 },
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+ },
77140 {
78141 lang = "Racket",
79142 slug = "racket",
@@ -98,8 +161,22 @@ return {
98161 short = "elixr",
99162 ft = "ex",
100163 },
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+ },
103180 {
104181 lang = "Python ML",
105182 slug = "pythonml",
@@ -108,7 +185,14 @@ return {
108185 short = "pyml",
109186 ft = "py",
110187 },
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+ },
112196 {
113197 lang = "Vanilla JS",
114198 slug = "vanillajs",
@@ -117,51 +201,4 @@ return {
117201 short = "vjs",
118202 ft = "js",
119203 },
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- },
167204 }
+0-10lua/leetcode/config/template.lua
@@ -24,13 +24,6 @@
2424 ---| "react"
2525 ---| "vanillajs"
2626
27----@alias lc.sql
28----| "pythondata"
29----| "mysql"
30----| "mssql"
31----| "oraclesql"
32----| "postgresql"
33-
3427 ---@alias lc.domain
3528 ---| "com"
3629 ---| "cn"
@@ -50,9 +43,6 @@ local M = {
5043 ---@type lc.lang
5144 lang = "cpp",
5245
53- ---@type lc.sql
54- sql = "mysql",
55-
5646 ---@type string
5747 directory = vim.fn.stdpath("data") .. "/leetcode/",
5848
+2-2lua/leetcode/pickers/language.lua
@@ -85,7 +85,7 @@ M.pick_lang = function(question, callback)
8585 return log.warn("Language already set to: " .. snippet.t.lang)
8686 end
8787
88- config[snippet.t.sql and "sql" or "lang"] = snippet.t.slug
88+ config.lang = snippet.t.slug
8989 actions.close(prompt_bufnr)
9090 callback(snippet)
9191 end)
@@ -116,7 +116,7 @@ M.pick = function(question)
116116 return log.warn("Language already set to: " .. snippet.t.lang)
117117 end
118118
119- config[snippet.t.sql and "sql" or "lang"] = snippet.t.slug
119+ config.lang = snippet.t.slug
120120 actions.close(prompt_bufnr)
121121 Question:init(question.cache)
122122 end)
+11-8lua/leetcode/ui/console/testcase.lua
@@ -77,24 +77,26 @@ function testcase:draw_extmarks()
7777
7878 if not md.params then return end
7979
80- local max_len = 1
80+ local max_lens = {}
81+ local j, k = 1, 1
8182 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)
8385 end
8486
85- local function get_param(idx, len)
87+ local function get_param(idx, param_idx, len)
8688 return {
87- { (" "):rep(max_len - len) },
89+ { (" "):rep(max_lens[idx] - len) },
8890 { "", "Operator" },
8991 { " " },
90- { md.params[idx].name, "Comment" },
92+ { md.params[param_idx].name, "Comment" },
9193 { " " },
92- { md.params[idx].type, "Type" },
94+ { md.params[param_idx].type, "Type" },
9395 }
9496 end
9597
96- local j = 1
9798 local invalid = false
99+ k = 1
98100
99101 for i, line in ipairs(lines) do
100102 pcall(function()
@@ -102,12 +104,13 @@ function testcase:draw_extmarks()
102104 end)
103105
104106 if line ~= "" then
105- local ok, text = pcall(get_param, j, line:len())
107+ local ok, text = pcall(get_param, k, j, line:len())
106108 if not ok or invalid then text = { { " invalid", "leetcode_error" } } end
107109
108110 self:add_extmark(i - 1, -1, { virt_text = text })
109111 j = j + 1
110112 else
113+ k = k + 1
111114 j = 1
112115 end
113116 end
+2-6lua/leetcode/ui/question.lua
@@ -13,9 +13,7 @@ local Info = require("leetcode.ui.info")
1313 ---@field bufnr integer
1414 ---@field console lc.Console
1515 ---@field lang string
16----@field sql string
1716 ---@field cache lc.Cache.Question
18----@field is_sql boolean
1917 local Question = {}
2018 Question.__index = Question
2119
@@ -78,7 +76,7 @@ function Question:init(problem)
7876 log.debug(problem)
7977 log.debug("Initializing question: " .. problem.frontend_id .. ". " .. problem.title_slug)
8078
81- local tabp = utils.detect_duplicate_question(problem.title_slug)
79+ local tabp = utils.detect_duplicate_question(problem.title_slug, config.lang)
8280 if tabp then return pcall(vim.cmd.tabnext, tabp) end
8381
8482 local q = api_question.by_title_slug(problem.title_slug)
@@ -86,12 +84,10 @@ function Question:init(problem)
8684 return log.warn("Question is for premium users only")
8785 end
8886
89- local is_sql = q.meta_data.database and true or false
9087 self = setmetatable({
9188 q = q,
9289 cache = problem,
93- is_sql = is_sql,
94- lang = is_sql and config.sql or config.lang,
90+ lang = config.lang,
9591 }, self)
9692
9793 return self:handle_mount()
+4-5lua/leetcode/utils.lua
@@ -61,14 +61,13 @@ function utils.map(mode, lhs, rhs, opts)
6161 vim.api.nvim_set_keymap(mode, lhs, rhs, options)
6262 end
6363
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)
6567 local tabs = utils.curr_question_tabs()
6668
6769 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
7271 return q.tabpage
7372 end
7473 end