On Thu, 25 Feb 2010, Vitor Eiji Justus Sakaguti wrote:

> On Wed, Feb 3, 2010 at 5:14 PM, Vitor Eiji Justus Sakaguti
> <[email protected]> wrote:
> > This works fine for the SQL code, but I dont want the tags 
> > themselves being highlighted as SQL, so I tried using offsets, but I 
> > can't seem to get it working, whenever I use ms or me, the SQL is 
> > not highlighted at all, and rs, re, hs and he make no difference 
> > whatsoever, no matter what value I set (eg e+1, s-1 etc)
> 
> OK, small follow up here.
> 
> I found out about \zs and \ze and tried them in my patterns, but I 
> could only get the closing tag to be colored as XML, but not the 
> opening one.
> 
> That is, the following line works as expected, highlights the closing 
> tag as XML syntax region sqlCode start="<sql:.\{-}>\s*\%[<!\[CDATA\[]" 
> keepend end="\ze\%[[]][]]>]\s*<\/sql:\w\{-}>" contai...@sql
> 
> But the following line doesn't work, it colors both tags as XML but 
> not the text inside as SQL
> syntax region sqlCode start="<sql:.\{-}>\s*\%[<!\[CDATA\[]\zs" keepend 
> end="\ze\%[[]][]]>]\s*<\/sql:\w\{-}>" contai...@sql
> 
> This is kind of odd, anyone has any ideas?
> 

File this in the 'oldie-but-goodie' category... I've had this on my 
(too-long) todo list since I first saw it.  As you probably discovered, 
it's very easy *if* you don't care about the opening XML tag being 
highlighted as XML.  But here's what I came up with to get it working.  
The first portion follows the general pattern for including other 
syntax.

""" ~/.vim/syntax/jelly.vim
""" also attached as jelly.vim in case the group mangles the long line
function! s:NoCurrent()
    if exists("b:current_syntax")
        unlet b:current_syntax
    endif
endfunction

" include XML syntax
runtime! syntax/xml.vim
call s:NoCurrent()

" include SQL syntax w/ ability to include it elsewhere
syntax include @sql syntax/sql.vim
call s:NoCurrent()

let b:current_syntax='jelly'

""" here's the tricky part:

syn match sqlCode 
+\%(<\(sql:\w\+\)\%(\_s\_[^>]*\)\{-}>\_s*\%[<!\[CDATA\[]\)\@<=\_.\{-}\%(\%(\]\]>\)\{-}\_s*<\/\1\)\...@=+
 contai...@sql
syn cluster xmlRegionHook add=sqlCode


Three things that help it work:

1. It uses a 'match', rather than a 'region'.  (I couldn't get regions 
to "play nice" with the other XML regions -- and especially with 
matchgroups, it seems impossible to get the highlighting right.)

2. It does NOT include the final closing '>' of the closing tag.  
(Otherwise, the normal XML highlighting is seen as matching sooner, 
since this pattern is using the lookbehind to match the start tag. (I 
think...))

3. It uses lookbehind and lookahead (\@<= and \...@=) rather than start and 
end (\zs and \ze).  Using those prevents the xmlStart- and -EndTags from 
working properly.

I used the test.jelly file attached for testing.  All of the 
sql:-namespaced nodes show up right for me.  (The SQL looks SQL-ish.)  
While the other three nodes look like normal XML.

This was a fun one, even though I suspect you already worked around it 
somehow.

-- 
Best,
Ben

-- 
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
function! s:NoCurrent()
   if exists("b:current_syntax")
      unlet b:current_syntax
   endif
endfunction

" include XML syntax
runtime! syntax/xml.vim
call s:NoCurrent()

" include SQL syntax w/ ability to include it elsewhere
syntax include @sql syntax/sql.vim
call s:NoCurrent()

let b:current_syntax='jelly'

syn match sqlCode 
+\%(<\(sql:\w\+\)\%(\_s\_[^>]*\)\{-}>\_s*\%[<!\[CDATA\[]\)\@<=\_.\{-}\%(\%(\]\]>\)\{-}\_s*<\/\1\)\...@=+
 contai...@sql
syn cluster xmlRegionHook add=sqlCode
<?xml version="1.0" encoding="UTF-8"?>
<xml>
   <sql:blah>select * from foo;</sql:blah>
   <sql:blah att="val">select * from foo;</sql:blah>
   <sql:blah att="val"><![CDATA[
      select * from foo;
   ]]></sql:blah>
   <sql:blah att="val">
      <![CDATA[
      select * from foo;
      ]]>
   </sql:blah>
   <asdf><![CDATA[blahblah]]></asdf>
   <nonsql>select * from foo;</nonsql>
   <ns:other>select * from foo;</ns:other>
</xml>

Reply via email to