2021-03-08 06:19:01 -06:00
# nvim-ts-autotag
2021-03-13 18:33:13 -06:00
Use treesitter to **autoclose** and **autorename** html tag
2021-03-08 06:19:01 -06:00
2023-04-13 07:06:12 -05:00
It works with:
- astro
- glimmer
- handlebars
- html
- javascript
- jsx
- markdown
- php
- rescript
- svelte
- tsx
2024-04-27 06:09:42 -05:00
- twig
2023-04-13 07:06:12 -05:00
- typescript
- vue
- xml
2021-03-07 21:47:09 -06:00
2024-05-15 08:07:00 -05:00
and more
2021-03-07 21:47:09 -06:00
## Usage
2024-05-19 07:02:34 -05:00
```text
2021-03-07 21:47:09 -06:00
Before Input After
------------------------------------
2021-08-04 10:37:59 -05:00
< div > < div > < / div >
< div > < / div > ciwspan< esc > < span > < / span >
2021-03-07 21:47:09 -06:00
------------------------------------
```
## Setup
2024-05-17 23:11:51 -05:00
Requires `Nvim 0.9.5` and up.
2021-03-10 00:09:41 -06:00
2024-05-19 07:02:34 -05:00
```lua
2024-05-17 23:03:08 -05:00
require('nvim-ts-autotag').setup({
opts = {
-- Defaults
enable_close = true, -- Auto close tags
enable_rename = true, -- Auto rename pairs of tags
enable_close_on_slash = false -- Auto close on trailing < /
},
-- Also override individual filetype configs, these take priority.
-- Empty by default, useful if one of the "opts" global settings
-- doesn't work well in a specific filetype
per_filetype = {
["html"] = {
enable_close = false
}
}
})
2021-03-07 21:47:09 -06:00
```
2024-05-19 07:02:34 -05:00
> [!CAUTION]
> If you are setting up via `nvim-treesitter.configs` it has been deprecated! Please migrate to the
> new way. It will be removed in `1.0.0`.
2024-05-15 08:07:00 -05:00
### Extending the default config
Let's say that there's a language that `nvim-ts-autotag` doesn't currently support and you'd like to support it in your
config. While it would be the preference of the author that you upstream your changes, perhaps you would rather not 😢.
For example, if you have a language that has a very similar layout in its Treesitter Queries as `html` , you could add an
alias like so:
```lua
require('nvim-ts-autotag').setup({
aliases = {
["your language here"] = "html",
}
})
-- or
local TagConfigs = require("nvim-ts-autotag.config.init")
TagConfigs:add_alias("your language here", "html")
```
That will make `nvim-ts-autotag` close tags according to the rules of the `html` config in the given language.
But what if a parser breaks for whatever reason, for example the upstream Treesitter tree changes its node names and now
the default queries that `nvim-ts-autotag` provides no longer work.
Fear not! You can directly extend and override the existing configs. For example, let's say the start and end tag
patterns have changed for `xml` . We can directly override the `xml` config:
```lua
local TagConfigs = require("nvim-ts-autotag.config.init")
TagConfigs:update(TagConfigs:get("xml"):override("xml", {
start_tag_pattern = { "STag" },
end_tag_pattern = { "ETag" },
}))
```
In fact, this very nearly what we do during our own internal initialization phase for `nvim-ts-autotag` .
2021-06-17 00:50:35 -05:00
### Enable update on insert
If you have that issue
[#19 ](https://github.com/windwp/nvim-ts-autotag/issues/19 )
2021-06-17 00:33:40 -05:00
```lua
vim.lsp.handlers['textDocument/publishDiagnostics'] = vim.lsp.with(
vim.lsp.diagnostic.on_publish_diagnostics,
{
underline = true,
virtual_text = {
spacing = 5,
severity_limit = 'Warning',
},
update_in_insert = true,
}
)
```
2024-05-19 07:02:34 -05:00
2024-05-13 04:14:04 -05:00
## Fork Status
2024-05-19 07:02:34 -05:00
2024-05-13 04:14:04 -05:00
This is forked from https://github.com/windwp/nvim-ts-autotag due to the primary maintainer's disappearance. Any
2024-05-19 07:02:34 -05:00
PRs/work given to this fork _may_ end up back in the original repository if the primary maintainer comes back.
2023-03-16 21:19:39 -05:00
2024-05-13 04:14:04 -05:00
Full credit to [@windwp ](https://github.com/windwp ) for the creation of this plugin. Here's to hoping they're ok and will be back sometime down the line.