1
1
local J = {
2
2
comment = ' {/*%s*/}' ,
3
- valid = { ' jsx_element' , ' jsx_fragment' , ' jsx_text' , ' <' , ' >' },
4
3
}
5
4
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 )
7
23
-- Name of the treesitter parsers that supports jsx syntax
8
24
return lang == ' tsx' or lang == ' javascript'
9
25
end
10
26
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 ()
20
29
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
29
31
return
30
32
end
31
33
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
40
50
end
41
51
end
42
52
end
53
+
54
+ return group == ' jsx' and J .comment
43
55
end
44
56
45
57
function J .calculate (ctx )
@@ -49,14 +61,27 @@ function J.calculate(ctx)
49
61
return
50
62
end
51
63
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
52
72
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
56
78
end
57
79
end
58
80
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
60
85
end
61
86
62
87
return J
0 commit comments