tmhedberg / SimpylFold

No-BS Python code folding for Vim
BSD 3-Clause "New" or "Revised" License
653 stars 56 forks source link

SimplyFold has issues recalculating the fold when adding lines to the end of a fold #116

Open ghost opened 4 years ago

ghost commented 4 years ago

Steps to reproduce:

+-- 13 lines: class A: Class Docstring-------------------------------------------- print('another line')

tmhedberg commented 4 years ago

I'm not able to reproduce this by following your directions. The additional line is correctly included in the fold for me. Does it still happen if you disable all other plugins?

On Tue, Jan 21, 2020 at 10:50 PM nabeel-ms notifications@github.com wrote:

Steps to reproduce:

  • Create python file as follows

class A: """Class Docstring """ def init(self): """init docstring """ pass

def myprint(self):
    """Testing function docstring        """
    print('Printed myprint')
    print('Printed myprint a second time')
  • Then, save it as test.py
  • Open the file again
  • Go to the last line
  • Add another line at the end of myprint

class A: """Class Docstring """ def init(self): """init docstring """ pass

def myprint(self):
    """Testing function docstring        """
    print('Printed myprint')
    print('Printed myprint a second time')
    print('another line')
  • Now, the last line is excluded from the fold
  • As an example, if you press zM, you get:

+-- 13 lines: class A: Class Docstring-------------------------------------------- print('another line')

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/tmhedberg/SimpylFold/issues/116?email_source=notifications&email_token=AACHCGLUO5JB6ADX32OJCUTQ67UCTA5CNFSM4KKAWDC2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IH3LX2A, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACHCGPQ4CT44JCRJXGG5DDQ67UCTANCNFSM4KKAWDCQ .

obrusvit commented 4 years ago

I can confirm this is occurring in my Vim setup exactly as nabeel-ms describes it. However, If I close the file and then I open it again, the folds are correct again.

tmhedberg commented 4 years ago

Please paste the output of :set foldmethod? foldexpr? foldlevel? foldminlines? foldnestmax?.

What is the state of the existing folds in the file at the time that you add the additional line at the end? Are they open or closed?

I'm still not seeing this behavior, so I'm trying to figure out what's different for me.

It would still be helpful if you could try this with all plugins except for this one disabled. A lot of weird problems can be caused by the interaction between multiple plugins that weren't designed to work together, since Vim doesn't really isolate them.

ghost commented 4 years ago

The output is as follows:

foldmethod=expr foldexpr=SimpylFold#FoldExpr(v:lnum) foldlevel=3 foldminlines=1 foldnestmax=20

I have uninstalled all my plugins excluding YouCompleteMe. In this case, to avoid issues, I used zR to reopen all folds before appending. I still encountered the same issues. However, it should be noted that when I use zx to recompute the folds in the file, the file folding is fixed and has no issues.

vecin2 commented 4 years ago

I've encountered the same issue. Thanks for the zx workaround! This is my output of running the above:

  foldexpr=SimpylFold#FoldExpr(v:lnum)
  foldlevel=0
  foldminlines=1
  foldnestmax=20

I removed all my config and left only this plugin then I was able to replicate with a file like the following:

def some_fuction(var):
    var = 1 + 2

Then I added and an extra line:

def some_fuction(var):
    var = 1 + 2
    var2 =2 

Then press <Esc> to go back to normal mode and then zm and this is what I got:

+--  2 lines: def some_fuction(var):------------------------
    var2 = 2
yongrenjie commented 4 years ago

Hi all - I suspect this has something to do with the caching of the folds. I have a similar issue whereby if I add lines before a fold, all subsequent folds get messed up badly upon saving. This technically sounds closer to #112 but since the video there is no longer accessible I'm not sure if the behaviour is the same.

With git bisect I find that the behaviour was introduced in commit 2dfeb35.

zx alone does not work for me, unfortunately. I find that I need to regenerate the cache, so the following autocmd (which I placed in ~/.vim/after/ftplugin/python.vim) solves my issue:

function UpdateFolds()
    call SimpylFold#Recache()
    FastFoldUpdate!
endfunction
autocmd BufWritePre <buffer> call UpdateFolds()

There is probably a tiny bit of lag especially with large files, but since it's only triggered upon saving (which takes a while anyway), it's quite difficult to notice. On a 1200-line file it takes me 100–200 ms to recalculate the folds and write the buffer. One could probably use normal zx in place of FastFoldUpdate! if FastFold isn't installed.