famiu / feline.nvim

A minimal, stylish and customizable statusline for Neovim written in Lua
GNU General Public License v3.0
1.04k stars 55 forks source link

[Bug] Feline complains about `vi_mode_colors` being a `nil` value, even if defined #99

Closed CodePurble closed 3 years ago

CodePurble commented 3 years ago

Neovim version

NVIM v0.5.0
Build type: Release
LuaJIT 2.0.5
Compilation: /usr/bin/cc -D_FORTIFY_SOURCE=2 -march=x86-64 -mtune=generic -O2 -pipe -fno-plt -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -DNVIM_TS_HAS_SET_MATCH_LIMIT -O2 -DNDEBUG -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wmissing-prototypes -Wimplicit-fallthrough -Wvla -fstack-protector-strong -fno-common -fdiagnostics-color=always -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -DMIN_LOG_LEVEL=3 -I/build/neovim/src/neovim-0.5.0/build/config -I/build/neovim/src/neovim-0.5.0/src -I/usr/include -I/build/neovim/src/neovim-0.5.0/build/src/nvim/auto -I/build/neovim/src/neovim-0.5.0/build/include
Compiled by builduser

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"

Run :checkhealth for more info

Feline version v0.1.1

Describe the bug I keep getting this error message every time I open neovim, and feline fails to load.

Error:

E5113: Error while calling lua chunk: ...acker/start/feline.nvim/lua/feline/providers/vi_mode.lua:45: attempt to index field 'vi_mode_colors' (a nil value)

To Reproduce Open neovim with a custom feline config

Expected behavior feline should load correctly

Does this error occur in the minimal init file? Yes, it occurs

Provide modified minimal_init.lua

-- Minimal init file to run Feline with the most basic functionality
-- Run from Feline top-level directory using:
-- nvim --noplugin -u minimal_init.lua

local function load_plugins()
    local packer = require('packer')
    local use = packer.use

    packer.reset()
    packer.init {
        package_root = '/tmp/nvim/site/pack',
        git = {
            clone_timeout = -1
        }
    }

    use 'wbthomason/packer.nvim'
    use {
        'famiu/feline.nvim',
        requires = {
            {
                'lewis6991/gitsigns.nvim',
                requires = { 'nvim-lua/plenary.nvim' },
                config = function()
                    require('gitsigns').setup()
                end
            },
            'kyazdani42/nvim-web-devicons'
        }
    }

    packer.sync()
end

