windwp / nvim-ts-autotag

Use treesitter to auto close and auto rename html tag
MIT License
1.63k stars 87 forks source link

Is there a way to treat eruby files as eruby + html just for autotags? #85

Closed nickjj closed 1 year ago

nickjj commented 1 year ago

Hi,

If I open a Rails app that uses erb templates, the file is detected as eruby and everything looks really nice.

However if you type <p> then it will not close the </p>. If you set set ft=html on the file then it looks off but <p> will correctly get closed with </p>. Setting the ft to eruby.html goes back to looking good but autotags don't work.

Is there custom configuration that can be done to say "make it look good by correctly using the correct TS file type but also allow autotags to treat this file as both eruby and html"?

Slotos commented 1 year ago

Try TSInstall embedded_template.

nickjj commented 1 year ago

I ran that command and re-opened nvim, it didn't change the outcome. The file type was detected as eruby and tags didn't get auto-closed.

Is there anything I would need to configure beyond that to use embedded_template?

Slotos commented 1 year ago

Apologies for the long wait. Here's a diff that makes this plugin support embedded_template:

diff --git a/lua/nvim-ts-autotag/internal.lua b/lua/nvim-ts-autotag/internal.lua
index ab3e572..8d1da2c 100644
--- a/lua/nvim-ts-autotag/internal.lua
+++ b/lua/nvim-ts-autotag/internal.lua
@@ -11,7 +11,8 @@ M.tbl_filetypes = {
     'php',
     'markdown',
     'glimmer','handlebars','hbs',
-    'htmldjango'
+    'htmldjango',
+    'embedded_template',
 }

Set up with:

    autotag = {
      enable = true,
      filetypes = { "html" , "xml", "eruby" }, -- eruby is what matters, the rest you just might want to have enabled too
    },

If this is to be made into a proper PR, internal.tbl_filetypes needs to be reworked to support two element tuple elements: {vim_filetype, ts_language_name}. internal.is_supported uses internal.tbl_filetypes to report whether TS language is supported (which is embedded_template in this case), and the same internal.tbl_filetypes provides default vim filetype attach match, which is eruby.

nickjj commented 1 year ago

Thanks, I applied those patches and I'm getting interesting behavior.

For example:

I have to fully close neovim and re-open it have auto-close tag work on a single file and the pattern repeats itself.

There's no errors being printed in a way that I can see.

Edit:

I'm running this version of neovim. It's the stock stable install using the app image installation method on Ubuntu 22.04 running inside of WSL 2:

NVIM v0.8.1
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
Compilation: /usr/bin/gcc-10 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -DNVIM_TS_HAS_SET_MATCH_LIMIT -DNVIM_TS_HAS_SET_ALLOCATOR -O2 -g -Og -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wdouble-promotion -Wmissing-noreturn -Wmissing-format-attribute -Wmissing-prototypes -Wimplicit-fallthrough -Wvla -fstack-protector-strong -fno-common -fdiagnostics-color=always -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -DMIN_LOG_LEVEL=3 -I/__w/neovim/neovim/build/cmake.config -I/__w/neovim/neovim/src -I/__w/neovim/neovim/.deps/usr/include -I/usr/include -I/__w/neovim/neovim/build/src/nvim/auto -I/__w/neovim/neovim/build/include
Compiled by root@08a8cca64bea

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/__w/neovim/neovim/build/nvim.AppDir/usr/share/nvim"
Slotos commented 1 year ago

Use filetypes = { "html" , "xml", "eruby", "embedded_template" }, This relates to default filetypes values and execution order.

