From f4528df26eb6528fb9af6dcb1b7e36632c25c24a Mon Sep 17 00:00:00 2001 From: "roy.crippen4" Date: Sun, 14 Jul 2024 13:24:34 -0400 Subject: [PATCH] fix(#199): Auto-tag react fragments Detects if `<>` was typed in a react file (`js`, `jsx`, or `tsx`) and autocloses the fragment tag. --- lua/nvim-ts-autotag/internal.lua | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lua/nvim-ts-autotag/internal.lua b/lua/nvim-ts-autotag/internal.lua index 61d0397..123fb04 100644 --- a/lua/nvim-ts-autotag/internal.lua +++ b/lua/nvim-ts-autotag/internal.lua @@ -242,6 +242,42 @@ local function check_close_tag(close_slash_tag) return false end +local function is_react_file() + local ft = vim.bo.ft + -- check filetypes first. + if ft == 'javascriptreact' or 'typescriptreact' then + return true + elseif ft ~= 'javascript' then + return false + end + + local ok, buf_parser = pcall(vim.treesitter.get_parser) + if not ok then + return false + end + + local tree = buf_parser:parse(true) + if not tree then + return false + end + + local root = tree[1]:root() + -- parse the tree for jsx nodes + local query = vim.treesitter.query.parse('javascript', [[ + (jsx_element) + (jsx_self_closing_element) + ]]) + + -- iterate over nodes to find jsx nodes + for _, node in query:iter_captures(root, 0, 0, -1) do + if node then + return true + end + end + return false +end + + M.close_tag = function() local ok, buf_parser = pcall(vim.treesitter.get_parser) if not ok then @@ -252,6 +288,9 @@ M.close_tag = function() if result == true and tag_name ~= nil then vim.api.nvim_put({ string.format("", tag_name) }, "", true, false) vim.cmd([[normal! F>]]) + elseif is_react_file() then + vim.api.nvim_put({ "" }, "", true, false) + vim.cmd([[normal! F>]]) end end