1
This commit is contained in:
Alex Yatskov 2025-01-01 18:43:57 -08:00
parent b9d337598e
commit 015e16de6d
4 changed files with 90 additions and 37 deletions

View File

@ -33,7 +33,10 @@ local function reflow()
builder:output(range.start_cursor.row, range.stop_cursor.row) builder:output(range.start_cursor.row, range.stop_cursor.row)
local new_range = Range.find_at_cursor(range.start_cursor) local new_range = Range.find_at_cursor(range.start_cursor)
print('-------- begin ----------------')
dump(trace)
new_range:hit_search(trace, 1):set_current() new_range:hit_search(trace, 1):set_current()
print('-------- end ----------------')
return true return true
end end

View File

@ -10,9 +10,6 @@ local options_current = {
go = { go = {
comma_last = true, comma_last = true,
}, },
lua = {
comma_last = true,
},
} }
local function setup(opt) local function setup(opt)

View File

@ -12,6 +12,7 @@ end
function ParamCursorCell:hit_test(cursor) function ParamCursorCell:hit_test(cursor)
if self.cursor == cursor then if self.cursor == cursor then
print('!!! hit cursor')
return {} return {}
end end
end end
@ -32,6 +33,10 @@ function ParamCursorCell:is_empty()
return not self.cursor:get_value():match('%S') return not self.cursor:get_value():match('%S')
end end
function ParamCursorCell:get_type()
return 'cursor'
end
local ParamRangeCell = {} local ParamRangeCell = {}
ParamRangeCell.__index = ParamRangeCell ParamRangeCell.__index = ParamRangeCell
@ -45,11 +50,15 @@ function ParamRangeCell:write(builder, wrapped)
end end
function ParamRangeCell:hit_test(cursor) function ParamRangeCell:hit_test(cursor)
return self.range:hit_test(cursor) local trace = self.range:hit_test(cursor)
if trace then
print('!!! hit range')
return trace
end
end end
function ParamRangeCell:hit_search(trace, depth) function ParamRangeCell:hit_search(trace, depth)
return self.range:hit_search(trace, depth + 1) return self.range:hit_search(trace, depth)
end end
function ParamRangeCell:get_start_row() function ParamRangeCell:get_start_row()
@ -64,6 +73,10 @@ function ParamRangeCell:is_empty()
return false return false
end end
function ParamRangeCell:get_type()
return 'range'
end
local Param = {} local Param = {}
Param.__index = Param Param.__index = Param
@ -84,32 +97,58 @@ function Param:append(cell)
table.insert(self.cells, cell) table.insert(self.cells, cell)
end end
function Param:stripped() function Param:hit_test(cursor)
local cells = {} for _, cell in ipairs(self.cells) do
for i = self:find_index_begin(), self:find_index_end() do print(cell:get_type())
table.insert(cells, self.cells[i])
end end
return Param.new(self.range, self.index, cells) for i = 1, #self.cells do
end local cell = self.cells[i]
function Param:hit_test(cursor)
for i, cell in ipairs(self.cells) do
local trace = cell:hit_test(cursor) local trace = cell:hit_test(cursor)
if trace then if trace then
table.insert(trace, 1, i) local start_index = self:find_index_start()
local stop_index = self:find_index_stop()
local index = math.min(i, stop_index)
index = math.max(1, index - start_index + 1)
print('lol', i, index, start_index, stop_index)
print('hit cell ' .. i .. ' ' .. cell:get_type())
table.insert(trace, 1, {
index = index,
count = stop_index - start_index + 1,
type = cell:get_type()
})
return trace return trace
end end
end end
end end
function Param:hit_search(trace, depth) function Param:hit_search(trace, depth)
local index = trace[depth] print('*** PARAM @', depth)
return self.cells[index]:hit_search(depth) dump(trace[depth])
local start_index = self:find_index_start()
local stop_index = self:find_index_stop()
local offset = math.max(0, start_index - 1)
assert(depth <= #trace)
local frame = trace[depth]
assert(frame.count == stop_index - start_index + 1)
local index = frame.index + offset
assert(index <= #self.cells)
local cell = self.cells[index]
assert(cell:get_type() == frame.type)
return cell:hit_search(trace, depth)
end end
function Param:write(builder, wrapped) function Param:write(builder, wrapped)
for i = self:find_index_begin(), self:find_index_end() do for i = self:find_index_start(), self:find_index_stop() do
self.cells[i]:write(builder, wrapped) self.cells[i]:write(builder, wrapped)
end end
end end
@ -138,7 +177,7 @@ function Param:get_stop_row()
end end
end end
function Param:find_index_begin() function Param:find_index_start()
for i = 1, #self.cells do for i = 1, #self.cells do
if not self.cells[i]:is_empty() then if not self.cells[i]:is_empty() then
return i return i
@ -146,7 +185,7 @@ function Param:find_index_begin()
end end
end end
function Param:find_index_end() function Param:find_index_stop()
for i = #self.cells, 1, -1 do for i = #self.cells, 1, -1 do
if not self.cells[i]:is_empty() then if not self.cells[i]:is_empty() then
return i return i
@ -164,7 +203,7 @@ function Param:is_wrapped()
end end
function Param:is_empty() function Param:is_empty()
return self:find_index_begin() == nil return self:find_index_start() == nil
end end
return Param return Param

View File

@ -74,7 +74,7 @@ function Range:parse()
local append_param = function() local append_param = function()
if param and not param:is_empty() then if param and not param:is_empty() then
table.insert(self.params, param:stripped()) table.insert(self.params, param)
end end
param = nil param = nil
end end
@ -105,32 +105,46 @@ end
function Range:hit_test(cursor) function Range:hit_test(cursor)
if self:contains(cursor) then if self:contains(cursor) then
local trace = nil
for i, param in pairs(self.params) do for i, param in pairs(self.params) do
local trace_param = param:hit_test(cursor) local trace = param:hit_test(cursor)
if trace_param then if trace then
trace = {i} print('hit param ' .. i)
for _, trace_param_frame in ipairs(trace_param) do
table.insert(trace, trace_param_frame) if #trace > 1 then
end print('++++++++++++++++++ do it ++++++++++++')
break
end
end end
if not trace then table.insert(trace, 1, {
trace = {0} index = i,
end count = #self.params,
type = 'range'
})
return trace return trace
end end
end
return {{
index = 0,
count = #self.params,
type = 'range'
}}
end
end end
function Range:hit_search(trace, depth) function Range:hit_search(trace, depth)
local index = trace[depth] print('*** RANGE @', depth)
if index == 0 then dump(trace[depth])
local frame = trace[depth]
assert(frame.type == 'range')
assert(frame.count == #self.params)
assert(frame.index <= #self.params)
if self.index == 0 then
return self.start_cursor return self.start_cursor
else else
return self.params[index]:hit_search(trace, depth + 1) return self.params[frame.index]:hit_search(trace, depth + 1)
end end
end end