Skip to content

Commit 2b0beab

Browse files
committed
refactor(nvim): make statusline buffer-aware for floating window support
1 parent 998ade9 commit 2b0beab

2 files changed

Lines changed: 59 additions & 18 deletions

File tree

lua/lib/fn.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,19 @@ M.diagnostic_counts = function(bufnr)
6666
return results
6767
end
6868

69-
--- Get a buffer-local variable, falling back to the alternate buffer if not found in the current one.
69+
--- Get a buffer-local variable. Falls back to the alternate buffer when no specific buffer is given.
7070
---@param name string
71+
---@param bufnr? number
7172
---@return any
72-
M.get_buf_var = function(name)
73+
M.get_buf_var = function(name, bufnr)
74+
if bufnr then
75+
local ok, val = pcall(vim.api.nvim_buf_get_var, bufnr, name)
76+
if ok and val and val ~= "" then
77+
return val
78+
end
79+
return nil
80+
end
81+
7382
local val = vim.b[name]
7483
if val == nil or val == "" then
7584
local alt_buf = vim.fn.bufnr("#")

plugin/config/statusline.lua

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ local cached_git_root = vim.fs.root(0, ".git")
1313
vim.api.nvim_create_autocmd({ "BufEnter", "DirChanged" }, {
1414
group = statusline_augroup,
1515
callback = function()
16-
cached_git_root = vim.fs.root(0, ".git")
16+
if vim.bo.buftype == "" then
17+
cached_git_root = vim.fs.root(0, ".git")
18+
end
1719
end,
1820
})
1921

@@ -150,11 +152,12 @@ local function c_project()
150152
end
151153

152154
-- Show the current filename
153-
local function c_filename()
155+
local function c_filename(bufnr)
156+
bufnr = bufnr or 0
154157
local hl = "%#StatusLineFilename#"
155158
local line = sep() .. " " .. hl
156-
local ft = vim.bo.filetype
157-
local bt = vim.bo.buftype
159+
local ft = vim.bo[bufnr].filetype
160+
local bt = vim.bo[bufnr].buftype
158161

159162
if bt == "terminal" then
160163
return ""
@@ -169,11 +172,11 @@ local function c_filename()
169172
elseif ft == "" or ft == "messages" then
170173
return ""
171174
else
172-
local path = vim.fn.expand("%:p:~")
175+
local path = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(bufnr), ":~")
173176
if ft == "oil" then
174177
line = line .. vim.fn.fnamemodify(path:sub(7), ":.")
175178
else
176-
local fullpath = vim.api.nvim_buf_get_name(0)
179+
local fullpath = vim.api.nvim_buf_get_name(bufnr)
177180
local filename = vim.fn.fnamemodify(fullpath, ":t")
178181
local relpath = vim.fn.fnamemodify(fullpath, ":.:h")
179182
local icon, icon_color = devicons.get_icon(filename, nil, { default = true })
@@ -211,8 +214,9 @@ local function c_bookmark()
211214
end
212215

213216
-- Show LSP diagnostics
214-
local function c_lsp_diagnostics()
215-
local counts = T.fn.diagnostic_counts(0)
217+
local function c_lsp_diagnostics(bufnr)
218+
bufnr = bufnr or 0
219+
local counts = T.fn.diagnostic_counts(bufnr)
216220

217221
if #counts == 0 then
218222
return ""
@@ -228,8 +232,8 @@ local function c_lsp_diagnostics()
228232
end
229233

230234
-- Show git status
231-
local function c_git_status()
232-
local status = T.fn.get_buf_var("gitsigns_status")
235+
local function c_git_status(bufnr)
236+
local status = T.fn.get_buf_var("gitsigns_status", bufnr)
233237
if not status or status == "" then
234238
return ""
235239
end
@@ -238,8 +242,8 @@ local function c_git_status()
238242
end
239243

240244
-- Show the current git branch
241-
local function c_git_branch()
242-
local head = T.fn.get_buf_var("gitsigns_head")
245+
local function c_git_branch(bufnr)
246+
local head = T.fn.get_buf_var("gitsigns_head", bufnr)
243247
if not head or head == "" then
244248
return ""
245249
end
@@ -278,22 +282,50 @@ local function c_tabs()
278282
return table.concat(tabs, "")
279283
end
280284

285+
-- When the active window is a floating/non-editing window (e.g. Sidekick),
286+
-- find a visible window with a regular file buffer to show status for.
287+
local function find_editing_bufnr(winid)
288+
local bufnr = vim.api.nvim_win_get_buf(winid)
289+
local buftype = vim.bo[bufnr].buftype
290+
local win_config = vim.api.nvim_win_get_config(winid)
291+
292+
if win_config.relative == "" and buftype == "" then
293+
return bufnr
294+
end
295+
296+
for _, w in ipairs(vim.api.nvim_list_wins()) do
297+
if w ~= winid then
298+
local cfg = vim.api.nvim_win_get_config(w)
299+
if cfg.relative == "" then
300+
local b = vim.api.nvim_win_get_buf(w)
301+
if vim.bo[b].buftype == "" then
302+
return b
303+
end
304+
end
305+
end
306+
end
307+
308+
return bufnr
309+
end
310+
281311
-- Construct the statusline (default)
282312
function _G.statusline()
313+
local winid = vim.g.statusline_winid or vim.api.nvim_get_current_win()
314+
local bufnr = find_editing_bufnr(winid)
283315
local hl = "%#StatusLine#"
284316

285317
local components = {
286318
hl,
287319
c_mode(),
288320
c_spinner(),
289321
c_project(),
290-
c_filename(),
322+
c_filename(bufnr),
291323
c_bookmark(),
292324
"%=",
293325
"%=",
294-
c_lsp_diagnostics(),
295-
c_git_status(),
296-
c_git_branch(),
326+
c_lsp_diagnostics(bufnr),
327+
c_git_status(bufnr),
328+
c_git_branch(bufnr),
297329
c_cursor_position(),
298330
c_tabs(),
299331
}

0 commit comments

Comments
 (0)