From c422fc7d5c7af7cbac2f064fb315b9d7b75692be Mon Sep 17 00:00:00 2001 From: numToStr Date: Thu, 16 Jun 2022 14:14:06 +0530 Subject: [PATCH] fix: invalid comment when element and attr is on the same line --- lua/Comment/jsx.lua | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lua/Comment/jsx.lua b/lua/Comment/jsx.lua index a5e9b98..d656997 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