Skip to content

Commit 2926bd9

Browse files
committed
feat(config): question filename customization
1 parent 2076e26 commit 2926bd9

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat
147147
["leave"] = {},
148148
},
149149

150+
---@type lc.filename
151+
filename = function(id, title, alias, extension)
152+
local parts = alias and { id, title, alias, extension } or { id, title, extension }
153+
return table.concat(parts, ".")
154+
end,
155+
150156
keys = {
151157
toggle = { "q" }, ---@type string|string[]
152158
confirm = { "<CR>" }, ---@type string|string[]
@@ -300,6 +306,21 @@ hooks = {
300306
},
301307
```
302308

309+
### filename
310+
311+
Custom function to both create and read questions
312+
313+
`alias` can be `nil` and is used for differentiating between `Python` and
314+
`Python3` languages, since they share the same file extension `.py`
315+
316+
```lua
317+
---@type lc.filename
318+
filename = function(id, title, alias, extension)
319+
local parts = alias and { id, title, alias, extension } or { id, title, extension }
320+
return table.concat(parts, ".")
321+
end,
322+
```
323+
303324
### theme
304325

305326
Override the [default theme](./lua/leetcode/theme/default.lua).

lua/leetcode-ui/question.lua

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,11 @@ end
6060
---@return string path, boolean existed
6161
function Question:path()
6262
local lang = utils.get_lang(self.lang)
63-
local alt = lang.alt and ("." .. lang.alt) or ""
6463

65-
-- handle legacy file names first
66-
local fn_legacy = --
67-
("%s.%s-%s.%s"):format(self.q.frontend_id, self.q.title_slug, lang.slug, lang.ft)
68-
self.file = config.storage.home:joinpath(fn_legacy)
64+
local id, title, alias, extension = self.q.frontend_id, self.q.title_slug, lang.alias, lang.ft
65+
local filename = config.user.filename(id, title, alias, extension)
6966

70-
if self.file:exists() then
71-
return self.file:absolute(), true
72-
end
73-
74-
local fn = ("%s.%s%s.%s"):format(self.q.frontend_id, self.q.title_slug, alt, lang.ft)
75-
self.file = config.storage.home:joinpath(fn)
67+
self.file = config.storage.home:joinpath(filename)
7668
local existed = self.file:exists()
7769

7870
if not existed then

lua/leetcode/config/langs.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
---@field hl? string
77
---@field comment string
88
---@field ft string
9-
---@field alt? string
9+
---@field alias? string
1010

1111
---@type lc.language[]
1212
return {
@@ -33,7 +33,7 @@ return {
3333
color = "#306998",
3434
ft = "py",
3535
comment = "#",
36-
alt = "python2",
36+
alias = "python2",
3737
},
3838
{
3939
lang = "Python3",

lua/leetcode/config/template.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838

3939
---@alias lc.storage table<"cache"|"home", string>
4040

41+
---@alias lc.filename fun(id: string, title: string, alias: string|nil, extension: string): string
42+
4143
---@class lc.UserConfig
4244
local M = {
4345
---@type string
@@ -112,6 +114,12 @@ local M = {
112114
["leave"] = {},
113115
},
114116

117+
---@type lc.filename
118+
filename = function(id, title, alias, extension)
119+
local parts = alias and { id, title, alias, extension } or { id, title, extension }
120+
return table.concat(parts, ".")
121+
end,
122+
115123
keys = {
116124
toggle = { "q" }, ---@type string|string[]
117125
confirm = { "<CR>" }, ---@type string|string[]

0 commit comments

Comments
 (0)