0

Ref to :h foldtext() The number of leading dashes depends on the foldlevel. As follow, I think the foldlevel of printf is 1.

enter image description here

But, you can see as follow, the number of dashes is 2.

enter image description here

I can't understand why inconsist

2 Answers 2

2

The documentation does not say how many dashes are there per foldlevel. As can be seen in the source:

txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
r = alloc((unsigned)(STRLEN(txt)
        + STRLEN(dashes)        /* for %s */
        + 20            /* for %3ld */
        + STRLEN(s)));      /* concatenated */
if (r != NULL)
{
    sprintf((char *)r, txt, dashes, count);
    len = (int)STRLEN(r);
    STRCAT(r, s);
    /* remove 'foldmarker' and 'commentstring' */
    foldtext_cleanup(r + len);
    rettv->vval.v_string = r;
}

So you can see, the number of dashes is actually equal to the foldlevel + 1

2
  • But why foldlevel+1? I feel this is not convenient by default. Commented Nov 1, 2018 at 7:57
  • Why? That is a question I cannot answer. It's what it is Commented Nov 1, 2018 at 7:59
1

As @Christian Brabandt showed from the source code, the string used for folding has a "+-" at the beginning of the string, resulting in the foldlevel+1 dashes. I can't answer whether this is intentional or a bug. You could open an issue on the vim github if you feel that this is a bug, and you will hopefully receive either justification for the extra dash or the "bug" will be fixed.

If you would like a "fix" for this, you can add the following to your vimrc:

function MyFoldText()
    let line = getline(v:foldstart)
    let sub = substitute(line, '^\s*\|//\s*\|/\*\|\*/\|{{{\d\=', '', 'g')
    let oneLessDash = strpart(string(v:folddashes),1,len(string(v:folddashes))-2)
    let foldlinecount = foldclosedend(v:foldstart) - foldclosed(v:foldstart) + 1
    let lineCount = " " . string(foldlinecount) . " lines: "
    return "+" . oneLessDash . lineCount . sub
endfunction
set foldtext=MyFoldText()

This will print the same foldtext as before with just one less dash.

P.S. Welcome to the community!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.