windwp / nvim-ts-autotag

Use treesitter to auto close and auto rename html tag

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Only working with html files

madjxatw opened this issue · comments

Both nvim-treesitter and nvim-ts-autotag have been properly configured, but it only works with html files. I couldn't manage to make it work with xml. Is this the issue of nvim-ts-autotag itself or the treesitter xml parser? nvim-treesitter started to officially deliver xml parser not long ago, so it could be buggy?

Inspecting the source, an XML file uses the same tag configuration as an HTML file: https://github.com/windwp/nvim-ts-autotag/blob/main/lua/nvim-ts-autotag/internal.lua#L36.

It's possible that the Treesitter grammar changed since this was first published, in any case, XML files use a different set of Treesitter node names than the HTML grammar. This can be verified using :TSPlaygroundToggle on both files.

I was able to get it basically working by adding the following in lua/nvim-ts-autotag/internal.lua. That lives here on my machine: ~/.local/share/nvim/lazy/nvim-ts-autotag/lua/nvim-ts-autotag/internal.lua but I'm sure depends on your package manager and OS. This strategy works to prove the concept but will be removed on update I believe, so you might want to either fork or use a local copy until it makes it onto main.

  1. Remove 'xml', from the local HTML_TAG.filetypes table, circa L36.
  2. Insert a local XML_TAG table like this directly beneath the HTML_TAG table:
    -- stylua: ignore
    local XML_TAG = {
        filetypes              = {'xml'},
        start_tag_pattern      = 'STag',
        start_name_tag_pattern = 'Name',
        end_tag_pattern        = 'ETag',
        end_name_tag_pattern   = 'Name',
        close_tag_pattern      = 'ETag',
        close_name_tag_pattern = 'Name',
        element_tag            = 'element',
        skip_tag_pattern       = {},
    }
  3. Add this table to the local all_tag table circa 106:
local all_tag = {
    HBS_TAG,
    SVELTE_TAG,
    JSX_TAG,
    XML_TAG,    --<<<- here
}

Exit, restart, and Bob's your uncle.

Verify the plugin is working HTML before doing this so you're sure it's being loaded correctly and just not working for XML.

I wasn't sure what to put for close_tag_pattern and close_name_tag_pattern so I just used the same values as the end_tag_pattern and end_name_tag_pattern items. The XML parser doesn't seem to have those differentiated "unmatched close" nodes. I could see them in the HTML but nothing corresponding in the XML TSPlayground output.

@windwp happy to put this in a PR if you're open to it, just let me know.

any update on this ? 😄

Inspecting the source, an XML file uses the same tag configuration as an HTML file: https://github.com/windwp/nvim-ts-autotag/blob/main/lua/nvim-ts-autotag/internal.lua#L36.

It's possible that the Treesitter grammar changed since this was first published, in any case, XML files use a different set of Treesitter node names than the HTML grammar. This can be verified using :TSPlaygroundToggle on both files.

I was able to get it basically working by adding the following in lua/nvim-ts-autotag/internal.lua. That lives here on my machine: ~/.local/share/nvim/lazy/nvim-ts-autotag/lua/nvim-ts-autotag/internal.lua but I'm sure depends on your package manager and OS. This strategy works to prove the concept but will be removed on update I believe, so you might want to either fork or use a local copy until it makes it onto main.

  1. Remove 'xml', from the local HTML_TAG.filetypes table, circa L36.
  2. Insert a local XML_TAG table like this directly beneath the HTML_TAG table:
    -- stylua: ignore
    local XML_TAG = {
        filetypes              = {'xml'},
        start_tag_pattern      = 'STag',
        start_name_tag_pattern = 'Name',
        end_tag_pattern        = 'ETag',
        end_name_tag_pattern   = 'Name',
        close_tag_pattern      = 'ETag',
        close_name_tag_pattern = 'Name',
        element_tag            = 'element',
        skip_tag_pattern       = {},
    }
  3. Add this table to the local all_tag table circa 106:
local all_tag = {
    HBS_TAG,
    SVELTE_TAG,
    JSX_TAG,
    XML_TAG,    --<<<- here
}

Exit, restart, and Bob's your uncle.

Verify the plugin is working HTML before doing this so you're sure it's being loaded correctly and just not working for XML.

I wasn't sure what to put for close_tag_pattern and close_name_tag_pattern so I just used the same values as the end_tag_pattern and end_name_tag_pattern items. The XML parser doesn't seem to have those differentiated "unmatched close" nodes. I could see them in the HTML but nothing corresponding in the XML TSPlayground output.

@windwp happy to put this in a PR if you're open to it, just let me know.

This solution is working for xml files. But In my case, I want this package to work on my tsx files. It is working fine on jsx files though. Any idea how to solve this?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.