diff --git a/lua/nvim-ts-autotag/internal.lua b/lua/nvim-ts-autotag/internal.lua index 2459624..98ce103 100644 --- a/lua/nvim-ts-autotag/internal.lua +++ b/lua/nvim-ts-autotag/internal.lua @@ -252,6 +252,9 @@ M.close_tag = function() if result == true and tag_name ~= nil then vim.api.nvim_put({ string.format("%s>", tag_name) }, "", true, false) vim.cmd([[normal! F>]]) + elseif utils.is_react_file() and utils.is_react_fragment() then + vim.api.nvim_put({ ">" }, "", true, false) + vim.cmd([[normal! F>]]) end end diff --git a/lua/nvim-ts-autotag/utils.lua b/lua/nvim-ts-autotag/utils.lua index ab5e276..6b5fbdc 100644 --- a/lua/nvim-ts-autotag/utils.lua +++ b/lua/nvim-ts-autotag/utils.lua @@ -2,6 +2,65 @@ local log = require("nvim-ts-autotag._log") local get_node_text = vim.treesitter.get_node_text local M = {} +---@return boolean +function M.is_react_file() + local ft = vim.bo.ft + -- check filetypes first. + if ft == "javascriptreact" or ft == "typescriptreact" then + return true + elseif ft ~= "javascript" then + return false + end + -- If we are in a `javascript` file, then check the content to see if the + -- current file counts as a react file + 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() + local queries = { "jsx_element", "jsx_self_closing_element" } + + for _, query in ipairs(queries) do + if M.node_exists(root, query) then + return true + end + end + + return false +end + +---@return boolean +function M.is_react_fragment() + local line = vim.fn.getline(".") + local col = vim.fn.col(".") - 2 + local strpart = vim.fn.strpart(line, col) + local char_at_cursor = vim.fn.strcharpart(strpart, 0, 1) ---@type string + return char_at_cursor == "<" +end + +---@param node TSNode +---@param query string +---@return boolean +function M.node_exists(node, query) + if node:type() == query then + return true + end + + for child in node:iter_children() do + if M.node_exists(child, query) then + return true + end + end + + return false +end + M.get_node_text = function(node) local _, txt = pcall(get_node_text, node, vim.api.nvim_get_current_buf()) return vim.split(txt, "\n") or {} diff --git a/sample/index.js b/sample/index.js new file mode 100644 index 0000000..072cb5d --- /dev/null +++ b/sample/index.js @@ -0,0 +1,23 @@ + +import React, { useCallback, useEffect } from 'react' + +const SamplePage = () => { + const [state, setstate] = useState(initialState) + + + return ( +