rebelot / heirline.nvim

Heirline.nvim is a no-nonsense Neovim Statusline plugin designed around recursive inheritance to be exceptionally fast and versatile.
MIT License
1.02k stars 40 forks source link

Conditional component is not evaluated #168

Open Subjective opened 1 year ago

Subjective commented 1 year ago

This is a somewhat trivial issue, I was just curious as to why the two seemingly equivalent components below don't behave the same.

They both display the grapple tag number of a file if it exists, but only the first one behaves as I would expect (see below demo for more info). The only difference between the two is that in the 1st one the condition require("grapple").exists() is checked directly inside the provider function, while in the 2nd one the condition is provided as a table entry. I'm not sure why this would cause them to behave differently.

-- 1
Grapple = {
  provider = function()
    -- condition check done inside provider
    if require("grapple").exists() then return string.format(" %s ", require("grapple").key()) end
  end,
  update = { "User", pattern = "GrappleStateUpdate", callback = vim.schedule_wrap(function() vim.cmd.redrawstatus() end) },
  init = require("astronvim.utils.status.init").update_events { "BufEnter" },
}
-- 2
Grapple = {
  provider = function() return string.format(" %s ", require("grapple").key()) end,
  -- condition provided to table
  condition = require("grapple").exists,
  update = { "User", pattern = "GrappleStateUpdate", callback = vim.schedule_wrap(function() vim.cmd.redrawstatus() end) },
  init = require("astronvim.utils.status.init").update_events { "BufEnter" },
}

Demo Below is a simple test for the 2nd component, which for some reason doesn't display the Grapple tag initially when toggled even though the condition is met and the update autocmd event is emitted. After some time (usually when I toggle the tag while other components in the statusline are updating), the component finally shows and updates as I would expect it to do when toggling a tag. (btw, this happens when I first start neovim, after the component finally shows, the issue disappears completely)

Test Autocmd ```lua vim.api.nvim_create_autocmd({ "User" }, { pattern = "GrappleStateUpdate", callback = function() local exists = require("grapple").exists() local key = require("grapple").key() print(string.format("exists: %s, tag #: %s", exists, key)) end, }) ```

Screen Recording 2023-09-11 at 7 54 42 PM