Skip to content

Commit cd53cfd

Browse files
committed
1st draft
1 parent 0aaea32 commit cd53cfd

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed

a.jsx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
function Yo() {
2+
return (
3+
<>
4+
<div>hello</div>
5+
</>
6+
);
7+
}
8+
9+
const Yoo = () => {
10+
return (
11+
<div>
12+
<section>
13+
<p>hello</p>
14+
<p
15+
attr={{
16+
...window,
17+
hello: () => {
18+
return (
19+
<section>
20+
<p>IDK</p>
21+
</section>
22+
);
23+
},
24+
}}
25+
>
26+
hello
27+
</p>
28+
</section>
29+
</div>
30+
);
31+
};
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+
}

a.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# this is markdown
2+
3+
```javascript
4+
function Yo() {
5+
return (
6+
<>
7+
<div>hello</div>
8+
</>
9+
);
10+
}
11+
```
12+
13+
```javascript
14+
const Yoo = () => {
15+
return (
16+
<div>
17+
<section>
18+
<p>hello</p>
19+
<p
20+
attr={{
21+
...window,
22+
hello: () => {
23+
return (
24+
<section>
25+
<p>IDK</p>
26+
</section>
27+
);
28+
},
29+
}}
30+
>
31+
hello
32+
</p>
33+
</section>
34+
</div>
35+
);
36+
};
37+
```
38+
39+
```tsx
40+
const Yoooo = () => (
41+
<>
42+
<div>hello</div>
43+
</>
44+
);
45+
```
46+
47+
```tsx
48+
const Yoooo = () => (
49+
<section>
50+
<div>hello</div>
51+
</section>
52+
);
53+
```
54+
55+
```tsx
56+
function Yo() {
57+
return <div>hello</div>;
58+
}
59+
```
60+
61+
```tsx
62+
function Yo() {
63+
return <></>;
64+
}
65+
```
66+
67+
```tsx
68+
class Yooo {
69+
render() {
70+
return (
71+
<>
72+
<div>hello</div>
73+
</>
74+
);
75+
}
76+
}
77+
```
78+
79+
<!-- This is block is just for testing -->
80+
81+
```lua
82+
local a = 123
83+
```

lua/Comment/ft.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ local L = {
7979
template = { M.double_hash },
8080
tmux = { M.hash },
8181
toml = { M.hash },
82+
tsx = { M.cxx_l, M.cxx_b }, -- Alias for `typescriptreact` bcz `tsx` is the parser's name
8283
typescript = { M.cxx_l, M.cxx_b },
8384
typescriptreact = { M.cxx_l, M.cxx_b },
8485
vim = { '"%s' },

lua/Comment/jsx.lua

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
local J = {
2+
comment = '{/*%s*/}',
3+
}
4+
5+
local query = [[
6+
(parenthesized_expression
7+
[(jsx_fragment) (jsx_element)] @jsx)
8+
9+
(return_statement
10+
[(jsx_fragment) (jsx_element)] @jsx)
11+
]]
12+
13+
local function is_jsx(lang)
14+
-- Name of the treesitter parsers that supports jsx syntax
15+
return lang == 'tsx' or lang == 'javascript'
16+
end
17+
18+
local function capture(child, range)
19+
local lang = child:lang()
20+
21+
if not is_jsx(lang) then
22+
return
23+
end
24+
25+
local Q = vim.treesitter.query.parse_query(lang, query)
26+
27+
for _, tree in ipairs(child:trees()) do
28+
for _, node in Q:iter_captures(tree:root(), child:source()) do
29+
local srow, _, erow = node:range()
30+
-- Why subtracting 2?
31+
-- 1. Lua indexes starts from 1
32+
-- 2. Removing the `return` keyword from the range
33+
if srow <= range.srow - 2 and erow >= range.erow then
34+
return J.comment
35+
end
36+
end
37+
end
38+
end
39+
40+
function J.calculate(ctx)
41+
local ok, P = pcall(vim.treesitter.get_parser)
42+
43+
if not ok then
44+
return
45+
end
46+
47+
for _, child in pairs(P:children()) do
48+
local captured = capture(child, ctx.range)
49+
if captured then
50+
return captured
51+
end
52+
end
53+
54+
return capture(P, ctx.range)
55+
end
56+
57+
return J

0 commit comments

Comments
 (0)