feat: embed template tags (#78)

* refactor: use lua apis to bind keys

* refactor: use nvim lua api for rename aucmd

* fix: callback for autoclose use nvim_win_set_cursor

* feat: support html in js template tags

fix: add default bufnr when creating bindings

test: lit templates

fix: template strings
This commit is contained in:
Benny Powers 2023-04-10 10:53:39 +03:00 committed by GitHub
parent 7a1c677985
commit b525525b6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 9 deletions

View File

@ -129,8 +129,35 @@ local setup_ts_tag = function()
buffer_tag[bufnr] = HTML_TAG
end
local function is_in_template_tag()
local cursor_node = ts_utils.get_node_at_cursor()
if not cursor_node then
return false
end
local has_element = false
local has_template_string = false
local current_node = cursor_node
while not (has_element and has_template_string) and current_node do
if not has_element and current_node:type() == 'element' then
has_element = true
end
if not has_template_string and current_node:type() == 'template_string' then
has_template_string = true
end
current_node = current_node:parent()
end
return has_element and has_template_string
end
local function get_ts_tag()
if is_in_template_tag() then
return HTML_TAG
else
return buffer_tag[vim.api.nvim_get_current_buf()]
end
end
local function find_child_match(opts)
@ -502,18 +529,26 @@ M.attach = function(bufnr, lang)
if is_in_table(M.tbl_filetypes, vim.bo.filetype) then
setup_ts_tag()
if M.enable_close == true then
vim.cmd(
[[inoremap <silent> <buffer> > ><c-c>:lua require('nvim-ts-autotag.internal').close_tag()<CR>a]]
)
vim.api.nvim_buf_set_keymap(bufnr or 0, 'i', ">", ">", {
noremap = true,
silent = true,
callback = function()
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
vim.api.nvim_buf_set_text(bufnr or 0, row-1, col, row-1, col, { '>' })
M.close_tag()
vim.api.nvim_win_set_cursor(0, {row, col+1})
end
})
end
if M.enable_rename == true then
bufnr = bufnr or vim.api.nvim_get_current_buf()
vim.cmd(
string.format(
[[autocmd! InsertLeave <buffer=%s> call v:lua.require('nvim-ts-autotag.internal').rename_tag() ]],
bufnr
)
)
vim.api.nvim_create_autocmd('InsertLeave', {
group = vim.api.nvim_create_augroup('ts-autotag-rename', { clear = true }),
buffer = bufnr,
callback = function()
M.rename_tag()
end
})
end
end
end

6
sample/index.ts Normal file
View File

@ -0,0 +1,6 @@
const html = x => x;
html`
`

View File

@ -194,6 +194,16 @@ local data = {
-- before = [[<div| ]],
-- after = [[<div>|</div> ]],
-- },
{
name = '19 lit template div',
filepath = './sample/index.ts',
filetype = 'typescript',
linenr = 3,
key = [[>]],
before = [[<div| ]],
after = [[<div>|</div> ]],
},
}
local autotag = require('nvim-ts-autotag')