Skip to content

Commit 21e1edb

Browse files
committed
fix(statusline): special bufs sometimes clearing statusline components
1 parent 0cc2384 commit 21e1edb

3 files changed

Lines changed: 36 additions & 18 deletions

File tree

lua/tinygit/statusline/blame.lua

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ local u = require("tinygit.shared.utils")
44
--------------------------------------------------------------------------------
55

66
---@param bufnr? number
7-
---@return string blame lualine stringifys result, so need to return empty string instead of nil
7+
---@return string? blame lualine stringifys result, so need to return empty string instead of nil
88
---@nodiscard
99
local function getBlame(bufnr)
1010
bufnr = bufnr or 0
11-
local config = require("tinygit.config").config.statusline.blame
11+
local bufPath = vim.api.nvim_buf_get_name(bufnr)
1212

1313
-- GUARD valid buffer
14-
if not vim.api.nvim_buf_is_valid(bufnr) then return "" end
15-
if vim.api.nvim_get_option_value("buftype", { buf = bufnr }) ~= "" then return "" end
14+
if not vim.api.nvim_buf_is_valid(bufnr) then return end
15+
if vim.bo[bufnr].buftype ~= "" then return end
16+
if vim.uv.fs_stat(bufPath) == nil then return end -- non-existing file
1617

17-
local bufPath = vim.api.nvim_buf_get_name(bufnr)
18+
local config = require("tinygit.config").config.statusline.blame
1819
local gitLogCmd = { "git", "log", "--max-count=1", "--format=%H\t%an\t%cr\t%s", "--", bufPath }
1920
local gitLogResult = vim.system(gitLogCmd):wait()
2021

@@ -48,12 +49,16 @@ end
4849
--------------------------------------------------------------------------------
4950

5051
---@param bufnr? number
51-
function M.refreshBlame(bufnr) vim.b["tinygit_blame"] = getBlame(bufnr) end
52+
function M.refreshBlame(bufnr)
53+
bufnr = bufnr or 0
54+
if not vim.api.nvim_buf_is_valid(bufnr) then return end
55+
vim.b[bufnr].tinygit_blame = getBlame(bufnr)
56+
end
5257

5358
vim.api.nvim_create_autocmd({ "BufEnter", "DirChanged", "FocusGained" }, {
5459
group = vim.api.nvim_create_augroup("tinygit_blame", { clear = true }),
5560
callback = function(ctx)
56-
-- so buftype is set before checking the buffer
61+
-- defer so buftype is set before checking the buffer
5762
vim.defer_fn(function() M.refreshBlame(ctx.buf) end, 1)
5863
end,
5964
})

lua/tinygit/statusline/branch-state.lua

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
local M = {}
22
--------------------------------------------------------------------------------
33

4-
---@return string state lualine stringifys result, so need to return empty string instead of nil
4+
---@return string? state lualine stringifys result, so need to return empty string instead of nil
55
---@nodiscard
66
local function getBranchState()
77
local cwd = vim.uv.cwd()
8-
if not cwd then return "" end -- file without cwd
8+
if not cwd then return end -- file without cwd
99

1010
local allBranchInfo = vim.system({ "git", "-C", cwd, "branch", "--verbose" }):wait()
1111
if allBranchInfo.code ~= 0 then return "" end -- not in git repo
@@ -34,15 +34,21 @@ end
3434

3535
--------------------------------------------------------------------------------
3636

37-
function M.refreshBranchState() vim.b.tinygit_branchState = getBranchState() end
37+
function M.refreshBranchState()
38+
local state = getBranchState()
39+
if state then vim.b.tinygit_branchState = state end
40+
end
3841

3942
function M.getBranchState() return vim.b.tinygit_branchState or "" end
4043

4144
vim.api.nvim_create_autocmd({ "BufEnter", "DirChanged", "FocusGained" }, {
4245
group = vim.api.nvim_create_augroup("tinygit_branchState", { clear = true }),
43-
callback = M.refreshBranchState,
46+
callback = function()
47+
-- defer so cwd changes take place before checking
48+
vim.defer_fn(M.refreshBranchState, 1)
49+
end,
4450
})
45-
M.refreshBranchState() -- initialize in case of lazy-loading
51+
vim.defer_fn(M.refreshBranchState, 1) -- initialize in case of lazy-loading
4652

4753
--------------------------------------------------------------------------------
4854
return M

lua/tinygit/statusline/file-state.lua

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
local M = {}
22
--------------------------------------------------------------------------------
33

4-
---@return string state lualine stringifys result, so need to return empty string instead of nil
4+
---@return string? state lualine stringifys result, so need to return empty string instead of nil
55
---@nodiscard
66
local function getFileState()
7-
local u = require("tinygit.shared.utils")
7+
if not vim.uv.cwd() then return end -- file without cwd
88

9+
local u = require("tinygit.shared.utils")
910
local gitroot = u.syncShellCmd { "git", "rev-parse", "--show-toplevel" }
1011
if not gitroot then return "" end
1112
local gitStatus = vim.system({ "git", "-C", gitroot, "status", "--porcelain" }):wait()
@@ -45,15 +46,21 @@ end
4546

4647
--------------------------------------------------------------------------------
4748

48-
function M.refreshFileState() vim.b.tinygit_fileState = getFileState() end
49+
function M.refreshFileState()
50+
local state = getFileState()
51+
if state then vim.b.tinygit_fileState = state end
52+
end
4953

5054
function M.getFileState() return vim.b.tinygit_fileState or "" end
5155

5256
vim.api.nvim_create_autocmd({ "BufEnter", "DirChanged", "FocusGained" }, {
53-
group = vim.api.nvim_create_augroup("tinygit_branchState", { clear = true }),
54-
callback = M.refreshFileState,
57+
group = vim.api.nvim_create_augroup("tinygit_fileState", { clear = true }),
58+
callback = function()
59+
-- defer so cwd changes take place before checking
60+
vim.defer_fn(M.refreshFileState, 1)
61+
end,
5562
})
56-
M.refreshFileState() -- initialize in case of lazy-loading
63+
vim.defer_fn(M.refreshFileState, 1) -- initialize in case of lazy-loading
5764

5865
--------------------------------------------------------------------------------
5966
return M

0 commit comments

Comments
 (0)