M lua/argonaut/cursor.lua => lua/argonaut/cursor.lua +47 -23
@@ 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())
+function Cursor:is_comment()
+ local name = self:get_syntax_name()
+ return name:find('Comment$') ~= nil
+end
- 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_string()
+ local name = self:get_syntax_name()
+ return name:find('String$') ~= nil
end
-function Cursor:next()
- assert(self:is_valid())
+function Cursor:get_previous()
+ if self > Cursor.get_first() then
+ local previous_cursor = Cursor.new(self.row, self.col - 1)
- local current_line = vim.fn.getline(self.row)
- local next_col = self.col + 1
- local next_row = self.row
+ 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
- if next_col > #current_line then
- next_row = next_row + 1
- next_col = 1
+ return previous_cursor
end
+end
+
+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)
- return Cursor.new(next_row, next_col)
+ 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)
M lua/argonaut/pairing.lua => lua/argonaut/pairing.lua +1 -1
@@ 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()
M lua/argonaut/range.lua => lua/argonaut/range.lua +3 -3
@@ 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()