Skip to content

Commit

Permalink
fix(html): indents
Browse files Browse the repository at this point in the history
  • Loading branch information
bennypowers committed Aug 12, 2024
1 parent 8e5e5ec commit fa86021
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
44 changes: 26 additions & 18 deletions lua/splitjoin/languages/html/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,36 @@ local function get_element(node)
return element
end

---@param node TSNode
local function split_attrs(node, options)
local parent = node:parent()
local base_indent = Node.get_base_indent(parent)
local indent = base_indent .. (options.default_indent or ' ')
if options.aligned then
indent = base_indent .. vim.split(Node.get_text(parent), '%s', {})[1]:gsub('.', ' ') .. ' '
end
for child in parent:iter_children() do
local child_type = child:type()
if child_type == 'attribute' then
local first_attr_child = child:parent():child(2)
local prefix = ''
if first_attr_child ~= child then
prefix = '\n'..indent
local element = node:parent()
if element then
local original_text = Node.get_text(element)
local base_indent = Node.get_base_indent(element)
local indent = base_indent .. (options.default_indent or ' ')
if options.aligned then
indent = base_indent .. vim.split(Node.get_text(element), '%s')[1]:gsub('.', ' ') .. ' '
end
for child in element:iter_children() do
if child:type() == 'attribute' then
local attr = child
local first_attr_child = attr:parent():child(2)
local prefix = ''
if first_attr_child ~= child then
prefix = '\n'..indent
end
local new_text = prefix..Node.get_text(attr)
-- TODO: this fails when successfully splitting, then moving the cursor to a separate parent
pcall(Node.replace, attr, new_text)
end
Node.replace(child, prefix..Node.get_text(child))
end
for child in element:iter_children() do
pcall(Node.trim_line_end,child)
end
Node.goto_node(node, 'start', 1)
vim.treesitter.get_parser(0, 'html'):invalidate(true)
vim.treesitter.get_parser(0, 'html'):invalidate(true)
end
for child in parent:iter_children() do
pcall(Node.trim_line_end,child)
end
Node.goto_node(node, 'start', 1)
end

local function split_children(node, options)
Expand Down
13 changes: 7 additions & 6 deletions lua/splitjoin/util/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ function Node.replace(node, replacement)
end
end
if starts_newline then table.insert(lines, 1, '') end
vim.api.nvim_buf_set_text(0,
row,
col,
row_end,
col_end,
lines)
pcall(vim.api.nvim_buf_set_text,
0,
row,
col,
row_end,
col_end,
lines)
end

---@param node TSNode
Expand Down

0 comments on commit fa86021

Please sign in to comment.