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.05k stars 40 forks source link

Document updating component upon user events #179

Open gbroques opened 11 months ago

gbroques commented 11 months ago

As a beginner new to Neovim autocommands, configuring heirline.nvim with a plugin that requires updatin a component upon a user event was a challenge.

For example, I created an LspMessages component with heirline.nvim that relies on linrongbin16/lsp-progress.nvim for the provider and needs to be updated when the LspProgressStatusUpdated user event is emitted:

local LspMessages = {
  provider = require('lsp-progress').progress,
  update = {
    'User',
    pattern = 'LspProgressStatusUpdated',
    callback = vim.schedule_wrap(function()
      vim.cmd('redrawstatus')
    end),
  }
}

The only example of update with a pattern in the cookbook is a builtin event for refreshing a cmoponent when the mode changes:

update = {
    "ModeChanged",
    pattern = "*:*",
    callback = vim.schedule_wrap(function()
        vim.cmd("redrawstatus")
    end),
},

The mistake I made was extrapolating the builtin event example for the user event:

local LspMessages = {
  provider = require('lsp-progress').progress,
  update = {
    'LspProgressStatusUpdated', -- ❌ Needs to be 'User'
    pattern = '*:*', -- ❌ Needs to be 'LspProgressStatusUpdated'
    callback = vim.schedule_wrap(function()
      vim.cmd('redrawstatus')
    end),
  }
}

The error you get with the above is "Invalid 'event': 'LspProgressStatusUpdated'".

I'm sure those more familiar with autocommands in Neovim wouldn't make this mistake, but for beginners an example for user events might be helpful!


Thank you again. heirline.nvim is very cool for building a customized statusline! :sunglasses:

linrongbin16 commented 11 months ago

hi @gbroques, also please pay attention that default 'lsp-progress' will handle the escaping of '%' char, e.g. double the '%'.

see: https://github.com/linrongbin16/lsp-progress.nvim/blob/cabf7fde50cc0dad367a03a1542d9670d1118bd0/lua/lsp-progress/defaults.lua#L83

and: https://github.com/linrongbin16/lsp-progress.nvim/blob/cabf7fde50cc0dad367a03a1542d9670d1118bd0/lua/lsp-progress/defaults.lua#L88

these two lines will transfer '%' to '%%'. also see related discuss why I am doing it: https://github.com/linrongbin16/lsp-progress.nvim/issues/98

heirline could have different behavior from lualine.

also it's super welcome if you could submit PR to give people a sample, share how you integrate lsp-progress with heirline.

gbroques commented 11 months ago

hi @gbroques, also please pay attention that default 'lsp-progress' will handle the escaping of '%' char, e.g. double the '%'.

see: https://github.com/linrongbin16/lsp-progress.nvim/blob/cabf7fde50cc0dad367a03a1542d9670d1118bd0/lua/lsp-progress/defaults.lua#L83

and: https://github.com/linrongbin16/lsp-progress.nvim/blob/cabf7fde50cc0dad367a03a1542d9670d1118bd0/lua/lsp-progress/defaults.lua#L88

these two lines will transfer '%' to '%%'. also see related discuss why I am doing it: linrongbin16/lsp-progress.nvim#98

heirline could have different behavior from lualine.

Thank you for note about the % escaping behavior!

also it's super welcome if you could submit PR to give people a sample, share how you integrate lsp-progress with heirline.

Sure! I'll consider submitting a PR for that then. Thanks again! :)