Skip to content

Commit

Permalink
Fixes #68. Correctly update and place cursor
Browse files Browse the repository at this point in the history
1. Don't set cursor if it has already been set from another source
2. Don't use BufLeave and VimLeave. Instead, use BufWinLeave and ExitPre to update and save tag state
  • Loading branch information
Calvin Bochulak authored and Calvin Bochulak committed Feb 26, 2024
1 parent 193b6c5 commit 70015a5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
23 changes: 8 additions & 15 deletions lua/grapple/autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,22 @@ local M = {}
function M.create()
vim.api.nvim_create_augroup("Grapple", { clear = true })

-- Save file tags when exiting
vim.api.nvim_create_autocmd({ "VimLeave" }, {
group = "Grapple",
callback = function()
local ok, _ = pcall(require("grapple").save)
if not ok then
require("grapple.log").warn("Unable to save tags when exiting neovim")
end
end,
})

-- Update file tag cursor
vim.api.nvim_create_autocmd({ "BufLeave" }, {
vim.api.nvim_create_autocmd({ "BufWinLeave", "ExitPre" }, {
group = "Grapple",
pattern = "*",
callback = function()
callback = function(opts)
local ok, _ = pcall(function()
local settings = require("grapple.settings")
local tag = require("grapple").find()
local tag = require("grapple").find({ buffer = opts.buf })
if tag ~= nil then
local cursor = vim.api.nvim_win_get_cursor(0)
local scope = require("grapple.state").ensure_loaded(settings.scope)
require("grapple.tags").update(scope, tag, cursor)

if cursor[1] ~= tag.cursor[1] or cursor[2] ~= tag.cursor[2] then
require("grapple.tags").update(scope, tag, cursor)
require("grapple").save()
end
end
end)
if not ok then
Expand Down
7 changes: 6 additions & 1 deletion lua/grapple/tags.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,12 @@ function tags.select(tag)

vim.api.nvim_cmd({ cmd = "edit", args = { Path:new(tag.file_path):make_relative() } }, {})
if tag.cursor then
vim.api.nvim_win_set_cursor(0, tag.cursor)
local cursor = vim.api.nvim_win_get_cursor(0)

-- Set the cursor only if it is at the top of the file
if cursor[1] == 1 and cursor[2] == 0 then
vim.api.nvim_win_set_cursor(0, tag.cursor)
end
end

return true
Expand Down

0 comments on commit 70015a5

Please sign in to comment.