Skip to content

Commit

Permalink
refactor to ts module
Browse files Browse the repository at this point in the history
  • Loading branch information
windwp committed Mar 10, 2021
1 parent 3662cba commit bad9525
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 22 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
```
25 changes: 25 additions & 0 deletions lua/nvim-ts-autotag.lua
Original file line number Diff line number Diff line change
@@ -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
50 changes: 31 additions & 19 deletions lua/nvim-ts-autotag/internal.lua
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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',
Expand All @@ -34,18 +36,13 @@ local JSX_TAG = {
}


M.test = false
M.enableRename = true
M.enableClose = true

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)
Expand All @@ -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
Expand All @@ -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 <silent> <buffer> > ><c-o>:lua require('nvim-ts-autotag').closeTag()<CR>]]
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 <buffer> 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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 <silent> <buffer> > ><c-o>:lua require('nvim-ts-autotag.internal').closeTag()<CR>]]
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 <buffer> 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
1 change: 1 addition & 0 deletions plugin/nvim-ts-autotag.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lua require "nvim-ts-autotag".init()
3 changes: 3 additions & 0 deletions sample/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
<body>





</body>
</html>

0 comments on commit bad9525

Please sign in to comment.