diff --git a/lua/Comment/jsx.lua b/lua/Comment/jsx.lua index a5e9b981..d6569978 100644 --- a/lua/Comment/jsx.lua +++ b/lua/Comment/jsx.lua @@ -82,19 +82,26 @@ local function capture(parser, range) local Q = vim.treesitter.query.parse_query(lang, query) - local id, lines = 0, nil + local id, lnum, lines = 0, nil, nil for _, tree in ipairs(parser:trees()) do for _, section in pairs(normalize(Q, tree, parser, range)) do if section.range.srow <= range.srow - 1 and section.range.erow >= range.erow - 1 then local region = section.range.erow - section.range.srow if not lines or region < lines then - id, lines = section.id, region + id, lnum, lines = section.id, section.range.srow, region end end end end + -- NOTE: + -- This is for the case when the opening element and attributes are on the same line, + -- so to prevent invalid comment, we can check if the line looks like an element + if lnum ~= nil and string.match(vim.fn.getline(lnum + 1), '%s+<%w+%s.*>$') then + return true + end + return Q.captures[id] == 'jsx' end