billx
billx

Reputation: 185

Change fold level based on syntax in vim

Given the following C++ code:

//
// Some comments at the begin of the code.
// For instance, license, author name, date.
// Referred as "Comment A"
//

namespace one {
  namespace two {

    // Here you have some code.
    // And this comment explain what the code is doing.
    void exampleFunction();

  }
}

and the following settings in my .vimrc

set foldmethod=syntax
autocmd Syntax c,cpp syntax region cCommentLicense start="^\s*//" end="\n\s*\(\S[^/]\|\_$\)"me=s,re=s,he=s fold

These settings enable me to fold paragraph where lines start with //.

My question

When foldlevel is modified (using zr and zm), the first comment (Comment A) is automatically folded. Is it possible to change its fold level (set it to 9 for instance) ?

The fold level of the other comments do not matter for me.

Upvotes: 1

Views: 489

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172748

With syntax-based folding, the fold level is determined by the syntax structure. To get a level-9 fold, you would have to define 9 nested syntax regions.

With :set foldmethod=expr, you have finer control over the fold levels, and can explicitly return 9. As long as you're only folding comments, you can probably write a simple equivalent expression, but if there are other folded constructs, too, that will become complex fast.

Upvotes: 2

Related Questions