From 34f6d24007331343cb11146ad14e838f4698ba22 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Mon, 13 May 2024 04:57:23 -0500 Subject: [PATCH] style: format with stylua --- lua/nvim-ts-autotag.lua | 28 ++-- lua/nvim-ts-autotag/_log.lua | 11 +- lua/nvim-ts-autotag/internal.lua | 14 +- lua/nvim-ts-autotag/utils.lua | 21 ++- tests/close_slash_tag_spec.lua | 116 +++++++------- tests/closetag_spec.lua | 134 ++++++++-------- tests/renametag_spec.lua | 255 +++++++++++++++---------------- tests/test-utils.lua | 109 +++++-------- 8 files changed, 324 insertions(+), 364 deletions(-) diff --git a/lua/nvim-ts-autotag.lua b/lua/nvim-ts-autotag.lua index 572fff6..8c56b5a 100644 --- a/lua/nvim-ts-autotag.lua +++ b/lua/nvim-ts-autotag.lua @@ -3,23 +3,23 @@ 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 - } - } + 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[[autocmd BufDelete * lua require('nvim-ts-autotag.internal').detach(vim.fn.expand(''))]] - vim.cmd[[augroup end]] + 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([[autocmd BufDelete * lua require('nvim-ts-autotag.internal').detach(vim.fn.expand(''))]]) + vim.cmd([[augroup end]]) end return M diff --git a/lua/nvim-ts-autotag/_log.lua b/lua/nvim-ts-autotag/_log.lua index 7873731..ead51db 100644 --- a/lua/nvim-ts-autotag/_log.lua +++ b/lua/nvim-ts-autotag/_log.lua @@ -1,13 +1,12 @@ if _G.__is_log then - return require('plenary.log').new { - plugin = 'nvim-ts-autotag', - level = (_G.__is_log == true and 'debug') or 'warn', - } + return require("plenary.log").new({ + plugin = "nvim-ts-autotag", + level = (_G.__is_log == true and "debug") or "warn", + }) else - return{ + return { debug = function(_) end, info = function(_) end, error = function(_) end, - } end diff --git a/lua/nvim-ts-autotag/internal.lua b/lua/nvim-ts-autotag/internal.lua index 36a1d45..565adc8 100644 --- a/lua/nvim-ts-autotag/internal.lua +++ b/lua/nvim-ts-autotag/internal.lua @@ -485,8 +485,8 @@ local function rename_start_tag() -- log.debug("do replace") -- log.debug(tag_name) -- log.debug(close_tag_name) - if close_tag_name == '>' then - tag_name = tag_name .. '>' + if close_tag_name == ">" then + tag_name = tag_name .. ">" end replace_text_node(close_tag_node, tag_name) end @@ -531,7 +531,7 @@ local function rename_end_tag() if start_tag_node ~= nil then local start_tag_name = get_tag_name(start_tag_node) if tag_name ~= start_tag_name then - log.debug('replace end tag') + log.debug("replace end tag") replace_text_node(start_tag_node, tag_name) end end @@ -550,8 +550,8 @@ local function is_before(regex, range) end end -local is_before_word = is_before('%w', 1) -local is_before_arrow = is_before('<', 0) +local is_before_word = is_before("%w", 1) +local is_before_arrow = is_before("<", 0) M.rename_tag = function() if is_before_word() and parsers.has_parser() then @@ -568,7 +568,7 @@ M.attach = function(bufnr, lang) if is_in_table(M.tbl_filetypes, vim.bo.filetype) then setup_ts_tag() - local group = vim.api.nvim_create_augroup('nvim-ts-autotag', { clear = true }) + local group = vim.api.nvim_create_augroup("nvim-ts-autotag", { clear = true }) if M.enable_close == true then vim.api.nvim_buf_set_keymap(bufnr or 0, "i", ">", ">", { noremap = true, @@ -589,7 +589,7 @@ M.attach = function(bufnr, lang) 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, { "/" }) if is_before_arrow() then - log.debug('is_before_arrow') + log.debug("is_before_arrow") M.close_slash_tag() end local new_row, new_col = unpack(vim.api.nvim_win_get_cursor(0)) diff --git a/lua/nvim-ts-autotag/utils.lua b/lua/nvim-ts-autotag/utils.lua index fd66137..8f9e40d 100644 --- a/lua/nvim-ts-autotag/utils.lua +++ b/lua/nvim-ts-autotag/utils.lua @@ -1,19 +1,16 @@ -local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils') -local log = require('nvim-ts-autotag._log') +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 or vim.treesitter.query.get_node_text or ts_utils.get_node_text local M = {} M.get_node_text = function(node) local _, txt = pcall(get_node_text, node, vim.api.nvim_get_current_buf()) - return vim.split(txt, '\n') or {} + return vim.split(txt, "\n") or {} end M.verify_node = function(node, node_tag) local txt = get_node_text(node, vim.api.nvim_get_current_buf()) - if - txt:match(string.format('^<%s>', node_tag)) - and txt:match(string.format('$', node_tag)) - then + if txt:match(string.format("^<%s>", node_tag)) and txt:match(string.format("$", node_tag)) then return true end return false @@ -30,22 +27,22 @@ M.dump_node = function(node) end M.is_close_empty_node = function(node) - local tag_name = '' + local tag_name = "" if node ~= nil then local text = M.get_node_text(node) tag_name = text[#text - 1] end - return tag_name:match('%<%/%>$') + return tag_name:match("%<%/%>$") end M.dump_node_text = function(target) - log.debug('=============================') + log.debug("=============================") for node in target:iter_children() do local node_type = node:type() local text = M.get_node_text(node) - log.debug('type:' .. node_type .. ' ') + log.debug("type:" .. node_type .. " ") log.debug(text) end - log.debug('=============================') + log.debug("=============================") end return M diff --git a/tests/close_slash_tag_spec.lua b/tests/close_slash_tag_spec.lua index 86eaf8b..a81c108 100644 --- a/tests/close_slash_tag_spec.lua +++ b/tests/close_slash_tag_spec.lua @@ -1,4 +1,4 @@ -local ts = require('nvim-treesitter.configs') +local ts = require("nvim-treesitter.configs") ts.setup({ ensure_installed = _G.ts_filetypes, highlight = { enable = true }, @@ -6,162 +6,162 @@ ts.setup({ local data = { { - name = '1 html close tag after inputting /', - filepath = './sample/index.html', - filetype = 'html', + name = "1 html close tag after inputting /", + filepath = "./sample/index.html", + filetype = "html", linenr = 10, key = [[/]], before = [[
<| ]], after = [[
|]], }, { - name = '2 html close tag after inputting /', - filepath = './sample/index.html', - filetype = 'html', + name = "2 html close tag after inputting /", + filepath = "./sample/index.html", + filetype = "html", linenr = 10, key = [[/]], before = [[
<| ]], after = [[
|]], }, { - name = '3 html don\'t close tag when no opening tag is found', - filepath = './sample/index.html', - filetype = 'html', + name = "3 html don't close tag when no opening tag is found", + filepath = "./sample/index.html", + filetype = "html", linenr = 10, key = [[/>]], before = [[
<|
]], after = [[
|
]], }, { - name = '4 html not close inside quote', - filepath = './sample/index.html', - filetype = 'html', + name = "4 html not close inside quote", + filepath = "./sample/index.html", + filetype = "html", linenr = 10, key = [[/]], before = [[
]], after = [[
]], }, { - name = '5 typescriptreact close tag after inputting /', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "5 typescriptreact close tag after inputting /", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 12, key = [[/]], before = [[<| ]], after = [[| ]], }, { - name = '6 typescriptreact close after inputting /', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "6 typescriptreact close after inputting /", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 12, key = [[/]], before = [[
<| ]], after = [[
| ]], }, { - name = '7 typescriptreact close on inline script after inputting /', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "7 typescriptreact close on inline script after inputting /", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 9, key = [[/]], before = [[const a = () =>
<| ]], after = [[const a = () =>
| ]], }, { - name = '8 typescriptreact not close on close tag', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "8 typescriptreact not close on close tag", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 12, key = [[/]], before = [[| ]], }, { - name = '9 typescriptreact not close on expresion', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "9 typescriptreact not close on expresion", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 12, key = [[/]], before = [[ ]], after = [[ ]], }, { - name = '10 typescriptreact not close on typescript', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "10 typescriptreact not close on typescript", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 6, key = [[/]], before = [[const data:Array
}]], after = [[{(card.data /| 0) &&
}]], }, { - name = '12 vue close tag after inputting /', - filepath = './sample/index.vue', - filetype = 'vue', + name = "12 vue close tag after inputting /", + filepath = "./sample/index.vue", + filetype = "vue", linenr = 4, key = [[/]], before = [[<| ]], after = [[|]], }, { - name = '13 vue not close on script', - filepath = './sample/index.vue', - filetype = 'vue', + name = "13 vue not close on script", + filepath = "./sample/index.vue", + filetype = "vue", linenr = 12, key = [[/]], before = [[const data:Array<| ]], after = [[| ]], }, { - name = '15 php close tag after inputting /', - filepath = './sample/index.php', - filetype = 'php', + name = "15 php close tag after inputting /", + filepath = "./sample/index.php", + filetype = "php", linenr = 25, key = [[/]], before = [[
<| ]], after = [[
| ]], }, { - name = '16 lit template div close after inputting /', - filepath = './sample/index.ts', - filetype = 'typescript', + name = "16 lit template div close after inputting /", + filepath = "./sample/index.ts", + filetype = "typescript", linenr = 3, key = [[/]], before = [[
<| ]], after = [[
| ]], }, { - name = '17 eruby template div close after inputting /', - filepath = './sample/index.html.erb', - filetype = 'eruby', + name = "17 eruby template div close after inputting /", + filepath = "./sample/index.html.erb", + filetype = "eruby", linenr = 10, key = [[/]], before = [[
<| ]], after = [[
| ]], }, { - name = '18 eruby template ruby string write raw /', - filepath = './sample/index.html.erb', - filetype = 'eruby', + name = "18 eruby template ruby string write raw /", + filepath = "./sample/index.html.erb", + filetype = "eruby", linenr = 10, key = [[/]], before = [[<%= ]], @@ -169,13 +169,13 @@ local data = { }, } -local autotag = require('nvim-ts-autotag') +local autotag = require("nvim-ts-autotag") autotag.test = true local run_data = _G.Test_filter(data) -describe('[close slash tag]', function() +describe("[close slash tag]", function() _G.Test_withfile(run_data, { - mode = 'i', + mode = "i", cursor_add = 0, }) end) diff --git a/tests/closetag_spec.lua b/tests/closetag_spec.lua index 2d1a6d0..90796e5 100644 --- a/tests/closetag_spec.lua +++ b/tests/closetag_spec.lua @@ -1,4 +1,4 @@ -local ts = require('nvim-treesitter.configs') +local ts = require("nvim-treesitter.configs") ts.setup({ ensure_installed = _G.ts_filetypes, highlight = { enable = true }, @@ -6,90 +6,90 @@ ts.setup({ local data = { { - name = '1 html close tag', - filepath = './sample/index.html', - filetype = 'html', + name = "1 html close tag", + filepath = "./sample/index.html", + filetype = "html", linenr = 10, key = [[>]], before = [[|
]], }, { - name = '2 html close tag', - filepath = './sample/index.html', - filetype = 'html', + name = "2 html close tag", + filepath = "./sample/index.html", + filetype = "html", linenr = 10, key = [[>]], before = [[
|
]], }, { - name = '3 html not close tag on close tag', - filepath = './sample/index.html', - filetype = 'html', + name = "3 html not close tag on close tag", + filepath = "./sample/index.html", + filetype = "html", linenr = 10, key = [[>]], before = [[
aaaa
|]], }, { - name = '4 html not close on input tag', - filepath = './sample/index.html', - filetype = 'html', + name = "4 html not close on input tag", + filepath = "./sample/index.html", + filetype = "html", linenr = 10, key = [[>]], before = [[| ]], }, { - name = '5 html not close inside quote', - filepath = './sample/index.html', - filetype = 'html', + name = "5 html not close inside quote", + filepath = "./sample/index.html", + filetype = "html", linenr = 10, key = [[>]], before = [[
]], after = [[
]], }, { - name = '6 html not close on exist tag', - filepath = './sample/index.html', - filetype = 'html', + name = "6 html not close on exist tag", + filepath = "./sample/index.html", + filetype = "html", linenr = 10, key = [[>]], before = [[
]], after = [[
|
]], }, { - name = '7 typescriptreact close tag', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "7 typescriptreact close tag", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 12, key = [[>]], before = [[| ]], }, { - name = '8 typescriptreact close', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "8 typescriptreact close", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 12, key = [[>]], before = [[
|
]], }, { - name = '9 typescriptreact not close on exist tag', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "9 typescriptreact not close on exist tag", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 12, key = [[>]], before = [[
]], after = [[
|
]], }, { - name = '10 typescriptreact close on inline script', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "10 typescriptreact close on inline script", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 9, key = [[>]], before = [[const a = () => ]], before = [[| ]], }, { - name = '12 typescriptreact not close on expresion', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "12 typescriptreact not close on expresion", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 12, key = [[>]], before = [[ ]], after = [[ ]], }, { - name = '13 typescriptreact not close on typescript', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "13 typescriptreact not close on typescript", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 6, key = [[>]], before = [[const data:Array]], before = [[{(card.data | 0) &&
}]], after = [[{(card.data >| 0) &&
}]], }, { - name = '15 vue auto close tag', - filepath = './sample/index.vue', - filetype = 'vue', + name = "15 vue auto close tag", + filepath = "./sample/index.vue", + filetype = "vue", linenr = 4, key = [[>]], before = [[|]], }, { - name = '16 vue not close on script', - filepath = './sample/index.vue', - filetype = 'vue', + name = "16 vue not close on script", + filepath = "./sample/index.vue", + filetype = "vue", linenr = 12, key = [[>]], before = [[const data:Array| ]], }, { - name = '17 typescriptreact nested indentifer ', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "17 typescriptreact nested indentifer ", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 12, key = [[>]], before = [[| ]], }, { - name = '18 php div ', - filepath = './sample/index.php', - filetype = 'php', + name = "18 php div ", + filepath = "./sample/index.php", + filetype = "php", linenr = 25, key = [[>]], before = [[|
]], -- }, { - name = '19 lit template div', - filepath = './sample/index.ts', - filetype = 'typescript', + name = "19 lit template div", + filepath = "./sample/index.ts", + filetype = "typescript", linenr = 3, key = [[>]], before = [[|
]], }, { - name = '20 eruby template div', - filepath = './sample/index.html.erb', - filetype = 'eruby', + name = "20 eruby template div", + filepath = "./sample/index.html.erb", + filetype = "eruby", linenr = 10, key = [[>]], before = [[|
]], }, { - name = '20 eruby template ruby string', - filepath = './sample/index.html.erb', - filetype = 'eruby', + name = "20 eruby template ruby string", + filepath = "./sample/index.html.erb", + filetype = "eruby", linenr = 10, key = [[>]], before = [[<%= ]], @@ -207,13 +207,13 @@ local data = { }, } -local autotag = require('nvim-ts-autotag') +local autotag = require("nvim-ts-autotag") autotag.test = true local run_data = _G.Test_filter(data) -describe('[close tag]', function() +describe("[close tag]", function() _G.Test_withfile(run_data, { - mode = 'i', + mode = "i", cursor_add = 0, before_each = function(value) end, }) diff --git a/tests/renametag_spec.lua b/tests/renametag_spec.lua index a18fe0e..539cbf6 100644 --- a/tests/renametag_spec.lua +++ b/tests/renametag_spec.lua @@ -1,4 +1,4 @@ -local ts = require 'nvim-treesitter.configs' +local ts = require("nvim-treesitter.configs") ts.setup({ ensure_installed = _G.ts_filetypes, highlight = { @@ -8,39 +8,38 @@ ts.setup({ fold = { enable = false }, }) - local data = { { - name = 'html rename open tag', - filepath = './sample/index.html', - filetype = 'html', + name = "html rename open tag", + filepath = "./sample/index.html", + filetype = "html", linenr = 10, key = [[ciwlala]], before = [[ dsadsa
]], after = [[ dsadsa ]], }, { - name = 'html rename open tag with attr', - filepath = './sample/index.html', - filetype = 'html', + name = "html rename open tag with attr", + filepath = "./sample/index.html", + filetype = "html", linenr = 10, key = [[ciwlala]], before = [[ dsadsa
]], after = [[ dsadsa ]], }, { - name = 'html rename close tag with attr', - filepath = './sample/index.html', - filetype = 'html', + name = "html rename close tag with attr", + filepath = "./sample/index.html", + filetype = "html", linenr = 10, key = [[ciwlala]], before = [[
dsadsa ]], after = [[ dsadsa ]], }, { - name = 'html not rename close tag on char <', - filepath = './sample/index.html', - filetype = 'html', + name = "html not rename close tag on char <", + filepath = "./sample/index.html", + filetype = "html", linenr = 10, key = [[i<]], before = [[
dsadsa |/button> ]], @@ -48,9 +47,9 @@ local data = { }, { - name = 'html not rename close tag with not valid', - filepath = './sample/index.html', - filetype = 'html', + name = "html not rename close tag with not valid", + filepath = "./sample/index.html", + filetype = "html", linenr = 12, key = [[ciwlala]], before = { @@ -74,9 +73,9 @@ local data = { -- }, { - name = 'html not rename close tag with not valid', - filepath = './sample/index.html', - filetype = 'html', + name = "html not rename close tag with not valid", + filepath = "./sample/index.html", + filetype = "html", linenr = 12, key = [[ciwlala]], before = { @@ -85,202 +84,202 @@ local data = { after = [[
]], }, { - name = 'typescriptreact rename open tag', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "typescriptreact rename open tag", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 12, key = [[ciwlala]], before = [[ dsadsa
]], after = [[ dsadsa ]], }, { - name = 'typescriptreact rename open tag with attr', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "typescriptreact rename open tag with attr", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 12, key = [[ciwlala]], before = [[ dsadsa
]], after = [[ dsadsa
]], }, { - name = 'typescriptreact rename close tag with attr', - filepath = './sample/index.tsx', - filetype = 'html', + name = "typescriptreact rename close tag with attr", + filepath = "./sample/index.tsx", + filetype = "html", linenr = 12, key = [[ciwlala]], before = [[
dsadsa ]], after = [[ dsadsa ]], }, { - name = '17 typescriptreact nested indentifer ', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "17 typescriptreact nested indentifer ", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 12, key = [[ciwlala]], before = [[ ]], after = [[ ]], }, { - name = '18 rename empty node ', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "18 rename empty node ", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 12, key = [[ilala]], before = [[<|>
]], after = [[
]], }, { - name = '19 rename start tag on svelte ', - filepath = './sample/index.svelte', - filetype = 'svelte', + name = "19 rename start tag on svelte ", + filepath = "./sample/index.svelte", + filetype = "svelte", linenr = 18, key = [[ciwlala]], before = [[<|data>]], after = [[]], }, { - name = '20 rename end tag on svelte ', - filepath = './sample/index.svelte', - filetype = 'svelte', + name = "20 rename end tag on svelte ", + filepath = "./sample/index.svelte", + filetype = "svelte", linenr = 18, key = [[ciwlala]], before = [[]], after = [[]], }, { - name = "21 rescript rename open tag", - filepath = './sample/index.res', + name = "21 rescript rename open tag", + filepath = "./sample/index.res", filetype = "rescript", - linenr = 12, - key = [[ciwlala]], - before = [[ dsadsa
]], - after = [[ dsadsa ]] - }, - { - name = "22 rescript rename open tag with attr", - filepath = './sample/index.res', - filetype = "rescript", - linenr = 12, - key = [[ciwlala]], - before = [[ dsadsa
]], - after = [[ dsadsa ]] - }, - { - name = "23 rescript rename close tag with attr", - filepath = './sample/index.res', - filetype = "rescript", - linenr = 12, - key = [[ciwlala]], - before = [[
dsadsa ]], - after = [[ dsadsa ]] - }, - { - name = '24 test check rename same with parent', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', linenr = 12, - key = 'ciwkey', + key = [[ciwlala]], + before = [[ dsadsa
]], + after = [[ dsadsa ]], + }, + { + name = "22 rescript rename open tag with attr", + filepath = "./sample/index.res", + filetype = "rescript", + linenr = 12, + key = [[ciwlala]], + before = [[ dsadsa ]], + after = [[ dsadsa ]], + }, + { + name = "23 rescript rename close tag with attr", + filepath = "./sample/index.res", + filetype = "rescript", + linenr = 12, + key = [[ciwlala]], + before = [[
dsadsa ]], + after = [[ dsadsa ]], + }, + { + name = "24 test check rename same with parent", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", + linenr = 12, + key = "ciwkey", before = { - '', - ' ', - '', - ' ', - '', + "", + " ", + "", + " ", + "", }, after = { - '', - ' ', - '', - ' ', - '', + "", + " ", + "", + " ", + "", }, }, { - name = '25 rename start have same node with parent', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "25 rename start have same node with parent", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 12, key = [[ciwlala]], before = { - '
', - ' ', - ' test ', - '
', - '
', + "
", + " ", + " test ", + "
", + "", }, after = { - '
', - ' ', - ' test ', - ' ', - '
', + "
", + " ", + " test ", + " ", + "
", }, }, { - name = '26 rename should not rename tag on attribute node', - filepath = './sample/index.tsx', - filetype = 'typescriptreact', + name = "26 rename should not rename tag on attribute node", + filepath = "./sample/index.tsx", + filetype = "typescriptreact", linenr = 12, key = [[ciwlala]], before = { - '
', + "
", '', '
', '
', - '
', - '
', - '
', + "
", + " ", + "", }, after = { - '
', + "
", '', '
', '
', - '
', - '
', - '
', + "
", + " ", + "", }, }, { - name = 'eruby rename open tag', - filepath = './sample/index.html.erb', - filetype = 'eruby', + name = "eruby rename open tag", + filepath = "./sample/index.html.erb", + filetype = "eruby", linenr = 10, key = [[ciwlala]], before = [[ dsadsa ]], after = [[ dsadsa ]], }, { - name = 'eruby rename open tag with attr', - filepath = './sample/index.html.erb', - filetype = 'eruby', + name = "eruby rename open tag with attr", + filepath = "./sample/index.html.erb", + filetype = "eruby", linenr = 10, key = [[ciwlala]], before = [[ dsadsa ]], after = [[ dsadsa ]], }, { - name = 'eruby rename close tag with attr', - filepath = './sample/index.html.erb', - filetype = 'eruby', + name = "eruby rename close tag with attr", + filepath = "./sample/index.html.erb", + filetype = "eruby", linenr = 10, key = [[ciwlala]], before = [[
dsadsa ]], after = [[ dsadsa ]], }, { - name = 'eruby not rename close tag on char <', - filepath = './sample/index.html.erb', - filetype = 'eruby', + name = "eruby not rename close tag on char <", + filepath = "./sample/index.html.erb", + filetype = "eruby", linenr = 10, key = [[i<]], before = [[
dsadsa |/button> ]], after = [[
dsadsa <|/button> ]], }, { - name = 'eruby not rename close tag with not valid', - filepath = './sample/index.html.erb', - filetype = 'eruby', + name = "eruby not rename close tag with not valid", + filepath = "./sample/index.html.erb", + filetype = "eruby", linenr = 12, key = [[ciwlala]], before = { @@ -290,9 +289,9 @@ local data = { after = [[]], }, { - name = 'eruby not rename tag-like ruby string', - filepath = './sample/index.html.erb', - filetype = 'eruby', + name = "eruby not rename tag-like ruby string", + filepath = "./sample/index.html.erb", + filetype = "eruby", linenr = 12, key = [[ciwlala]], before = { @@ -313,12 +312,12 @@ local data = { }, } -local autotag = require('nvim-ts-autotag') +local autotag = require("nvim-ts-autotag") autotag.test = true local run_data = _G.Test_filter(data) -describe('[rename tag]', function() +describe("[rename tag]", function() _G.Test_withfile(run_data, { cursor_add = 0, before_each = function(value) end, diff --git a/tests/test-utils.lua b/tests/test-utils.lua index 072485c..11fd88b 100644 --- a/tests/test-utils.lua +++ b/tests/test-utils.lua @@ -1,11 +1,11 @@ -local utils = require('nvim-ts-autotag.utils') -local log = require('nvim-ts-autotag._log') +local utils = require("nvim-ts-autotag.utils") +local log = require("nvim-ts-autotag._log") local api = vim.api local helpers = {} function helpers.feed(text, feed_opts, is_replace) - feed_opts = feed_opts or 'n' + feed_opts = feed_opts or "n" if not is_replace then text = vim.api.nvim_replace_termcodes(text, true, false, true) end @@ -13,23 +13,19 @@ function helpers.feed(text, feed_opts, is_replace) end function helpers.insert(text, is_replace) - helpers.feed('i' .. text, 'x', is_replace) + helpers.feed("i" .. text, "x", is_replace) end utils.insert_char = function(text) - api.nvim_put({ text }, 'c', true, true) + api.nvim_put({ text }, "c", true, true) end utils.feed = function(text, num) - local result = '' + local result = "" for _ = 1, num, 1 do result = result .. text end - api.nvim_feedkeys( - api.nvim_replace_termcodes(result, true, false, true), - 'x', - true - ) + api.nvim_feedkeys(api.nvim_replace_termcodes(result, true, false, true), "x", true) end _G.eq = assert.are.same @@ -50,38 +46,22 @@ end local compare_text = function(linenr, text_after, name, cursor_add, end_cursor) cursor_add = cursor_add or 0 - local new_text = vim.api.nvim_buf_get_lines( - 0, - linenr - 1, - linenr + #text_after - 1, - true - ) + local new_text = vim.api.nvim_buf_get_lines(0, linenr - 1, linenr + #text_after - 1, true) for i = 1, #text_after, 1 do - local t = string.gsub(text_after[i], '%|', '') - if t - and new_text[i] - and t:gsub('%s+$', '') ~= new_text[i]:gsub('%s+$', '') - then - eq(t, new_text[i], '\n\n text error: ' .. name .. '\n') + local t = string.gsub(text_after[i], "%|", "") + if t and new_text[i] and t:gsub("%s+$", "") ~= new_text[i]:gsub("%s+$", "") then + eq(t, new_text[i], "\n\n text error: " .. name .. "\n") end - local p_after = string.find(text_after[i], '%|') + local p_after = string.find(text_after[i], "%|") if p_after then local row, col = utils.get_cursor() if end_cursor then - eq(row, linenr + i - 2, '\n\n cursor row error: ' .. name .. '\n') - eq( - col + 1, - end_cursor, - '\n\n end cursor column error : ' .. name .. '\n' - ) + eq(row, linenr + i - 2, "\n\n cursor row error: " .. name .. "\n") + eq(col + 1, end_cursor, "\n\n end cursor column error : " .. name .. "\n") else - eq(row, linenr + i - 2, '\n\n cursor row error: ' .. name .. '\n') + eq(row, linenr + i - 2, "\n\n cursor row error: " .. name .. "\n") p_after = p_after + cursor_add - eq( - col, - math.max(p_after - 2, 0), - '\n\n cursor column error : ' .. name .. '\n' - ) + eq(col, math.max(p_after - 2, 0), "\n\n cursor column error : " .. name .. "\n") end end end @@ -90,7 +70,7 @@ end _G.Test_withfile = function(test_data, cb) for _, value in pairs(test_data) do - it('test ' .. value.name, function(done) + it("test " .. value.name, function(done) local text_before = {} value.linenr = value.linenr or 1 local pos_before = { @@ -101,11 +81,11 @@ _G.Test_withfile = function(test_data, cb) value.before = { value.before } end for index, text in pairs(value.before) do - local txt = string.gsub(text, '%|', '') + local txt = string.gsub(text, "%|", "") table.insert(text_before, txt) - if string.match(text, '%|') then - if string.find(text, '%|') then - pos_before.colnr = string.find(text, '%|') + if string.match(text, "%|") then + if string.find(text, "%|") then + pos_before.colnr = string.find(text, "%|") pos_before.linenr = value.linenr + index - 1 end end @@ -113,61 +93,46 @@ _G.Test_withfile = function(test_data, cb) if not vim.tbl_islist(value.after) then value.after = { value.after } end - vim.bo.filetype = value.filetype or 'text' - vim.cmd(':bd!') + vim.bo.filetype = value.filetype or "text" + vim.cmd(":bd!") if cb.before_each then cb.before_each(value) end if vim.fn.filereadable(vim.fn.expand(value.filepath)) == 1 then - vim.cmd(':e ' .. value.filepath) + vim.cmd(":e " .. value.filepath) if value.filetype then vim.bo.filetype = value.filetype end - vim.cmd(':e') + vim.cmd(":e") else - vim.cmd(':new') + vim.cmd(":new") if value.filetype then vim.bo.filetype = value.filetype end end - vim.api.nvim_buf_set_lines( - 0, - value.linenr - 1, - value.linenr + #text_before, - false, - text_before - ) - vim.api.nvim_win_set_cursor( - 0, - { pos_before.linenr, pos_before.colnr - 1 } - ) - log.debug('insert:' .. value.key) + vim.api.nvim_buf_set_lines(0, value.linenr - 1, value.linenr + #text_before, false, text_before) + vim.api.nvim_win_set_cursor(0, { pos_before.linenr, pos_before.colnr - 1 }) + log.debug("insert:" .. value.key) - if type(value.key) == 'string' then - if cb.mode == 'i' then + if type(value.key) == "string" then + if cb.mode == "i" then helpers.insert(value.key, value.not_replace_term_code) else - helpers.feed(value.key, 'x') + helpers.feed(value.key, "x") end else for _, key in pairs(value.key) do - helpers.feed(key, 'x') + helpers.feed(key, "x") vim.wait(1) end end vim.wait(2) - helpers.feed('') - compare_text( - value.linenr, - value.after, - value.name, - cb.cursor_add, - value.end_cursor - ) + helpers.feed("") + compare_text(value.linenr, value.after, value.name, cb.cursor_add, value.end_cursor) if cb.after_each then cb.after_each(value) end - vim.cmd(':bd!') + vim.cmd(":bd!") end) end end @@ -183,7 +148,7 @@ _G.dump_node_text = function(target) for node in target:iter_children() do local node_type = node:type() local text = utils.get_node_text(node) - log.debug('type:' .. node_type .. ' ') + log.debug("type:" .. node_type .. " ") log.debug(text) end end