Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5a26a58

Browse files
committed
fix: ul, ol tags
1 parent 20fa4b0 commit 5a26a58

File tree

9 files changed

+173
-130
lines changed

9 files changed

+173
-130
lines changed

lua/leetcode-ui/group/tag/init.lua

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
local theme = require("leetcode.theme")
2+
local u = require("leetcode-ui.utils")
23

34
local utils = require("leetcode.parser.utils")
45
local Group = require("leetcode-ui.group")
5-
local Lines = require("leetcode-ui.lines")
6+
local Indent = require("nui.text")
67

78
local ts = vim.treesitter
89

910
local log = require("leetcode.logger")
1011

1112
---@class lc.ui.Tag : lc.ui.Group
1213
---@field name string
13-
---@field tags string[]
14+
---@field tags lc.ui.Tag[]
1415
---@field node TSNode
1516
---@field text string
1617
local Tag = Group:extend("LeetTag")
1718

19+
function Tag:add_indent(item)
20+
if item.class and item.class.name == "LeetLine" then
21+
table.insert(item._texts, 1, Indent("\t", "leetcode_indent"))
22+
return
23+
end
24+
25+
for _, c in ipairs(item:contents()) do
26+
self:add_indent(c)
27+
end
28+
end
29+
1830
function Tag:get_text(node) return ts.get_node_text(node, self.text) end
1931

2032
---@param node TSNode
@@ -64,7 +76,6 @@ function Tag:get_el_data(node)
6476
end
6577
end
6678

67-
-- local res = { tag = tag, attrs = attrs }
6879
return { tag = tag, attrs = attrs }
6980
end
7081

@@ -76,7 +87,7 @@ function Tag:parse_helper() --
7687
if ntype == "text" then
7788
self:append(self:get_text(child))
7889
elseif ntype == "element" then
79-
self:append(Tag:from(self, child))
90+
self:append(self:from(child))
8091
elseif ntype == "entity" then
8192
local text = self:get_text(child)
8293

@@ -108,10 +119,6 @@ end
108119

109120
local function req_tag(str) return require("leetcode-ui.group.tag." .. str) end
110121

111-
-- ---@param node TSNode
112-
-- function Tag:from(node)
113-
-- end
114-
115122
function Tag:contents()
116123
local items = Tag.super.contents(self)
117124

@@ -122,60 +129,58 @@ function Tag:contents()
122129
return items
123130
end
124131

