windwp / nvim-ts-autotag

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

Don't work. #64

Closed seriiserii825 closed 3 months ago

seriiserii825 commented 2 years ago

Install plugins in neovim + lua.

  use "nvim-treesitter/nvim-treesitter"
  use "windwp/nvim-ts-autotag"

Create a file tree-sitter.lua and include in init.lua

require 'nvim-treesitter.configs'.setup {
  -- A list of parser names, or "all"
  ensure_installed = { "lua" },
  -- Install parsers synchronously (only applied to `ensure_installed`)
  sync_install = false,

  autotag = {
    enable = true,
    filetypes = { "html", "xml", "php", "vue" },
  },

  highlight = {
    -- `false` will disable the whole extension
    enable = true,

    -- NOTE: these are the names of the parsers and not the filetype. (for example if you want to
    -- disable highlighting for the `tex` filetype, you need to include `latex` in this list as this is
    -- the name of the parser)
    -- list of language that will be disabled
    disable = { "c", "rust" },

    -- Setting this to true will run `:h syntax` and tree-sitter at the same time.
    -- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
    -- Using this option may slow down your editor, and you may see some duplicate highlights.
    -- Instead of true it can also be a list of languages
    additional_vim_regex_highlighting = false,
  },
}

In html file i can't rename html tag.

tomspeak commented 2 years ago

not working for me either, no errors, using the default set up. I am on a new version of NeoVim though.

neovim: NVIM v0.8.0-dev+406-g502f03fc0
windwp/nvim-ts-autotag: latest master
tiagaoalb commented 2 years ago

Same here.

YoungeonLee commented 2 years ago

Have you ran :TSInstall html?

danilobjr commented 2 years ago

Have you ran :TSInstall html?

@YoungeonLee I have, but still the same.

I'm trying to run on LunarVim.

$ lvim -v
NVIM v0.7.2
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by brew@HMBRW-A-001-M1-004.local
YoungeonLee commented 2 years ago

@danilobjr Try adding

lvim.builtin.treesitter.autotag.enable = true 

to your config. If that doesn't work, try running :TSEnable autotag.

danilobjr commented 2 years ago

lvim.builtin.treesitter.autotag.enable = true

@YoungeonLee This works like a charm now. Thank you so much! 🎉

kristupas-g commented 2 years ago

require("nvim-treesitter.configs").setup({ ensure_installed = { "c", "c_sharp", "css", "go", "graphql", "html", "json", "http", "javascript", "lua", "markdown", "python", "rust", "typescript", "yaml", "bash", "comment", "java", }, sync_install = false, highlight = { enable = true, additional_vim_regex_highlighting = true, }, indent = { enable = true, disable = { "yaml" } }, rainbow = { enable = true, extended_mode = true, max_file_lines = nil, }, context_commentstring = { enable = true, enable_autocmd = false, }, autotag = { enable = true, }, }) This is my setup and it only works in html files, no jsx or tsx files

PS Seems like adding tsx parser solved the problem for me

vricop commented 1 year ago

@danilobjr Try adding

lvim.builtin.treesitter.autotag.enable = true 

to your config. If that doesn't work, try running :TSEnable autotag.

That worked, thanks man!

Gnarus-G commented 1 year ago

I'm having the same problem. It just doesn't work. I tried in html, and tsx. Neither autotagging nor autorenaming Here is the setup I tried with. image Here is my treesitter setup. image

I'm on nvim 0.8

zRich commented 1 year ago

Finally, I figured out how to make it work.

image

Content of plugins.configs.treesitter

local options = {
  auto_install = true,
  ensure_installed = {
    "lua",
    "vim",
    "go",
    "toml",
    "css",
    "tsx",
    "css",
    "html",
    "lua"
  },
  highlight = {
    enable = true,
    use_languagetree = true,
  },
  autotag = {
    enable = true,
    filetypes = {
      'html', 'javascript', 'typescript', 'javascriptreact', 'typescriptreact', 'svelte', 'vue', 'tsx', 'jsx', 'rescript',
      'css', 'lua', 'xml', 'php', 'markdown'
    },
  },
  indent = { enable = true },
}

