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 range = Range.find_closest()
if range then
local trace = range:hit_test(
Cursor.get_current()
)
local trace = range:hit_test(Cursor.get_current())
assert(trace)
dump(trace)
end

View File

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

View File

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

View File

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