125-
---@param text string
126-
---@param opts lc.ui.opts
127-
---@param node TSNode
128-
---@param tags string[]
129-
---@param tag string
130-
function Tag:init(text, opts, node, tags, tag) --
131-
Tag.super.init(self, {}, opts or {})
132-
133-
self.text = text
134-
self.node = node
135-
self.tags = tags
136-
self.name = tag
137-
138-
log.info(tag)
139-
140-
self:parse_helper()
141-
end
142-
143-
---@type fun(text: string, opts: lc.ui.opts, node?: TSNode, tags: string[], tag?: string): lc.ui.Tag
144-
local LeetTag = Tag
145-
146-
---@param tag lc.ui.Tag
147132
---@param node TSNode
148-
function Tag.static:from(tag, node)
149-
local t = {
133+
function Tag:from(node)
134+
local tbl = {
150135
pre = req_tag("pre"),
151-
ul = req_tag("ul"),
152-
ol = req_tag("ol"),
136+
ul = req_tag("list.ul"),
137+
ol = req_tag("list.ol"),
138+
li = req_tag("li"),
153139
img = req_tag("img"),
154140
a = req_tag("a"),
155141
}
156142

157-
local el = tag:get_el_data(node)
158-
local tags = tag.tags
143+
local tags = self.tags
144+
local el = self:get_el_data(node)
159145

160-
-- log.info(tag.name)
146+
table.insert(tags, self)
147+
local parsed = (tbl[el.tag] or Tag)(self.text, {}, node, tags)
148+
table.remove(tags)
149+
150+
return parsed
151+
end
152+
153+
---@param text string
154+
---@param opts lc.ui.opts
155+
---@param node TSNode
156+
---@param tags lc.ui.Tag[]
157+
function Tag:init(text, opts, node, tags) --
158+
self.text = text
159+
self.node = node
160+
self.tags = tags
161161

162-
table.insert(tags, el.tag)
162+
self.data = self:get_el_data(node)
163+
self.name = self.data.tag
163164

164-
local opts = { hl = theme.get_dynamic(tags) }
165-
local parsed = (t[tags[#tags]] or LeetTag)(tag.text, opts, node, tags, el.tag)
165+
opts = vim.tbl_extend("force", {
166+
hl = utils.hl(self),
167+
}, opts or {})
166168

167-
table.remove(tags)
169+
Tag.super.init(self, {}, opts)
168170

169-
return parsed
171+
self:parse_helper()
170172
end
171173

174+
---@type fun(text: string, opts: lc.ui.opts, node: TSNode, tags: lc.ui.Tag[]): lc.ui.Tag
175+
local LeetTag = Tag
176+
172177
---@param text string
173178
function Tag.static:parse(text) --
174179
local ok, parser = pcall(ts.get_string_parser, text, "html")
175180
assert(ok, parser)
176181
local root = parser:parse()[1]:root()
177182

178-
return LeetTag(text, { spacing = 3, hl = "leetcode_normal" }, root, {}, nil)
183+
return LeetTag(text, { spacing = 3, hl = "leetcode_normal" }, root, {})
179184
end
180185

181186
return LeetTag

lua/leetcode-ui/group/tag/li.lua

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
local Tag = require("leetcode-ui.group.tag")
2+
local Lines = require("leetcode-ui.lines")
3+
local Text = require("nui.text")
4+
local List = require("leetcode-ui.group.tag.list")
5+
6+
local u = require("leetcode-ui.utils")
7+
local log = require("leetcode.logger")
8+
9+
---@class lc.ui.Tag.li : lc.ui.Tag
10+
---@field indent string
11+
local Li = Tag:extend("LeetTagUl")
12+
13+
function Li:contents()
14+
local items = Li.super.contents(self)
15+
16+
table.insert(items, 1, Text(self.pre, "leetcode_list"))
17+
18+
return items
19+
end
20+
21+
function Li:init(text, opts, node, tags)
22+
Li.super.init(self, text, opts, node, tags)
23+
24+
self.pre = ""
25+
26+
local r = #self.tags
27+
while r >= 1 and not u.is_instance(self.tags[r], List) do
28+
r = r - 1
29+
end
30+
31+
local parent = self.tags[r]
32+
33+
if parent then
34+
if parent.name == "ol" then log.debug(parent) end
35+
36+
if parent.order then
37+
self.pre = parent.order .. ". "
38+
parent.order = parent.order + 1
39+
else
40+
self.pre = "* "
41+
end
42+
end
43+
end
44+
45+
---@type fun(): lc.ui.Tag.pre
46+
local LeetTagUl = Li
47+
48+
return LeetTagUl
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local Tag = require("leetcode-ui.group.tag")
2+
3+
---@class lc.ui.Tag.List : lc.ui.Tag
4+
---@field indent integer
5+
local List = Tag:extend("LeetTagList")
6+
7+
function List:contents()
8+
local items = List.super.contents(self)
9+
10+
for _, item in ipairs(items) do
11+
self:add_indent(item)
12+
end
13+
14+
return items
15+
end
16+
17+
---@type fun(): lc.ui.Tag.List
18+
local LeetTagList = List
19+
20+
return LeetTagList

lua/leetcode-ui/group/tag/list/ol.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
local List = require("leetcode-ui.group.tag.list")
2+
3+
local log = require("leetcode.logger")
4+
5+
---@class lc.ui.Tag.ol : lc.ui.Tag.List
6+
---@field order integer
7+
local Ol = List:extend("LeetTagOl")
8+
9+
function Ol:init(text, opts, node, tags)
10+
self.order = 1
11+
log.info("ol")
12+
13+
Ol.super.init(self, text, opts, node, tags)
14+
end
15+
16+
---@type fun(): lc.ui.Tag.ol
17+
local LeetTagOl = Ol
18+
19+
return LeetTagOl

lua/leetcode-ui/group/tag/list/ul.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
local List = require("leetcode-ui.group.tag.list")
2+
3+
local log = require("leetcode.logger")
4+
5+
---@class lc.ui.Tag.ul : lc.ui.Tag.List
6+
local Ul = List:extend("LeetTagUl")
7+
8+
function Ul:init(text, opts, node, tags)
9+
log.info("ul")
10+
11+
Ul.super.init(self, text, opts, node, tags)
12+
end
13+
14+
---@type fun(): lc.ui.Tag.ul
15+
local LeetTagUl = Ul
16+
17+
return LeetTagUl

lua/leetcode-ui/group/tag/ol.lua

Lines changed: 0 additions & 52 deletions
This file was deleted.

lua/leetcode-ui/group/tag/ul.lua

Lines changed: 0 additions & 27 deletions
This file was deleted.

lua/leetcode/logger/init.lua

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ local logger = {}
1010

1111
local function normalize(msg) return type(msg) == "string" and t(msg) or vim.inspect(msg) end
1212

13-
---@private
14-
---@param msg any
15-
---@param lvl? integer
16-
---@return any
17-
logger.log = vim.schedule_wrap(function(msg, lvl)
13+
-- ---@private
14+
-- ---@param msg any
15+
-- ---@param lvl? integer
16+
-- ---@return any
17+
-- logger.log = vim.schedule_wrap(function(msg, lvl)
18+
-- end)
19+
20+
function logger.log(msg, lvl)
1821
if not config.user.logging then return end
1922

2023
local title = config.name
@@ -24,7 +27,7 @@ logger.log = vim.schedule_wrap(function(msg, lvl)
2427
if lvl == lvls.DEBUG then msg = debug.traceback(msg .. "\n") end
2528

2629
vim.notify(msg, lvl, { title = title })
27-
end)
30+
end
2831

2932
---@param msg any
3033
logger.info = function(msg) logger.log(msg) end

lua/leetcode/parser/utils.lua

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,18 @@ function utils.entity(entity)
5353
end
5454
end
5555

56-
---@param tags string[]
56+
---@param tag lc.ui.Tag
5757
---@return string
58-
function utils.hl(tags) return theme.get_dynamic(vim.deepcopy(tags)) end
58+
function utils.hl(tag) --
59+
local tag_names = {}
60+
61+
for _, v in ipairs(tag.tags) do
62+
if v.name then table.insert(tag_names, v.name) end
63+
end
64+
65+
if tag.name then table.insert(tag_names, tag.name) end
66+
67+
return theme.get_dynamic(tag_names)
68+
end
5969

6070
return utils

0 commit comments

Comments
 (0)