Skip to content

Commit dd60ffa

Browse files
committed
3rd draft
1 parent 14cd8b5 commit dd60ffa

File tree

3 files changed

+113
-65
lines changed

3 files changed

+113
-65
lines changed

a.jsx

+51-34
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
function Yo() {
2-
return (
3-
<>
4-
<div>hello</div>
5-
</>
6-
);
7-
}
8-
91
const Yoo = () => {
102
return (
113
<div>
124
<section>
135
<p>hello</p>
146
<p
7+
he="llo"
8+
wor="ld"
159
attr={{
1610
...window,
1711
hello: () => {
@@ -25,33 +19,56 @@ const Yoo = () => {
2519
>
2620
hello
2721
</p>
22+
<p>{true ? "true" : "false"}</p>
23+
<p>{true && "true"}</p>
24+
<p>
25+
{true && (
26+
<section>
27+
<p>This is awesome</p>
28+
</section>
29+
)}
30+
</p>
31+
<div id="div">
32+
{getUser({
33+
name: "numToStr",
34+
job: "making plugins",
35+
})}
36+
</div>
2837
</section>
2938
</div>
3039
);
3140
};
32-
33-
class Yooo {
34-
render() {
35-
return (
36-
<>
37-
<div>hello</div>
38-
</>
39-
);
40-
}
41-
}
42-
43-
const Yoooo = () => (
44-
<>
45-
<div>hello</div>
46-
</>
47-
);
48-
49-
const Yoooo = () => (
50-
<section>
51-
<div>hello</div>
52-
</section>
53-
);
54-
55-
function Yoooo() {
56-
return <div>hello</div>;
57-
}
41+
//
42+
// const Yoooo = () => (
43+
// <section>
44+
// <div>hello</div>
45+
// </section>
46+
// );
47+
//
48+
// function Yo() {
49+
// return (
50+
// <>
51+
// <div>hello</div>
52+
// </>
53+
// );
54+
// }
55+
//
56+
// class Yooo {
57+
// render() {
58+
// return (
59+
// <>
60+
// <div>hello</div>
61+
// </>
62+
// );
63+
// }
64+
// }
65+
//
66+
// const Yoooo = () => (
67+
// <>
68+
// <div>hello</div>
69+
// </>
70+
// );
71+
//
72+
// function Yoooo() {
73+
// return <div>hello</div>;
74+
// }

a.md

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ const Yoo = () => {
3232
>
3333
hello
3434
</p>
35+
<div>
36+
{getUser({
37+
name: "numToStr",
38+
job: "making plugins",
39+
})}
40+
</div>
3541
</section>
3642
</div>
3743
);

lua/Comment/jsx.lua

+56-31
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,57 @@
11
local J = {
22
comment = '{/*%s*/}',
3-
valid = { 'jsx_element', 'jsx_fragment', 'jsx_text', '<', '>' },
43
}
54

6-
local function is_jsx_tree(lang)
5+
local query = [[
6+
; If somehow we can group all the attributes into one
7+
(jsx_opening_element [(jsx_attribute) (comment)] @nojsx)
8+
9+
; If somehow we can group all the comments into one
10+
(jsx_expression (comment)) @jsx
11+
12+
(jsx_expression
13+
[(object) (call_expression)] @nojsx)
14+
15+
(parenthesized_expression
16+
[(jsx_fragment) (jsx_element)] @jsx)
17+
18+
(return_statement
19+
[(jsx_fragment) (jsx_element)] @jsx)
20+
]]
21+
22+
local function is_jsx(lang)
723
-- Name of the treesitter parsers that supports jsx syntax
824
return lang == 'tsx' or lang == 'javascript'
925
end
1026

11-
local function is_jsx_node(node)
12-
if not node then
13-
return false
14-
end
15-
return vim.tbl_contains(J.valid, node:type())
16-
end
17-
18-
local function capture(child, range)
19-
local lang = child:lang()
27+
local function capture(parser, range)
28+
local lang = parser:lang()
2029

21-
local rng = {
22-
range.srow - 1,
23-
range.scol,
24-
range.erow - 1,
25-
range.ecol,
26-
}
27-
28-
if not (is_jsx_tree(lang) and child:contains(rng)) then
30+
if not is_jsx(lang) then
2931
return
3032
end
3133

32-
for _, tree in ipairs(child:trees()) do
33-
local root = tree:root()
34-
local node = root:descendant_for_range(unpack(rng))
35-
local srow, _, erow = node:range()
36-
if srow <= range.srow - 1 and erow >= range.erow - 1 then
37-
local nxt, prev = node:next_sibling(), node:prev_sibling()
38-
if is_jsx_node(prev) or is_jsx_node(node) or is_jsx_node(nxt) then
39-
return J.comment
34+
local Q = vim.treesitter.query.parse_query(lang, query)
35+
36+
local lines, group
37+
38+
for _, tree in ipairs(parser:trees()) do
39+
for id, node in Q:iter_captures(tree:root(), parser:source(), range.srow - 1, range.erow) do
40+
local srow, _, erow = node:range()
41+
-- print(Q.captures[id])
42+
-- print(srow, range.srow - 1)
43+
-- print(erow, range.erow - 1)
44+
-- print(srow <= range.srow - 1 and erow >= range.erow - 1)
45+
if srow <= range.srow - 1 and erow >= range.erow - 1 then
46+
local region = erow - srow
47+
if not lines or region < lines then
48+
lines, group = region, Q.captures[id]
49+
end
4050
end
4151
end
4252
end
53+
54+
return group == 'jsx' and J.comment
4355
end
4456

4557
function J.calculate(ctx)
@@ -49,14 +61,27 @@ function J.calculate(ctx)
4961
return
5062
end
5163

64+
local rng = {
65+
ctx.range.srow - 1,
66+
ctx.range.scol,
67+
ctx.range.erow - 1,
68+
ctx.range.ecol,
69+
}
70+
71+
-- This is for `markdown` which embeds multiple `tsx` blocks
5272
for _, child in pairs(P:children()) do
53-
local captured = capture(child, ctx.range)
54-
if captured then
55-
return captured
73+
if child:contains(rng) then
74+
local captured = capture(child, ctx.range)
75+
if captured then
76+
return captured
77+
end
5678
end
5779
end
5880

59-
return capture(P, ctx.range)
81+
if P:contains(rng) then
82+
-- This is for `tsx` itself
83+
return capture(P, ctx.range)
84+
end
6085
end
6186

6287
return J

0 commit comments

Comments
 (0)