require('nvim-treesitter.configs').setup(options)

I enabled auto install lang parsers.

I am on neovim 0.8

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.

angelollima commented 1 year ago

@danilobjr Try adding

lvim.builtin.treesitter.autotag.enable = true 

to your config. If that doesn't work, try running :TSEnable autotag.

works well with html, how could i do it with jsx?

angelollima commented 1 year ago

@danilobjr Try adding

lvim.builtin.treesitter.autotag.enable = true 

to your config. If that doesn't work, try running :TSEnable autotag.

Where do i have to put this line?

peskovdev commented 1 year ago

Same for me. nvim-ts-autotag closes tags, but doesn't rename.

Update! It works!

But r or x mappings to change/delete one character don't trigger plugin!

s1nistr4 commented 1 year ago

Yeah, this isn't working for me either. I followed the instructions exactly as they are in the readme and nothing happens.

gwapocom commented 1 year ago

same here even tried this but nothing for PHP and TS files

Finally, I figured out how to make it work.

image

Content of plugins.configs.treesitter

local options = {
  auto_install = true,
  ensure_installed = {
    "lua",
    "vim",
    "go",
    "toml",
    "css",
    "tsx",
    "css",
    "html",
    "lua"
  },
  highlight = {
    enable = true,
    use_languagetree = true,
  },
  autotag = {
    enable = true,
    filetypes = {
      'html', 'javascript', 'typescript', 'javascriptreact', 'typescriptreact', 'svelte', 'vue', 'tsx', 'jsx', 'rescript',
      'css', 'lua', 'xml', 'php', 'markdown'
    },
  },
  indent = { enable = true },
}

require('nvim-treesitter.configs').setup(options)

I enabled auto install lang parsers.

I am on neovim 0.8

nmsobri commented 1 year ago

it's sad, this plugin doesn't documented properly on how to install.. i need to find open ticket just to figure out on how to install it properly

hvquy001 commented 1 year ago

Many people, including myself, initially believe that simply configuring nvim-treesitter with the following code will make it work:

  autotag = {
    enable = true
  }

However, to ensure it functions correctly, you also need to install the package using your Package Manager.

In my case, using NvChad and Lazy, the configuration should be:

{
    "nvim-treesitter/nvim-treesitter",
    opts = overrides.treesitter,
  },
  {
    "windwp/nvim-ts-autotag",
    dependencies = "nvim-treesitter/nvim-treesitter",
    config = function ()
      require('nvim-ts-autotag').setup({
        -- your config
      })
    end,
    lazy = true,
    event = "VeryLazy"
  },

It would be helpful to include this information in the documentation.

thenewvu commented 1 year ago

For me, I have to disable alvan/vim-closetag plugin to make this plug-in work.

acdesouza commented 1 year ago

I'm using lazy.nvim. I could make it work on HTML files. But, it didn't work on ERB(Ruby template language)

I did a :TSEnable autotag on a erb file and it wasn't able to end a p tag.

https://user-images.githubusercontent.com/97436/234002768-cff1b6bf-d6f0-4981-936c-d533411df560.mov

https://user-images.githubusercontent.com/97436/234002775-88bba0e1-9513-4949-b148-b58543c4d16e.mov

