2023-01-29 03:20:14 +00:00
|
|
|
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,
|
|
|
|
}
|
2022-12-30 23:10:22 +00:00
|
|
|
|
2023-01-29 03:20:14 +00:00
|
|
|
local function setup(opts)
|
|
|
|
if opts then
|
|
|
|
for key, value in pairs(opts) do
|
|
|
|
ArgonautConfig[key] = value
|
|
|
|
end
|
|
|
|
end
|
2022-12-30 23:10:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function find_range(brace_pair)
|
|
|
|
local filter = 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'
|
2023-01-29 03:20:14 +00:00
|
|
|
local row1, col1 = unpack(vim.fn.searchpairpos(brace_pair[1], '', brace_pair[2], 'Wnb', filter))
|
|
|
|
if row1 > 0 and col1 > 0 then
|
|
|
|
local row2, col2 = unpack(vim.fn.searchpairpos(brace_pair[1], '', brace_pair[2], 'Wcn', filter))
|
|
|
|
if row2 > 0 and col2 > 0 then
|
|
|
|
return {
|
|
|
|
brace_pair = brace_pair,
|
|
|
|
row1 = row1,
|
|
|
|
col1 = col1,
|
|
|
|
row2 = row2,
|
|
|
|
col2 = col2
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2022-12-30 23:10:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function find_ranges(brace_pairs)
|
|
|
|
local ranges = {}
|
|
|
|
for _, brace_pair in ipairs(brace_pairs) do
|
2023-01-29 03:20:14 +00:00
|
|
|
local range = find_range(brace_pair)
|
|
|
|
if range then
|
|
|
|
table.insert(ranges, find_range(brace_pair))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if #ranges > 0 then
|
|
|
|
return ranges
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function compare_ranges(range1, range2)
|
|
|
|
local _, row, col, _ = unpack(vim.fn.getpos('.'))
|
|
|
|
|
|
|
|
local row_diff1 = row - range1.row1
|
|
|
|
local row_diff2 = row - range2.row1
|
|
|
|
if row_diff1 < row_diff2 then
|
|
|
|
return -1
|
|
|
|
elseif row_diff1 > row_diff2 then
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
local col_diff1 = col - range1.col1
|
|
|
|
local col_diff2 = col - range2.col1
|
|
|
|
if col_diff1 < col_diff2 then
|
|
|
|
return -1
|
|
|
|
elseif col_diff1 > col_diff2 then
|
|
|
|
return 1
|
2022-12-30 23:10:22 +00:00
|
|
|
end
|
2023-01-29 03:20:14 +00:00
|
|
|
|
|
|
|
return 0
|
2022-12-30 23:10:22 +00:00
|
|
|
end
|
|
|
|
|
2023-01-29 03:20:14 +00:00
|
|
|
local function find_range_closest(brace_pairs)
|
2022-12-30 23:10:22 +00:00
|
|
|
local ranges = find_ranges(brace_pairs)
|
2023-01-29 03:20:14 +00:00
|
|
|
print(#ranges)
|
|
|
|
if ranges then
|
|
|
|
return vim.fn.sort(ranges, compare_ranges)[1]
|
|
|
|
end
|
2022-12-30 23:10:22 +00:00
|
|
|
end
|
|
|
|
|
2023-01-29 03:20:14 +00:00
|
|
|
local function reflow()
|
|
|
|
local brace_pairs = {{'(', ')'}, {'[', ']'}, {'{', '}'}, {'<', '>'}}
|
|
|
|
local range = find_range_closest(brace_pairs)
|
|
|
|
if range then
|
|
|
|
print(range.brace_pair[1])
|
|
|
|
end
|
2022-12-30 18:40:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
2023-01-29 03:20:14 +00:00
|
|
|
reflow = reflow,
|
|
|
|
ssetup = setup,
|
2022-12-30 18:40:23 +00:00
|
|
|
}
|