Skip to content

Commit 14cd8b5

Browse files
committed
2st draft
1 parent cd53cfd commit 14cd8b5

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

a.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const Yoo = () => {
1717
<section>
1818
<p>hello</p>
1919
<p
20+
he="llo"
21+
wor="ld"
2022
attr={{
2123
...window,
2224
hello: () => {

lua/Comment/jsx.lua

+23-18
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
11
local J = {
22
comment = '{/*%s*/}',
3+
valid = { 'jsx_element', 'jsx_fragment', 'jsx_text', '<', '>' },
34
}
45

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)
6+
local function is_jsx_tree(lang)
147
-- Name of the treesitter parsers that supports jsx syntax
158
return lang == 'tsx' or lang == 'javascript'
169
end
1710

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+
1818
local function capture(child, range)
1919
local lang = child:lang()
2020

21-
if not is_jsx(lang) then
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
2229
return
2330
end
2431

25-
local Q = vim.treesitter.query.parse_query(lang, query)
26-
2732
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
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
3439
return J.comment
3540
end
3641
end

0 commit comments

Comments
 (0)