1
This commit is contained in:
Alex Yatskov 2024-12-31 16:25:43 -08:00
parent 68a581f12b
commit 2d1b8cd756
3 changed files with 52 additions and 28 deletions

View File

@ -2,57 +2,81 @@ local Cursor = {}
Cursor.__index = Cursor
function Cursor.new(row, col)
local cursor = {
row = row,
col = col,
}
local cursor = {row = row, col = col}
return setmetatable(cursor, Cursor)
end
function Cursor.get_first()
local row = 1
local line = vim.fn.getline(row)
return Cursor.new(row, #line)
end
function Cursor.get_last()
local row = vim.fn.line('$')
local line = vim.fn.getline(row)
return Cursor.new(row, #line)
end
function Cursor.get_current()
local _, row, col, _ = unpack(vim.fn.getpos('.'))
return Cursor.new(row, col)
end
function Cursor:set_current()
assert(self:is_valid())
vim.fn.setcursorcharpos({self.row, self.col})
end
function Cursor:get_value()
assert(self:is_valid())
local line = vim.fn.getline(self.row)
return line:sub(self.col, self.col)
end
function Cursor:get_syntax_name()
local syn_id = vim.fn.synID(self.row, self.col, false)
return vim.fn.synIDattr(syn_id, 'name')
end
function Cursor:is_valid()
return self.row > 0 and self.col > 0
end
function Cursor:is_literal()
assert(self:is_valid())
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$') ~= nil or syn_attr:find('Comment$') ~= nil
function Cursor:is_comment()
local name = self:get_syntax_name()
return name:find('Comment$') ~= nil
end
function Cursor:next()
assert(self:is_valid())
function Cursor:is_string()
local name = self:get_syntax_name()
return name:find('String$') ~= nil
end
local current_line = vim.fn.getline(self.row)
local next_col = self.col + 1
local next_row = self.row
function Cursor:get_previous()
if self > Cursor.get_first() then
local previous_cursor = Cursor.new(self.row, self.col - 1)
if next_col > #current_line then
next_row = next_row + 1
next_col = 1
if previous_cursor.col < 1 then
previous_cursor.row = previous_cursor.row - 1
local line = vim.fn.getline(previous_cursor.row )
previous_cursor.col = #line
end
return previous_cursor
end
end
return Cursor.new(next_row, next_col)
function Cursor:get_next()
if self < Cursor.get_last() then
local line = vim.fn.getline(self.row)
local next_cursor = Cursor.new(self.row, self.col + 1)
if next_cursor.col > #line then
next_cursor.row = next_cursor.row + 1
next_cursor.col = 1
end
return next_cursor
end
end
function Cursor.__lt(cursor_1, cursor_2)

View File

@ -51,7 +51,7 @@ function Pairing:find_closest(forward)
local ignore_func = function()
local cursor = Cursor.get_current()
return cursor:is_literal()
return cursor:is_comment() or cursor:is_string()
end
local escaped_pair = self:get_escaped()

View File

@ -79,11 +79,11 @@ function Range:parse()
param = nil
end
local cursor = self.start_cursor:next()
local cursor = self.start_cursor:get_next()
while cursor ~= self.stop_cursor do
local value = cursor:get_value()
if cursor:is_literal() then
if cursor:is_string() or cursor:is_comment() then
append_param_value(cursor, 'cursor')
else
local range = find_at_cursor(cursor)
@ -97,7 +97,7 @@ function Range:parse()
end
end
cursor = cursor:next()
cursor = cursor:get_next()
end
append_param()