@@ -13,7 +13,9 @@ local cached_git_root = vim.fs.root(0, ".git")
1313vim .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()
150152end
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()
211214end
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()
228232end
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()
238242end
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 , " " )
279283end
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)
282312function _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