Compare commits

..

No commits in common. "9ce1d3765bf1e64c7b39a5fd1f30238f25f764d3" and "968b2ccc5d015d9b81e1484890dbc4392471dacf" have entirely different histories.

4 changed files with 56 additions and 98 deletions

View File

@ -22,7 +22,7 @@ It works with:
## Usage
```text
``` text
Before Input After
------------------------------------
<div > <div></div>
@ -30,17 +30,25 @@ Before Input After
------------------------------------
```
## Setup
Neovim 0.7 and nvim-treesitter to work
Requires `Nvim 0.9.0` and up.
User treesitter setup
```lua
require('nvim-ts-autotag').setup()
```
require'nvim-treesitter.configs'.setup {
autotag = {
enable = true,
}
}
> [!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`.
```
or you can use a set up function
``` lua
require('nvim-ts-autotag').setup()
```
### Enable update on insert
@ -60,10 +68,9 @@ vim.lsp.handlers['textDocument/publishDiagnostics'] = vim.lsp.with(
}
)
```
## Default values
```lua
``` lua
local filetypes = {
'html', 'javascript', 'typescript', 'javascriptreact', 'typescriptreact', 'svelte', 'vue', 'tsx', 'jsx', 'rescript',
'xml',
@ -80,15 +87,25 @@ local skip_tag = {
### Override default values
```lua
``` lua
require'nvim-treesitter.configs'.setup {
autotag = {
enable = true,
enable_rename = true,
enable_close = true,
enable_close_on_slash = true,
filetypes = { "html" , "xml" },
}
}
-- OR
require('nvim-ts-autotag').setup({
filetypes = { "html" , "xml" },
})
```
## Fork Status
This is forked from https://github.com/windwp/nvim-ts-autotag due to the primary maintainer's disappearance. Any
PRs/work given to this fork _may_ end up back in the original repository if the primary maintainer comes back.
PRs/work given to this fork *may* end up back in the original repository if the primary maintainer comes back.
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.

View File

@ -2,28 +2,10 @@ local internal = require("nvim-ts-autotag.internal")
local M = {}
---@deprecated
---Will be removed in 1.0.0
function M.init()
local _, nvim_ts = pcall(require, "nvim-treesitter")
if not nvim_ts then
return
end
nvim_ts.define_modules({
require("nvim-treesitter").define_modules({
autotag = {
attach = function(bufnr, lang)
vim.deprecate(
"Nvim Treesitter Setup",
"`require('nvim-ts-autotag').setup()`",
"1.0.0",
"nvim-ts-autotag",
true
)
internal.attach(bufnr, lang)
end,
detach = function(bufnr)
internal.detach(bufnr)
end,
module_path = "nvim-ts-autotag.internal",
is_supported = function(lang)
return internal.is_supported(lang)
end,

View File

@ -1,3 +1,6 @@
local _, ts_utils = pcall(require, "nvim-treesitter.ts_utils")
local configs = require("nvim-treesitter.configs")
local parsers = require("nvim-treesitter.parsers")
local log = require("nvim-ts-autotag._log")
local utils = require("nvim-ts-autotag.utils")
@ -146,16 +149,19 @@ M.enable_rename = true
M.enable_close = true
M.enable_close_on_slash = false
local did_setup = false
M.setup = function(opts)
opts = opts or {}
M.tbl_filetypes = opts.filetypes or M.tbl_filetypes
M.tbl_skipTag = opts.skip_tag or M.tbl_skipTag
M.enable_rename = opts.enable_rename or M.enable_rename
M.enable_close = opts.enable_close or M.enable_close
M.enable_close_on_slash = opts.enable_close_on_slash or M.enable_close_on_slash
did_setup = true
if opts.enable_rename ~= nil then
M.enable_rename = opts.enable_rename
end
if opts.enable_close ~= nil then
M.enable_close = opts.enable_close
end
if opts.enable_close_on_slash ~= nil then
M.enable_close_on_slash = opts.enable_close_on_slash
end
end
local function is_in_table(tbl, val)
@ -188,7 +194,7 @@ local setup_ts_tag = function()
end
local function is_in_template_tag()
local cursor_node = utils.get_node_at_cursor()
local cursor_node = ts_utils.get_node_at_cursor()
if not cursor_node then
return false
end
@ -276,7 +282,7 @@ local function get_tag_name(node)
end
local function find_tag_node(opt)
local target = opt.target or utils.get_node_at_cursor()
local target = opt.target or ts_utils.get_node_at_cursor()
local tag_pattern = opt.tag_pattern
local name_tag_pattern = opt.name_tag_pattern
local skip_tag_pattern = opt.skip_tag_pattern
@ -352,7 +358,7 @@ local function check_close_tag(close_slash_tag)
if close_slash_tag then
-- Find start node from non closed tag
local current = utils.get_node_at_cursor()
local current = ts_utils.get_node_at_cursor()
-- log.debug(current)
target = find_start_tag(current)
-- log.debug(target)
@ -395,7 +401,7 @@ local function check_close_tag(close_slash_tag)
end
M.close_tag = function()
local buf_parser = vim.treesitter.get_parser()
local buf_parser = parsers.get_parser()
if not buf_parser then
return
end
@ -408,7 +414,7 @@ M.close_tag = function()
end
M.close_slash_tag = function()
local buf_parser = vim.treesitter.get_parser()
local buf_parser = parsers.get_parser()
if not buf_parser then
return
end
@ -581,12 +587,8 @@ local is_before_word = is_before("%w", 1)
local is_before_arrow = is_before("<", 0)
M.rename_tag = function()
if is_before_word() then
local parser = vim.treesitter.get_parser()
if not parser then
return
end
parser:parse(true)
if is_before_word() and parsers.has_parser() then
parsers.get_parser():parse(true)
rename_start_tag()
rename_end_tag()
end
@ -594,11 +596,8 @@ end
M.attach = function(bufnr, lang)
M.lang = lang
if not did_setup then
local config = require("nvim-treesitter.configs").get_module("autotag")
M.setup(config)
end
local config = configs.get_module("autotag")
M.setup(config)
if is_in_table(M.tbl_filetypes, vim.bo.filetype) then
setup_ts_tag()
@ -643,7 +642,7 @@ M.attach = function(bufnr, lang)
end
M.detach = function(bufnr)
bufnr = tonumber(bufnr) or vim.api.nvim_get_current_buf()
local bufnr = tonumber(bufnr) or vim.api.nvim_get_current_buf()
buffer_tag[bufnr] = nil
end

View File

@ -1,5 +1,6 @@
local _, ts_utils = pcall(require, "nvim-treesitter.ts_utils")
local log = require("nvim-ts-autotag._log")
local get_node_text = vim.treesitter.get_node_text
local get_node_text = vim.treesitter.get_node_text or vim.treesitter.query.get_node_text or ts_utils.get_node_text
local M = {}
M.get_node_text = function(node)
@ -18,47 +19,6 @@ M.get_cursor = function(bufnr)
local row, col = unpack(vim.api.nvim_win_get_cursor(bufnr or 0))
return row - 1, col
end
-- Credit to `nvim-treesitter`, where this was adapted from
--- Get the root of the given tree for a given row & column position
---@param row integer 0-indexed row position
---@param col integer 0-indexec column position
---@param root_lang_tree vim.treesitter.LanguageTree
M.get_root_for_position = function(row, col, root_lang_tree)
local lang_tree = root_lang_tree:language_for_range({ row, col, row, col })
for _, tree in pairs(lang_tree:trees()) do
local root = tree:root()
if root and vim.treesitter.is_in_node_range(root, row, col) then
return root, tree, lang_tree
end
end
return nil, nil, lang_tree
end
-- Credit to `nvim-treesitter`, where this was adapted from
--- Get the current TSNode at the cursor
---@param winnr integer?
---@return TSNode?
M.get_node_at_cursor = function(winnr)
winnr = winnr or 0
local row, col = unpack(vim.api.nvim_win_get_cursor(winnr))
row = row - 1
local buf = vim.api.nvim_win_get_buf(winnr)
local root_lang_tree = vim.treesitter.get_parser(buf)
if not root_lang_tree then
return
end
local root = M.get_root_for_position(row, col, root_lang_tree)
if not root then
return
end
return root:named_descendant_for_range(row, col, row, col)
end
M.dump_node = function(node)
local text = M.get_node_text(node)
for _, txt in pairs(text) do