1

Wrap on comma

This commit is contained in:
Alex Yatskov 2025-01-19 09:49:45 -08:00
parent e1511ca730
commit 56e61a6e7d
3 changed files with 65 additions and 12 deletions

View File

@ -34,7 +34,7 @@ local function reflow()
local new_range = Range.find_at_cursor(range.start_cursor) local new_range = Range.find_at_cursor(range.start_cursor)
assert(new_range) assert(new_range)
new_range:hit_search(trace, 1):set_current() new_range:hit_search(trace, 1).cursor:set_current()
return true return true
end end
@ -42,7 +42,9 @@ end
local function inspect() local function inspect()
local range = Range.find_closest() local range = Range.find_closest()
if range then if range then
local trace = range:hit_test(Cursor.get_current()) local trace = range:hit_test(
Cursor.get_current()
)
assert(trace) assert(trace)
dump(trace) dump(trace)
end end

View File

@ -23,8 +23,17 @@ function ParamCursorCell:hit_search(trace, depth)
local frame = trace[depth] local frame = trace[depth]
assert(frame.type == 'cursor_cell') assert(frame.type == 'cursor_cell')
assert(frame.char == self.cursor:get_value()) assert(frame.char == self.cursor:get_value())
assert(depth == #trace)
return self.cursor return self
end
function ParamCursorCell:is_before_cursor(cursor)
return self.cursor < cursor
end
function ParamCursorCell:is_after_cursor(cursor)
return self.cursor > cursor
end end
function ParamCursorCell:get_start_row() function ParamCursorCell:get_start_row()
@ -59,11 +68,23 @@ function ParamRangeCell:hit_test(cursor)
end end
end end
function ParamRangeCell:is_before_cursor(cursor)
return self.range.start_cursor < cursor
end
function ParamRangeCell:is_after_cursor(cursor)
return self.range.stop_cursor > cursor
end
function ParamRangeCell:hit_search(trace, depth) function ParamRangeCell:hit_search(trace, depth)
local frame = trace[depth] local frame = trace[depth]
assert(frame.type == 'range_cell') assert(frame.type == 'range_cell')
if depth == #trace then
return self
else
return self.range:hit_search(trace, depth + 1) return self.range:hit_search(trace, depth + 1)
end
end end
function ParamRangeCell:get_start_row() function ParamRangeCell:get_start_row()
@ -136,10 +157,13 @@ function Param:hit_search(trace, depth)
local frame = trace[depth] local frame = trace[depth]
assert(frame.type == 'param') assert(frame.type == 'param')
if depth == #trace then
return self
else
local index_clamped = frame.cell_index + self:find_index_begin() - 1 local index_clamped = frame.cell_index + self:find_index_begin() - 1
assert(index_clamped <= #self.cells) assert(index_clamped <= #self.cells)
return self.cells[index_clamped]:hit_search(trace, depth + 1) return self.cells[index_clamped]:hit_search(trace, depth + 1)
end
end end
function Param:write(builder, wrapped) function Param:write(builder, wrapped)
@ -148,6 +172,18 @@ function Param:write(builder, wrapped)
end end
end end
function Param:is_before_cursor(cursor)
if #self.cells > 0 then
return self.cells[1]:is_before_cursor(cursor)
end
end
function Param:is_after_cursor(cursor)
if #self.cells > 0 then
return self.cells[#self.cells]:is_after_cursor(cursor)
end
end
function Param:get_previous() function Param:get_previous()
if self.index > 1 then if self.index > 1 then
return self.range.params[self.index - 1] return self.range.params[self.index - 1]

View File

@ -120,6 +120,23 @@ function Range:hit_test(cursor)
param_index = 0, param_index = 0,
} }
if cursor == self.start_cursor and self.params[1]:is_after_cursor(cursor) then
cursor = cursor:get_next()
elseif cursor == self.stop_cursor and self.params[#self.params]:is_before_cursor(cursor) then
cursor = cursor:get_previous()
end
for i = 2, #self.params do
local current_param = self.params[i]
local previous_param = self.params[i - 1]
if previous_param:is_before_cursor(cursor) and current_param:is_after_cursor(cursor) then
if not previous_param:hit_test(cursor) and not current_param:hit_test(cursor) then
cursor = cursor:get_previous()
break
end
end
end
for i, param in pairs(self.params) do for i, param in pairs(self.params) do
local trace = param:hit_test(cursor) local trace = param:hit_test(cursor)
if trace then if trace then
@ -128,8 +145,6 @@ function Range:hit_test(cursor)
return trace return trace
end end
end end
return {frame}
end end
end end
@ -140,8 +155,8 @@ function Range:hit_search(trace, depth)
assert(frame.param_count == #self.params) assert(frame.param_count == #self.params)
assert(frame.param_index <= #self.params) assert(frame.param_index <= #self.params)
if frame.param_index == 0 then if depth == #trace then
return self.start_cursor return self
else else
return self.params[frame.param_index]:hit_search(trace, depth + 1) return self.params[frame.param_index]:hit_search(trace, depth + 1)
end end