Completion of the syntax area at the beginning of the specified pattern

This is a continuation:

VIM: Easy Steps to Create a Syntax Highlight File - For Log Files

I am trying to use the "region-match" tool to syntactically allocate stacks in some log files: these log files (based on log4j) look something like this:

YYYY-MM-DD HH:MM:ss,SSSS...INFO...Message YYYY-MM-DD HH:MM:ss,SSSS...INFO...Message YYYY-MM-DD HH:MM:ss,SSSS...ERROR...Message ...stack trace... ...stack trace... ...blah blah, more server-vomit... ... YYYY-MM-DD HH:MM:ss,SSSS...INFO...Message 

So far, I have been able to almost do what I want:

 :syntax region error matchgroup=string start=/^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2},\d\{3}.* ERROR/ end=/^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2},\d\{3}/ 

But the problem is that the match goes too far - it includes the next entry (i.e., the match includes the next YYYY-MM-DD ....).

Do I believe from this example (discussed in the cited text) in the VIM manual so that I can highlight the intermediate result? (But it looks like I can't match the syntax for my example)

http://vimdoc.sourceforge.net/htmldoc/syntax.html#:syn-excludenl

So, to be clear: I need to match the first line of YYYY-MM-DD ... (including "ERROR"), and then all subsequent lines to NOT, including the next line of YYYY-MM-DD.

+4
source share
4 answers

There are many difficulties with overlapping areas in the Vim syntax highlighting simulator. The order in which matches and regions are defined matters, and it can be very difficult to get it to do what you want.

The main thing I suggest is to look :help syn-pattern-offset . This makes it possible to make a region at the beginning of the template, by the way. For example, if your end pattern is:

 end=/pattern/re=s-1 

Then the area will end with a character before p pattern.

It takes a lot of effort to get it working, and I'm far from being an expert in this, but to get you started, try the following:

 syntax match logDate /^\d\{4}-\d\{2}-\d\{2}/ containedin=logDateTimeTypeLine nextgroup=logTime skipwhite syntax match logTime /\d\{2}:\d\{2}:\d\{2},\d\{3}/ containedin=logDateTimeTypeLine,logTrace syntax match logDateTimeTypeLine /^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2},\d\{3}.*/ syntax region logTrace matchgroup=logErrorStartLine start=/^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2},\d\{3}.*ERROR.*/ms=s,rs=e+1 end=/^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2},\d\{3}/me=s-1,he=s-1,re=s-1 hi link logTrace Error hi link logDateTimeTypeLine Keyword hi link logDate String hi link logTime Comment hi logErrorStartLine guifg=red 
+5
source
 :help keepend :syntax region error matchgroup=string start=/.../ end=/.../ keepend 
+2
source

My file is an adaptation :-) here it is:

 syntax region fatal start=/^\d\{4}-\d\{2}-\d\{2}.*FATAL.*/ end=/\n\d\{4}-\d\{2}-\d\{2}/me=s-1,re=s-1 syntax region error start=/^\d\{4}-\d\{2}-\d\{2}.*ERROR.*/ end=/\n\d\{4}-\d\{2}-\d\{2}/me=s-1,re=s-1 syntax region warn start=/^\d\{4}-\d\{2}-\d\{2}.*WARN.*/ end=/\n\d\{4}-\d\{2}-\d\{2}/me=s-1,re=s-1 syntax region info start=/^\d\{4}-\d\{2}-\d\{2}.*INFO.*/ end=/\n\d\{4}-\d\{2}-\d\{2}/me=s-1,re=s-1 syntax region debug start=/^\d\{4}-\d\{2}-\d\{2}.*DEBUG.*/ end=/\n\d\{4}-\d\{2}-\d\{2}/me=s-1,re=s-1 syntax region trace start=/^\d\{4}-\d\{2}-\d\{2}.*TRACE.*/ end=/\n\d\{4}-\d\{2}-\d\{2}/me=s-1,re=s-1 " Highlight colors for log levels. hi fatal ctermfg=DarkRed ctermbg=Black hi error ctermfg=Red ctermbg=Black hi warn ctermfg=Magenta ctermbg=Black hi info ctermfg=Green ctermbg=Black hi debug ctermfg=LightCyan ctermbg=Black hi trace ctermfg=LightMagenta ctermbg=Black 
+1
source

Tell Vim to use a (zero length) positive lookbehind using the \@= construct. Use the same start , but change your end to:

 /\(^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2},\d\{3}\)\@=/ 

Please note that this is incompatible with Vi.

0
source

Source: https://habr.com/ru/post/1300308/


All Articles