我试图在 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'
然而,当我在下面的 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 代码?提前致谢。
您需要修改并重新定义 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
您可以清除现有的语法突出显示它,然后我们可以在 contains
中添加 with with @SQL
重新定义。将它放在 ~/.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'
我是一名优秀的程序员,十分优秀!