Skip to content

Commit

Permalink
refactor: don't save on Grapple.{select, cycle} + make cursor optional
Browse files Browse the repository at this point in the history
  • Loading branch information
cbochs committed Mar 17, 2024
1 parent 42a150d commit 8a2ed41
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lua/grapple.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function Grapple.select(opts)
opts = opts or {}

local app = App.get()
app:enter_with_save(opts.scope, function(container)
app:enter_without_save(opts.scope, function(container)
local path, _ = extract_path(opts)
opts.path = path

Expand Down Expand Up @@ -223,7 +223,7 @@ function Grapple.cycle(direction, opts)
opts = opts or {}

local app = require("grapple.app").get()
app:enter_with_save(opts.scope, function(container)
app:enter_without_save(opts.scope, function(container)
if container:is_empty() then
return
end
Expand Down
24 changes: 16 additions & 8 deletions lua/grapple/tag.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local Path = require("grapple.path")
---@class grapple.tag
---@field path string absolute path
---@field name string | nil (optional) tag name
---@field cursor integer[] (1, 0)-indexed cursor position
---@field cursor integer[] | nil (optional) (1, 0)-indexed cursor position
local Tag = {}
Tag.__index = Tag

Expand All @@ -14,7 +14,7 @@ function Tag:new(path, name, cursor)
return setmetatable({
path = path,
name = name,
cursor = cursor or { 1, 0 },
cursor = cursor,
}, self)
end

Expand All @@ -29,12 +29,20 @@ function Tag:select(command)
command = command or vim.cmd.edit
command(short_path)

-- If the cursor has already been set, update instead
local current_cursor = vim.api.nvim_win_get_cursor(0)
if current_cursor[1] > 1 or current_cursor[2] > 0 then
self.cursor = current_cursor
else
pcall(vim.api.nvim_win_set_cursor, 0, self.cursor)
if self.cursor then
local current_cursor = vim.api.nvim_win_get_cursor(0)
local last_line = vim.api.nvim_buf_line_count(0)

-- Clamp the cursor to the last line
local cursor = {
math.min(self.cursor[1], last_line),
self.cursor[2],
}

-- If the cursor has already been set, don't set again
if current_cursor[1] == 1 or current_cursor[2] == 0 then
pcall(vim.api.nvim_win_set_cursor, 0, cursor)
end
end
end

Expand Down

0 comments on commit 8a2ed41

Please sign in to comment.