[Python] Treesitter-based Python code folding not applied when initially loading file in Neovim

Discussão em 'Python' iniciado por Stack, Julho 27, 2025.

  1. Stack

    Stack Membro Participativo

    Every time a Python file is loaded, no folds are automatically applied based on the fold options that have been set (see below). Instead, I have to manually press zx to force the folds to be applied.

    This problem is limited to Python files and the use of the Treesitter-based expression folding method. Initial and automatic folding works fine when the "indent" method is used, or another file type is loaded (e.g. Lua).

    How can I resolve this issue?

    I have checked that:

    • The Python Treesitter parser has been installed and is functioning correctly (i.e. the output of :checkhealth nvim-treesitter is fine).
    • No Python-specific autocommands have been interfering with the code folding settings (i.e. both :autocmd FileType python and :autocmd BufRead,BufEnter *.py do not show any autocommands).
    • No other Python-specific Neovim plugins have been installed.

    Fold Options

    vim.opt.foldcolumn = "1" -- Show fold indication in column
    vim.opt.foldlevel = 1 -- The default fold level (higher means more folds open by default)
    vim.opt.foldtext = "" -- Do not show fold information (just show existing text on folded line)
    vim.opt.foldmethod = "expr" -- Use custom expression for folding
    vim.opt.foldexpr = "nvim_treesitter#foldexpr()" -- Set expression for folding to treesitter

    Continue reading...

Compartilhe esta Página