Note the different attempts by using embedded_template, erb, and eruby.

  {
    'nvim-treesitter/nvim-treesitter',
    build = function()
      require("nvim-treesitter.install").update({ with_sync = 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'
    },
    config = function()
      require'nvim-treesitter.configs'.setup {
        ensure_installed = { "ruby", "html", "embedded_template", "css", "scss", "javascript" },
        highlight = {
          disable = { "lua" },
          enable  = { "ruby", "html", "embedded_template", "eruby", "css", "scss", "javascript" }
        },
        additional_vim_regex_highlighting = false,
        endwise = {
          enable = true,
        },
        autotag = {
          enable = true,
          filetypes = { "html" , "xml", "eruby", "erb", "embedded_template" },
        }
      }
    end
  },

UPDATE: After reading this https://github.com/windwp/nvim-ts-autotag/issues/85#issuecomment-1365556082, applying the suggested patch, and changed to call the setup on nvim-ts-autotag, I got the same behavior.

acdesouza commented 1 year ago

I'm using lazy.nvim. I could make it work on HTML files. But, it didn't work on ERB(Ruby template language)...

If my tests are correct, this PR https://github.com/windwp/nvim-ts-autotag/pull/104 fixed the issue for eruby files.

I need to config it separately, this way:

  {
    'windwp/nvim-ts-autotag',
    config = function()
      require('nvim-ts-autotag').setup({
        filetypes = { "html" , "xml", "eruby", "embedded_template" }
      })
    end
  }

https://user-images.githubusercontent.com/97436/235331284-20b79b9e-c533-4a07-9334-9deeeaecde10.mov

tienpvse1 commented 1 year ago

Turn out, it missing the tsx module for treesitting for my case, make sure you have it installed by

:TSInstall tsx
kleinepls commented 1 year ago

The plugin only seems to work when exiting insert mode with esc, and not with ctrl-c

K4R7IK commented 1 year ago

Many people, including myself, initially believe that simply configuring nvim-treesitter with the following code will make it work:

  autotag = {
    enable = true
  }

However, to ensure it functions correctly, you also need to install the package using your Package Manager.

In my case, using NvChad and Lazy, the configuration should be:

{
    "nvim-treesitter/nvim-treesitter",
    opts = overrides.treesitter,
  },
  {
    "windwp/nvim-ts-autotag",
    dependencies = "nvim-treesitter/nvim-treesitter",
    config = function ()
      require('nvim-ts-autotag').setup({
        -- your config
      })
    end,
    lazy = true,
    event = "VeryLazy"
  },

It would be helpful to include this information in the documentation.

@hvquy001 if you would not mind, I would like to update README using your information. This will help in proper installation. I myself struggle with the installation process until found your comment. Thanks

exosyphon commented 1 year ago

Many people, including myself, initially believe that simply configuring nvim-treesitter with the following code will make it work:

  autotag = {
    enable = true
  }

However, to ensure it functions correctly, you also need to install the package using your Package Manager. In my case, using NvChad and Lazy, the configuration should be:

{
    "nvim-treesitter/nvim-treesitter",
    opts = overrides.treesitter,
  },
  {
    "windwp/nvim-ts-autotag",
    dependencies = "nvim-treesitter/nvim-treesitter",
    config = function ()
      require('nvim-ts-autotag').setup({
        -- your config
      })
    end,
    lazy = true,
    event = "VeryLazy"
  },

It would be helpful to include this information in the documentation.

@hvquy001 if you would not mind, I would like to update README using your information. This will help in proper installation. I myself struggle with the installation process until found your comment. Thanks

Thank you for posting this. I got this to work immediately with lazy.nvim using this config.

idelice commented 1 year ago

This worked for me as well

Something this crucial should be documented imo...

dongalor commented 11 months ago

Hey there!

I have autotag = { enable = true, } in my treesitter config, but it works only after I execute :TSEnable autotag after starting nvim.

How to enable it automatically?

Thanks!

Update:

I discovered that it enables automatically when I open a file from NerdTree after starting nvim in a directory, rather than directly opening a path from the command line. It's possible to live with that, but I'd love to know whether it's possible to fix. :)

griffinbaker12 commented 10 months ago

This config worked for me by throwing it in my packer.lua: use { 'windwp/nvim-ts-autotag', config = function() require('nvim-ts-autotag').setup({ filetypes = { "html", "javascript", "javascriptreact", "typescriptreact"} })

pedroferreira37 commented 8 months ago

I'm facing the autotag issue in html, but it only happens when script tag is in the buffer, weird behavior.

guoliang commented 6 months ago

For me auto tag doesn't work in .html files, but it works in .ts files. haven't found a solution.

I have tried the proposed solution which worked for some here, but it doesn't work in my case.

dexterspg commented 6 months ago

I am using lazy vim It does not work for xml files for me.

ensure_installed = { "c", "query", "vimdoc", "vim", "lua", "xml", "javascript", "typescript", "css", "java", "html", "python", "vue" }, autotag = { enable = true, enable_rename = true, enable_close = true, enable_close_on_slash = true, filetypes = { "html", "xml", "vue", "typescript" }, },

tolu-c commented 6 months ago

Mine doesn't work yet,

custom/plugins.lua

...
{
    "windwp/nvim-ts-autotag",
    ft = {
      "javascript", "typescript", 'javascriptreact', 'typescriptreact'
    },
    config = function()
      require('nvim-ts-autotag').setup()
    end,
    lazy = true,
    event = "VeryLazy"
  },
  {
    "nvim-treesitter/nvim-treesitter",
    opts = function()
      opts = require "plugins.configs.treesitter"
      opts.ensure_installed = {
        "lua", "javascript", "typescript", "jsx", "tsx", "css", "ts", "js"
      }
      return opts
    end
  }
...

plugins/configs/treesitter.lua

local options = {
  ensure_installed = { "lua", "vim", "vimdoc" },

  highlight = {
    enable = true,
    use_languagetree = true,
  },

  indent = { enable = true },
  autotag = { enable = true },
}

return options

Please help, Thanks.

renatomoor commented 6 months ago

I managed to get it work inspired by https://github.com/windwp/nvim-ts-autotag/issues/64#issuecomment-1509722026 This config is working for me using NvChad and Lazy:

  {
    "nvim-treesitter/nvim-treesitter",
      opts = {
        auto_install = true,
        autotag = {
           enable = true,
           filetypes = {
            'html', 'javascript', 'typescript', 'svelte', 'vue', 'tsx', 'jsx', 'rescript',
            'css', 'lua', 'xml', 'php', 'markdown'
           },
        },
        indent = { enable = true },
        ensure_installed = {
           -- defaults 
          "vim",
          "lua",
          "vimdoc",

          -- web dev 
          "html",
          "css",
          "javascript",
          "typescript",
          "tsx",
          "astro",
          "vue",
          "svelte",
          "markdown",
          "markdown_inline",
          "json",
          "scss",
          "yaml"
        }
      }
  },

  {
    "windwp/nvim-ts-autotag",
    dependencies = "nvim-treesitter/nvim-treesitter",
    config = function ()
      require('nvim-ts-autotag').setup()
    end,
    lazy = true,
    event = "VeryLazy"
  },
GNITOAHC commented 5 months ago

Mine isn't working with markdown, too.

local status_ok, configs = pcall(require, "nvim-treesitter.configs")
if not status_ok then
    return
end

local autotag_filetypes = {
    'html', 'javascript', 'typescript', 'javascriptreact', 'typescriptreact', 'svelte', 'vue', 'tsx', 'jsx', 'rescript',
    'xml',
    'php',
    'markdown',
    'glimmer','handlebars','hbs'
}

configs.setup {
    ensure_installed = { "c", "cpp", "lua", "python", "c_sharp", "typescript", "javascript", "markdown", "scss", "fish", "vim" },
    sync_install = false,
    ignore_install = { "" }, -- List of parsers to ignore installing
    autopairs = {
        enable = true,
    },
    autotag = {
        enable = true,
        filetypes = autotag_filetypes,
    },
    highlight = {
        enable = true, -- false will disable the whole extension
        disable = { "" }, -- list of language that will be disabled
        additional_vim_regex_highlighting = false,
    },
    indent = { enable = true, disable = { "yaml" } },
    rainbow = {
        enable = true,
        extended_mode = true, -- Also highlight non-bracket delimiters like html tags, boolean or table: lang -> boolean
        max_file_lines = nil, -- Do not enable for files with more than n lines, int
    }
}
ishaan-kapoor commented 5 months ago

Many people, including myself, initially believe that simply configuring nvim-treesitter with the following code will make it work:

  autotag = {
    enable = true
  }

However, to ensure it functions correctly, you also need to install the package using your Package Manager.

In my case, using NvChad and Lazy, the configuration should be:

{
    "nvim-treesitter/nvim-treesitter",
    opts = overrides.treesitter,
  },
  {
    "windwp/nvim-ts-autotag",
    dependencies = "nvim-treesitter/nvim-treesitter",
    config = function ()
      require('nvim-ts-autotag').setup({
        -- your config
      })
    end,
    lazy = true,
    event = "VeryLazy"
  },

It would be helpful to include this information in the documentation.

I am using vanilla NeoVim with lazy package manager, ans this does not work for me (Stable) NVIM v0.9.6-dev-0ef2718 - Released: 19th Jan, 2024

UPDATE: Works now, just TSInstall the parser for whatever filetype you want

PriceHiller commented 3 months ago

Hey folks, I highly encourage you to check out the new changes on main.

This plugin will not work for certain filetypes unless you have their associated treesitter parser installed.

nvim-treesitter does include an auto_install flag in its config to automatically install parsers for known filetypes. Recommend you give that a shot or install the parsers as needed.

@windwp am I ok to close this issue? Been open since 2022 and the core solution is to install the correct parsers (or let nvim-treesitter auto install them).

windwp commented 3 months ago

We can close it. The old setting configuration will not work with the current version, so it's better to open a new issue.

nigelnindodev commented 3 months ago

Many people, including myself, initially believe that simply configuring nvim-treesitter with the following code will make it work:

  autotag = {
    enable = true
  }

However, to ensure it functions correctly, you also need to install the package using your Package Manager.

In my case, using NvChad and Lazy, the configuration should be:

{
    "nvim-treesitter/nvim-treesitter",
    opts = overrides.treesitter,
  },
  {
    "windwp/nvim-ts-autotag",
    dependencies = "nvim-treesitter/nvim-treesitter",
    config = function ()
      require('nvim-ts-autotag').setup({
        -- your config
      })
    end,
    lazy = true,
    event = "VeryLazy"
  },

It would be helpful to include this information in the documentation.

Thanks a lot! What's missing in this answer is to verify if your target language is in ensure_installed (I was missing html): Screenshot from 2024-05-27 20-02-28

alfredbaudisch commented 3 months ago

There's now a deprecation message when adding autotag inside treesitter.setup or when creating a standalone config for nvim-ts-autotag.

Correct usage is to add import nvim-ts-autotag inside the treesitter config:

require('nvim-ts-autotag').setup({
    enable = true,
    filetypes = { "html", "xml", "tsx" },
})

My full working setup:

return {
    "nvim-treesitter/nvim-treesitter",
    event = { "BufReadPre", "BufNewFile" },
    build = ":TSUpdate",
    dependencies = {
        "windwp/nvim-ts-autotag",
    },
    config = function()
        -- import nvim-treesitter plugin
        local treesitter = require("nvim-treesitter.configs")

        require('nvim-ts-autotag').setup({
            enable = true,
            filetypes = { "html", "xml", "tsx" },
        })

        -- configure treesitter
        treesitter.setup({ -- enable syntax highlighting
            highlight = {
                enable = true,
            },
            -- enable indentation
            indent = { enable = true },

            -- ensure these language parsers are installed
            ensure_installed = {
                "json", "html", -- add others here
            },
            incremental_selection = {
                enable = true,
                keymaps = {
                    init_selection = "<C-space>",
                    node_incremental = "<C-space>",
                    scope_incremental = false,
                    node_decremental = "<bs>",
                },
            },
        })
    end,
}