2016-03-08 32 views
1

我想突出顯示SQL查詢裏面的python三引號字符串在Vim中。使用這種question我有一個pysql.vim文件,其中包含:vim:higlight SQL裏面的python三引號字符串

if exists('b:current_syntax') 
finish 
endif 

" Load Python syntax at the top level 
runtime! syntax/python.vim 
unlet b:current_syntax 

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

syntax region sqlSnippet start=/\zs\v(SELECT|FROM|AND|WHERE|OR|ON|GROUP BY|ORDER BY)/ end=/\ze'''/ [email protected] containedin=pythonString 

let b:current_syntax = 'pysql' 

然而,當我在下面foo.py文件運行set syntax=pysql之前

def get_first_events_after_install(application_id, os, event_id, 
            year_start, month_start, day_start, 
            year_end, month_end, day_end, 
            perform=False): 

    query = \ 
     '''SELECT event_id, counter, name FROM 
     (
     -- This selects event_ids and counts number of occurrences for a 
     -- specific in a specific time frame 
     SELECT event_id, COUNT(1) as counter FROM fault.all_events_monthly 
     WHERE month_partition 
      BETWEEN '201511' AND '201601' 
      AND app_partition IN (434) 
      AND ref_type_partition IN ('apple_ifa') 
     GROUP by event_id 
     ) t_counter 
    INNER JOIN 
     (
      -- This selects events names and along event_id 
      SELECT id, name from mysql.ruby.events 
     ) t_name 
    ON t_counter.event_id = t_name.id 
    ORDER BY counter DESC'''.\ 
     format(month_part=month_partitions, os_part=os_partitions, 
       ys=year_start, ms=month_start, ds=day_start, 
       ye=year_end, me=month_end, de=day_end, app_id=application_id, 
       ev_id=event_id) 

代碼後串查詢也突出顯示爲SQL(你可以在下圖中看到這個)。所以我的問題是:我如何在三重(或三重雙)引號字符串中強調SQL代碼?提前致謝。

enter image description here

+0

你的機會得到一個答案可能是在[VI和Vim堆棧交易所(http://vi.stackexchange.com/)更好。 – DreyFax

回答

1

您將需要修改和重新定義pythonString語法。從/usr/share/vim/vim74/syntax/python.vim

" Triple-quoted strings can contain doctests. 
syn region pythonString 
    \ start=+[uU]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1" 
    \ contains=pythonEscape,@Spell 
syn region pythonString 
    \ start=+[uU]\=\z('''\|"""\)+ end="\z1" keepend 
    \ contains=pythonEscape,pythonSpaceError,pythonDoctest,@Spell 
syn region pythonRawString 
    \ start=+[uU]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1" 
    \ [email protected] 
syn region pythonRawString 
    \ start=+[uU]\=[rR]\z('''\|"""\)+ end="\z1" keepend 
    \ contains=pythonSpaceError,pythonDoctest,@Spell 

隨着syn clear pythonString可以清除現有的語法高亮 它,然後我們可以重新定義@SQLcontains增加。在~/.vim/after/syntax/python.vim把這個似乎運作良好:

" SQL syntax file won't load if this is set 
unlet b:current_syntax 

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

" We don't need these (a string inside a string!) 
syn cluster SQL remove=sqlString,sqlComment 

" Clear existing syntax 
syntax clear pythonString 

" Triple-quoted strings can contain SQL. This is the same as the original 
" except with @SQL added in contains= 
syn region pythonString 
    \ start=+[uU]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1" 
    \ contains=pythonEscape,@Spell 
syn region pythonString 
    \ start=+[uU]\=\z('''\|"""\)+ end="\z1" keepend 
    \ contains=pythonEscape,pythonSpaceError,pythonDoctest,@Spell,@SQL 
syn region pythonRawString 
    \ start=+[uU]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1" 
    \ [email protected] 
syn region pythonRawString 
    \ start=+[uU]\=[rR]\z('''\|"""\)+ end="\z1" keepend 
    \ contains=pythonSpaceError,pythonDoctest,@Spell,@SQL 

" Re-set current syntax 
let b:current_syntax = 'python' 
+0

謝謝!我如何添加一些沒有被標準sql.vim語法文件突出顯示的關鍵字?我在'let b:current_syntax ='pysql''之前嘗試了 'syn關鍵字sqlKeyword INNER RIGHT LEFT OUTER JOIN',但它不起作用。 – petobens

+0

將它放在'syntax clear pythonString'行之上似乎可行。 – petobens

+0

@petobens您可以設置SQL風格。請參閱[':help ft_sql.txt'](http://vimhelp.appspot.com/ft_sql.txt.html)和'syntax/sql.vim'文件。默認是相當基本的,但它支持更多。 – Carpetsmoker