11local 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'
925end
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
4355end
4456
4557function 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
6085end
6186
6287return J
0 commit comments