freddiehaddad / feline.nvim

A minimal, stylish and customizable statusline, statuscolumn, and winbar for Neovim
GNU General Public License v3.0
310 stars 10 forks source link

bug: component does not use custom highlight name #68

Closed steschwa closed 9 months ago

steschwa commented 9 months ago

Did you check docs and existing issues?

Neovim version (nvim -v)

0.9.5

Operating system/version

macOS 14.2

Describe the bug

Setting a custom provider highlight name using a function does create a highlight name using it's other properties (e.g. fg / bg) instead of using the predefined name.

The example in USAGE.md doesn't work either. The Name of the hl should be something like StatusComponentVim... (according to https://github.com/freddiehaddad/feline.nvim/blob/main/lua/feline/providers/vi_mode.lua#L56) but rather creates StatusComponent_60A040_1F1F23_bold

TLDR: After some digging around i found function parse_hl https://github.com/freddiehaddad/feline.nvim/blob/main/lua/feline/generator.lua#L120, which just ignores the predefined hl.name. My naive approach was to just return the hl.name or generate a new name if none was passed and that seems to work

Steps To Reproduce

  1. Define component (see Repro for complete example):
    
    local p_vi_mode = require("feline.providers.vi_mode")

local vi_mode_component = { provider = function() return string.format(" %s ", p_vi_mode.get_vim_mode()) end, hl = function() return { name = "FelineStatusbarViMode" .. p_vi_mode.get_mode_highlight_name(), fg = "#ffffff", bg = "#ff0000" } end }


2. Restart Nvim to update config
3. Check output of `:filter /^FelineStatusbarViMode/ highlight` which should be empty
4. Check output of `:filter /^StatusComponent_/ highlight` which should output the auto-generated hl: `StatusComponent_ffffff_ff0000_NONE xxx guifg=#ffffff guibg=#ff0000`

### Expected Behavior

I would expect that Feline does create a hl for me but uses the predefined name `FelineStatusbarViMode` instead of the fallback `StatusComponent_...`

### Repro

```Lua
require("lazy").setup(
    {
        {
            "freddiehaddad/feline.nvim",
            config = function()
                local f = require("feline")

                local p_vi_mode = require("feline.providers.vi_mode")

                local c = {
                    mode = {
                        provider = function()
                            return string.format(" %s ", p_vi_mode.get_vim_mode())
                        end,
                        hl = function()
                            return {
                                name = "FelineStatusbarViMode" .. p_vi_mode.get_mode_highlight_name(),
                                fg = "#ffffff",
                                bg = "#ff0000"
                            }
                        end
                    }
                }

                local components = {
                    active = {
                        {
                            c.mode
                        }
                    },
                    inactive = {}
                }

                f.setup({components = components})
            end
        }
    }
)
freddiehaddad commented 9 months ago

Hello @steschwa, thanks for posting. I think I'm a little confused by the bug report. When using a highlight name (i.e. string), the intention is that the highlight has already been defined and you want to use it for the component.

For example, if you used nvim_set_hl to create the highlight group FelineStatusbarViMode then you would reference it in the component hl config:

local c = {
    mode = {
        provider = function()
            return string.format(" %s ", p_vi_mode.get_vim_mode())
        end,
        hl = 'FelineStatusbarViMode'
    }
}

If you're wanting feline to handle the highlight for the component, specifying a name doesn't really matter since it will be created internally and used for the life of the component.

All you really need to provide is the fg, bg, and style properties for the hl to achieve what you need (at least in the example).

I agree that this part is a little confusing as an example and might possibly be incorrect. Perhaps this was how the highlighting used to work at some point. I will look through the code to try and understand what the objective here is:

-- As a function returning a table hl = function() return { name = require('feline.providers.vi_mode').get_mode_highlight_name(), fg = require('feline.providers.vi_mode').get_mode_color(), style = 'bold' } end

Is something not not working? From what I see in your example, all you need for the component is:

local c = {
    mode = {
        provider = function()
            return string.format(" %s ", p_vi_mode.get_vim_mode())
        end,
        hl = {
            fg = "#ffffff",
            bg = "#ff0000",
        },
    }
}
steschwa commented 9 months ago

Oh yeah i think i just got confused by the example. My understanding was that "if i specify a name, feline uses that name to generate a hl group (using fg/bg and style)".

Using a name key when hl is a table doesn't make much sense then i guess.

Thanks for clarification