1

More cleanup

This commit is contained in:
Alex Yatskov 2024-12-31 19:09:40 -08:00
parent 397e266f59
commit 40c833a957
2 changed files with 64 additions and 43 deletions

View File

@ -1,54 +1,67 @@
local ParamCell = {} local ParamCursorCell = {}
ParamCell.__index = ParamCell ParamCursorCell.__index = ParamCursorCell
function ParamCell.new(value, type) function ParamCursorCell.new(cursor)
assert(type == 'range' or type == 'cursor') local param_cell_cursor = {cursor = cursor}
local param_cell = {value = value, type = type} return setmetatable(param_cell_cursor, ParamCursorCell)
return setmetatable(param_cell, ParamCell)
end end
function ParamCell:write(builder, wrapped) function ParamCursorCell:write(builder)
if self.type == 'range' then builder:write(self.cursor:get_value())
self.value:write(builder, wrapped)
elseif self.type == 'cursor' then
builder:write(self.value:get_value())
end
end end
function ParamCell:hit_test(cursor) function ParamCursorCell:hit_test(cursor)
if self.type == 'range' then if self.cursor == cursor then
return self.value:hit_test(cursor)
elseif self.type == 'cursor' and self.value == cursor then
return {} return {}
end end
end end
function ParamCell:hit_search(trace, depth) function ParamCursorCell:hit_search()
if self.type == 'range' then return self.cursor
return self.value:hit_search(trace, depth + 1)
elseif self.type == 'cursor' then
return self.value
end
end end
function ParamCell:get_start_row() function ParamCursorCell:get_start_row()
if self.type == 'range' then return self.cursor.row
return self.value.start_cursor.row
elseif self.type == 'cursor' then
return self.value.row
end
end end
function ParamCell:get_stop_row() function ParamCursorCell:get_stop_row()
if self.type == 'range' then return self.cursor.row
return self.value.stop_cursor.row
elseif self.type == 'cursor' then
return self.value.row
end
end end
function ParamCell:is_empty() function ParamCursorCell:is_empty()
return self.type == 'cursor' and not self.value:get_value():match('%S') return not self.cursor:get_value():match('%S')
end
local ParamRangeCell = {}
ParamRangeCell.__index = ParamRangeCell
function ParamRangeCell.new(range)
local param_cell_range = {range = range}
return setmetatable(param_cell_range, ParamRangeCell)
end
function ParamRangeCell:write(builder, wrapped)
self.value:write(builder, wrapped)
end
function ParamRangeCell:hit_test(cursor)
return self.value:hit_test(cursor)
end
function ParamRangeCell:hit_search(trace, depth)
return self.value:hit_search(trace, depth + 1)
end
function ParamRangeCell:get_start_row()
return self.value.start_cursor.row
end
function ParamRangeCell:get_stop_row()
return self.value.stop_cursor.row
end
function ParamRangeCell:is_empty()
return false
end end
local Param = {} local Param = {}
@ -59,8 +72,16 @@ function Param.new(range, index, cells)
return setmetatable(param, Param) return setmetatable(param, Param)
end end
function Param:append(value, type) function Param.new_cursor_cell(cursor)
table.insert(self.cells, ParamCell.new(value, type)) return ParamCursorCell.new(cursor)
end
function Param.new_range_cell(range)
return ParamRangeCell(range)
end
function Param:append(cell)
table.insert(self.cells, cell)
end end
function Param:stripped() function Param:stripped()

View File

@ -64,12 +64,12 @@ function Range:parse()
local param = nil local param = nil
local param_index = 1 local param_index = 1
local append_param_value = function(value, type) local append_param_value = function(cell)
if not param then if not param then
param = Param.new(self, param_index) param = Param.new(self, param_index)
param_index = param_index + 1 param_index = param_index + 1
end end
param:append(value, type) param:append(cell)
end end
local append_param = function() local append_param = function()
@ -84,16 +84,16 @@ function Range:parse()
local value = cursor:get_value() local value = cursor:get_value()
if cursor:is_string() or cursor:is_comment() then if cursor:is_string() or cursor:is_comment() then
append_param_value(cursor, 'cursor') append_param_value(Param.new_cursor_cell(cursor))
else else
local range = find_at_cursor(cursor) local range = find_at_cursor(cursor)
if range then if range then
append_param_value(range, 'range') append_param_value(Param.new_range_cell(range))
cursor = range.stop_cursor cursor = range.stop_cursor
elseif value == ',' then elseif value == ',' then
append_param() append_param()
else else
append_param_value(cursor, 'cursor') append_param_value(Param.new_cursor_cell(cursor))
end end
end end