2011-02-02 9 views

Respuesta

10

copio-pegado-cambiado de here:

function! TextEnableCodeSnip(filetype,start,end,textSnipHl) abort 
    let ft=toupper(a:filetype) 
    let group='textGroup'.ft 
    if exists('b:current_syntax') 
    let s:current_syntax=b:current_syntax 
    " Remove current syntax definition, as some syntax files (e.g. cpp.vim) 
    " do nothing if b:current_syntax is defined. 
    unlet b:current_syntax 
    endif 
    execute 'syntax include @'.group.' syntax/'.a:filetype.'.vim' 
    try 
    execute 'syntax include @'.group.' after/syntax/'.a:filetype.'.vim' 
    catch 
    endtry 
    if exists('s:current_syntax') 
    let b:current_syntax=s:current_syntax 
    else 
    unlet b:current_syntax 
    endif 
    execute 'syntax region textSnip'.ft.' 
    \ matchgroup='.a:textSnipHl.' 
    \ start="'.a:start.'" end="'.a:end.'" 
    \ [email protected]'.group 
endfunction 

au FileType python call TextEnableCodeSnip('sqlpostgres', "'''", "'''", 'SpecialComment') 

Ahora cada línea múltiple de un solo triple quote string obtiene la sintaxis sql. Las cadenas de comillas dobles seguidas son simples. Cambié sqlpostgres.vim para dar matices SQL de verde para diferenciar los dos idiomas, y se ve muy bien en el esquema de tintero de 256 colores.

También relacionado: Embedded syntax highligting in Vim

+0

bien, verde apesta, pero la idea es sonido –

+0

¿Cómo uso esto? – skyler

1

Usted puede probar la siguiente opción en el archivo:
setfiletype=python.sql

Se da tanto en el tipo de archivo a su archivo, y debe aplicarse tanto resaltado de sintaxis.

Si funciona para usted, usted puede añadir la siguiente línea a su .vimrc aplicarlo a todos los archivos de pitón que se edita:

autocmd BufRead,BufNewFile *.py setfiletype=python.sql

Sin embargo, en realidad no lidiar con el conflicto entre la dos grupos destacados ... Por lo tanto, podría funcionar o no en su caso.

También puede crear una función para cambiar rápidamente el tipo de archivo de la memoria intermedia abierta:

function! ToggleFiletype() 
    if &filetype=="sql" 
    set filetype=python 
    endif 

    if &filetype=="python" 
    set filetype=sql 
    endif 
endfunction 

map <F11> <Esc>:call ToggleFiletype()<cr> 
+0

au contraire, aplica la sintaxis sql al código python, y las cadenas son simples. –

1

Sé que hay una respuesta aceptada, pero aún así, aquí hay otra forma de hacerlo:

if exists("b:current_syntax") 
    finish 
endif 

" Load Python syntax at the top level 
runtime! syntax/python.vim 

" Needed to make syntax/sql.vim do something 
unlet b:current_syntax 

" Load SQL syntax 
syntax include @SQL syntax/sql.vim 

" Need to add the keepend here 
syn region pythonString matchgroup=pythonQuotes 
     \ start=+[uU]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1" 
     \ contains=pythonEscape,@Spell keepend 
syn region pythonRawString matchgroup=pythonQuotes 
     \ start=+[uU]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1" 
     \ [email protected] keepend 

syn region SQLEmbedded [email protected] containedin=pythonString,pythonRawString contained 
    \ start=+\v(ALTER|BEGIN|CALL|COMMENT|COMMIT|CONNECT|CREATE|DELETE|DROP|END|EXPLAIN|EXPORT|GRANT|IMPORT|INSERT|LOAD|LOCK|MERGE|REFRESH|RENAME|REPLACE|REVOKE|ROLLBACK|SELECT|SET|TRUNCATE|UNLOAD|UNSET|UPDATE|UPSERT)+ 
    \ end=+;+ 

let b:current_syntax = "pysql" 

Con eso, destacando aperturas en una de las palabras clave de SQL asignados y se detiene en el primer ; y puede reiniciarse en la siguiente palabra clave SQL, o se detiene al final de la cadena de Python (consulte keepend).

Cuestiones relacionadas