Skip to content

Commit 0ec2245

Browse files
committed
fix: invalid comment when element and attr is on the same line
1 parent 4f28496 commit 0ec2245

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

lua/Comment/jsx.lua

+9-2
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,26 @@ local function capture(parser, range)
8282

8383
local Q = vim.treesitter.query.parse_query(lang, query)
8484

85-
local id, lines = 0, nil
85+
local id, lnum, lines = 0, nil, nil
8686

8787
for _, tree in ipairs(parser:trees()) do
8888
for _, section in pairs(normalize(Q, tree, parser, range)) do
8989
if section.range.srow <= range.srow - 1 and section.range.erow >= range.erow - 1 then
9090
local region = section.range.erow - section.range.srow
9191
if not lines or region < lines then
92-
id, lines = section.id, region
92+
id, lnum, lines = section.id, section.range.srow, region
9393
end
9494
end
9595
end
9696
end
9797

98+
-- NOTE:
99+
-- This is for the case when the opening element and attributes are on the same line,
100+
-- so to prevent invalid comment, we can check if the line looks like an element
101+
if lnum ~= nil and string.match(vim.fn.getline(lnum + 1), '%s+<%w+%s.*>$') then
102+
return true
103+
end
104+
98105
return Q.captures[id] == 'jsx'
99106
end
100107

0 commit comments

Comments
 (0)