Skip to content

Commit 87716d0

Browse files
committed
fix(status): clean up bufnr throughout condition and provider
1 parent f2b5bfe commit 87716d0

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

lua/astroui/status/condition.lua

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ local buf_matchers = {
4444
-- @usage local heirline_component = { provider = "Example Provider", condition = function() return require("astroui.status").condition.buffer_matches { buftype = { "terminal" } } end }
4545
function M.buffer_matches(patterns, bufnr, op)
4646
if not op then op = "or" end
47-
if not bufnr then bufnr = 0 end
47+
if not bufnr then bufnr = vim.api.nvim_get_current_buf() end
4848
if vim.api.nvim_buf_is_valid(bufnr) then
4949
for kind, pattern_list in pairs(patterns) do
5050
if buf_matchers[kind](pattern_list, bufnr) then
@@ -83,7 +83,8 @@ function M.is_statusline_showcmd() return vim.fn.has "nvim-0.9" == 1 and vim.opt
8383
-- @usage local heirline_component = { provider = "Example Provider", condition = require("astroui.status").condition.is_git_repo }
8484
function M.is_git_repo(bufnr)
8585
if type(bufnr) == "table" then bufnr = bufnr.bufnr end
86-
return vim.b[bufnr or 0].gitsigns_head or vim.b[bufnr or 0].gitsigns_status_dict
86+
if not bufnr then bufnr = 0 end
87+
return vim.b[bufnr].gitsigns_head or vim.b[bufnr].gitsigns_status_dict
8788
end
8889

8990
--- A condition function if there are any git changes
@@ -92,7 +93,8 @@ end
9293
-- @usage local heirline_component = { provider = "Example Provider", condition = require("astroui.status").condition.git_changed }
9394
function M.git_changed(bufnr)
9495
if type(bufnr) == "table" then bufnr = bufnr.bufnr end
95-
local git_status = vim.b[bufnr or 0].gitsigns_status_dict
96+
if not bufnr then bufnr = 0 end
97+
local git_status = vim.b[bufnr].gitsigns_status_dict
9698
return git_status and (git_status.added or 0) + (git_status.removed or 0) + (git_status.changed or 0) > 0
9799
end
98100

@@ -102,7 +104,8 @@ end
102104
-- @usage local heirline_component = { provider = "Example Provider", condition = require("astroui.status").condition.file_modified }
103105
function M.file_modified(bufnr)
104106
if type(bufnr) == "table" then bufnr = bufnr.bufnr end
105-
return vim.bo[bufnr or 0].modified
107+
if not bufnr then bufnr = 0 end
108+
return vim.bo[bufnr].modified
106109
end
107110

108111
--- A condition function if the current buffer is read only
@@ -111,7 +114,8 @@ end
111114
-- @usage local heirline_component = { provider = "Example Provider", condition = require("astroui.status").condition.file_read_only }
112115
function M.file_read_only(bufnr)
113116
if type(bufnr) == "table" then bufnr = bufnr.bufnr end
114-
local buffer = vim.bo[bufnr or 0]
117+
if not bufnr then bufnr = 0 end
118+
local buffer = vim.bo[bufnr]
115119
return not buffer.modifiable or buffer.readonly
116120
end
117121

@@ -121,12 +125,13 @@ end
121125
-- @usage local heirline_component = { provider = "Example Provider", condition = require("astroui.status").condition.has_diagnostics }
122126
function M.has_diagnostics(bufnr)
123127
if type(bufnr) == "table" then bufnr = bufnr.bufnr end
128+
if not bufnr then bufnr = 0 end
124129
if package.loaded["astrocore"] and require("astrocore").config.features.diagnostics_mode == 0 then return false end
125130
-- TODO: remove when dropping support for neovim 0.9
126131
if vim.diagnostic.count then
127-
return vim.tbl_contains(vim.diagnostic.count(bufnr or 0), function(v) return v > 0 end, { predicate = true })
132+
return vim.tbl_contains(vim.diagnostic.count(bufnr), function(v) return v > 0 end, { predicate = true })
128133
else
129-
return #vim.diagnostic.get(bufnr or 0) > 0
134+
return #vim.diagnostic.get(bufnr) > 0
130135
end
131136
end
132137

@@ -136,7 +141,8 @@ end
136141
-- @usage local heirline_component = { provider = "Example Provider", condition = require("astroui.status").condition.has_filetype }
137142
function M.has_filetype(bufnr)
138143
if type(bufnr) == "table" then bufnr = bufnr.bufnr end
139-
local filetype = vim.bo[bufnr or 0].filetype
144+
if not bufnr then bufnr = 0 end
145+
local filetype = vim.bo[bufnr].filetype
140146
return filetype and filetype ~= ""
141147
end
142148

@@ -146,7 +152,8 @@ end
146152
-- @usage local heirline_component = { provider = "Example Provider", condition = require("astroui.status").condition.is_file }
147153
function M.is_file(bufnr)
148154
if type(bufnr) == "table" then bufnr = bufnr.bufnr end
149-
return vim.bo[bufnr or 0].buftype == ""
155+
if not bufnr then bufnr = 0 end
156+
return vim.bo[bufnr].buftype == ""
150157
end
151158

152159
--- A condition function if a virtual environment is activated
@@ -166,12 +173,13 @@ function M.aerial_available() return package.loaded["aerial"] end
166173
-- @usage local heirline_component = { provider = "Example Provider", condition = require("astroui.status").condition.lsp_attached }
167174
function M.lsp_attached(bufnr)
168175
if type(bufnr) == "table" then bufnr = bufnr.bufnr end
176+
if not bufnr then bufnr = 0 end
169177
return (
170178
-- HACK: Check for lsp utilities loaded first, get_active_clients seems to have a bug if called too early (tokyonight colorscheme seems to be a good way to expose this for some reason)
171179
package.loaded["astrolsp"]
172180
-- TODO: remove get_active_clients when dropping support for Neovim 0.9
173181
---@diagnostic disable-next-line: deprecated
174-
and next((vim.lsp.get_clients or vim.lsp.get_active_clients) { bufnr = bufnr or 0 }) ~= nil
182+
and next((vim.lsp.get_clients or vim.lsp.get_active_clients) { bufnr = bufnr }) ~= nil
175183
)
176184
or (package.loaded["conform"] and next(require("conform").list_formatters(bufnr)) ~= nil)
177185
or (package.loaded["lint"] and next(require("lint")._resolve_linter_by_ft(vim.bo[bufnr].filetype or "")) ~= nil)
@@ -183,7 +191,7 @@ end
183191
-- @usage local heirline_component = { provider = "Example Provider", condition = require("astroui.status").condition.treesitter_available }
184192
function M.treesitter_available(bufnr)
185193
if type(bufnr) == "table" then bufnr = bufnr.bufnr end
186-
if not bufnr then bufnr = vim.api.nvim_get_current_buf() end
194+
if not bufnr then bufnr = 0 end
187195
local ft = vim.bo[bufnr].filetype
188196
local lang = vim.treesitter.language.get_lang(ft)
189197
local parser_avail, _ = pcall(vim.treesitter.get_string_parser, "", lang)

lua/astroui/status/provider.lua

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ function M.numbercolumn(opts)
6666
return function(self)
6767
local lnum, rnum, virtnum = vim.v.lnum, vim.v.relnum, vim.v.virtnum
6868
local num, relnum = vim.opt.number:get(), vim.opt.relativenumber:get()
69-
if not self.bufnr then self.bufnr = vim.api.nvim_get_current_buf() end
70-
local sign = vim.opt.signcolumn:get():find "nu" and resolve_sign(self.bufnr, lnum)
69+
local bufnr = self and self.bufnr or 0
70+
local sign = vim.opt.signcolumn:get():find "nu" and resolve_sign(bufnr, lnum)
7171
local str
7272
if virtnum ~= 0 then
7373
str = "%="
@@ -369,14 +369,14 @@ function M.unique_path(opts)
369369
return parts
370370
end
371371
return function(self)
372-
opts.bufnr = self and self.bufnr or opts.bufnr
373-
local name = opts.buf_name(opts.bufnr)
372+
local bufnr = self and self.bufnr or opts.bufnr
373+
local name = opts.buf_name(bufnr)
374374
local unique_path = ""
375375
-- check for same buffer names under different dirs
376376
local current
377377
for _, value in ipairs(vim.t.bufs or {}) do
378-
if name == opts.buf_name(value) and value ~= opts.bufnr then
379-
if not current then current = path_parts(opts.bufnr) end
378+
if name == opts.buf_name(value) and value ~= bufnr then
379+
if not current then current = path_parts(bufnr) end
380380
local other = path_parts(value)
381381

382382
for i = #current - 1, 1, -1 do
@@ -405,9 +405,7 @@ end
405405
-- @see astroui.status.utils.stylize
406406
function M.file_modified(opts)
407407
opts = extend_tbl({ str = "", icon = { kind = "FileModified" }, show_empty = true }, opts)
408-
return function(self)
409-
return status_utils.stylize(condition.file_modified((self or {}).bufnr) and opts.str or nil, opts)
410-
end
408+
return function(self) return status_utils.stylize(condition.file_modified(self or {}) and opts.str or nil, opts) end
411409
end
412410

413411
--- A provider function for showing if the current file is read-only
@@ -417,9 +415,7 @@ end
417415
-- @see astroui.status.utils.stylize
418416
function M.file_read_only(opts)
419417
opts = extend_tbl({ str = "", icon = { kind = "FileReadOnly" }, show_empty = true }, opts)
420-
return function(self)
421-
return status_utils.stylize(condition.file_read_only((self or {}).bufnr) and opts.str or nil, opts)
422-
end
418+
return function(self) return status_utils.stylize(condition.file_read_only(self or {}) and opts.str or nil, opts) end
423419
end
424420

425421
--- A provider function for showing the current filetype icon
@@ -586,8 +582,7 @@ end
586582
-- @see astroui.status.utils.stylize
587583
function M.treesitter_status(opts)
588584
return function(self)
589-
if not self.bufnr then self.bufnr = vim.api.nvim_get_current_buf() end
590-
return status_utils.stylize(condition.treesitter_available(self.bufnr) and "TS" or "", opts)
585+
return status_utils.stylize(condition.treesitter_available(self and self.bufnr or 0) and "TS" or "", opts)
591586
end
592587
end
593588

0 commit comments

Comments
 (0)