1

I am trying to use expr fold in vim: I create a file in ~/.vim/ftplugin/python/folding.vim and its' content:

setlocal foldmethod=expr
setlocal foldexpr=GetFold(v:lnum)

function! GetFold(lnum)
    if a:lnum > 10
        return 1
    endif
    return 0
endfunctio

this 'GetFold' function is a simple version for test

however when I open a python file, the fold does not work, no folding occurred, while I expect the lines over 10 should fold

the python file content:

import numpy
import numpy as np

def tm(a):
    a += 1
    print(a)

class _A_3(object):
    def __init__(self):
        x = 1
        y = 1
        z = 1
    def update(self):
        x = 2
        y = 2
        z = 2

if __name__ == '__main__':
    arr = np.array([1,2,3],dtype=np.float64)
    print(str(type(arr)))
    tm(0)
    ta = A()

I checked the filetype, :set filetype? it shows filetype=python, so filetype no problem.

I checked the foldmethod, :set foldmethod? it shows foldmethod=expr, so foldmethod no problem.

I checked if the 'GetFold' function exists and works correctly:

:echom GetFold(5) it shows 0, :echom GetFold(12) it shows 1, so 'GetFold' function seems ok.

I use vim's 'foldlevel()' function to check:

:echom foldlevel(5) it shows 0, :echom foldlevel(11) it shows 0, :echom foldlevel(15) it shows 0 also.

So, it seems vim does not set the foldlevel by my function, I can't figure out where is the problem. I checked the :help fold-expr, but I think follows the instruction as it says: enter image description here

my vim version infomation: enter image description here

change 'GetFold' return from 1,0 to '1','0' and line 11's return to '>1' dose not help.

Daniel
  • 51
  • 1
  • What is the output of `:verbose set foldexpr?` when editing a python buffer? – romainl May 11 '22 at 15:05
  • Thanks for your hint, `verbose set foldexpr` outputs `foldexpr=0` and `Last set from ~/.vim/view/~=+test.py= line 38`. I figure out the problem is that I didn't comment out the `au BufWinLeave * silent mkview` and `au BufWinLeave * loadview` in my vimrc. Now the fold-expr works fine. – Daniel May 12 '22 at 13:54
  • Views and sessions are the arch-enemies of Vim tinkerers. – romainl May 12 '22 at 15:17

0 Answers0