local Window = require("jjannotate.window") local backend = require("jjannotate.backend") local cache = require("jjannotate.cache") local util = require("jjannotate.util") --- Attach to an annotate buffer created/restored externally ---@param buf integer local function attach(buf) if vim.b[buf].jjannotate then return end local ref = util.parse_uri("annotate", vim.api.nvim_buf_get_name(buf)) if not ref or cache:get_by_revision(ref) then return end local b = backend.detect(ref.repo) if not b then return end ---@type jjannotate.Revision local revision = vim.tbl_extend("force", ref, { backend = b }) vim.b[buf].jjannotate = true Window.open(revision, false) end local M = {} local augroup = vim.api.nvim_create_augroup("jjannotate_attach", { clear = true }) function M.setup() vim.api.nvim_create_autocmd("BufReadCmd", { desc = "Load a jjannotate buffer", group = augroup, pattern = "jjannotate://annotate/*", callback = function(args) attach(args.buf) end, }) vim.api.nvim_create_autocmd("BufFilePost", { desc = "Attach to a renamed jjannotate buffer", group = augroup, pattern = "jjannotate://*", callback = function(args) attach(args.buf) end, }) end return M