irongit

A Neovim plugin enabling you to solve LeetCode problems.

chore: merge with master

kawreauthored
parent 4ef4e08parent cd90b3ecommit 8c8801f6af115046c3b492dff0810b99b933d6c1Browse files

8 files changed, +76 -16

+2-1.neoconf.json
@@ -7,7 +7,8 @@
77 "telescope.nvim",
88 "plenary.nvim",
99 "nui.nvim",
10- "nvim-web-devicons"
10+ "nvim-web-devicons",
11+ "image.nvim"
1112 ]
1213 }
1314 }
+16-1README.md
@@ -49,12 +49,12 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/309088c9-4ff7-4dff-ab61-a
4949 "kawre/leetcode.nvim",
5050 build = ":TSUpdate html",
5151 dependencies = {
52- "nvim-treesitter/nvim-treesitter",
5352 "nvim-telescope/telescope.nvim",
5453 "nvim-lua/plenary.nvim", -- required by telescope
5554 "MunifTanjim/nui.nvim",
5655
5756 -- optional
57+ "nvim-treesitter/nvim-treesitter",
5858 "rcarriga/nvim-notify",
5959 "nvim-tree/nvim-web-devicons",
6060 },
@@ -127,6 +127,9 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat
127127 ---@type fun(question: { lang: string })[]
128128 LeetQuestionNew = {},
129129 },
130+
131+ ---@type boolean
132+ image_support = false, -- setting this to `true` will disable question description wrap
130133 }
131134 ```
132135
@@ -193,6 +196,15 @@ hooks = {
193196 },
194197 ```
195198
199+### image support
200+
201+Whether to render question description images using [image.nvim]
202+
203+```lua
204+---@type boolean
205+image_support = false, -- setting this to `true` will disable question description wrap
206+```
207+
196208 ## 📋 Commands
197209
198210 ### `Leet` opens menu dashboard
@@ -268,10 +280,12 @@ local leet_arg = "leetcode.nvim"
268280
269281 return {
270282 "kawre/leetcode.nvim",
283+ ...
271284 lazy = leet_arg ~= vim.fn.argv()[1],
272285 opts = {
273286 arg = leet_arg,
274287 },
288+ ...
275289 }
276290 ```
277291
@@ -288,6 +302,7 @@ return {
288302
289303 - [alpha-nvim](https://github.com/goolord/alpha-nvim)
290304
305+[image.nvim]: https://github.com/3rd/image.nvim
291306 [lazy.nvim]: https://github.com/folke/lazy.nvim
292307 [leetcode]: https://leetcode.com
293308 [leetcode.nvim]: https://github.com/kawre/leetcode.nvim
+2-2lua/leetcode-menu/components/calendar.lua
@@ -44,7 +44,7 @@ function Calendar:handle_res(res)
4444 self.curr_time = os.time({
4545 year = time.year - 1,
4646 month = time.month,
47- day = 1,
47+ day = time.day,
4848 hour = 1,
4949 isdst = false,
5050 })
@@ -129,7 +129,7 @@ function Calendar:handle_weeks(m)
129129 self.calendar_lines[i]:append(" ")
130130 end
131131
132- for _ = 1, get_days_in_month(curr.month, curr.year) do
132+ for _ = curr.day, get_days_in_month(curr.month, curr.year) do
133133 self:handle_weekdays(m)
134134 end
135135
+3-0lua/leetcode/config/template.lua
@@ -84,6 +84,9 @@ local M = {
8484 LeetQuestionNew = {},
8585 },
8686
87+ ---@type boolean
88+ image_support = false, -- setting this to `true` will disable question description wrap
89+
8790 cn = {
8891 translate = true, ---@type boolean
8992 },
+9-0lua/leetcode/parser/html.lua
@@ -165,6 +165,15 @@ function Html:handle_link(text, tag_data)
165165 if attr.name == "src" then return attr end
166166 end, tag_data.attrs)[1] or {}
167167 link = src.value or ""
168+
169+ -- local bufnr = vim.api.nvim_get_current_buf()
170+ -- local image = image_api.from_url(link, {
171+ -- -- buffer = bufnr,
172+ -- -- with_virtual_padding = true,
173+ -- }, function(image)
174+ -- --
175+ -- image:render()
176+ -- end)
168177 end
169178
170179 local line = NuiLine()
+1-0lua/leetcode/pickers/question-tabs.lua
@@ -25,6 +25,7 @@ end
2525
2626 local function display_difficulty(q)
2727 local lang = utils.get_lang(q.lang)
28+ if not lang then return {} end
2829 return { lang.icon, "leetcode_lang_" .. lang.slug }
2930 end
3031
+41-10lua/leetcode/ui/description.lua
@@ -1,4 +1,9 @@
11 local config = require("leetcode.config")
2+local log = require("leetcode.logger")
3+
4+local img_ok, image_api = pcall(require, "image")
5+local img_sup = img_ok and config.user.image_support
6+
27 local parser = require("leetcode.parser")
38 local t = require("leetcode.translator")
49
@@ -16,6 +21,7 @@ local Split = require("nui.split")
1621 ---@field title any
1722 ---@field layout lc-ui.Layout
1823 ---@field visible boolean
24+---@field images table<string, Image>
1925 local description = {}
2026 description.__index = description
2127
@@ -46,7 +52,7 @@ function description:mount()
4652 })
4753 utils.set_win_opts(self.split.winid, {
4854 winhighlight = "Normal:NormalFloat,FloatBorder:FloatBorder",
49- wrap = true,
55+ wrap = not img_sup,
5056 colorcolumn = "",
5157 foldlevel = 999,
5258 foldcolumn = "1",
@@ -58,6 +64,9 @@ function description:mount()
5864 spell = false,
5965 signcolumn = "no",
6066 })
67+ if not img_ok and config.user.image_support then
68+ log.error("image.nvim not found but `image_support` is enabled")
69+ end
6170
6271 self:draw()
6372 self:autocmds()
@@ -75,16 +84,37 @@ function description:toggle()
7584 end
7685
7786 function description:draw()
78- -- local c = vim.api.nvim_win_get_cursor(self.split.winid)
7987 self.layout:draw(self.split)
80- -- vim.api.nvim_win_set_cursor(self.split.winid, c)
88+ self:draw_imgs()
8189 end
8290
83--- function description:redraw()
84--- local c = vim.api.nvim_win_get_cursor(self.split.winid)
85--- self.layout:draw()
86--- vim.api.nvim_win_set_cursor(self.split.winid, c)
87--- end
91+function description:draw_imgs()
92+ if not img_sup then return end
93+
94+ local lines = vim.api.nvim_buf_get_lines(self.split.bufnr, 1, -1, false)
95+ for i, line in ipairs(lines) do
96+ for link in line:gmatch("->%((http[s]?://%S+)%)") do
97+ local img = self.images[link]
98+
99+ if not img then
100+ self.images[link] = {}
101+
102+ image_api.from_url(link, {
103+ buffer = self.split.bufnr,
104+ window = self.split.winid,
105+ with_virtual_padding = true,
106+ }, function(image)
107+ if not image then return end
108+
109+ self.images[link] = image
110+ image:render({ y = i + 1 })
111+ end)
112+ elseif not vim.tbl_isempty(img) then
113+ img:clear(true)
114+ end
115+ end
116+ end
117+end
88118
89119 ---@private
90120 function description:populate()
@@ -147,14 +177,15 @@ function description:init(parent)
147177 focusable = true,
148178 })
149179
150- local obj = setmetatable({
180+ self = setmetatable({
151181 split = split,
152182 parent = parent,
153183 layout = {},
154184 visible = false,
185+ images = {},
155186 }, self)
156187
157- return obj:mount()
188+ return self:mount()
158189 end
159190
160191 return description
+2-2lua/leetcode/utils.lua
@@ -112,14 +112,14 @@ end
112112
113113 ---@return lc.language
114114 function utils.get_lang(slug)
115- return vim.tbl_filter(function(l) return l.slug == slug end, config.langs)[1] or {}
115+ return vim.tbl_filter(function(l) return l.slug == slug end, config.langs)[1]
116116 end
117117
118118 ---@param event lc.hook
119119 function utils.exec_hooks(event, ...)
120120 local log = require("leetcode.logger")
121121 local fns = config.user.hooks[event]
122- if not fns then log.error("Unknown hook event: " .. event) end
122+ if not fns then log.error("unknown hook event: " .. event) end
123123
124124 for i, fn in ipairs(fns) do
125125 local ok, msg = pcall(fn, ...)