1
argonaut.nvim/lua/argonaut/types.lua

486 lines
10 KiB
Lua
Raw Normal View History

2024-04-22 01:11:56 +00:00
--
-- Cursor
--
2024-04-22 00:20:57 +00:00
2024-04-25 03:02:10 +00:00
local Cursor = {}
2024-04-27 01:13:28 +00:00
Cursor.__index = Cursor
2024-04-22 00:20:57 +00:00
2024-04-22 01:11:56 +00:00
function Cursor.new(row, col)
local cursor = {row = row, col = col}
2024-04-27 01:13:28 +00:00
return setmetatable(cursor, Cursor)
2024-04-22 00:20:57 +00:00
end
2024-04-22 04:57:41 +00:00
function Cursor:is_valid()
return self.row > 0 and self.col > 0
end
function Cursor:is_string()
2024-04-25 02:58:01 +00:00
assert(self:is_valid())
2024-04-22 01:11:56 +00:00
local syn_id = vim.fn.synID(self.row, self.col, false)
local syn_attr = vim.fn.synIDattr(syn_id, 'name')
return syn_attr:find('String$')
end
2024-04-25 02:58:01 +00:00
function Cursor.get_current()
local _, row, col, _ = unpack(vim.fn.getpos('.'))
return Cursor.new(row, col)
end
function Cursor:set_current()
assert(self:is_valid())
2024-04-26 05:13:08 +00:00
vim.fn.setcursorcharpos({self.row, self.col})
2024-04-25 02:58:01 +00:00
end
2024-04-27 01:13:28 +00:00
function Cursor.__eq(self, other)
2024-04-26 03:28:10 +00:00
return self.row == other.row and self.col == other.col
end
2024-04-22 01:11:56 +00:00
--
-- BracePair
--
2024-04-25 03:02:10 +00:00
local BracePair = {}
2024-04-27 01:13:28 +00:00
BracePair.__index = BracePair
2024-04-22 00:20:57 +00:00
2024-04-25 02:58:01 +00:00
function BracePair.new(open, close)
2024-04-25 03:48:40 +00:00
local pair = {open = open, close = close}
2024-04-27 01:13:28 +00:00
return setmetatable(pair, BracePair)
2024-04-22 01:11:56 +00:00
end
function BracePair.from_brace(brace)
2024-04-25 03:48:40 +00:00
local all_pairs = {
2024-04-25 02:58:01 +00:00
{'(', ')'},
{'[', ']'},
{'{', '}'},
{'<', '>'},
}
2024-04-25 03:48:40 +00:00
for _, pair in ipairs(all_pairs) do
if pair[1] == brace or pair[2] == brace then
return BracePair.new(pair[1], pair[2])
2024-04-22 01:11:56 +00:00
end
end
end
2024-04-25 03:48:40 +00:00
function BracePair:get_escaped()
2024-04-25 02:58:01 +00:00
local escape_func = function(brace_raw)
2024-04-22 00:20:57 +00:00
if brace_raw == '[' or brace_raw == ']' then
return '\\' .. brace_raw
else
return brace_raw
end
end
2024-04-25 02:58:01 +00:00
return BracePair.new(
escape_func(self.open),
escape_func(self.close)
)
2024-04-22 01:11:56 +00:00
end
2024-04-25 03:48:40 +00:00
function BracePair:find_closest(backward)
2024-04-22 01:11:56 +00:00
-- See flags: https://neovim.io/doc/user/builtin.html#search()
local flags = 'Wcn'
if backward then
2024-04-27 03:18:28 +00:00
flags = 'Wcnb'
2024-04-22 01:11:56 +00:00
end
2024-04-25 02:58:01 +00:00
local ignore_func = function()
2024-04-25 03:48:40 +00:00
local cursor = Cursor.get_current()
2024-04-25 02:58:01 +00:00
return cursor:is_string()
end
2024-04-25 03:48:40 +00:00
local escaped_pair = self:get_escaped()
2024-04-22 01:11:56 +00:00
local position = vim.fn.searchpairpos(
escaped_pair.open,
'',
escaped_pair.close,
flags,
2024-04-25 02:58:01 +00:00
ignore_func
2024-04-22 01:11:56 +00:00
)
2024-04-25 02:58:01 +00:00
local brace_cursor = Cursor.new(unpack(position))
if brace_cursor:is_valid() then
return brace_cursor
2024-04-22 00:20:57 +00:00
end
end
2024-04-27 03:59:30 +00:00
function BracePair.__eq(self, other)
return self.open == other.open and self.close == other.close
end
2024-04-23 05:36:25 +00:00
--
-- BraceStack
--
2024-04-25 03:02:10 +00:00
local BraceStack = {}
2024-04-27 01:13:28 +00:00
BraceStack.__index = BraceStack
2024-04-23 05:36:25 +00:00
function BraceStack.new()
local stack = {stack = {}}
2024-04-27 01:13:28 +00:00
return setmetatable(stack, BraceStack)
2024-04-23 05:36:25 +00:00
end
function BraceStack:update(brace)
2024-04-25 03:48:40 +00:00
local pair = BracePair.from_brace(brace)
if pair then
if brace == pair.close and self:top() == pair.open then
2024-04-23 05:36:25 +00:00
self:pop()
else
self:push(brace)
end
end
end
function BraceStack:push(brace)
table.insert(self.stack, brace)
end
function BraceStack:pop()
assert(not self:empty())
return table.remove(self.stack, #self.stack)
end
function BraceStack:empty()
return #self.stack == 0
end
function BraceStack:top()
2024-04-27 01:09:02 +00:00
return self.stack[#self.stack]
2024-04-23 05:36:25 +00:00
end
2024-04-22 01:11:56 +00:00
--
-- BraceRange
--
2024-04-25 03:02:10 +00:00
local BraceRange = {}
2024-04-27 01:13:28 +00:00
BraceRange.__index = BraceRange
2024-04-22 00:20:57 +00:00
2024-04-25 03:48:40 +00:00
function BraceRange.new(start, stop, pair)
local range = {
start = start,
stop = stop,
pair = pair,
2024-04-22 00:20:57 +00:00
}
2024-04-27 01:13:28 +00:00
return setmetatable(range, BraceRange)
2024-04-22 00:20:57 +00:00
end
2024-04-22 01:11:56 +00:00
2024-04-25 03:48:40 +00:00
function BraceRange.find_closest(pair)
local stop = pair:find_closest(false)
if stop then
local start = pair:find_closest(true)
if start then
2024-04-26 03:28:10 +00:00
return BraceRange.new(start, stop, pair)
2024-04-22 01:11:56 +00:00
end
end
end
2024-04-22 04:57:41 +00:00
2024-04-25 02:58:01 +00:00
function BraceRange.find_closest_any()
2024-04-25 03:48:40 +00:00
local ranges = {}
2024-04-22 04:57:41 +00:00
for _, brace in ipairs({'(', '[', '{', '<'}) do
2024-04-25 03:48:40 +00:00
local pair = BracePair.from_brace(brace)
local range = BraceRange.find_closest(pair)
if range then
table.insert(ranges, range)
2024-04-22 04:57:41 +00:00
end
end
2024-04-25 03:48:40 +00:00
if #ranges > 0 then
2024-04-27 03:18:28 +00:00
table.sort(ranges)
2024-04-25 03:48:40 +00:00
return ranges[1]
2024-04-22 05:07:24 +00:00
end
2024-04-22 04:57:41 +00:00
end
2024-04-24 02:04:14 +00:00
2024-04-24 04:43:26 +00:00
function BraceRange:is_wrapped()
2024-04-25 03:48:40 +00:00
return self.start.row < self.stop.row
2024-04-24 04:43:26 +00:00
end
2024-04-24 02:04:14 +00:00
2024-04-27 03:18:28 +00:00
function BraceRange.__lt(range_1, range_2)
local cursor = Cursor:get_current()
local row_diff1 = range_1.start.row - cursor.row
local col_diff1 = range_1.start.col - cursor.col
local row_diff2 = range_2.start.row - cursor.row
local col_diff2 = range_2.start.col - cursor.col
if row_diff1 < row_diff2 then
return false
elseif row_diff1 > row_diff2 then
return true
elseif col_diff1 < col_diff2 then
return false
elseif col_diff1 > col_diff2 then
return true
else
return true
end
end
2024-04-24 02:04:14 +00:00
--
2024-04-25 03:48:40 +00:00
-- Param
2024-04-24 02:04:14 +00:00
--
2024-04-25 03:48:40 +00:00
local Param = {}
2024-04-27 01:13:28 +00:00
Param.__index = Param
2024-04-24 02:04:14 +00:00
2024-04-25 03:48:40 +00:00
function Param.new(text, pair)
local param = {
2024-04-24 02:04:14 +00:00
text = text,
2024-04-25 03:48:40 +00:00
pair = pair,
2024-04-27 01:41:25 +00:00
offset = nil,
2024-04-24 02:04:14 +00:00
}
2024-04-27 01:13:28 +00:00
return setmetatable(param, Param)
2024-04-24 02:04:14 +00:00
end
2024-04-25 03:48:40 +00:00
function Param:append(char)
2024-04-24 02:04:14 +00:00
self.text = self.text .. char
end
2024-04-26 05:13:08 +00:00
function Param:activate()
2024-04-27 01:41:25 +00:00
self.offset = #self.text
end
function Param:is_active()
return self.offset ~= nil
2024-04-26 03:44:39 +00:00
end
2024-04-26 05:13:08 +00:00
function Param:flush()
2024-04-27 01:41:25 +00:00
if self.offset then
self.offset = math.min(self.offset, #self.text:match('(.-)%s*$'))
2024-04-27 03:18:28 +00:00
self.offset = math.max(self.offset - #self.text:match('^%s*'), 1)
2024-04-27 01:41:25 +00:00
end
2024-04-26 05:13:08 +00:00
self.text = self.text:match('^%s*(.-)%s*$')
2024-04-27 01:09:02 +00:00
return #self.text > 0
2024-04-25 03:58:44 +00:00
end
2024-04-24 02:04:14 +00:00
--
2024-04-25 03:48:40 +00:00
-- ParamList
2024-04-24 02:04:14 +00:00
--
2024-04-25 03:48:40 +00:00
local ParamList = {}
2024-04-27 01:13:28 +00:00
ParamList.__index = ParamList
2024-04-24 02:04:14 +00:00
2024-04-25 03:48:40 +00:00
function ParamList.new()
local params = {
current = nil,
parsed = {},
2024-04-24 02:04:14 +00:00
}
2024-04-27 01:13:28 +00:00
return setmetatable(params, ParamList)
2024-04-24 02:04:14 +00:00
end
2024-04-25 03:48:40 +00:00
function ParamList:flush()
if self.current then
2024-04-27 01:09:02 +00:00
if self.current:flush() then
table.insert(self.parsed, self.current)
end
2024-04-25 03:48:40 +00:00
self.current = nil
2024-04-24 02:04:14 +00:00
end
end
2024-04-25 03:48:40 +00:00
function ParamList:update(char, brace_stack, range, cursor)
2024-04-24 02:04:14 +00:00
if not cursor:is_string() then
brace_stack:update(char)
if brace_stack:empty() and char == ',' then
self:flush()
return
end
end
2024-04-25 03:48:40 +00:00
if self.current then
self.current:append(char)
2024-04-24 02:04:14 +00:00
else
2024-04-25 03:48:40 +00:00
self.current = Param.new(char, range)
2024-04-24 02:04:14 +00:00
end
2024-04-26 03:44:39 +00:00
if cursor == Cursor.get_current() then
2024-04-26 05:13:08 +00:00
self.current:activate()
2024-04-26 03:44:39 +00:00
end
2024-04-24 02:04:14 +00:00
end
2024-04-25 03:48:40 +00:00
function ParamList:parse(range)
2024-04-24 02:04:14 +00:00
local brace_stack = BraceStack:new()
2024-04-25 03:48:40 +00:00
for row = range.start.row, range.stop.row do
2024-04-24 02:04:14 +00:00
local line = vim.fn.getline(row)
local start_col = 1
2024-04-25 03:48:40 +00:00
if row == range.start.row then
start_col = range.start.col + 1
2024-04-24 02:04:14 +00:00
end
local stop_col = #line
2024-04-25 03:48:40 +00:00
if row == range.stop.row then
stop_col = range.stop.col - 1
2024-04-24 02:04:14 +00:00
end
for col = start_col, stop_col do
2024-04-25 03:48:40 +00:00
self:update(line:sub(col, col), brace_stack, range, Cursor.new(row, col))
2024-04-24 02:04:14 +00:00
end
end
2024-04-24 03:38:56 +00:00
self:flush()
2024-04-24 02:04:14 +00:00
end
2024-04-24 04:43:26 +00:00
2024-04-25 03:32:12 +00:00
--
-- WrapContext
--
local WrapContext = {}
2024-04-27 01:13:28 +00:00
WrapContext.__index = WrapContext
2024-04-25 03:32:12 +00:00
2024-04-25 05:17:50 +00:00
function WrapContext.new(opt)
2024-04-25 03:32:12 +00:00
local wrap_context = {
2024-04-25 05:17:50 +00:00
opt = opt,
2024-04-25 03:32:12 +00:00
indent = '',
prefix = '',
suffix = '',
2024-04-25 03:48:40 +00:00
range = nil,
params = nil,
2024-04-25 03:32:12 +00:00
}
2024-04-27 01:13:28 +00:00
return setmetatable(wrap_context, WrapContext)
2024-04-25 03:32:12 +00:00
end
function WrapContext:parse()
2024-04-25 03:48:40 +00:00
self.range = BraceRange.find_closest_any()
if not self.range then
2024-04-25 03:32:12 +00:00
return false
end
2024-04-25 03:48:40 +00:00
local first_line = vim.fn.getline(self.range.start.row)
2024-04-25 03:32:12 +00:00
self.indent = first_line:match('^(%s*)')
2024-04-25 03:48:40 +00:00
self.prefix = first_line:sub(#self.indent + 1, self.range.start.col)
2024-04-25 03:32:12 +00:00
2024-04-25 03:48:40 +00:00
local last_line = vim.fn.getline(self.range.stop.row)
self.suffix = last_line:sub(self.range.stop.col)
2024-04-25 03:32:12 +00:00
2024-04-25 03:48:40 +00:00
self.params = ParamList.new()
self.params:parse(self.range)
2024-04-25 03:32:12 +00:00
return true
end
2024-04-27 03:59:30 +00:00
function WrapContext:wrap(opt)
2024-04-26 05:13:08 +00:00
vim.fn.setline(self.range.start.row, self.indent .. self.prefix)
2024-04-25 03:32:12 +00:00
2024-04-26 05:13:08 +00:00
local cursor = nil
2024-04-25 03:48:40 +00:00
local row = self.range.start.row
2024-04-26 05:13:08 +00:00
2024-04-25 03:48:40 +00:00
for i, param in ipairs(self.params.parsed) do
2024-04-25 05:17:50 +00:00
local first_param = i == 1
local last_param = i == #self.params.parsed
local line = ''
2024-04-27 03:59:30 +00:00
if opt.comma_prefix then
line = line .. self.indent .. opt.line_prefix
2024-04-25 05:17:50 +00:00
if not first_param then
line = line .. ', '
end
2024-04-26 05:13:08 +00:00
line = line .. param.text
2024-04-25 05:17:50 +00:00
else
2024-04-27 03:59:30 +00:00
line = line .. self.indent .. opt.line_prefix .. param.text
if not last_param or opt.comma_last then
2024-04-25 05:17:50 +00:00
line = line .. ','
end
2024-04-25 03:38:40 +00:00
end
2024-04-25 03:32:12 +00:00
2024-04-27 03:59:30 +00:00
if last_param and not opt.brace_last_wrap then
2024-04-25 03:38:40 +00:00
line = line .. self.suffix
end
2024-04-25 03:32:12 +00:00
vim.fn.append(row, line)
2024-04-26 05:13:08 +00:00
row = row + 1
vim.fn.execute(string.format('%d>', row))
2024-04-25 03:32:12 +00:00
2024-04-27 03:59:30 +00:00
if first_param and opt.comma_prefix_indent then
2024-04-25 05:17:50 +00:00
local prev_shiftwidth = vim.o.shiftwidth
vim.o.shiftwidth = prev_shiftwidth - 2
2024-04-26 05:13:08 +00:00
vim.fn.execute(string.format('%d>', row))
2024-04-25 05:17:50 +00:00
vim.o.shiftwidth = prev_shiftwidth
end
2024-04-25 03:32:12 +00:00
2024-04-27 01:41:25 +00:00
if param:is_active() then
2024-04-26 05:13:08 +00:00
cursor = Cursor.get_current()
2024-04-27 01:41:25 +00:00
cursor.col = #vim.fn.getline(cursor.row):match('^%s*') + param.offset
2024-04-26 05:13:08 +00:00
end
2024-04-25 03:32:12 +00:00
end
2024-04-27 03:59:30 +00:00
if opt.brace_last_wrap then
2024-04-25 03:38:40 +00:00
vim.fn.append(row, self.indent .. self.suffix)
2024-04-27 03:59:30 +00:00
if opt.brace_last_indent then
2024-04-27 03:36:40 +00:00
vim.fn.execute(string.format('%d>', row + 1))
end
2024-04-25 03:38:40 +00:00
end
2024-04-26 05:13:08 +00:00
if cursor then
cursor:set_current()
end
2024-04-25 03:32:12 +00:00
end
2024-04-27 03:59:30 +00:00
function WrapContext:unwrap(opt)
local padding = ''
if opt.brace_pad then
padding = ' '
end
2024-04-25 05:20:28 +00:00
2024-04-27 03:59:30 +00:00
local line = self.indent .. self.prefix .. padding
local cursor = nil
2024-04-25 05:20:28 +00:00
for i, param in ipairs(self.params.parsed) do
2024-04-27 01:41:25 +00:00
if param:is_active() then
2024-04-27 06:01:16 +00:00
cursor = Cursor.get_current()
cursor.row = cursor.row - 1
cursor.col = #line + param.offset
2024-04-26 05:13:08 +00:00
end
line = line .. param.text
2024-04-25 05:20:28 +00:00
if i < #self.params.parsed then
line = line .. ', '
end
end
2024-04-27 03:59:30 +00:00
line = line .. padding .. self.suffix
2024-04-25 05:20:28 +00:00
vim.fn.setline(self.range.start.row, line)
vim.fn.execute(string.format('%d,%dd_', self.range.start.row + 1, self.range.stop.row))
2024-04-26 05:13:08 +00:00
if cursor then
cursor:set_current()
end
2024-04-25 05:20:28 +00:00
end
2024-04-25 03:32:12 +00:00
function WrapContext:toggle()
2024-04-27 03:59:30 +00:00
local opt = self:specialize_opt()
2024-04-25 03:48:40 +00:00
if self.range:is_wrapped() then
2024-04-27 03:59:30 +00:00
self:unwrap(opt)
2024-04-25 05:20:28 +00:00
else
2024-04-27 03:59:30 +00:00
self:wrap(opt)
2024-04-25 03:32:12 +00:00
end
end
2024-04-27 03:59:30 +00:00
function WrapContext:specialize_opt()
local opt = {}
for key, value in pairs(self.opt) do
if type(value) == 'table' then
opt[key] = false
for _, brace in ipairs(value) do
if self.range.pair == BracePair.from_brace(brace) then
opt[key] = true
break
end
end
else
opt[key] = value
end
end
return opt
end
2024-04-25 02:39:09 +00:00
return {
2024-04-25 03:32:12 +00:00
WrapContext = WrapContext,
2024-04-25 02:39:09 +00:00
}