vim

For discussion on all things (neo)vim.
berkeleytrue 2020-10-17T04:11:53.074Z

Is there a way to get fold data? I'm trying to figure out how folds effect the line numbers of the buffer. I've found some work arounds that do a lot to get this information (saving state, unfolding everything, check if foldables sections line by line, then returning to prev state), but it seems like there should just be a function that I can call similar to winsaveview that will say something like "lines 3-5 are folded". Does this exist? (searching help and google hasn't turned up anything.)

nbardiuk 2020-10-17T12:28:25.074500Z

I guess the simplest way is to iterate over lines and find which are folded

nbardiuk 2020-10-17T12:28:43.074700Z

function! Folds() abort
    let folds = []
    let i = 1
    while i <= line('$')
      let foldends = foldclosedend(i)
      if foldends != -1
        call insert(folds, [i, foldends])
        let i = foldends
      endif
      let i = i+1
    endwhile
    return folds
endfunction

nbardiuk 2020-10-17T12:29:11.074900Z

:echo json_encode(Folds())

berkeleytrue 2020-10-18T15:53:46.075200Z

@nbardiuk Thanks, I think this is just what I was looking for!

berkeleytrue 2020-10-17T04:14:14.074300Z

for ref https://www.vim.org/scripts/script.php?script_id=732