1
This commit is contained in:
Alex Yatskov 2025-01-19 10:52:37 -08:00
parent fd0db3b11b
commit 1df9315390
4 changed files with 72 additions and 77 deletions

View File

@ -42,9 +42,7 @@ 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( local trace = range:hit_test(Cursor.get_current())
Cursor.get_current()
)
assert(trace) assert(trace)
dump(trace) dump(trace)
end end

View File

@ -4,7 +4,7 @@ local options_current = {
brace_last_indent = false, brace_last_indent = false,
brace_last_wrap = true, brace_last_wrap = true,
brace_pad = false, brace_pad = false,
comma_last = {['{'] = true}, comma_last = {['{'] = true, ['['] = true},
comma_prefix = false, comma_prefix = false,
comma_prefix_indent = false, comma_prefix_indent = false,
}, },

View File

@ -2,8 +2,8 @@ local ParamCursorCell = {}
ParamCursorCell.__index = ParamCursorCell ParamCursorCell.__index = ParamCursorCell
function ParamCursorCell.new(cursor) function ParamCursorCell.new(cursor)
local param_cell_cursor = {cursor = cursor} local cell = {cursor = cursor}
return setmetatable(param_cell_cursor, ParamCursorCell) return setmetatable(cell, ParamCursorCell)
end end
function ParamCursorCell:write(builder) function ParamCursorCell:write(builder)
@ -12,10 +12,7 @@ end
function ParamCursorCell:hit_test(cursor) function ParamCursorCell:hit_test(cursor)
if self.cursor == cursor then if self.cursor == cursor then
return {{ return {{char = self.cursor:get_value(), type = 'cursor_cell'}}
char = self.cursor:get_value(),
type = 'cursor_cell',
}}
end end
end end
@ -52,8 +49,8 @@ local ParamRangeCell = {}
ParamRangeCell.__index = ParamRangeCell ParamRangeCell.__index = ParamRangeCell
function ParamRangeCell.new(range) function ParamRangeCell.new(range)
local param_cell_range = {range = range} local cell = {range = range}
return setmetatable(param_cell_range, ParamRangeCell) return setmetatable(cell, ParamRangeCell)
end end
function ParamRangeCell:write(builder, wrapped) function ParamRangeCell:write(builder, wrapped)
@ -119,33 +116,18 @@ function Param:append(cell)
table.insert(self.cells, cell) table.insert(self.cells, cell)
end end
function Param:validate()
local index_begin = self:find_index_begin()
local index_end = self:find_index_end()
for i = index_begin, index_end - 1 do
local cell = self.cells[i]
local next_cell = self.cells[i + 1]
if cell:get_stop_row() ~= next_cell:get_start_row() then
return false
end
end
return true
end
function Param:hit_test(cursor) function Param:hit_test(cursor)
local index_begin = self:find_index_begin() local start_index = self:get_start_index()
local index_end = self:find_index_end() local stop_index = self:get_stop_index()
for i = 1, index_begin - 1, 1 do for i = 1, start_index - 1, 1 do
local cell = self.cells[i] local cell = self.cells[i]
if cell:hit_test(cursor) then if cell:hit_test(cursor) then
cursor = cursor:get_next() cursor = cursor:get_next()
end end
end end
for i = #self.cells, index_end + 1, -1 do for i = #self.cells, stop_index + 1, -1 do
local cell = self.cells[i] local cell = self.cells[i]
if cell:hit_test(cursor) then if cell:hit_test(cursor) then
cursor = cursor:get_previous() cursor = cursor:get_previous()
@ -158,7 +140,7 @@ function Param:hit_test(cursor)
table.insert(trace, 1, { table.insert(trace, 1, {
type = 'param', type = 'param',
cell_count = #self.cells, cell_count = #self.cells,
cell_index = i - index_begin + 1, cell_index = i - start_index + 1,
}) })
return trace return trace
@ -173,14 +155,14 @@ function Param:hit_search(trace, depth)
if depth == #trace then if depth == #trace then
return self return self
else else
local index_clamped = frame.cell_index + self:find_index_begin() - 1 local index_clamped = frame.cell_index + self:get_start_index() - 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 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:get_start_index(), self:get_stop_index() do
self.cells[i]:write(builder, wrapped) self.cells[i]:write(builder, wrapped)
end end
end end
@ -221,7 +203,7 @@ function Param:get_stop_row()
end end
end end
function Param:find_index_begin() function Param:get_start_index()
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
@ -229,7 +211,7 @@ function Param:find_index_begin()
end end
end end
function Param:find_index_end() function Param:get_stop_index()
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
@ -237,6 +219,21 @@ function Param:find_index_end()
end end
end end
function Param:is_contiguous()
local start_index = self:get_start_index()
local stop_index = self:get_stop_index()
for i = start_index, stop_index - 1 do
local cell = self.cells[i]
local next_cell = self.cells[i + 1]
if cell:get_stop_row() ~= next_cell:get_start_row() then
return false
end
end
return true
end
function Param:is_wrapped() function Param:is_wrapped()
local previous_param = self:get_previous() local previous_param = self:get_previous()
if previous_param then if previous_param then
@ -247,7 +244,7 @@ function Param:is_wrapped()
end end
function Param:is_empty() function Param:is_empty()
return self:find_index_begin() == nil return self:get_start_index() == nil
end end
return Param return Param

