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 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
4051 end
4152 end
4253 end
54+
55+ return winner .name == ' jsx' and J .comment
4356end
4457
4558function J .calculate (ctx )
@@ -49,14 +62,27 @@ function J.calculate(ctx)
4962 return
5063 end
5164
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
5273 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
5679 end
5780 end
5881
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
6086end
6187
6288return J
0 commit comments