1
This commit is contained in:
Alex Yatskov 2024-04-21 18:11:56 -07:00
parent df5b365abf
commit 648bf0e110

View File

@ -1,22 +1,50 @@
--
-- Cursor
--
Cursor = {} Cursor = {}
function Cursor.new(x, y) function Cursor.new(row, col)
local cursor = { local cursor = {row = row, col = col}
x = x,
y = y,
}
return setmetatable(cursor, {__index = Cursor}) return setmetatable(cursor, {__index = Cursor})
end end
function Cursor:print() function Cursor.current()
print('x: ' .. self.x .. ' y: ' .. self.y) local _, row, col, _ = unpack(vim.fn.getpos('.'))
return Cursor.new(row, col)
end end
function Cursor:is_string_literal()
local syn_id = vim.fn.synID(self.row, self.col, false)
local syn_attr = vim.fn.synIDattr(syn_id, 'name')
return syn_attr:find('String$')
end
function Cursor:is_valid()
return self.row > 0 and self.col > 0
end
--
-- BracePair
--
BracePair = {} BracePair = {}
function BracePair.new(brace) function BracePair.new(brace_open, brace_close)
local escape = function(brace_raw) local brace_pair = {open = brace_open, close = brace_close}
return setmetatable(brace_pair, {__index = BracePair})
end
function BracePair.from_brace(brace)
for _, brace_set in ipairs({{'(', ')'}, {'[', ']'}, {'{', '}'}, {'<', '>'}}) do
if brace_set[1] == brace or brace_set[2] == brace then
return BracePair.new(brace_set[1], brace_set[2])
end
end
end
function BracePair:escaped()
local escape_brace = function(brace_raw)
if brace_raw == '[' or brace_raw == ']' then if brace_raw == '[' or brace_raw == ']' then
return '\\' .. brace_raw return '\\' .. brace_raw
else else
@ -24,22 +52,38 @@ function BracePair.new(brace)
end end
end end
for _, brace_set in ipairs({{'(', ')'}, {'[', ']'}, {'{', '}'}, {'<', '>'}}) do return BracePair.new(escape_brace(self.open), escape_brace(self.close))
if brace_set[1] == brace or brace_set[2] == brace then end
local brace_pair = {
open = brace_set[1],
close = brace_set[2],
escaped = {
open = escape(brace_set[1]),
closed = escape(brace_set[2]),
}
}
return setmetatable(brace_pair, {__index = BracePair}) function BracePair:find(backward)
-- See flags: https://neovim.io/doc/user/builtin.html#search()
local flags = 'Wcn'
if backward then
flags = 'Wbn'
end end
local escaped_pair = self:escaped()
local position = vim.fn.searchpairpos(
escaped_pair.open,
'',
escaped_pair.close,
flags,
function()
local cursor = Cursor:current()
return cursor:is_string_literal()
end
)
local cursor = Cursor.new(unpack(position))
if cursor:is_valid() then
return cursor
end end
end end
--
-- BraceRange
--
BraceRange = {} BraceRange = {}
function BraceRange.new(start_cursor, stop_cursor, brace_pair, brace_params) function BraceRange.new(start_cursor, stop_cursor, brace_pair, brace_params)
@ -47,8 +91,18 @@ function BraceRange.new(start_cursor, stop_cursor, brace_pair, brace_params)
start_cursor = start_cursor, start_cursor = start_cursor,
stop_cursor = stop_cursor, stop_cursor = stop_cursor,
brace_pair = brace_pair, brace_pair = brace_pair,
brace_params = brace_params or {}, brace_params = brace_params,
} }
return setmetatable(brace_range, {__index = BraceRange}) return setmetatable(brace_range, {__index = BraceRange})
end end
function BraceRange.find(brace_pair)
local start_cursor = brace_pair:find(false)
if start_cursor then
local stop_cursor = brace_pair:find(true)
if stop_cursor then
return BraceRange.new(start_cursor, stop_cursor, brace_pair, {})
end
end
end