Reputation: 3249
I have (something equivalent to) the following in a syntax file:
syn sync clear
syn sync fromstart
syn region fooBlock start="\<interface\>" end="\ze\<implementation\>" fold
syn region fooBlock start="\<implementation\>" end="\%$" fold
Syntax highlighting works just fine (as one can see by adding e.g. matchgroup=Keyword
). Folding also basically works, but when I insert a new fold (by typing e.g. implementation
, vim starts the fold one line later than it should. Example:
- interface
| l1
| l2
implementation
- l3
| l4
| l5
When typing this, obviously I would expect the second fold to start one line earlier. Typing zx
corrects that, as does saving and reopening the file or typing :syn sync fromstart
.
What can I do to have vim correctly insert the folds as I type?
I already looked in :help fold-syntax
, but as shown, :syn sync fromstart
in the syntax file does not work.
Maybe there is a better way than using \ze
(or the me=s-1
suffix, which also yields the same result) to highlight/fold multipart regions.
Upvotes: 1
Views: 275
Reputation: 172510
You're trying to express the fact that the interface
section ends one line before an implementation
line. Syntax regions (and apparently especially folding) work best when there's a definite region start and region end marker (like begin
and end
).
I've tried to make your example work by adding \n
to the pattern, but that also didn't work. I guess to get reliable folding, you need to switch from syntax-folding to a 'foldexpr'
, like this:
:setlocal foldmethod=expr foldexpr=getline(v:lnum)=='interface'\|\|getline(v:lnum)=='implementation'?'>1':'='
Upvotes: 1