Basically, the first attach call from nvim-treesitter works with default data (that's why patch to the plugin code was needed). But then the third line of attach function calls setup that replaces internal.tbl_filetypes with data you provide in nvim-treesitter setup. And that doesn't include embedded_template, leading to tree-sitter skipping attach calls due to internal.is_supported reporting that the language is no longer supported.

Slotos commented 1 year ago

By the way, this bug only affects treesitter setup use style.

If you opt for require('nvim-ts-autotag').setup({ filetypes = { "html" , "xml", "eruby", "embedded_template" } }) instead, you should be seeing the desired behaviour without any patches.

nickjj commented 1 year ago

Thanks. I tried out the first solution (with the patch) and it works.

Of the 2 solutions which is preferred? For example is one faster than the other for processing the file, etc.?

For the non-patch version do I need to set up nvim-ts-autotag using that setup function along with removing autotag.filetypes from the treesitter's setup function? I can test this but wanted to ask openly in case someone else runs into this.

Slotos commented 1 year ago

Generally, treesitter setup route is the common one. If at any point nvim-treesitter changes attach routine, it will handle it without the need to update this plugin. And with this plugin being fully dependent on nvim-treesitter, I can't come up with the reason to separate the two.

require('nvim-ts-autotag').setup basically sets up autocommands equivalent to those that nvim-treesitter adds.

I didn't notice anything that would make one faster than the other.

nickjj commented 1 year ago

Ok. If that's the case, does that mean we'll want to modify this plugin based on your original patch? The one where you added embedded_template to M.tbl_filetypes.

Slotos commented 1 year ago

I'd refactor tbl_filetypes entirely, turning it into a set of {"vim_ft", "ts_language"} tuples, with a single string shorthand akin to "html" being treated as {"html", "html"}. And I'd remove setup initialization route entirely.

Might write a PR for that that in a week or two, once my employment is decided.

nickjj commented 1 year ago

Ok, I'll keep an eye out for that. In the mean time I'll keep this current patch active.

Thanks a lot for the help! Having auto-closing tags that works well is a huge quality of life improvement.

bluz71 commented 1 year ago
diff --git a/lua/nvim-ts-autotag/internal.lua b/lua/nvim-ts-autotag/internal.lua
index ab3e572..8d1da2c 100644
--- a/lua/nvim-ts-autotag/internal.lua
+++ b/lua/nvim-ts-autotag/internal.lua
@@ -11,7 +11,8 @@ M.tbl_filetypes = {
     'php',
     'markdown',
     'glimmer','handlebars','hbs',
-    'htmldjango'
+    'htmldjango',
+    'embedded_template',
 }

Hello,

The above patch works well with Ruby-on-Rails erb templates. Is there any reason why this can't be incorporated into nvim-ts-autotag now? I can make a PR if needed. I would like to see this push forward.

Best regards.

stale[bot] commented 1 year ago

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.

bluz71 commented 1 year ago

Please don’t close, some of us want this.

pseudogram commented 2 months ago

I wanted a config that allowed this plugin to work in a an erb or html file. With all the advice above I couldn't figure out why it wouldn't work for me.

Here is minimal Lazy config that allows autotags to work with .html.erb (embedded ruby files)

First, Treesitter:

return {
    'nvim-treesitter/nvim-treesitter',
    config = function()
        require 'nvim-treesitter.configs'.setup {
             ensure_installed = {"html", "embedded_template" },
        }
    end,
    dependencies = {
        'windwp/nvim-ts-autotag' -- Without this dependency set, auto tag completion would not work... for me
    },
}

Next, Autotag:

return {
    'windwp/nvim-ts-autotag',
    config = function()
        require('nvim-ts-autotag').setup({})
    end,
}

Now. If you're you're following along with the the thread above OR the thread "Dont work.", the above excludes a lot of tidbits that proved useless while I was experimenting.

Here is my final config, with useless comments included:

return {
    'nvim-treesitter/nvim-treesitter',
    build = function()
        require("nvim-treesitter.install").update({ with_sync = true })()
    end,
    lazy = false,
    config = function()
        require 'nvim-treesitter.configs'.setup {
            -- A list of parser names, or "all" (the five listed parsers should always be installed)
            ensure_installed = { "c", "lua", "vim", "vimdoc", "query", "python",
                "css", "javascript", "typescript", "ruby", "html", "xml",
                "embedded_template" },
            -- autotag = {
            --     enable = true,
            -- },
        }
    end,
    dependencies = {
        -- "RRethy/nvim-treesitter-endwise", -- Needed to prevent treesitter ident issue with Ruby: https://github.com/tree-sitter/tree-sitter-ruby/issues/230#issuecomment-1312403487
        'windwp/nvim-ts-autotag'
    },
}
return {
    'windwp/nvim-ts-autotag',
    config = function()
        require('nvim-ts-autotag').setup({
            opts = {
                enable_close = true,          -- Auto close tags
                enable_rename = true,         -- Auto rename pairs of tags
                enable_close_on_slash = true -- Auto close on trailing </
            },
        })
    end,
    -- lazy = true,
    -- event = "VeryLazy",
}

NVIM v0.10.0 Build type: Release LuaJIT 2.1.1716656478 Run "nvim -V1 -v" for more info

nvim-treesitter - 2024/06/24 - d4a888ae3cff358cb239643c45b2b38bb60e29c6 nvim-ts-autotag - 2024/06/21 - ddfccbf0df1b9349c2b9e9b17f4afa8f9b6c1ed1