1
This commit is contained in:
Alex Yatskov 2024-04-23 21:43:26 -07:00
parent f3da4a3f2d
commit 23fd61dab8

View File

@ -187,6 +187,9 @@ function BraceRange.find_closest()
end end
end end
function BraceRange:is_wrapped()
return self.start_cursor.row ~= self.stop_cursor.row
end
-- --
-- Arg -- Arg
@ -279,3 +282,71 @@ function ArgList:parse(brace_range)
self:flush() self:flush()
end end
local function wrap_brace_range(brace_range, arg_list)
vim.fn.setline(brace_range.start_cursor.row, arg_list.indent .. arg_list.prefix)
-- local cursor_pos = nil
local row = brace_range.start_cursor.row
for _, arg in ipairs(arg_list.args) do
-- local on_last_row = i == #brace_range.params
local line = brace_range.indent .. arg.text
-- if opts.tail_comma or not on_last_row then
-- line = line .. ','
-- end
-- if on_last_row and not opts.wrap_closing_brace then
-- line = line .. brace_range.suffix
-- end
vim.fn.append(row, line)
vim.fn.execute(string.format('%d>', row + 1))
-- if param.offset then
-- cursor_pos = get_cursor_pos()
-- cursor_pos.col = cursor_pos.col + param.offset
-- cursor_pos.row = row + 1
-- end
row = row + 1
end
vim.fn.append(row, arg_list.indent .. arg_list.suffix)
-- if opts.wrap_closing_brace then
-- vim.fn.append(row, brace_range.indent .. brace_range.suffix)
-- end
--
-- if cursor_pos then
-- vim.fn.setcursorcharpos({cursor_pos.row, cursor_pos.col})
-- end
end
local function unwrap_brace_range(brace_range, arg_list)
local line = brace_range.indent .. brace_range.prefix
for i, param in ipairs(brace_range.params) do
line = line .. param.text
if i < #brace_range.params then
line = line .. ', '
end
end
line = line .. brace_range.suffix
vim.fn.setline(brace_range.start_cursor.row, line)
vim.fn.execute(string.format('%d,%dd_', brace_range.start_cursor.row + 1, brace_range.stop_cursor.rowrow2))
end
local function reflow()
local brace_range = BraceRange.find_closest()
if brace_range then
local arg_list = ArgList.new()
arg_list.parse(brace_range)
if brace_range.is_wrapped() then
unwrap_brace_range(brace_range, arg_list)
else
wrap_brace_range(brace_range, arg_list)
end
end
end