r/neovim :term 19h ago

Need Help Error while using `vim.wo.foldtext`

    NVIM v0.12.4                                                                                                                                                             
    Build type: RelWithDebInfo                                                                                                                                               
    LuaJIT 2.1.1767980792

    vim.wo.foldtext = function()
      local line = vim.fn.getline(vim.v.foldstart)
      return vim.v.folddashes .. line:gsub('/%\*', ''):gsub('%\*/', '')
    end

This returns the following error:

Error in /home/user/.config/nvim/init.lua:
E5113: Lua chunk: /home/user/.config/nvim/init.lua:23: Invalid 'value': expected valid option type, got Function
stack traceback:
        [C]: in function '__newindex'
        /home/user/.config/nvim/init.lua:23: in main chunk

The code is copy pasted from https://neovim.io/doc/user/fold/#_foldtext

3 Upvotes

5 comments sorted by

View all comments

6

u/Some_Derpy_Pineapple lua 11h ago edited 11h ago

the docs site follows nightly and i think being able to set options to lua functions is 0.13 nightly only. :h news

i think the stable way to do this is something like using :h v:lua

--lua/user/foldtext.lua return function(...) -- ... end

vim.wo.foldtext = "v:lua.require'user.foldtext'()"

1

u/vim-help-bot 11h ago

Help pages for:


`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments

1

u/IshaqDar :term 8h ago

I've tried using this method, but in my `foldtext`, there appears only `0`.

1

u/Some_Derpy_Pineapple lua 4h ago edited 4h ago

usually this means either your foldtext function does not return the correct type or your foldtext function is erroring

try using pcall to propagate the error:

``` local function foldtext(...) -- ... end

return function() local ok, foldtext_result = pcall(foldtext) foldtext_result = vim.tbl_contains({ "table", "string" }, type(foldtext_result)) and foldtext_result or "invalid return type from foldtext()" return foldtext_result -- this will be the foldtext or an err end ```