It's possible indeed!
As @Biggybi mentioned, you have to use foldtext
to achieve that.
You assign a function to foldtext
, and within this function, you can access the v:foldstart
and v:foldend
variables.
From there, you can access the indentation. Here's a simple example:
function! NeatFoldText()
let lines_count = v:foldend - v:foldstart + 1
return repeat(' ', indent(nextnonblank(v:foldstart))) . lines_count . ' lines '
endfunction
set foldtext=NeatFoldText()
If you want to have something a bit more advanced, here's what I have in my vimrc:
function! NeatFoldText()
let foldchar = matchstr(&fillchars, 'fold:\zs.')
let lines_count = v:foldend - v:foldstart + 1
let lines_count_text = printf("┈─ %1s lines ─┈", lines_count) . repeat(foldchar, 10)
let foldtextstart = repeat(' ', indent(nextnonblank(v:foldstart))) . " ••• "
let foldtextend = lines_count_text . repeat(foldchar, 8)
let foldtextlength = strlen(substitute(foldtextstart . foldtextend, '.', 'x', 'g')) + &foldcolumn
return foldtextstart . repeat(foldchar, winwidth(0) - foldtextlength) . foldtextend
endfunction
foldtext
option controls how close folds are displayed, however I don't know if it can respect the indentation.