vim:在python三引号字符串中的higlight SQL

时间:2016-03-08 13:30:53

标签: python regex vim

我正在尝试在Vim中的python三引号字符串中突出显示SQL查询。使用此question我有一个pysql.vim文件,其中包含:

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'''/ contains=@SQL containedin=pythonString

let b:current_syntax = 'pysql'

但是,当我在以下set syntax=pysql文件

中运行foo.py
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

1 个答案:

答案 0 :(得分:2)

您需要修改并重新定义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"
    \ contains=@Spell
syn region  pythonRawString
    \ start=+[uU]\=[rR]\z('''\|"""\)+ end="\z1" keepend
    \ contains=pythonSpaceError,pythonDoctest,@Spell

使用syn clear pythonString,您可以清除现有的语法突出显示 它,然后我们可以使用@SQL中添加的contains重新定义。把它放在~/.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"
    \ contains=@Spell
syn region  pythonRawString
    \ start=+[uU]\=[rR]\z('''\|"""\)+ end="\z1" keepend
    \ contains=pythonSpaceError,pythonDoctest,@Spell,@SQL

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