1
This commit is contained in:
Alex Yatskov 2024-04-24 20:32:12 -07:00
parent 79ddb89088
commit 14809767fc
2 changed files with 114 additions and 78 deletions

View File

@ -1,71 +1,10 @@
local config = require('argonaut.config')
local types = require('argonaut.types')
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 = arg_list.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 = arg_list.indent .. arg_list.prefix
for i, arg in ipairs(arg_list.args) do
line = line .. arg.text
if i < #arg_list.args then
line = line .. ', '
end
end
line = line .. arg_list.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.row))
end
local function reflow()
local brace_range = types.BraceRange.find_closest_any()
if brace_range then
local arg_list = types.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
local wrap_context = types.WrapContext.new(config.get())
if wrap_context:parse() then
wrap_context:toggle()
end
end

View File

@ -236,9 +236,6 @@ local ArgList = {}
function ArgList.new()
local arg_list = {
indent = '',
prefix = '',
suffix = '',
arg = nil,
args = {},
}
@ -272,13 +269,6 @@ end
function ArgList:parse(brace_range)
local brace_stack = BraceStack:new()
local first_line = vim.fn.getline(brace_range.start_cursor.row)
self.indent = first_line:match('^(%s*)')
self.prefix = first_line:sub(#self.indent + 1, brace_range.start_cursor.col)
local last_line = vim.fn.getline(brace_range.stop_cursor.row)
self.suffix = last_line:sub(brace_range.stop_cursor.col)
for row = brace_range.start_cursor.row, brace_range.stop_cursor.row do
local line = vim.fn.getline(row)
@ -293,15 +283,122 @@ function ArgList:parse(brace_range)
end
for col = start_col, stop_col do
local char = line:sub(col, col)
self:update(char, brace_stack, brace_range, Cursor.new(row, col))
self:update(line:sub(col, col), brace_stack, brace_range, Cursor.new(row, col))
end
end
self:flush()
end
--
-- WrapContext
--
local WrapContext = {}
function WrapContext.new(opts)
local wrap_context = {
opts = opts,
indent = '',
prefix = '',
suffix = '',
brace_range = nil,
arg_list = nil,
}
return setmetatable(wrap_context, {__index = WrapContext})
end
function WrapContext:parse()
self.brace_range = BraceRange.find_closest_any()
if not self.brace_range then
return false
end
local first_line = vim.fn.getline(self.brace_range.start_cursor.row)
self.indent = first_line:match('^(%s*)')
self.prefix = first_line:sub(#self.indent + 1, self.brace_range.start_cursor.col)
local last_line = vim.fn.getline(self.brace_range.stop_cursor.row)
self.suffix = last_line:sub(self.brace_range.stop_cursor.col)
self.arg_list = ArgList.new()
self.arg_list:parse(self.brace_range)
return true
end
function WrapContext:wrap()
local line = self.indent .. self.prefix
for i, arg in ipairs(self.arg_list.args) do
line = line .. arg.text
if i < #self.arg_list.args then
line = line .. ', '
end
end
line = line .. self.suffix
vim.fn.setline(self.brace_range.start_cursor.row, line)
vim.fn.execute(string.format(
'%d,%dd_',
self.brace_range.start_cursor.row + 1,
self.brace_range.stop_cursor.row
))
end
function WrapContext:unwrap()
vim.fn.setline(
self.brace_range.start_cursor.row,
self.indent .. self.prefix
)
-- local cursor_pos = nil
local row = self.brace_range.start_cursor.row
for _, arg in ipairs(self.arg_list.args) do
-- local on_last_row = i == #brace_range.params
local line = self.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, self.indent .. self.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
function WrapContext:toggle()
if self.brace_range:is_wrapped() then
self:wrap()
else
self:unwrap()
end
end
return {
BraceRange = BraceRange,
ArgList = ArgList,
WrapContext = WrapContext,
}