irongit

A Neovim plugin enabling you to solve LeetCode problems.

156 lines3.7 KBLua
1local config = require("leetcode.config")
2local log = require("leetcode.logger")
3
4---@class lc.Utils
5local utils = {}
6
7function utils.deprecate_usr_cmd(name, new)
8 vim.api.nvim_create_user_command(name, function()
9 local cmd = require("leetcode.command")
10 cmd.deprecate(name, new)
11 pcall(vim.cmd, new) ---@diagnostic disable-line
12 end, {})
13end
14
15---@param fn string
16function utils.cmd(fn)
17 return ("<cmd>lua require('leetcode.command').%s()<cr>"):format(fn)
18end
19
20---@param title_slug string
21---@param lang lc.lang
22function utils.detect_duplicate_question(title_slug, lang)
23 local tabs = utils.question_tabs()
24
25 for _, q in ipairs(tabs) do
26 if title_slug == q.question.q.title_slug and lang == q.question.lang then
27 return q.tabpage
28 end
29 end
30end
31
32---@return { tabpage: integer, question: lc.ui.Question }[]
33function utils.question_tabs()
34 local questions = {}
35
36 for _, q in ipairs(_Lc_state.questions) do
37 local tabp = utils.question_tabp(q)
38 if tabp then
39 table.insert(questions, { tabpage = tabp, question = q })
40 end
41 end
42
43 return questions
44end
45
46---@param q lc.ui.Question
47---@return integer|nil
48function utils.question_tabp(q)
49 local ok, tabp = pcall(vim.api.nvim_win_get_tabpage, q.winid)
50 if ok then
51 return tabp
52 end
53end
54
55---@return lc.ui.Question
56function utils.curr_question()
57 local tabp = vim.api.nvim_get_current_tabpage()
58 local tabs = utils.question_tabs()
59
60 local tab = vim.tbl_filter(function(tab)
61 return tab.tabpage == tabp
62 end, tabs)[1] or {}
63 if tab.question then
64 return tab.question, tabp
65 else
66 log.error("No current question found")
67 end
68end
69
70---@return lc.language
71function utils.get_lang(slug)
72 ---@param l lc.language
73 return vim.tbl_filter(function(l)
74 return l.slug == slug
75 end, config.langs)[1]
76end
77
78---@return lc.language
79function utils.get_lang_by_name(name)
80 ---@param l lc.language
81 return vim.tbl_filter(function(l)
82 return l.lang == name
83 end, config.langs)[1]
84end
85
86---@param event lc.hook
87---@return fun()[]|nil
88function utils.get_hooks(event)
89 local fns = config.user.hooks[event]
90 if not fns then
91 return
92 end
93
94 if type(fns) == "function" then
95 fns = { fns }
96 end
97
98 return vim.list_extend(fns, config.hooks[event] or {})
99end
100
101---@param event lc.hook
102function utils.exec_hooks(event, ...)
103 local fns = utils.get_hooks(event)
104 if not fns then
105 return log.error("unknown hook event: " .. event)
106 end
107
108 for i, fn in ipairs(fns) do
109 local ok, msg = pcall(fn, ...)
110 if not ok then
111 log.error(("bad hook #%d in `%s` event: %s"):format(i, event, msg))
112 end
113 end
114end
115
116---@param content string
117---@param translated_content string
118function utils.translate(content, translated_content)
119 if config.is_cn then
120 if config.user.cn.translate_problems then
121 return translated_content or content
122 else
123 return content or translated_content
124 end
125 else
126 return content
127 end
128end
129
130function utils.auth_guard()
131 if not config.auth.is_signed_in then
132 error("User not logged-in", 0)
133 end
134end
135
136function utils.norm_ins(str)
137 local ins = vim.inspect(str)
138 return ins:sub(2, #ins - 1)
139end
140
141function utils.with_version(v, with, without)
142 with = with or function() end
143 without = without or function() end
144 return (vim.fn.has("nvim-" .. v) == 1 and with or without)()
145end
146
147---@param name lc.editor.section
148---@param start? boolean
149function utils.section_tag(name, start)
150 if name == "code" then
151 return ("@leet %s"):format(start and "start" or "end")
152 end
153 return ("@leet %s %s"):format(name, start and "start" or "end")
154end
155
156return utils