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 winner = {}
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 lines = erow - srow
47
+ if not winner .lines or lines < winner .lines then
48
+ winner .lines = lines
49
+ winner .name = Q .captures [id ]
50
+ end
40
51
end
41
52
end
42
53
end
54
+
55
+ return winner .name == ' jsx' and J .comment
43
56
end
44
57
45
58
function J .calculate (ctx )
@@ -49,14 +62,27 @@ function J.calculate(ctx)
49
62
return
50
63
end
51
64
65
+ local rng = {
66
+ ctx .range .srow - 1 ,
67
+ ctx .range .scol ,
68
+ ctx .range .erow - 1 ,
69
+ ctx .range .ecol ,
70
+ }
71
+
72
+ -- This is for `markdown` which embeds multiple `tsx` blocks
52
73
for _ , child in pairs (P :children ()) do
53
- local captured = capture (child , ctx .range )
54
- if captured then
55
- return captured
74
+ if child :contains (rng ) then
75
+ local captured = capture (child , ctx .range )
76
+ if captured then
77
+ return captured
78
+ end
56
79
end
57
80
end
58
81
59
- return capture (P , ctx .range )
82
+ if P :contains (rng ) then
83
+ -- This is for `tsx` itself
84
+ return capture (P , ctx .range )
85
+ end
60
86
end
61
87
62
88
return J
0 commit comments