Skip to content

Commit

Permalink
feat: remove-tag
Browse files Browse the repository at this point in the history
  • Loading branch information
dorage committed Dec 31, 2024
1 parent 514cbbf commit b0b982e
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
38 changes: 37 additions & 1 deletion lua/tree-emmet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,43 @@ M.split_join_tag = function()
end

-- https://docs.emmet.io/actions/remove-tag/
M.remove_tag = function() end
M.remove_tag = function()
local element_node = module.get_element_node()
if element_node == nil then
return
end

local start_row, start_col, end_row, end_col = element_node:range()
local lines = vim.api.nvim_buf_get_lines(0, start_row, end_row + 1, false)

-- self closing element
if element_node:type() == "jsx_self_closing_element" then
str.remove_ranges(lines, { { 1, start_col + 1, end_row - start_row + 1, end_col } })
return
end

local opening_start_row, opening_start_col, opening_end_row, opening_end_col =
module.get_opening_element(element_node):range()
local closing_start_row, closing_start_col, closing_end_row, closing_end_col =
module.get_closing_element(element_node):range()

lines = str.remove_ranges(lines, {
{
opening_start_row - start_row + 1,
opening_start_col + 1,
(opening_start_row - start_row + 1) + (opening_end_row - start_row),
opening_end_col,
},
{
closing_start_row - start_row + 1,
closing_start_col + 1,
(closing_start_row - start_row + 1) + (closing_end_row - start_row),
closing_end_col,
},
})

vim.api.nvim_buf_set_lines(0, start_row, end_row + 1, false, lines)
end

-- Merge Lines
-- https://docs.emmet.io/actions/merge-lines/
Expand Down
16 changes: 16 additions & 0 deletions tests/sources/remove-tag_spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const Element = () => {
return (
<>
<div
id="multiline"
onClick={() => {}}
className="test asdfasdjfhasdlkjhasdhfjkasdhfjk"
>
abc
{123}
<div></div>
</div>
<div id="singline">abc</div>
</>
);
};
79 changes: 79 additions & 0 deletions tests/tree-emmet/remove-tag_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
local plugin = require("tree-emmet")
local str = require("tree-emmet.string")

---move cursor to search position
---@param search any
local function move(search)
-- move to text content element
local start_row, start_col = unpack(vim.fn.searchpos(search))
vim.api.nvim_win_set_cursor(0, { start_row, start_col })
end

describe("remove tag in tsx", function()
before_each(function()
vim.cmd("edit tests/sources/remove-tag_spec.tsx")
end)

after_each(function()
-- undo to very first state
vim.cmd("u0")
-- close current buffer
vim.cmd("bd")
end)

it("must remove multiline tag", function()
move("multiline")

plugin.remove_tag()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)

local expected = str.split(
[[export const Element = () => {
return (
<>
abc
{123}
<div></div>
<div id="singline">abc</div>
</>
);
};]],
"[^\n]+"
)

for i = 1, #lines, 1 do
assert.are_equal(str.trim(expected[i]), str.trim(lines[i]))
end
end)

it("must remove tag", function()
move("singline")

plugin.remove_tag()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)

local expected = str.split(
[[export const Element = () => {
return (
<>
<div
id="multiline"
onClick={() => {}}
className="test asdfasdjfhasdlkjhasdhfjkasdhfjk"
>
abc
{123}
<div></div>
</div>
abc
</>
);
};]],
"[^\n]+"
)

for i = 1, #lines, 1 do
assert.are_equal(str.trim(expected[i]), str.trim(lines[i]))
end
end)
end)
1 change: 0 additions & 1 deletion tests/tree-emmet/split-join-tag_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ export const Element = () => {
};]],
"[^\n]+"
)
print(vim.inspect(lines))

for i = 1, #lines, 1 do
assert.are_equal(str.trim(expected_join[i]), str.trim(lines[i]))
Expand Down

0 comments on commit b0b982e

Please sign in to comment.