From bad9525f0bdb7bd3d1836cd0009eb3362c08a041 Mon Sep 17 00:00:00 2001 From: windwp Date: Wed, 10 Mar 2021 13:09:41 +0700 Subject: [PATCH] refactor to ts module --- README.md | 24 +++++++++++++-- lua/nvim-ts-autotag.lua | 25 ++++++++++++++++ lua/nvim-ts-autotag/internal.lua | 50 ++++++++++++++++++++------------ plugin/nvim-ts-autotag.vim | 1 + sample/index.html | 3 ++ 5 files changed, 81 insertions(+), 22 deletions(-) create mode 100644 lua/nvim-ts-autotag.lua create mode 100644 plugin/nvim-ts-autotag.vim diff --git a/README.md b/README.md index b07821d..912a58a 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,20 @@ Before Input After ## Setup Neovim 0.5 with and nvim-treesitter to work +User treesitter setup +```lua +require'nvim-treesitter.configs'.setup { + autotag = { + enable = true, + } +} + +``` +or you can use a set up function + ``` lua require('nvim-ts-autotag').setup() + ``` ## Default values @@ -37,10 +49,16 @@ local skip_tags = { ### Override default values ``` lua + +require'nvim-treesitter.configs'.setup { + autotag = { + enable = true, + filetypes = { "html" , "xml" }, + } +} +-- OR require('nvim-ts-autotag').setup({ filetypes = { "html" , "xml" }, }) -``` -# Ref -[vim-closetag](https://github.com/alvan/vim-closetag/edit/master/README.md) +``` diff --git a/lua/nvim-ts-autotag.lua b/lua/nvim-ts-autotag.lua new file mode 100644 index 0000000..9172542 --- /dev/null +++ b/lua/nvim-ts-autotag.lua @@ -0,0 +1,25 @@ + +local internal= require('nvim-ts-autotag.internal') + +local M = {} + +function M.init() + require "nvim-treesitter".define_modules { + autotag = { + module_path = 'nvim-ts-autotag.internal', + is_supported = function(lang) + return internal.is_supported(lang) + end + } + } +end + +function M.setup(opts) + internal.setup(opts) + vim.cmd[[augroup nvim_ts_xmltag]] + vim.cmd[[autocmd!]] + vim.cmd[[autocmd FileType * call v:lua.require('nvim-ts-autotag.internal').attach()]] + vim.cmd[[augroup end]] +end + +return M diff --git a/lua/nvim-ts-autotag/internal.lua b/lua/nvim-ts-autotag/internal.lua index 889245f..d298359 100644 --- a/lua/nvim-ts-autotag/internal.lua +++ b/lua/nvim-ts-autotag/internal.lua @@ -1,9 +1,10 @@ local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils') +local configs = require'nvim-treesitter.configs' local M = {} M.tbl_filetypes = { - 'html', 'xml', 'javascript', 'javascriptreact', 'typescriptreact', 'svelte', 'vue', 'php' + 'html', 'xml', 'javascript', 'javascriptreact', 'typescriptreact', 'svelte', 'vue' } M.tbl_skipTag = { @@ -22,6 +23,7 @@ local HTML_TAG = { element_tag = 'element', skip_tag_pattern = {'quoted_attribute_value', 'end_tag'}, } + local JSX_TAG = { start_tag_pattern = 'jsx_opening_element', start_name_tag_pattern = 'identifier', @@ -34,7 +36,6 @@ local JSX_TAG = { } -M.test = false M.enableRename = true M.enableClose = true @@ -42,10 +43,6 @@ 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 - vim.cmd[[augroup nvim_ts_xmltag]] - vim.cmd[[autocmd!]] - vim.cmd[[autocmd FileType * call v:lua.require('nvim-ts-autotag').on_file_type()]] - vim.cmd[[augroup end]] end local function is_in_table(tbl, val) @@ -56,7 +53,12 @@ local function is_in_table(tbl, val) return false end -local function isJsX() +M.is_supported=function (lang) + print(vim.inspect(lang)) + return is_in_table(M.tbl_filetypes,lang) +end + +local function is_jsx() if is_in_table({'typescriptreact', 'javascriptreact'}, vim.bo.filetype) then return true end @@ -65,21 +67,11 @@ end local function get_ts_tag() local ts_tag = HTML_TAG - if(isJsX()) then ts_tag = JSX_TAG end + if(is_jsx()) then ts_tag = JSX_TAG end return ts_tag end M.on_file_type = function () - if is_in_table(M.tbl_filetypes,vim.bo.filetype) then - vim.cmd[[inoremap > >:lua require('nvim-ts-autotag').closeTag()]] - local bufnr = vim.api.nvim_get_current_buf() - if M.enableRename == true then - vim.cmd("augroup nvim_ts_xmltag_" .. bufnr) - vim.cmd[[autocmd!]] - vim.cmd[[autocmd InsertLeave call v:lua.require('nvim-ts-autotag').renameTag() ]] - vim.cmd[[augroup end]] - end - end end local function find_child_match(opts) local target = opts.target @@ -201,7 +193,7 @@ end local function checkStartTag() local ts_tag = HTML_TAG - if(isJsX()) then ts_tag = JSX_TAG end + if(is_jsx()) then ts_tag = JSX_TAG end local tag_node = find_tag_node({ tag_pattern = ts_tag.start_tag_pattern, name_tag_pattern = ts_tag.start_name_tag_pattern, @@ -272,4 +264,24 @@ M.renameTag = function () checkEndTag() end +M.attach = function (bufnr, lang) + local config = configs.get_module('autotag') + M.setup(config) + if is_in_table(M.tbl_filetypes,vim.bo.filetype) then + vim.cmd[[inoremap > >:lua require('nvim-ts-autotag.internal').closeTag()]] + bufnr = bufnr or vim.api.nvim_get_current_buf() + if M.enableRename == true then + vim.cmd("augroup nvim_ts_xmltag_" .. bufnr) + vim.cmd[[autocmd!]] + vim.cmd[[autocmd InsertLeave call v:lua.require('nvim-ts-autotag.internal').renameTag() ]] + vim.cmd[[augroup end]] + end + end +end + +M.detach = function (bufnr ) + +end + +-- _G.AUTO = M return M diff --git a/plugin/nvim-ts-autotag.vim b/plugin/nvim-ts-autotag.vim new file mode 100644 index 0000000..829bf90 --- /dev/null +++ b/plugin/nvim-ts-autotag.vim @@ -0,0 +1 @@ +lua require "nvim-ts-autotag".init() diff --git a/sample/index.html b/sample/index.html index ad99728..f54b007 100644 --- a/sample/index.html +++ b/sample/index.html @@ -9,5 +9,8 @@ + + +