1

150 lines
3.5 KiB
Lua
Raw Normal View History

2024-12-31 18:56:10 -08:00
local ParamCell = {}
ParamCell.__index = ParamCell
2024-12-30 20:31:50 -08:00
2024-12-31 18:56:10 -08:00
function ParamCell.new(value, type)
assert(type == 'range' or type == 'cursor')
local param_cell = {value = value, type = type}
return setmetatable(param_cell, ParamCell)
2024-12-30 20:31:50 -08:00
end
2024-12-31 18:56:10 -08:00
function ParamCell:write(builder, wrapped)
if self.type == 'range' then
self.value:write(builder, wrapped)
elseif self.type == 'cursor' then
builder:write(self.value:get_value())
2024-12-31 10:05:01 -08:00
end
end
2024-12-31 18:56:10 -08:00
function ParamCell:hit_test(cursor)
if self.type == 'range' then
return self.value:hit_test(cursor)
elseif self.type == 'cursor' and self.value == cursor then
return {}
2024-12-31 10:05:01 -08:00
end
end
2024-12-31 18:56:10 -08:00
function ParamCell:hit_search(trace, depth)
if self.type == 'range' then
return self.value:hit_search(trace, depth + 1)
elseif self.type == 'cursor' then
return self.value
end
end
function ParamCell:get_start_row()
if self.type == 'range' then
return self.value.start_cursor.row
elseif self.type == 'cursor' then
return self.value.row
end
end
function ParamCell:get_stop_row()
if self.type == 'range' then
return self.value.stop_cursor.row
elseif self.type == 'cursor' then
return self.value.row
end
end
2024-12-30 20:31:50 -08:00
2024-12-31 18:56:10 -08:00
function ParamCell:is_empty()
return self.type == 'cursor' and not self.value:get_value():match('%S')
end
local Param = {}
Param.__index = Param
function Param.new(range, index, cells)
local param = {range = range, index = index, cells = cells or {}}
2024-12-30 20:31:50 -08:00
return setmetatable(param, Param)
end
function Param:append(value, type)
2024-12-31 18:56:10 -08:00
table.insert(self.cells, ParamCell.new(value, type))
2024-12-30 20:31:50 -08:00
end
function Param:stripped()
local cells = {}
for i = self:find_index_begin(), self:find_index_end() do
table.insert(cells, self.cells[i])
end
2024-12-31 18:56:10 -08:00
2024-12-30 20:31:50 -08:00
return Param.new(self.range, self.index, cells)
end
function Param:hit_test(cursor)
for i, cell in ipairs(self.cells) do
2024-12-31 18:56:10 -08:00
local trace = cell:hit_test(cursor)
2024-12-30 20:31:50 -08:00
if trace then
2024-12-31 18:56:10 -08:00
table.insert(trace, 1, i)
2024-12-30 20:31:50 -08:00
return trace
end
end
end
function Param:hit_search(trace, depth)
local index = trace[depth]
2024-12-31 18:56:10 -08:00
return self.cells[index]:hit_search(depth)
2024-12-30 20:31:50 -08:00
end
function Param:write(builder, wrapped)
for i = self:find_index_begin(), self:find_index_end() do
2024-12-31 18:56:10 -08:00
self.cells[i]:write(builder, wrapped)
2024-12-30 20:31:50 -08:00
end
end
2024-12-31 18:56:10 -08:00
function Param:get_previous()
if self.index > 1 then
return self.range.params[self.index - 1]
2024-12-31 10:05:01 -08:00
end
end
2024-12-31 18:56:10 -08:00
function Param:get_next()
if self.index < #self.range.params then
return self.range.params[self.index + 1]
end
end
function Param:get_start_row()
if #self.cells > 0 then
return self.cells[1]:get_start_row()
end
end
function Param:get_stop_row()
if #self.cells > 0 then
return self.cells[#self.cells]:get_stop_row()
2024-12-31 10:05:01 -08:00
end
end
2024-12-30 20:31:50 -08:00
function Param:find_index_begin()
for i = 1, #self.cells do
2024-12-31 18:56:10 -08:00
if not self.cells[i]:is_empty() then
2024-12-30 20:31:50 -08:00
return i
end
end
end
function Param:find_index_end()
for i = #self.cells, 1, -1 do
2024-12-31 18:56:10 -08:00
if not self.cells[i]:is_empty() then
2024-12-30 20:31:50 -08:00
return i
end
end
end
2024-12-31 18:56:10 -08:00
function Param:is_wrapped()
local previous_param = self:get_previous()
if previous_param then
return previous_param:get_stop_row() ~= self:get_start_row()
else
return self.range.start_cursor.row ~= self:get_start_row()
end
end
2024-12-30 20:31:50 -08:00
function Param:is_empty()
return self:find_index_begin() == nil
end
return Param