views:

145

answers:

2

I'm putting together a syntax for editing Java Manifest files (at github, if anyone's interested). I'm trying to collapse multiple single-line-comments (which I'm matching at the moment with syntax match manifestComment "#.*"). However, if I try to use a syntax region, then the entire file is marked and the entire thing collapses.

What I'm trying to achieve is this:

# A comment
# Another comment
# A third comment
Manifest-Version: 1

and have it collapse down into:

+--  3 lines: # A comment ----
Manifest-Version: 1 

The problem is that there's no distinct 'end' character, and the fold syntax doesn't help; so I can't do syntax region commentBlock start="^#" end="^[^#]". Roughly, the syntax region should start from the first hash character, and then continue down the lines until it finds a line which doesn't begin with a hash.

+1  A: 

How about syntax region commentBlock start="^#" end="^#\@!"?

The \@! is like (?!pattern) in Perl, i.e. it matches with zero-width if the preceding atom -- # in this case -- does not match at the current position.

Andy Stewart
I didn't have much success with this one; or at least, I didn't manage to get it to work. Perhaps I'm doing something wrong? The syntax file is at http://github.com/alblue/vim-manifest/blob/master/syntax/manifest.vim - and I tried replacing line 15 with this one, but even with :set foldmethod=syntax, I couldn't get them to fold as one. It didn't even find any regions.
AlBlue
It turns out you need to add the `fold` argument, i.e.: `syntax region commentBlock start="^#" end="^#\@!" fold`. And, as you pointed out, set the `foldmethod` to `syntax`. There's more info at `:h syn-fold`.Having said all that, I prefer kemp's `foldexpr` solution ;)
Andy Stewart
+2  A: 
:set foldmethod=expr
:set foldexpr=getline(v:lnum)[0]==\"#\"

For information, :h fold-expr.

kemp
Note that this should then go in a filetype plugin (:help ftplugin)
Geoff Reedy
Thanks, I'll give this a go and report back.
AlBlue
This seemed to work. However, if I used 'set' then it affected other files/buffers that I had open. I found that I needed to use 'setlocal'. In addition, even if I put the 'setlocal' in the ftdetect, then it would be invoked for any other script as well.The solution I went with is to use this, but with 'setlocal' in the manifest, as here:http://github.com/alblue/vim-manifest/blob/c40978f6fd119d2f904759be80491893d22870f2/syntax/manifest.vimThanks everyone for the help!
AlBlue