210 lines
5.8 KiB
Lua
210 lines
5.8 KiB
Lua
local ArgonautConfig = {
|
|
line_prefix = '',
|
|
padded_braces = {},
|
|
tail_comma = false,
|
|
tail_comma_braces = {},
|
|
tail_indent_braces = {},
|
|
wrap_closing_brace = true,
|
|
comma_first = false,
|
|
comma_first_indent = false,
|
|
}
|
|
|
|
local function setup(opts)
|
|
if opts then
|
|
for key, value in pairs(opts) do
|
|
ArgonautConfig[key] = value
|
|
end
|
|
end
|
|
end
|
|
|
|
local function get_cursor_pos()
|
|
local _, row, col, _ = unpack(vim.fn.getpos('.'))
|
|
return {row = row, col = col}
|
|
end
|
|
|
|
local function is_string_literal(pos)
|
|
if not pos then
|
|
pos = get_cursor_pos()
|
|
end
|
|
|
|
local syn_id = vim.fn.synID(pos.row, pos.col, false)
|
|
local syn_attr = vim.fn.synIDattr(syn_id, 'name')
|
|
|
|
return syn_attr:find('String$')
|
|
end
|
|
|
|
local function find_brace_alt(brace)
|
|
local brace_pairs = {{'(', ')'}, {'[', ']'}, {'{', '}'}, {'<', '>'}}
|
|
|
|
for _, brace_pair in ipairs(brace_pairs) do
|
|
if brace_pair[1] == brace then
|
|
return brace_pair[2], true
|
|
elseif brace_pair[2] == brace then
|
|
return brace_pair[1], false
|
|
end
|
|
end
|
|
end
|
|
|
|
local function escape_brace(brace)
|
|
if brace == '[' or brace == ']' then
|
|
return '\\' .. brace
|
|
else
|
|
return brace
|
|
end
|
|
end
|
|
|
|
local function find_brace_range(brace)
|
|
local brace_alt, _ = find_brace_alt(brace)
|
|
assert(brace_alt)
|
|
|
|
---@diagnostic disable-next-line: param-type-mismatch
|
|
local row1, col1 = unpack(vim.fn.searchpairpos(escape_brace(brace), '', escape_brace(brace_alt), 'Wnb', is_string_literal))
|
|
if row1 > 0 and col1 > 0 then
|
|
---@diagnostic disable-next-line: param-type-mismatch
|
|
local row2, col2 = unpack(vim.fn.searchpairpos(escape_brace(brace), '', escape_brace(brace_alt), 'Wcn', is_string_literal))
|
|
if row2 > 0 and col2 > 0 then
|
|
return {
|
|
brace = brace,
|
|
row1 = row1,
|
|
col1 = col1,
|
|
row2 = row2,
|
|
col2 = col2
|
|
}
|
|
end
|
|
end
|
|
end
|
|
|
|
local function find_all_brace_ranges(braces)
|
|
local brace_ranges = {}
|
|
for _, brace in ipairs(braces) do
|
|
local brace_range = find_brace_range(brace)
|
|
if brace_range then
|
|
table.insert(brace_ranges, brace_range)
|
|
end
|
|
end
|
|
|
|
if #brace_ranges > 0 then
|
|
return brace_ranges
|
|
end
|
|
end
|
|
|
|
local function find_closest_brace_range(braces)
|
|
local cursor_pos = get_cursor_pos()
|
|
local compare_brace_ranges = function(brace_range_1, brace_range_2)
|
|
local row_diff1 = cursor_pos.row - brace_range_1.row1
|
|
local row_diff2 = cursor_pos.row - brace_range_2.row1
|
|
if row_diff1 < row_diff2 then
|
|
return -1
|
|
elseif row_diff1 > row_diff2 then
|
|
return 1
|
|
end
|
|
|
|
local col_diff1 = cursor_pos.col - brace_range_1.col1
|
|
local col_diff2 = cursor_pos.col - brace_range_2.col1
|
|
if col_diff1 < col_diff2 then
|
|
return -1
|
|
elseif col_diff1 > col_diff2 then
|
|
return 1
|
|
end
|
|
|
|
return 0
|
|
end
|
|
|
|
local brace_ranges = find_all_brace_ranges(braces)
|
|
if brace_ranges then
|
|
return vim.fn.sort(brace_ranges, compare_brace_ranges)[1]
|
|
end
|
|
end
|
|
|
|
local function parse_brace_range(brace_range)
|
|
brace_range.params = {}
|
|
brace_range.indent = 0
|
|
|
|
local brace_range_param = ''
|
|
local flush_brace_range_param = function()
|
|
if #brace_range_param > 0 then
|
|
brace_range_param, _ = brace_range_param:gsub('^%s*([%S%s]-)%s*$', '%1')
|
|
table.insert(brace_range.params, brace_range_param)
|
|
brace_range_param = ''
|
|
end
|
|
end
|
|
|
|
local brace_stack = {}
|
|
local update_brace_stack = function(c)
|
|
local brace_stack_size = #brace_stack
|
|
local brace_alt, brace_open = find_brace_alt(c)
|
|
if brace_stack_size > 0 and brace_alt == brace_stack[brace_stack_size] and not brace_open then
|
|
table.remove(brace_stack, brace_stack_size)
|
|
elseif brace_alt then
|
|
table.insert(brace_stack, c)
|
|
end
|
|
end
|
|
|
|
local brace_range_elements = {}
|
|
for row = brace_range.row1, brace_range.row2 do
|
|
local line = vim.fn.getline(row)
|
|
|
|
local col1 = 0
|
|
if row == brace_range.row1 then
|
|
col1 = brace_range.col1 + 1
|
|
_, brace_range.indent = line:find('^%s*') ---@diagnostic disable-line: undefined-field
|
|
end
|
|
|
|
local col2 = #line
|
|
if row == brace_range.row2 then
|
|
col2 = brace_range.col2 - 1
|
|
end
|
|
|
|
for i = col1, col2 do
|
|
table.insert(brace_range_elements, {
|
|
char = line:sub(i, i), ---@diagnostic disable-line: undefined-field
|
|
literal = is_string_literal({row = row, col = i}),
|
|
})
|
|
end
|
|
end
|
|
|
|
if #brace_range_elements > 0 then
|
|
for _, brace_range_element in ipairs(brace_range_elements) do
|
|
local append = true
|
|
if not brace_range_element.literal then
|
|
update_brace_stack(brace_range_element.char)
|
|
if #brace_stack == 0 and brace_range_element.char == ',' then
|
|
flush_brace_range_param()
|
|
append = false
|
|
end
|
|
end
|
|
|
|
if append then
|
|
brace_range_param = brace_range_param .. brace_range_element.char
|
|
end
|
|
end
|
|
|
|
flush_brace_range_param()
|
|
end
|
|
end
|
|
|
|
local function wrap_brace_range(brace_range)
|
|
|
|
end
|
|
|
|
local function unwrap_brace_range(brace_range)
|
|
|
|
end
|
|
|
|
local function reflow()
|
|
local brace_range = find_closest_brace_range({'(', '[', '{', '<'})
|
|
if brace_range then
|
|
parse_brace_range(brace_range)
|
|
if brace_range.row1 == brace_range.row2 then
|
|
wrap_brace_range(brace_range)
|
|
else
|
|
unwrap_brace_range()
|
|
end
|
|
end
|
|
end
|
|
|
|
return {
|
|
reflow = reflow,
|
|
ssetup = setup,
|
|
}
|