2011-02-02 13 views

risposta

10

copio-incollato-modificata da 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') 

Ora ogni righe triple-single quote string ottiene la sintassi sql. Le stringhe con doppie virgolette sono ancora chiare. Ho cambiato sqlpostgres.vim per dare sfumature SQL di verde per differenziare le due lingue, e sembra dolce nello schema inkpot a 256 colori.

legato anche: Embedded syntax highligting in Vim

+0

ok, il verde fa schifo, ma l'idea è sana –

+0

Come lo uso? – skyler

1

si può provare la seguente opzione sul vostro file:
setfiletype=python.sql

Dà sia il tipo di file al file, e deve applicare sia l'evidenziazione della sintassi.

Se funziona per voi, è possibile aggiungere la seguente riga al vostro .vimrc per applicarlo a tutti i file python che si modifica:

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

Tuttavia, non si tratta in realtà con il conflitto tra la due gruppi di evidenziazione ... Quindi potrebbe o non potrebbe funzionare nel tuo caso.

Si potrebbe anche creare una funzione per attivare o disattivare rapidamente il tipo di file di buffer aperta:

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, applica la sintassi sql al codice python e le stringhe sono semplici. –

1

So che c'è una risposta accettata, ma ancora, ecco un altro modo di farlo:

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 questo, mettendo in evidenza inizia in uno dei dati parole chiave SQL e si arresta al primo ; e può riavviarsi alla successiva parola chiave SQL o si arresta alla fine della stringa python (vedere keepend).

Problemi correlati