1
argonaut.nvim/lua/argonaut.lua

196 lines
5.4 KiB
Lua
Raw Normal View History

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
2023-01-29 06:43:56 +00:00
local function get_cursor_pos()
local _, row, col, _ = unpack(vim.fn.getpos('.'))
return {row = row, col = col}
end
2023-02-08 02:10:12 +00:00
local function is_string_literal(pos)
2023-02-07 06:36:11 +00:00
if not pos then
pos = get_cursor_pos()
2023-01-29 06:43:56 +00:00
end
2023-02-07 06:36:11 +00:00
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
2023-02-08 02:10:12 +00:00
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)
local row1, col1 = unpack(vim.fn.searchpairpos(escape_brace(brace), '', escape_brace(brace_alt), 'Wnb', is_string_literal))
2023-01-29 03:20:14 +00:00
if row1 > 0 and col1 > 0 then
2023-02-08 02:10:12 +00:00
local row2, col2 = unpack(vim.fn.searchpairpos(escape_brace(brace), '', escape_brace(brace_alt), 'Wcn', is_string_literal))
2023-01-29 03:20:14 +00:00
if row2 > 0 and col2 > 0 then
return {
2023-02-08 02:10:12 +00:00
brace = brace,
2023-01-29 03:20:14 +00:00
row1 = row1,
col1 = col1,
row2 = row2,
col2 = col2
}
end
end
2022-12-30 23:10:22 +00:00
end
2023-02-08 02:10:12 +00:00
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)
2023-01-29 03:20:14 +00:00
end
end
2023-02-08 02:10:12 +00:00
if #brace_ranges > 0 then
return brace_ranges
2023-01-29 03:20:14 +00:00
end
end
2023-02-08 02:10:12 +00:00
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
2023-02-07 06:36:11 +00:00
if row_diff1 < row_diff2 then
return -1
elseif row_diff1 > row_diff2 then
return 1
end
2023-01-29 03:20:14 +00:00
2023-02-08 02:10:12 +00:00
local col_diff1 = cursor_pos.col - brace_range_1.col1
local col_diff2 = cursor_pos.col - brace_range_2.col1
2023-02-07 06:36:11 +00:00
if col_diff1 < col_diff2 then
return -1
elseif col_diff1 > col_diff2 then
return 1
end
2023-01-29 03:20:14 +00:00
2023-02-07 06:36:11 +00:00
return 0
end
2022-12-30 23:10:22 +00:00
2023-02-08 02:10:12 +00:00
local brace_ranges = find_all_brace_ranges(braces)
if brace_ranges then
return vim.fn.sort(brace_ranges, compare_brace_ranges)[1]
2023-01-29 03:20:14 +00:00
end
2022-12-30 23:10:22 +00:00
end
2023-02-08 02:10:12 +00:00
local function parse_brace_range(brace_range)
local brace_range_params = {}
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
2023-02-07 06:36:11 +00:00
local line = vim.fn.getline(row)
local col1 = 0
2023-02-08 02:10:12 +00:00
if row == brace_range.row1 then
col1 = brace_range.col1 + 1
2023-02-07 06:36:11 +00:00
end
local col2 = #line
2023-02-08 02:10:12 +00:00
if row == brace_range.row2 then
col2 = brace_range.col2 - 1
2023-02-07 06:36:11 +00:00
end
for i = col1, col2 do
2023-02-08 02:10:12 +00:00
table.insert(brace_range_elements, {
2023-02-07 06:36:11 +00:00
char = line:sub(i, i), ---@diagnostic disable-line: undefined-field
2023-02-08 02:10:12 +00:00
literal = is_string_literal({row = row, col = i}),
2023-02-07 06:36:11 +00:00
})
end
end
2023-02-08 02:10:12 +00:00
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
2023-02-07 06:36:11 +00:00
end
end
2023-02-08 02:10:12 +00:00
if append then
brace_range_param = brace_range_param .. brace_range_element.char
2023-02-07 06:36:11 +00:00
end
end
2023-02-08 02:10:12 +00:00
flush_brace_range_param()
2023-02-07 06:36:11 +00:00
end
2023-02-08 02:10:12 +00:00
for _, p in ipairs(brace_range_params) do
2023-02-07 06:36:11 +00:00
print(p)
end
end
2023-01-29 03:20:14 +00:00
local function reflow()
2023-02-08 02:10:12 +00:00
local brace_range = find_closest_brace_range({'(', '[', '{', '<'})
if brace_range then
parse_brace_range(brace_range)
2023-01-29 03:20:14 +00:00
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
}