irongit

A Neovim plugin enabling you to solve LeetCode problems.

feat(config)!: replace `directory` with `storage` table

kawreauthored
parent 2bef48ecommit 33211315da7b54822fd6a6c516e021423ef8f8d4Browse files

9 files changed, +86 -57

+18-5lua/leetcode-ui/question.lua
@@ -34,11 +34,25 @@ end
3434 ---@private
3535 function Question:create_file()
3636 local lang = utils.get_lang(self.lang)
37- local alt = lang.alt and ("-" .. lang.alt) or ""
37+ local alt = lang.alt and ("." .. lang.alt) or ""
38+
39+ -- handle legacy file names first
40+ local fn_legacy = --
41+ ("%s.%s-%s.%s"):format(self.q.frontend_id, self.q.title_slug, lang.slug, lang.ft)
42+ self.file = config.storage.home:joinpath(fn_legacy)
43+
44+ if self.file:exists() then --
45+ return self.file:absolute()
46+ end
47+
3848 local fn = ("%s.%s%s.%s"):format(self.q.frontend_id, self.q.title_slug, alt, lang.ft)
49+ self.file = config.storage.home:joinpath(fn)
3950
40- self.file = config.home:joinpath(fn)
41- if not self.file:exists() then self.file:write(self:get_snippet(), "w") end
51+ if not self.file:exists() then --
52+ self.file:write(self:get_snippet(), "w")
53+ end
54+
55+ return self.file:absolute()
4256 end
4357
4458 ---@private
@@ -88,8 +102,7 @@ Question.unmount = vim.schedule_wrap(function(self)
88102 end)
89103
90104 function Question:handle_mount()
91- self:create_file()
92- vim.cmd("$tabe " .. self.file:absolute())
105+ vim.cmd("$tabe " .. self:create_file())
93106
94107 -- https://github.com/kawre/leetcode.nvim/issues/14
95108 if self.lang == "rust" then pcall(require("rust-tools.standalone").start_standalone_client) end
+10-44lua/leetcode.lua
@@ -3,10 +3,11 @@ local config = require("leetcode.config")
33 ---@class lc.LeetCode
44 local leetcode = {}
55
6-function leetcode.should_skip()
6+---@param arg string
7+function leetcode.should_skip(arg)
78 if vim.fn.argc() ~= 1 then return true end
89
9- local usr_arg, arg = config.user.arg, vim.fn.argv()[1]
10+ local usr_arg = vim.fn.argv()[1]
1011 if usr_arg ~= arg then return true end
1112
1213 local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
@@ -19,48 +20,15 @@ function leetcode.should_skip()
1920 return false
2021 end
2122
22-function leetcode.setup_cmds()
23- local cmd = require("leetcode.command")
24- cmd.setup()
25-end
26-
27-function leetcode.validate()
28- local utils = require("leetcode.utils")
29-
30- assert(vim.fn.has("nvim-0.9.0") == 1, "Neovim >= 0.9.0 required")
23+function leetcode.setup_cmds() require("leetcode.command").setup() end
3124
32- if not utils.get_lang(config.lang) then --
33- ---@type lc.lang[]
34- local lang_slugs = vim.tbl_map(function(lang) return lang.slug end, config.langs)
25+---@param cfg lc.UserConfig
26+function leetcode.start(cfg)
27+ if leetcode.should_skip(cfg.arg or config.default.arg) then return end
3528
36- local matches = {}
37- for _, slug in ipairs(lang_slugs) do
38- local percent = slug:match(config.lang) or config.lang:match(slug)
39- if percent then table.insert(matches, slug) end
40- end
41-
42- if not vim.tbl_isempty(matches) then
43- local log = require("leetcode.logger")
44- log.warn("Did you mean: { " .. table.concat(matches, ", ") .. " }?")
45- end
46-
47- error("Unsupported Language: " .. config.lang)
48- end
49-end
50-
51-function leetcode.start()
52- if leetcode.should_skip() then return end
53-
54- leetcode.validate()
55-
56- local path = require("plenary.path")
57- config.home = path:new(config.user.directory) ---@diagnostic disable-line
58- config.home:mkdir()
59-
60- config.cache_dir = path:new(vim.fn.stdpath("cache") .. "/leetcode/") ---@diagnostic disable-line
61- config.cache_dir:mkdir()
29+ config.apply(cfg or {})
6230
63- vim.api.nvim_set_current_dir(config.home:absolute())
31+ vim.api.nvim_set_current_dir(config.storage.home:absolute())
6432
6533 leetcode.setup_cmds()
6634 config.load_plugins()
@@ -77,14 +45,12 @@ end
7745
7846 ---@param cfg? lc.UserConfig
7947 function leetcode.setup(cfg)
80- config.apply(cfg or {})
81-
8248 local group_id = vim.api.nvim_create_augroup("leetcode_start", { clear = true })
8349 vim.api.nvim_create_autocmd("VimEnter", {
8450 group = group_id,
8551 pattern = "*",
8652 nested = true,
87- callback = leetcode.start,
53+ callback = function() leetcode.start(cfg or {}) end,
8854 })
8955 end
9056
+1-1lua/leetcode/cache/cookie.lua
@@ -3,7 +3,7 @@ local log = require("leetcode.logger")
33
44 local config = require("leetcode.config")
55 ---@type Path
6-local file = config.cache_dir:joinpath(("cookie%s"):format(config.is_cn and "_cn" or ""))
6+local file = config.storage.cache:joinpath(("cookie%s"):format(config.is_cn and "_cn" or ""))
77
88 local hist = {}
99
+1-1lua/leetcode/cache/problemlist.lua
@@ -6,7 +6,7 @@ local config = require("leetcode.config")
66 local interval = config.user.cache.update_interval
77
88 ---@type Path
9-local file = config.cache_dir:joinpath(("problemlist%s"):format(config.is_cn and "_cn" or ""))
9+local file = config.storage.cache:joinpath(("problemlist%s"):format(config.is_cn and "_cn" or ""))
1010
1111 ---@type { at: integer, payload: lc.cache.payload }
1212 local hist = nil
+45-2lua/leetcode/config/init.lua
@@ -1,4 +1,5 @@
11 local template = require("leetcode.config.template")
2+local P = require("plenary.path")
23
34 ---@type lc.ui.Question[]
45 _Lc_questions = {}
@@ -16,9 +17,8 @@ local config = {
1617 is_cn = false,
1718 debug = false,
1819 lang = "cpp",
19- home = {}, ---@type Path
20- cache_dir = {}, ---@type Path
2120 version = "1.0.1",
21+ storage = {}, ---@type table<string, Path>
2222
2323 translator = false,
2424
@@ -33,10 +33,53 @@ local config = {
3333 ---
3434 ---@param cfg lc.UserConfig Configurations to be merged.
3535 function config.apply(cfg)
36+ cfg.storage = cfg.storage or {}
37+
38+ -- deprecate `directory` config
39+ if cfg.directory then
40+ local log = require("leetcode.logger")
41+ log.warn("Config: `directory` is deprecated. Use `storage.home` instead.")
42+ cfg.storage.home = cfg.directory
43+ end
44+
45+ cfg.storage = vim.tbl_map(vim.fn.expand, cfg.storage)
46+
3647 config.user = vim.tbl_deep_extend("force", config.default, cfg)
3748
3849 config.debug = config.user.debug or false ---@diagnostic disable-line
3950 config.lang = config.user.lang
51+
52+ config.validate()
53+
54+ config.storage.home = P:new(config.user.storage.home) ---@diagnostic disable-line
55+ config.storage.home:mkdir()
56+
57+ config.storage.cache = P:new(config.user.storage.cache) ---@diagnostic disable-line
58+ config.storage.cache:mkdir()
59+end
60+
61+function config.validate()
62+ local utils = require("leetcode.utils")
63+
64+ assert(vim.fn.has("nvim-0.9.0") == 1, "Neovim >= 0.9.0 required")
65+
66+ if not utils.get_lang(config.lang) then --
67+ ---@type lc.lang[]
68+ local lang_slugs = vim.tbl_map(function(lang) return lang.slug end, config.langs)
69+
70+ local matches = {}
71+ for _, slug in ipairs(lang_slugs) do
72+ local percent = slug:match(config.lang) or config.lang:match(slug)
73+ if percent then table.insert(matches, slug) end
74+ end
75+
76+ if not vim.tbl_isempty(matches) then
77+ local log = require("leetcode.logger")
78+ log.warn("Did you mean: { " .. table.concat(matches, ", ") .. " }?")
79+ end
80+
81+ error("Unsupported Language: " .. config.lang)
82+ end
4083 end
4184
4285 function config.load_plugins()
+1-1lua/leetcode/config/langs.lua
@@ -33,7 +33,7 @@ return {
3333 color = "#306998",
3434 ft = "py",
3535 comment = "#",
36- alt = "py2",
36+ alt = "python2",
3737 },
3838 {
3939 lang = "Python3",
+7-2lua/leetcode/config/template.lua
@@ -39,6 +39,8 @@
3939
4040 ---@alias lc.inject { before?: string|string[], after?: string|string[] }
4141
42+---@alias lc.storage table<"cache"|"home", string>
43+
4244 ---@class lc.UserConfig
4345 local M = {
4446 ---@type string
@@ -53,8 +55,11 @@ local M = {
5355 translate_problems = true, ---@type boolean
5456 },
5557
56- ---@type string
57- directory = vim.fn.stdpath("data") .. "/leetcode/",
58+ ---@type lc.storage
59+ storage = {
60+ home = vim.fn.stdpath("data") .. "/leetcode/",
61+ cache = vim.fn.stdpath("cache") .. "/leetcode/",
62+ },
5863
5964 ---@type boolean
6065 logging = true,
+2-0lua/leetcode/pickers/question.lua
@@ -104,11 +104,13 @@ local function filter_questions(questions, opts)
104104
105105 ---@param q lc.cache.Question
106106 return vim.tbl_filter(function(q)
107+ local flag = true
107108 if opts.difficulty then
108109 if not vim.tbl_contains(opts.difficulty, q.difficulty:lower()) then return false end
109110 end
110111
111112 if opts.status then
113+ if not q.status then return true end
112114 if not vim.tbl_contains(opts.status, q.status) then return false end
113115 end
114116
+1-1lua/leetcode/runner/init.lua
@@ -3,7 +3,7 @@ local interpreter = require("leetcode.api.interpreter")
33 local config = require("leetcode.config")
44
55 ---@type Path
6-local leetbody = config.cache_dir:joinpath("body")
6+local leetbody = config.storage.cache:joinpath("body")
77 leetbody:touch()
88
99 ---@class lc.Runner