1
This commit is contained in:
Alex Yatskov 2024-04-21 21:57:41 -07:00
parent 648bf0e110
commit 29b01b8289

View File

@ -9,21 +9,21 @@ function Cursor.new(row, col)
return setmetatable(cursor, {__index = Cursor}) return setmetatable(cursor, {__index = Cursor})
end end
function Cursor.current() function Cursor.get()
local _, row, col, _ = unpack(vim.fn.getpos('.')) local _, row, col, _ = unpack(vim.fn.getpos('.'))
return Cursor.new(row, col) return Cursor.new(row, col)
end end
function Cursor:is_string_literal() function Cursor:is_valid()
return self.row > 0 and self.col > 0
end
function Cursor:is_string()
local syn_id = vim.fn.synID(self.row, self.col, false) local syn_id = vim.fn.synID(self.row, self.col, false)
local syn_attr = vim.fn.synIDattr(syn_id, 'name') local syn_attr = vim.fn.synIDattr(syn_id, 'name')
return syn_attr:find('String$') return syn_attr:find('String$')
end end
function Cursor:is_valid()
return self.row > 0 and self.col > 0
end
-- --
-- BracePair -- BracePair
-- --
@ -69,8 +69,8 @@ function BracePair:find(backward)
escaped_pair.close, escaped_pair.close,
flags, flags,
function() function()
local cursor = Cursor:current() local cursor = Cursor:get()
return cursor:is_string_literal() return cursor:is_string()
end end
) )
@ -106,3 +106,38 @@ function BraceRange.find(brace_pair)
end end
end end
end end
function BraceRange.find_all()
local brace_range_compare = function(brace_range_1, brace_range_2)
local cursor = Cursor:get()
local row_diff1 = cursor.row - brace_range_1.start_cursor.row
local row_diff2 = cursor.row - brace_range_2.start_cursor.row
if row_diff1 < row_diff2 then
return -1
elseif row_diff1 > row_diff2 then
return 1
end
local col_diff1 = cursor.col - brace_range_1.start_cursor.col
local col_diff2 = cursor.col - brace_range_2.start_cursor.col
if col_diff1 < col_diff2 then
return -1
elseif col_diff1 > col_diff2 then
return 1
end
return 0
end
local brace_ranges = {}
for _, brace in ipairs({'(', '[', '{', '<'}) do
local brace_pair = BracePair.from_brace(brace)
local brace_range = BraceRange.find(brace_pair)
if brace_range then
table.insert(brace_ranges, brace_range)
end
end
return vim.fn.sort(brace_ranges, brace_range_compare)
end