View File

@ -27,7 +27,28 @@ local function find_closest()
end end
if #ranges > 0 then if #ranges > 0 then
table.sort(ranges) local range_compare = function(range_1, range_2)
local cursor = Cursor:get_current()
local row_diff_1 = range_1.start_cursor.row - cursor.row
local col_diff_1 = range_1.start_cursor.col - cursor.col
local row_diff_2 = range_2.start_cursor.row - cursor.row
local col_diff_2 = range_2.start_cursor.col - cursor.col
if row_diff_1 < row_diff_2 then
return false
elseif row_diff_1 > row_diff_2 then
return true
elseif col_diff_1 < col_diff_2 then
return false
elseif col_diff_1 > col_diff_2 then
return true
else
return true
end
end
table.sort(ranges, range_compare)
return ranges[1] return ranges[1]
end end
end end
@ -45,7 +66,7 @@ local function find_at_cursor(start_cursor, parent_range)
return return
end end
if parent_range and not parent_range:contains(stop_cursor) then if parent_range and not parent_range:contains_cursor(stop_cursor) then
return return
end end
@ -62,7 +83,6 @@ function Range.new(start_cursor, stop_cursor, pairing)
} }
setmetatable(range, Range) setmetatable(range, Range)
if range:parse() then if range:parse() then
return range return range
end end
@ -109,21 +129,11 @@ function Range:parse()
end end
append_param() append_param()
return self:validate() return self:is_contiguous()
end
function Range:validate()
for _, param in ipairs(self.params) do
if not param:validate() then
return false
end
end
return true
end end
function Range:hit_test(cursor) function Range:hit_test(cursor)
if self:contains(cursor) then if self:contains_cursor(cursor) then
local frame = { local frame = {
type = 'range', type = 'range',
pairing = self.pairing.open .. self.pairing.close, pairing = self.pairing.open .. self.pairing.close,
@ -138,10 +148,10 @@ function Range:hit_test(cursor)
end end
for i = 2, #self.params do for i = 2, #self.params do
local current_param = self.params[i] local param = self.params[i]
local previous_param = self.params[i - 1] local previous_param = self.params[i - 1]
if previous_param:is_before_cursor(cursor) and current_param:is_after_cursor(cursor) then if previous_param:is_before_cursor(cursor) and param:is_after_cursor(cursor) then
if not previous_param:hit_test(cursor) and not current_param:hit_test(cursor) then if not previous_param:hit_test(cursor) and not param:hit_test(cursor) then
cursor = cursor:get_previous() cursor = cursor:get_previous()
break break
end end
@ -249,12 +259,18 @@ function Range:write(builder, wrapped)
end end
end end
function Range:contains(cursor) function Range:contains_cursor(cursor)
return cursor >= self.start_cursor and cursor <= self.stop_cursor return cursor >= self.start_cursor and cursor <= self.stop_cursor
end end
function Range:query_option(name) function Range:is_contiguous()
return Options.query(name, self.pairing) for _, param in ipairs(self.params) do
if not param:is_contiguous() then
return false
end
end
return true
end end
function Range:is_wrapped() function Range:is_wrapped()
@ -263,28 +279,12 @@ function Range:is_wrapped()
return true return true
end end
end end
return false return false
end end
function Range.__lt(range_1, range_2) function Range:query_option(name)
local cursor = Cursor:get_current() return Options.query(name, self.pairing)
local row_diff1 = range_1.start_cursor.row - cursor.row
local col_diff1 = range_1.start_cursor.col - cursor.col
local row_diff2 = range_2.start_cursor.row - cursor.row
local col_diff2 = range_2.start_cursor.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 end
return { return {