Merge pull request #17 from windwp/hbs

Hbs
This commit is contained in:
windwp 2021-05-09 22:44:30 +07:00 committed by GitHub
commit cb2d352beb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 119 additions and 13 deletions

View File

@ -19,6 +19,7 @@ function M.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[[autocmd BufDelete * call v:lua.require('nvim-ts-autotag.internal').detach()]]
vim.cmd[[augroup end]]
end

View File

@ -1,12 +1,14 @@
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 log = require('nvim-ts-autotag._log')
-- local utils=require('nvim-ts-autotag.utils')
local M = {}
M.tbl_filetypes = {
'html', 'javascript', 'typescript', 'javascriptreact', 'typescriptreact', 'svelte', 'vue', 'tsx', 'jsx'
'html', 'javascript', 'typescript', 'javascriptreact', 'typescriptreact', 'svelte', 'vue', 'tsx', 'jsx',
'glimmer','handlebars','hbs'
}
M.tbl_skipTag = {
@ -14,8 +16,10 @@ M.tbl_skipTag = {
'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr','menuitem'
}
local ERROR_TAG = "ERROR"
local HTML_TAG = {
filetypes = {'html'},
start_tag_pattern = 'start_tag',
start_name_tag_pattern = 'tag_name',
end_tag_pattern = "end_tag",
@ -26,8 +30,11 @@ local HTML_TAG = {
skip_tag_pattern = {'quoted_attribute_value', 'end_tag'},
}
local ERROR_TAG = "ERROR"
local JSX_TAG = {
filetypes = {
'typescriptreact', 'javascriptreact', 'javascript.jsx',
'typescript.tsx', 'javascript', 'typescript'},
start_tag_pattern = 'jsx_opening_element',
start_name_tag_pattern = 'identifier|nested_identifier',
end_tag_pattern = "jsx_closing_element",
@ -39,6 +46,36 @@ local JSX_TAG = {
}
local HBS_TAG = {
filetypes = {'glimmer', 'handlebars','hbs'},
start_tag_pattern = 'element_node_start',
start_name_tag_pattern = 'tag_name',
end_tag_pattern = "element_node_end",
end_name_tag_pattern = "tag_name",
close_tag_pattern = 'element_node_end',
close_name_tag_pattern = 'tag_name',
element_tag = 'element_node',
skip_tag_pattern = {'element_node_end', 'attribute_node', 'concat_statement' },
}
local SVELTE_TAG = {
filetypes = {'svelte'},
start_tag_pattern = 'start_tag',
start_name_tag_pattern = 'tag_name',
end_tag_pattern = "end_tag",
end_name_tag_pattern = "tag_name",
close_tag_pattern = 'ERROR',
close_name_tag_pattern = 'ERROR',
element_tag = 'element',
skip_tag_pattern = {'quoted_attribute_value', 'end_tag'},
}
local all_tag = {
HBS_TAG,
SVELTE_TAG,
JSX_TAG
}
M.enable_rename = true
M.enable_close = true
@ -62,16 +99,23 @@ M.is_supported = function (lang)
return is_in_table(M.tbl_filetypes,lang)
end
local function is_jsx()
return is_in_table({
'typescriptreact', 'javascriptreact', 'javascript.jsx', 'typescript.tsx', 'javascript', 'typescript'},
vim.bo.filetype)
end
local buffer_tag={}
local function get_ts_tag()
local ts_tag = HTML_TAG
if is_jsx() then ts_tag = JSX_TAG end
return ts_tag
local bufnr = vim.api.nvim_get_current_buf()
if buffer_tag[bufnr] then
return buffer_tag[bufnr]
end
for _,value in pairs(all_tag) do
if is_in_table(value.filetypes,vim.bo.filetype) then
buffer_tag[bufnr] = value
return value
end
end
buffer_tag[bufnr] = HTML_TAG
return HTML_TAG
end
M.on_file_type = function ()
@ -147,7 +191,7 @@ local function find_tag_node(opt)
end
if node == nil then return nil end
local name_node = node
local tbl_name_pattern={}
local tbl_name_pattern = {}
if string.match(name_tag_pattern,"%|") then
tbl_name_pattern = vim.split(name_tag_pattern, '|')
for _, pattern in pairs(tbl_name_pattern) do
@ -166,6 +210,11 @@ local function find_tag_node(opt)
pattern = pattern
})
end
-- check current node is have same name of tag_match
if is_in_table(tbl_name_pattern, node:type())
then return node
end
return name_node
end
@ -291,11 +340,17 @@ local function rename_start_tag()
replace_text_node(close_tag_node, tag_name)
end
else
local error_tag = get_tag_name(error_node)
if error_tag=='</>' then
replace_text_node(error_node, "</" .. tag_name .. ">")
end
-- have both parent node and child node is error
if close_tag_node:type() == ERROR_TAG then
replace_text_node(error_node, "</" .. tag_name .. ">")
end
end
end
end
@ -367,7 +422,8 @@ M.attach = function (bufnr,lang)
end
M.detach = function ( )
local bufnr = vim.api.nvim_get_current_buf()
buffer_tag[bufnr] = nil
end
-- _G.AUTO = M

View File

@ -20,11 +20,13 @@ end
M.dump_node_text = function(target)
log.debug('=============================')
for node in target:iter_children() do
local node_type = node:type()
local text = ts_utils.get_node_text(node)
log.debug("type:" .. node_type .. " ")
log.debug(text)
end
log.debug('=============================')
end
return M

4
sample/index.hbs Normal file
View File

@ -0,0 +1,4 @@
<div class='dsfds'>
<good></good>
<window></window>
</div>

25
sample/index.svelte Normal file
View File

@ -0,0 +1,25 @@
<script lang="ts">
import { onMount } from 'svelte';
let data = 'sdada';
let loading = true;
function toggle() {
loading = !loading;
}
onMount(() => {
});
</script>
<div class="w-screen h-screen">
<div class="w-100 bg-blue-400 h-72 pt-2">
<visual></lala>
</div>
</div>

View File

@ -144,6 +144,24 @@ local data = {
before = [[<|><div></div></>]],
after = [[<|lala><div></div></lala>]]
},
{
name = "19 rename start tag on svelte " ,
filepath = './sample/index.svelte',
filetype = "svelte",
linenr = 18,
key = [[ciwlala]],
before = [[<|data></data>]],
after = [[<|lala></lala>]]
},
{
name = "20 rename end tag on svelte " ,
filepath = './sample/index.svelte',
filetype = "svelte",
linenr = 18,
key = [[ciwlala]],
before = [[<data></da|ta>]],
after = [[<lala></lala>]]
},
-- {
-- only = true,
-- name = "18 rename node to empty node " ,