diff --git a/lua/neogen/mark.lua b/lua/neogen/mark.lua index 3a5f7d2..d3a0046 100644 --- a/lua/neogen/mark.lua +++ b/lua/neogen/mark.lua @@ -3,6 +3,8 @@ local mark = {} local api = vim.api local ns = api.nvim_create_namespace("neogen") +--- Start marks creation and get useful informations +---@private mark.start = function(self) if self.started then mark:stop() @@ -17,11 +19,18 @@ mark.start = function(self) self.started = true end +--- Get a mark specified with i index +---@param i number +---@private mark.get_mark = function(self, i) local id = self.ids[i] return api.nvim_buf_get_extmark_by_id(self.bufnr, ns, id, {}) end +--- Add a mark with position +---@param pos table Position as line, col +---@return number the id of the inserted mark +---@private mark.add_mark = function(self, pos) local line, col = unpack(pos) local id = api.nvim_buf_set_extmark(self.bufnr, ns, line, col, {}) @@ -29,10 +38,17 @@ mark.add_mark = function(self, pos) return id end +--- Get how many marks are created +---@return number +---@private mark.mark_len = function(self) return #self.ids end +--- Verify if the marks can be jumpable +---@param reverse boolean +---@return boolean +---@private mark.jumpable = function(self, reverse) if not self.started then return false @@ -49,12 +65,15 @@ mark.jumpable = function(self, reverse) return ret end +--- Jump to next/previous mark if possible +---@param reverse boolean +---@private mark.jump = function(self, reverse) if mark:jumpable(reverse) then self.index = reverse and self.index - 1 or self.index + 1 if self.index > 0 and self.index <= #self.ids then local line, row = unpack(self:get_mark(self.index)) - api.nvim_win_set_cursor(self.winid, {line + 1, row}) + api.nvim_win_set_cursor(self.winid, { line + 1, row }) else self:stop(true) end @@ -63,6 +82,8 @@ mark.jump = function(self, reverse) end end +--- Clear marks and stop jumping ability +---@private mark.stop = function(self, should_jump) local bufnr = self.bufnr if bufnr and bufnr > 0 and api.nvim_buf_is_valid(bufnr) then @@ -74,7 +95,7 @@ mark.stop = function(self, should_jump) if should_jump and winid and winid > 0 and api.nvim_win_is_valid(winid) then local pos = api.nvim_buf_get_extmark_by_id(self.bufnr, ns, self.last_jump, {}) local line, col = unpack(pos) - api.nvim_win_set_cursor(winid, {line + 1, col}) + api.nvim_win_set_cursor(winid, { line + 1, col }) end api.nvim_buf_del_extmark(bufnr, ns, self.last_jump) end