Skip to content

Commit

Permalink
Improved spacing and indents (#73)
Browse files Browse the repository at this point in the history
* Checking indentation mode

* Collapse extra spacing when `join`

* Remove empty strings except first and last when `split`
  • Loading branch information
Wansmer authored Feb 28, 2023
1 parent 87e5fb6 commit 92bee6e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
10 changes: 8 additions & 2 deletions lua/treesj/treesj/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ function TreeSJ:build_tree(mode)
end

if not tsj:is_ignore('split') and tsj:has_preset() then
local sw = vim.fn.shiftwidth()
local is_norm = tsj:root():preset('split').inner_indent == 'normal'
local need_sw = not (tsj:is_omit() or tsj:parent():is_omit() or is_norm)

local sw = need_sw and vim.fn.shiftwidth() or 0

tsj._root_indent = tsj:get_prev_indent() + sw
end

Expand Down Expand Up @@ -151,7 +155,9 @@ function TreeSJ:split()
end
end

self:_update_text(vim.tbl_flatten(tu._split(self)))
local lines = tu.remove_empty_middle_lines(vim.tbl_flatten(tu._split(self)))

self:_update_text(lines)
end
end

Expand Down
63 changes: 54 additions & 9 deletions lua/treesj/treesj/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -170,25 +170,51 @@ local function set_indent(child)
end

---Append text to last item in list. If last item is a table, merge to last of this table.
---If `to_merge` is a table, merge the first element of `to_merge` to the last element of `lines`
---and push the rest elements to the end of `lines`.
---@param lines string|string[] List-like table
---@vararg string|table
local function merge_text_to_prev_line(lines, ...)
---@param to_merge string|table
local function merge_text_to_prev_line(lines, to_merge)
local prev = lines[#lines]
local text = table.concat({ ... })

if vim.trim(text) == '' or not prev then
if not prev or not to_merge then
return
end

local text = ''
local is_tbl = type(to_merge) == 'table'

if is_tbl then
text = table.remove(to_merge, 1)
else
text = to_merge
end

local prev_text = type(prev) == 'table' and prev[#prev] or lines[#lines]
if vim.endswith(prev_text, ' ') then
text = vim.trim(text)
end

if type(prev) == 'table' then
prev[#prev] = prev_text .. text
else
lines[#lines] = prev_text .. text
if vim.trim(text) ~= '' then
if type(prev) == 'table' then
prev[#prev] = prev_text .. text
else
lines[#lines] = prev_text .. text
end
end

if is_tbl then
lines[#lines + 1] = to_merge
end
end

---Collapse extra spacing: if elem ends with space and next elem starts with space
---@param lines string[]
local function collapse_spacing(lines)
for i, str in ipairs(lines) do
local next = lines[i + 1]
if next and (vim.endswith(str, ' ') and vim.startswith(next, ' ')) then
lines[i] = string.gsub(str, ' $', '')
end
end
end

Expand Down Expand Up @@ -223,6 +249,8 @@ function M._join(tsj)
end
end

collapse_spacing(lines)

return table.concat(lines)
end

Expand Down Expand Up @@ -267,6 +295,23 @@ local function process_configured_container(child, lines)
end
end

---Remove empty strings except first and last
---@param lines string[]
---@return string[]
function M.remove_empty_middle_lines(lines)
local first = table.remove(lines, 1)
local last = table.remove(lines, #lines)
local remover = function(str)
if type(str) == 'string' then
return vim.trim(str) ~= ''
else
return true
end
end

return vim.tbl_flatten({ first, vim.tbl_filter(remover, lines), last })
end

---Make result lines for 'split'
---@param tsj TreeSJ TreeSJ instance
---@return table
Expand Down

0 comments on commit 92bee6e

Please sign in to comment.