lualine.nvim icon indicating copy to clipboard operation
lualine.nvim copied to clipboard

Bug: unable to set custom location function

Open Crypto-Spartan opened this issue 7 months ago • 0 comments
trafficstars

Self Checks

  • [X] I'm using the latest lualine.
  • [X] I didn't find the issue in existing issues or PRs.

How to reproduce the problem

This is my lualine config, using lazy as my plugin manager:

local function cursor_location()
    local cursor_pos = vim.api.nvim_win_get_cursor(0)
    local line, col = cursor_pos[1], cursor_pos[2]
    local total_lines = vim.api.nvim_buf_line_count(0)
    local line_text = vim.api.nvim_buf_get_lines(0, line-1, line, false)
    local line_len = line_text[1]:len()
    local line_pct = (line / total_lines) * 100
    local col_pct = (col / line_len) * 100
    local location_str = ('Line: %d/%d (%2d%%) | Col: %d/%d (%2d%%)'):format(line, total_lines, line_pct, col, line_len, col_pct)
    return location_str
end

return {
    'nvim-lualine/lualine.nvim',
    dependencies = { 'nvim-tree/nvim-web-devicons' },
    event = 'VeryLazy',
    opts = {
        sections = {
            lualine_z = { cursor_location },
        },
    },
}

Expected behaviour

I expect the Z position of the lualine to have a string such as: Line: 1/10 (10%) | Col: 1/10 (10%)

Actual behaviour

Lualine fails to display anything at all, and I get the following error immediately after launching neovim:

Error executing vim.schedule lua callback: ...e/nvim/lazy/lualine.nvi/lua/lualine/utils/nvim_opts.lua:109 E542: Unbalanced groups
stack traceback:
    [C]: in function 'nvim_win_set_option'
    ...e/nvim/lazy/lualine.nvim/lua/lualine/utils/nvim_opts.lua:109: in function 'setter_fn'
    ...e/nvim/lazy/lualine.nvim/lua/lualine/utils/nvim_opts.lua:51: in function 'set_opt'
    ...e/nvim/lazy/lualine.nvim/lua/lualine/utils/nvim_opts.lua:100: in function 'set'
    ...user/.local/share/nvim/lazy/lualine.nvim/lua/lualine.lua:418: in function 'refresh'
    ...user/.local/share/nvim/lazy/lualine.nvim/lua/lualine.lua:517: in function <...user/.local/share/nvim/lazy/lualine.nvim/lua/lualine.lua:511>

Minimal config to reproduce the issue

to use, launch neovim like this: nvim -u repro.lua

repro.lua:

local root = vim.fn.fnamemodify('./.repro', ':p')

for _, name in ipairs({ 'config', 'data', 'state', 'runtime', 'cache' }) do
    vim.env[('XDG_%s_HOME'):format(name:upper())] = root .. '/' .. name
end

local lazypath = root .. '/plugins/lazy.nvim'
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({
        'git',
        'clone',
        '--filter=blob:none',
        '--single-branch',
        'https://github.com/folke/lazy.nvim.git',
        lazypath
    })
end
vim.opt.runtimepath:prepend(lazypath)

local function cursor_location()
    local cursor_pos = vim.api.nvim_win_get_cursor(0)
    local line, col = cursor_pos[1], cursor_pos[2]
    local total_lines = vim.api.nvim_buf_line_count(0)
    local line_pct = (line / total_lines) * 100
    local line_text = vim.api.nvim_buf_get_lines(0, line-1, line, false)
    local line_len = line_text[1]:len()
    local col_pct = (col / line_len) * 100
    local location_str = ('Line: %d/%d (%2d%%) | Col: %d/%d (%2d%%)'):format(line, total_lines, line_pct, col, line_len, col_pct)
    return location_str
end


local plugins = {
    'folke/tokyonight.nvim',
    {
        'nvim-lualine/lualine.nvim',
        dependencies = { 'nvim-tree/nvim-web-devicons' },
        event = 'VeryLazy',
        opts = {
            sections = {
                lualine_z = { cursor_location },
            },
        },
    },
}

require('lazy').setup(plugins, {
    root = root .. '/plugins',
})

vim.cmd.colorscheme('tokyonight')

Additional Information

I see that lualine is using vim.api.nvim_buf_set_option & vim.api.nvim_win_set_option, both of which have been deprecated since neovim v0.10.

https://github.com/nvim-lualine/lualine.nvim/blob/master/lua/lualine/utils/nvim_opts.lua#L136 https://github.com/nvim-lualine/lualine.nvim/blob/master/lua/lualine/utils/nvim_opts.lua#L148

Crypto-Spartan avatar Apr 01 '25 23:04 Crypto-Spartan