1
This commit is contained in:
Alex Yatskov 2024-04-24 20:02:10 -07:00
parent b89a021ba1
commit 79ddb89088
2 changed files with 76 additions and 79 deletions

View File

@ -1,11 +1,75 @@
local config = require('argonaut.config') local config = require('argonaut.config')
local types = require('argonaut.types') local types = require('argonaut.types')
local function setup(opts, filetypes) local function wrap_brace_range(brace_range, arg_list)
config.set(opts, filetypes) 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
end
end end
return { return {
reflow = types.reflow, reflow = reflow,
setup = setup, setup = config.set,
} }

View File

@ -2,7 +2,7 @@
-- Cursor -- Cursor
-- --
Cursor = {} local Cursor = {}
function Cursor.new(row, col) function Cursor.new(row, col)
local cursor = {row = row, col = col} local cursor = {row = row, col = col}
@ -34,7 +34,7 @@ end
-- BracePair -- BracePair
-- --
BracePair = {} local BracePair = {}
function BracePair.new(open, close) function BracePair.new(open, close)
local brace_pair = {open = open, close = close} local brace_pair = {open = open, close = close}
@ -105,7 +105,7 @@ end
-- BraceStack -- BraceStack
-- --
BraceStack = {} local BraceStack = {}
function BraceStack.new() function BraceStack.new()
local stack = {stack = {}} local stack = {stack = {}}
@ -144,7 +144,7 @@ end
-- BraceRange -- BraceRange
-- --
BraceRange = {} local BraceRange = {}
function BraceRange.new(start_cursor, stop_cursor, brace_pair, brace_params) function BraceRange.new(start_cursor, stop_cursor, brace_pair, brace_params)
local brace_range = { local brace_range = {
@ -213,7 +213,7 @@ end
-- Arg -- Arg
-- --
Arg = {} local Arg = {}
function Arg.new(text, brace_pair) function Arg.new(text, brace_pair)
local arg = { local arg = {
@ -232,7 +232,7 @@ end
-- ArgList -- ArgList
-- --
ArgList = {} local ArgList = {}
function ArgList.new() function ArgList.new()
local arg_list = { local arg_list = {
@ -301,74 +301,7 @@ 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 = 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 = BraceRange.find_closest_any()
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
return { return {
reflow = reflow BraceRange = BraceRange,
ArgList = ArgList,
} }