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

605 lines
13 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_literal()
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')
2024-05-03 03:14:54 +00:00
return syn_attr:find('String$') ~= nil or syn_attr:find('Comment$') ~= nil
2024-04-22 01:11:56 +00:00
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()
return cursor:is_literal()
2024-04-25 02:58:01 +00:00
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-05-03 03:14:54 +00:00
function Param.new(pair, opt)
2024-04-25 03:48:40 +00:00
local param = {
2024-05-02 03:20:50 +00:00
pair = pair,
2024-05-03 03:14:54 +00:00
opt = opt,
2024-05-02 03:20:50 +00:00
text = '',
2024-05-03 03:14:54 +00:00
literals = {},
2024-05-02 04:50:58 +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-05-02 03:20:50 +00:00
function Param:append(char, cursor)
2024-05-02 04:50:58 +00:00
assert(cursor:is_valid())
2024-04-24 02:04:14 +00:00
self.text = self.text .. char
2024-05-03 03:14:54 +00:00
table.insert(self.literals, cursor:is_literal())
2024-05-02 04:50:58 +00:00
2024-05-02 03:20:50 +00:00
if cursor == Cursor.get_current() then
self.offset = #self.text
end
2024-04-27 01:41:25 +00:00
end
function Param:is_active()
return self.offset ~= nil
2024-04-26 03:44:39 +00:00
end
2024-05-02 04:50:58 +00:00
function Param:slice(start, stop)
2024-05-03 03:14:54 +00:00
assert(#self.text == #self.literals)
2024-05-02 04:50:58 +00:00
local text = ''
2024-05-03 03:14:54 +00:00
local literals = {}
2024-05-02 04:50:58 +00:00
2024-05-03 01:10:16 +00:00
for i = start, stop do
2024-05-02 04:50:58 +00:00
text = text .. self.text:sub(i, i)
2024-05-03 03:14:54 +00:00
table.insert(literals, self.literals[i])
2024-05-01 01:31:16 +00:00
end
2024-05-02 04:50:58 +00:00
self.text = text
2024-05-03 03:14:54 +00:00
self.literals = literals
2024-05-02 04:50:58 +00:00
2024-05-01 01:31:16 +00:00
if self.offset then
2024-05-02 04:50:58 +00:00
self.offset = math.min(self.offset, stop)
self.offset = math.max(self.offset - start + 1, 1)
2024-04-27 01:41:25 +00:00
end
2024-05-02 04:50:58 +00:00
end
2024-05-03 03:14:54 +00:00
function Param:trim()
assert(#self.text == #self.literals)
2024-05-02 04:50:58 +00:00
self:slice(1, #self.text - #self.text:match('%s*$'))
self:slice(1 + #self.text:match('^%s*'), #self.text)
2024-04-27 01:41:25 +00:00
2024-05-03 03:14:54 +00:00
if self.opt.trim_inner_spaces then
local text = ''
local literals = {}
local offset = self.offset
2024-05-03 01:10:16 +00:00
2024-05-03 03:14:54 +00:00
for i = 1, #self.text do
local char = self.text:sub(i, i)
local literal = self.literals[i]
2024-05-03 01:10:16 +00:00
2024-05-03 03:14:54 +00:00
if literal or not char:match('%s') or not text:match('%s$') then
text = text .. char
table.insert(literals, literal)
elseif offset and offset >= i then
self.offset = math.max(1, self.offset - 1)
end
2024-05-03 01:10:16 +00:00
end
2024-05-03 03:14:54 +00:00
self.text = text
self.literals = literals
end
2024-05-03 01:10:16 +00:00
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-05-03 03:14:54 +00:00
function ParamList.new(range, opt)
2024-04-25 03:48:40 +00:00
local params = {
range = range,
2024-05-03 03:14:54 +00:00
opt = opt,
2024-04-25 03:48:40 +00:00
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-05-03 03:14:54 +00:00
if self.current:trim() then
2024-04-27 01:09:02 +00:00
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
function ParamList:update(char, brace_stack, cursor)
if not cursor:is_literal() then
2024-04-24 02:04:14 +00:00
brace_stack:update(char)
if brace_stack:empty() and char == ',' then
self:flush()
return
end
end
2024-05-02 03:20:50 +00:00
if not self.current then
2024-05-03 03:14:54 +00:00
self.current = Param.new(self.range, self.opt)
2024-04-24 02:04:14 +00:00
end
2024-04-26 03:44:39 +00:00
2024-05-02 03:20:50 +00:00
self.current:append(char, cursor)
2024-04-24 02:04:14 +00:00
end
function ParamList:parse()
2024-04-24 02:04:14 +00:00
local brace_stack = BraceStack:new()
for row = self.range.start.row, self.range.stop.row do
2024-04-24 02:04:14 +00:00
local line = vim.fn.getline(row)
local start_col = 1
if row == self.range.start.row then
start_col = self.range.start.col + 1
2024-04-24 02:04:14 +00:00
end
local stop_col = #line
if row == self.range.stop.row then
stop_col = self.range.stop.col - 1
2024-04-24 02:04:14 +00:00
end
for col = start_col, stop_col do
self:update(line:sub(col, col), brace_stack, 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-28 18:16:30 +00:00
--
-- Builder
--
local Builder = {}
Builder.__index = Builder
function Builder.new(indent_level, indent_block)
local builder = {
lines = {},
line = '',
indent_level = indent_level,
indent_block = indent_block,
}
return setmetatable(builder, Builder)
end
function Builder:indent()
self.indent_level = self.indent_level + 1
end
function Builder:unindent()
assert(self.indent_level > 0)
self.indent_level = self.indent_level - 1
end
function Builder:update(text)
self.line = self.line .. text
end
function Builder:flush()
local indent = string.rep(self.indent_block, self.indent_level)
table.insert(self.lines, indent .. self.line)
self.line = ''
end
function Builder:get_offset()
local indent = string.rep(self.indent_block, self.indent_level)
return Cursor.new(#self.lines, #self.line + #indent)
end
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-29 05:11:34 +00:00
opt = nil,
base_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
2024-04-29 05:11:34 +00:00
function WrapContext:config_opt()
self.opt = {}
for key, value in pairs(self.base_opt) do
if type(value) == 'table' then
self.opt[key] = false
for _, brace in ipairs(value) do
if self.range.pair == BracePair.from_brace(brace) then
self.opt[key] = true
break
end
end
else
self.opt[key] = value
end
2024-04-25 03:32:12 +00:00
end
2024-04-29 05:11:34 +00:00
end
2024-04-25 03:32:12 +00:00
2024-04-29 05:11:34 +00:00
function WrapContext:config_indent(line)
2024-04-30 01:46:58 +00:00
local padding = #line:match('^(%s*)')
2024-04-28 18:16:30 +00:00
if vim.o.expandtab then
2024-04-30 01:46:58 +00:00
self.indent_level = math.floor(padding / vim.o.shiftwidth)
2024-04-28 18:16:30 +00:00
self.indent_block = string.rep(' ', vim.o.shiftwidth)
else
2024-04-30 01:46:58 +00:00
self.indent_level = padding
2024-04-28 18:16:30 +00:00
self.indent_block = '\t'
end
2024-04-29 05:11:34 +00:00
end
function WrapContext:parse()
self.range = BraceRange.find_closest_any()
if not self.range then
return false
end
2024-04-25 03:32:12 +00:00
2024-04-29 05:11:34 +00:00
self:config_opt()
local first_line = vim.fn.getline(self.range.start.row)
2024-04-25 03:48:40 +00:00
local last_line = vim.fn.getline(self.range.stop.row)
2024-04-29 05:11:34 +00:00
self:config_indent(first_line)
self.prefix = first_line:sub(self.indent_level * #self.indent_block + 1, self.range.start.col)
2024-04-25 03:48:40 +00:00
self.suffix = last_line:sub(self.range.stop.col)
2024-04-25 03:32:12 +00:00
2024-05-03 03:14:54 +00:00
self.params = ParamList.new(self.range, self.opt)
self.params:parse()
2024-04-25 03:32:12 +00:00
return true
end
2024-04-29 05:11:34 +00:00
function WrapContext:update_builder_param(builder, param)
2024-04-29 02:14:07 +00:00
local text = param.text
2024-04-29 05:11:34 +00:00
if #self.opt.line_prefix > 0 then
text = param.text:match('^%s*[' .. self.opt.line_prefix .. ']?%s*(.*)')
2024-04-29 02:14:07 +00:00
end
local cursor = nil
if param:is_active() then
2024-04-29 05:11:34 +00:00
local offset_delta = #param.text - #text
2024-04-29 02:14:07 +00:00
cursor = builder:get_offset()
cursor.row = cursor.row + self.range.start.row
2024-04-29 05:11:34 +00:00
cursor.col = cursor.col + param.offset - offset_delta
2024-04-29 02:14:07 +00:00
end
builder:update(text)
return cursor
end
2024-04-29 05:11:34 +00:00
function WrapContext:wrap()
2024-04-28 18:16:30 +00:00
local builder = Builder.new(self.indent_level, self.indent_block)
builder:update(self.prefix)
builder:flush()
builder:indent()
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
for i, param in ipairs(self.params.parsed) do
2024-04-28 18:16:30 +00:00
local is_first_param = i == 1
local is_last_param = i == #self.params.parsed
2024-04-25 05:17:50 +00:00
2024-04-29 05:11:34 +00:00
if self.opt.comma_prefix then
builder:update(self.opt.line_prefix)
2024-04-28 18:16:30 +00:00
if not is_first_param then
builder:update(', ')
2024-04-29 05:11:34 +00:00
elseif self.opt.comma_prefix_indent and not is_last_param then
2024-04-29 02:14:07 +00:00
builder:update(' ')
2024-04-25 05:17:50 +00:00
end
2024-04-29 05:11:34 +00:00
cursor = self:update_builder_param(builder, param) or cursor
2024-04-25 05:17:50 +00:00
else
2024-04-29 05:11:34 +00:00
builder:update(self.opt.line_prefix)
cursor = self:update_builder_param(builder, param) or cursor
if not is_last_param or self.opt.comma_last then
2024-04-28 18:16:30 +00:00
builder:update(',')
2024-04-25 05:17:50 +00:00
end
2024-04-25 03:38:40 +00:00
end
2024-04-25 03:32:12 +00:00
2024-04-29 05:11:34 +00:00
if is_last_param and not self.opt.brace_last_wrap then
2024-04-28 18:16:30 +00:00
builder:update(self.suffix)
2024-04-25 03:38:40 +00:00
end
2024-04-25 03:32:12 +00:00
2024-04-28 18:16:30 +00:00
builder:flush()
end
2024-04-25 03:32:12 +00:00
2024-04-29 05:11:34 +00:00
if not self.opt.brace_last_indent then
2024-04-28 18:16:30 +00:00
builder:unindent()
2024-04-25 03:32:12 +00:00
end
2024-04-29 05:11:34 +00:00
if self.opt.brace_last_wrap then
2024-04-28 18:16:30 +00:00
builder:update(self.suffix)
2024-04-29 02:14:07 +00:00
builder:flush()
2024-04-28 18:16:30 +00:00
end
local row = self.range.start.row
for i, line in ipairs(builder.lines) do
if i == 1 then
vim.fn.setline(row, line)
else
vim.fn.append(row, line)
row = row + 1
2024-04-27 03:36:40 +00:00
end
2024-04-25 03:38:40 +00:00
end
2024-04-26 05:13:08 +00:00
2024-04-29 05:11:34 +00:00
if not cursor then
cursor = self.range.start
2024-04-26 05:13:08 +00:00
end
2024-04-29 05:11:34 +00:00
cursor:set_current()
2024-04-25 03:32:12 +00:00
end
2024-04-29 05:11:34 +00:00
function WrapContext:unwrap()
2024-04-27 03:59:30 +00:00
local padding = ''
2024-04-29 05:11:34 +00:00
if self.opt.brace_pad then
2024-04-27 03:59:30 +00:00
padding = ' '
end
2024-04-25 05:20:28 +00:00
2024-04-29 05:11:34 +00:00
local builder = Builder.new(
self.indent_level,
self.indent_block
)
2024-04-28 18:28:38 +00:00
builder:update(self.prefix)
builder:update(padding)
2024-04-27 03:59:30 +00:00
local cursor = nil
2024-04-28 18:28:38 +00:00
for i, param in ipairs(self.params.parsed) do
2024-04-29 05:11:34 +00:00
cursor = self:update_builder_param(builder, param) or cursor
2024-04-25 05:20:28 +00:00
if i < #self.params.parsed then
2024-04-28 18:28:38 +00:00
builder:update(', ')
2024-04-25 05:20:28 +00:00
end
end
2024-04-28 18:28:38 +00:00
builder:update(padding)
builder:update(self.suffix)
builder:flush()
2024-04-26 05:13:08 +00:00
2024-04-28 18:28:38 +00:00
vim.fn.setline(self.range.start.row, builder.lines[1])
2024-04-29 05:11:34 +00:00
vim.fn.execute(string.format('%d,%dd_', self.range.start.row + 1, self.range.stop.row))
if not cursor then
cursor = self.range.start
2024-04-26 05:13:08 +00:00
end
2024-04-29 05:11:34 +00:00
cursor:set_current()
2024-04-25 05:20:28 +00:00
end
2024-04-25 03:32:12 +00:00
function WrapContext:toggle()
2024-04-25 03:48:40 +00:00
if self.range:is_wrapped() then
2024-04-29 05:11:34 +00:00
self:unwrap()
2024-04-25 05:20:28 +00:00
else
2024-04-29 05:11:34 +00:00
self:wrap()
2024-04-25 03:32:12 +00:00
end
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
}