_G.load_config = function()
    vim.opt.termguicolors = true

    -- Replace this part of the config with whatever Feline configuration you're using
    local lsp = require("feline.providers.lsp")
    local vi_mode_utils = require("feline.providers.vi_mode")
    local utils = require("utils")
    local onedark_colours = utils.all_colours.onedark_colours
    local nord_colours = utils.all_colours.nord_colours

    --
    -- COLOURS
    --
    local vi_mode_colors = {
        ['NORMAL'] = onedark_colours.green,
        ['OP'] = onedark_colours.green,
        ['INSERT'] = onedark_colours.red,
        ['VISUAL'] = onedark_colours.skyblue,
        ['BLOCK'] = onedark_colours.skyblue,
        ['REPLACE'] = onedark_colours.violet,
        ['V-REPLACE'] = onedark_colours.violet,
        ['ENTER'] = onedark_colours.cyan,
        ['MORE'] = onedark_colours.cyan,
        ['SELECT'] = onedark_colours.orange,
        ['COMMAND'] = onedark_colours.green,
        ['SHELL'] = onedark_colours.green,
        ['TERM'] = onedark_colours.green,
        ['NONE'] = onedark_colours.yellow
    }

    local components = {
        active = {},
        inactive = {}
    }

    -- left -> active[1]
    -- mid -> active[2]
    -- right -> active[3]
    table.insert(components.active, {})
    table.insert(components.active, {})
    table.insert(components.active, {})

    -- left -> active[1]
    -- mid -> active[2]
    -- right -> active[3]
    table.insert(components.inactive, {})
    table.insert(components.inactive, {})
    table.insert(components.inactive, {})

    --
    -- LEFT COMPONENTS
    --
    table.insert(components.active[1], {
        provider = "git_branch",
        icon = "  ",
        hl = {
            fg = "black",
            bg = vi_mode_utils.get_mode_color(),
            style = "bold"
        },
        right_sep = {
            str = "right_filled",
            hl = {
                bg = nord_colours.nord3,
                fg = vi_mode_utils.get_mode_color(),
                style = "bold"
            }
        }
    })

    table.insert(components.active[1], {
        provider = "git_diff_added",
        hl = {
            fg = "green",
            bg = nord_colours.nord3
        }
    })

    table.insert(components.active[1], {
        provider = "git_diff_changed",
        hl = {
            fg = "orange",
            bg = nord_colours.nord3
        }
    })

    table.insert(components.active[1], {
        provider = "git_diff_removed",
        hl = {
            fg = "red",
            bg = nord_colours.nord3
        },
        right_sep = {
            str = "right_filled",
            hl = {
                fg = nord_colours.nord3
            }
        }
    })

    table.insert(components.inactive[1], {
        provider = " ",
        icon = "",
    })

    --
    -- MIDDLE COMPONENTS
    --
    table.insert(components.active[2], {
        provider = "file_info",
        icon = "",
        type = "relative",
        left_sep = "left"
    })

    table.insert(components.active[2], {
        provider = "@"
    })

    table.insert(components.active[2], {
        provider = "position",
        left_sep = " ",
        right_sep = "right",
    })

    table.insert(components.inactive[2], {
        provider = "file_info",
        icon = "",
        type = "relative",
        left_sep = "left",
        right_sep = "right"
    })

    --
    -- RIGHT COMPONENTS
    --
    table.insert(components.active[3], {
        provider = "diagnostic_errors",
        enabled = function() return lsp.diagnostics_exist("Error") end,
        icon = "✖ ",
        hl = { fg = "#FF0000" },
        right_sep = " "
    })

    table.insert(components.active[3], {
        provider = "diagnostic_warnings",
        enabled = function() return lsp.diagnostics_exist("Warning") end,
        icon = " ",
        hl = { fg = "#F0F722" },
        right_sep = " "
    })

    table.insert(components.active[3], {
        provider = "diagnostic_info",
        enabled = function() return lsp.diagnostics_exist("Information") end,
        icon = " ",
        hl = { fg = "#1176DB" },
        right_sep = " "
    })

    table.insert(components.active[3], {
        provider = "diagnostic_hints",
        enabled = function() return lsp.diagnostics_exist("Hint") end,
        icon = "ℍ ",
        hl = { fg = "#C678DD" },
        right_sep = " "
    })

    table.insert(components.active[3], {
        provider = "file_encoding",
        left_sep = {"left", " "},
        right_sep = " ",
    })

    table.insert(components.active[3], {
        provider = vim.bo.fileformat:upper(),
        left_sep = {"left", " "},
        right_sep = " ",
    })

    table.insert(components.active[3], {
        provider = utils.get_buf_indentation_style,
        left_sep = {"left", " "},
        right_sep = " "
    })

    table.insert(components.active[3], {
        provider = utils.get_asyncrun_running,
        hl = {fg = nord_colours.nord4, bg = nord_colours.nord3, style = "bold"},
        left_sep = {str = "left_filled", hl = {fg = nord_colours.nord3}}
    })

    table.insert(components.active[3], {
        provider = utils.get_python_venv,
        hl = {
            fg = nord_colours.nord3,
            bg = nord_colours.nord9,
            style = "bold"
        },
        left_sep = {
            {str = "left_filled", hl = {fg = nord_colours.nord9, bg = nord_colours.nord3}},
        },
    })

    local colours = onedark_colours
    colours.bg = nord_colours.nord1
    colours.fg = "#D0D0D0"
    require("feline").setup({
        components = components,
        colors = colours,
        force_inactive = {
            filetypes = {
                "NvimTree",
                "packer",
                "startify",
                "fugitive",
                "fugitiveblame",
                "dashboard"
            },
            buftypes = {
                "terminal"
            },
            bufnames = {}
        },
        vi_mode_colors = vi_mode_colors
    })
end

local install_path = '/tmp/nvim/site/pack/packer/start/packer.nvim'

vim.opt.packpath = {'/tmp/nvim/site'}

if vim.fn.isdirectory(install_path) == 0 then
    vim.fn.system({'git', 'clone', 'https://github.com/wbthomason/packer.nvim', install_path})
end

load_plugins()
vim.api.nvim_command('autocmd User PackerComplete ++once lua load_config()')

Screenshots image

Additional context The configuration used to work fine until I moved to v0.1.1

famiu commented 3 years ago

That's not a bug. You defined the hl wrong. The hl for the vi_mode component is supposed to be a function that returns those values. Like this:

hl = function()
    return {
        fg = "black",
        bg = vi_mode_utils.get_mode_color(),
        style = "bold"
    }
end

The issue with what you did is that the vi_mode_utils.get_mode_color() gets called once when the configuration is being evaluated, not while Feline is parsing it

ram02z commented 3 years ago

You are also missing LINES from your vi_mode_colors table. If you omit it, you will get an error when you use Visual line mode. See: https://github.com/famiu/feline.nvim/blob/5d152e2cc28c172b42f4caba8baf0973f6a6ece6/lua/feline/defaults.lua#L43-L59

CodePurble commented 3 years ago

That's not a bug. You defined the hl wrong. The hl for the vi_mode component is supposed to be a function that returns those values. Like this:

hl = function()
    return {
        fg = "black",
        bg = vi_mode_utils.get_mode_color(),
        style = "bold"
    }
end

The issue with what you did is that the vi_mode_utils.get_mode_color() gets called once when the configuration is being evaluated, not while Feline is parsing it

Thanks for pointing that out. That has fixed it. I'll remember that.