Skip to content

Support custom filename formatting #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lua/leetcode-ui/question.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ function Question:path()
local lang = utils.get_lang(self.lang)
local alt = lang.alt and ("." .. lang.alt) or ""

local fn_user_format = config.storage.format
if type(fn_user_format) == "function" then
local format_opts = {
id = "" .. self.q.frontend_id,
slug = self.q.title_slug,
ft_alt = alt,
ft = lang.ft,
}
local fn_user = fn_user_format(format_opts)
if type(fn_user) == "string" and #fn_user > 0 then
self.file = config.storage.home:joinpath(fn_user)
local existed = self.file:exists()

if not existed then
self.file:write(self:snippet(), "w")
end

return self.file:absolute(), existed
end
end

-- handle legacy file names first
local fn_legacy = --
("%s.%s-%s.%s"):format(self.q.frontend_id, self.q.title_slug, lang.slug, lang.ft)
Expand Down
7 changes: 5 additions & 2 deletions lua/leetcode/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ local config = {
debug = false,
lang = "cpp",
version = "1.0.1",
storage = {}, ---@type table<string, Path>
storage = {}, ---@type lc.storage
theme = {}, ---@type lc.highlights
plugins = {},

Expand Down Expand Up @@ -54,7 +54,8 @@ function config.setup()
config.user.storage.home = config.user.directory
end

config.user.storage = vim.tbl_map(vim.fn.expand, config.user.storage)
config.user.storage.home = vim.fn.expand(config.user.storage.home)
config.user.storage.cache = vim.fn.expand(config.user.storage.cache)

config.debug = config.user.debug or false ---@diagnostic disable-line
config.lang = config.user.lang
Expand All @@ -65,6 +66,8 @@ function config.setup()
config.storage.cache = P:new(config.user.storage.cache) ---@diagnostic disable-line
config.storage.cache:mkdir()

config.storage.format = config.user.storage.format

for _, plug_load_fn in ipairs(lazy_plugs) do
plug_load_fn()
end
Expand Down
6 changes: 5 additions & 1 deletion lua/leetcode/config/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@

---@alias lc.inject { before?: string|string[], after?: string|string[] }

---@alias lc.storage table<"cache"|"home", string>
---@alias lc.storage.format_opts { id : string, slug : string, ft_alt : string, ft : string }
---@alias lc.storage { cache: string, home: string, format: fun(opts: lc.storage.format_opts): string | nil }

---@alias lc.picker { provider?: "fzf-lua" | "telescope" }

Expand All @@ -58,6 +59,9 @@ local M = {
storage = {
home = vim.fn.stdpath("data") .. "/leetcode",
cache = vim.fn.stdpath("cache") .. "/leetcode",
format = function()
return nil
end,
},

---@type table<string, boolean>
Expand Down