Skip to content

Commit

Permalink
fix: check valid buffer when scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis6991 committed Aug 17, 2023
1 parent 55f8fc7 commit f9a0882
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 38 deletions.
36 changes: 17 additions & 19 deletions lua/gitsigns/actions.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
local void = require('gitsigns.async').void
local scheduler = require('gitsigns.async').scheduler

local async = require('gitsigns.async')
local config = require('gitsigns.config').config
local mk_repeatable = require('gitsigns.repeat').mk_repeatable
local popup = require('gitsigns.popup')
Expand Down Expand Up @@ -167,7 +165,7 @@ end
--- @param bufnr integer
local function update(bufnr)
manager.update(bufnr)
scheduler()
async.scheduler_if_buf_valid(bufnr)
if vim.wo.diff then
require('gitsigns.diffthis').update(bufnr)
end
Expand Down Expand Up @@ -202,7 +200,7 @@ local function get_hunks(bufnr, bcache, greedy, staged)
return
end
hunks = run_diff(text, buftext, false)
scheduler()
async.scheduler()
return hunks
end

Expand Down Expand Up @@ -255,7 +253,7 @@ end
--- • {greedy}: (boolean)
--- Stage all contiguous hunks. Only useful if 'diff_opts'
--- contains `linematch`. Defaults to `true`.
M.stage_hunk = mk_repeatable(void(function(range, opts)
M.stage_hunk = mk_repeatable(async.void(function(range, opts)
opts = opts or {}
local bufnr = current_buf()
local bcache = cache[bufnr]
Expand Down Expand Up @@ -306,7 +304,7 @@ end
--- • {greedy}: (boolean)
--- Stage all contiguous hunks. Only useful if 'diff_opts'
--- contains `linematch`. Defaults to `true`.
M.reset_hunk = mk_repeatable(void(function(range, opts)
M.reset_hunk = mk_repeatable(async.void(function(range, opts)
opts = opts or {}
local bufnr = current_buf()
local bcache = cache[bufnr]
Expand Down Expand Up @@ -353,7 +351,7 @@ end
---
--- Attributes: ~
--- {async}
M.undo_stage_hunk = void(function()
M.undo_stage_hunk = async.void(function()
local bufnr = current_buf()
local bcache = cache[bufnr]
if not bcache then
Expand All @@ -375,7 +373,7 @@ end)
---
--- Attributes: ~
--- {async}
M.stage_buffer = void(function()
M.stage_buffer = async.void(function()
local bufnr = current_buf()

local bcache = cache[bufnr]
Expand Down Expand Up @@ -411,7 +409,7 @@ end)
---
--- Attributes: ~
--- {async}
M.reset_buffer_index = void(function()
M.reset_buffer_index = async.void(function()
local bufnr = current_buf()
local bcache = cache[bufnr]
if not bcache then
Expand Down Expand Up @@ -490,7 +488,7 @@ end

--- @param opts? Gitsigns.NavOpts
--- @param forwards boolean
local nav_hunk = void(function(opts, forwards)
local nav_hunk = async.void(function(opts, forwards)
opts = process_nav_opts(opts)
local bufnr = current_buf()
local bcache = cache[bufnr]
Expand Down Expand Up @@ -859,7 +857,7 @@ end
--- Display full commit message with hunk.
--- • {ignore_whitespace}: (boolean)
--- Ignore whitespace when running blame.
M.blame_line = void(function(opts)
M.blame_line = async.void(function(opts)
if popup.focus_open('blame') then
return
end
Expand All @@ -876,7 +874,7 @@ M.blame_line = void(function(opts)
popup.create({ { { 'Loading...', 'Title' } } }, config.preview_config)
end, 1000)

scheduler()
async.scheduler_if_buf_valid()
local buftext = util.buf_lines(bufnr)
local fileformat = vim.bo[bufnr].fileformat
local lnum = api.nvim_win_get_cursor(0)[1]
Expand Down Expand Up @@ -905,7 +903,7 @@ M.blame_line = void(function(opts)
insert_hunk_hlmarks(blame_fmt, hunk)
end

scheduler()
async.scheduler_if_buf_valid(bufnr)

popup.create(lines_format(blame_fmt, result), config.preview_config, 'blame')
end)
Expand Down Expand Up @@ -948,7 +946,7 @@ end
---
--- @param base string|nil The object/revision to diff against.
--- @param global boolean|nil Change the base of all buffers.
M.change_base = void(function(base, global)
M.change_base = async.void(function(base, global)
base = util.calc_base(base)

if global then
Expand Down Expand Up @@ -1134,7 +1132,7 @@ local function buildqflist(target)
local stat = vim.loop.fs_stat(f_abs)
if stat and stat.type == 'file' then
local a = r:get_show_text(':0:' .. f)
scheduler()
async.scheduler()
local hunks = run_diff(a, util.file_lines(f_abs))
hunks_to_qflist(f_abs, hunks, qflist)
end
Expand Down Expand Up @@ -1170,7 +1168,7 @@ end
--- • {open}: (boolean)
--- Open the quickfix/location list viewer.
--- Defaults to `true`.
M.setqflist = void(function(target, opts)
M.setqflist = async.void(function(target, opts)
opts = opts or {}
if opts.open == nil then
opts.open = true
Expand All @@ -1179,7 +1177,7 @@ M.setqflist = void(function(target, opts)
items = buildqflist(target),
title = 'Hunks',
}
scheduler()
async.scheduler()
if opts.use_location_list then
local nr = opts.nr or 0
vim.fn.setloclist(nr, {}, ' ', qfopts)
Expand Down Expand Up @@ -1263,7 +1261,7 @@ end
---
--- Attributes: ~
--- {async}
M.refresh = void(function()
M.refresh = async.void(function()
manager.reset_signs()
require('gitsigns.highlight').setup_highlights()
require('gitsigns.current_line_blame').setup()
Expand Down
9 changes: 9 additions & 0 deletions lua/gitsigns/async.lua
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,13 @@ end
---able to call the API.
M.scheduler = M.wrap(vim.schedule, 1)

--- @param buf? integer
M.scheduler_if_buf_valid = M.wrap(function(buf, cb)
vim.schedule(function()
if not buf or vim.api.nvim_buf_is_valid(buf) then
cb()
end
end)
end, 2)

return M
6 changes: 3 additions & 3 deletions lua/gitsigns/attach.lua
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ local attach_throttled = throttle_by_id(function(cbuf, ctx, aucmd)

if not git_obj and not ctx then
git_obj = try_worktrees(cbuf, file, encoding)
async.scheduler()
async.scheduler_if_buf_valid(cbuf)
end

if not git_obj then
Expand All @@ -300,7 +300,7 @@ local attach_throttled = throttle_by_id(function(cbuf, ctx, aucmd)
end
local repo = git_obj.repo

async.scheduler()
async.scheduler_if_buf_valid(cbuf)
Status:update(cbuf, {
head = repo.abbrev_head,
root = repo.toplevel,
Expand Down Expand Up @@ -329,7 +329,7 @@ local attach_throttled = throttle_by_id(function(cbuf, ctx, aucmd)

-- On windows os.tmpname() crashes in callback threads so initialise this
-- variable on the main thread.
async.scheduler()
async.scheduler_if_buf_valid(cbuf)

if config.on_attach and config.on_attach(cbuf) == false then
dprint('User on_attach() returned false')
Expand Down
19 changes: 5 additions & 14 deletions lua/gitsigns/manager.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local void = require('gitsigns.async').void
local awrap = require('gitsigns.async').wrap
local async = require('gitsigns.async')

local gs_cache = require('gitsigns.cache')
local cache = gs_cache.cache
Expand Down Expand Up @@ -30,14 +29,6 @@ local signs_staged --- @type Gitsigns.Signs

local M = {}

local scheduler_if_buf_valid = awrap(function(buf, cb)
vim.schedule(function()
if vim.api.nvim_buf_is_valid(buf) then
cb()
end
end)
end, 2)

--- @param bufnr integer
--- @param signs Gitsigns.Signs
--- @param hunks Gitsigns.Hunk.Hunk[]
Expand Down Expand Up @@ -421,13 +412,13 @@ M.update = throttle_by_id(function(bufnr, bcache)
local old_hunks, old_hunks_staged = bcache.hunks, bcache.hunks_staged
bcache.hunks, bcache.hunks_staged = nil, nil

scheduler_if_buf_valid(bufnr)
async.scheduler_if_buf_valid(bufnr)
local buftext = util.buf_lines(bufnr)
local git_obj = bcache.git_obj

if not bcache.compare_text or config._refresh_staged_on_update then
bcache.compare_text = git_obj:get_show_text(bcache:get_compare_rev())
scheduler_if_buf_valid(bufnr)
async.scheduler_if_buf_valid(bufnr)
end

bcache.hunks = run_diff(bcache.compare_text, buftext)
Expand All @@ -440,7 +431,7 @@ M.update = throttle_by_id(function(bufnr, bcache)
bcache.hunks_staged = gs_hunks.filter_common(hunks_head, bcache.hunks)
end

scheduler_if_buf_valid(bufnr)
async.scheduler_if_buf_valid(bufnr)

-- Note the decoration provider may have invalidated bcache.hunks at this
-- point
Expand Down Expand Up @@ -536,7 +527,7 @@ function M.setup()
signs_staged = Signs.new(config._signs_staged, 'staged')
end

M.update_debounced = debounce_trailing(config.update_debounce, void(M.update))
M.update_debounced = debounce_trailing(config.update_debounce, async.void(M.update))
end

return M
4 changes: 2 additions & 2 deletions lua/gitsigns/watcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ local function handle_moved(bufnr, old_relpath)
git_obj.file = git_obj.repo.toplevel .. util.path_sep .. git_obj.relpath
bcache.file = git_obj.file
git_obj:update_file_info()
async.scheduler()
async.scheduler_if_buf_valid(bufnr)

local bufexists = util.bufexists(bcache.file)
local old_name = api.nvim_buf_get_name(bufnr)
Expand All @@ -69,7 +69,7 @@ local watch_gitdir_handler = async.void(function(bufnr)

git_obj.repo:update_abbrev_head()

async.scheduler()
async.scheduler_if_buf_valid(bufnr)
Status:update(bufnr, { head = git_obj.repo.abbrev_head })

local was_tracked = git_obj.object_name ~= nil
Expand Down

0 comments on commit f9a0882

Please sign in to comment.