I have some Python files in which I want to highlight the SQL queries in string literals. Assume that all string literals in those files contain SQL queries.
I saved the following syntax file as pysql.vim:
if exists("b:current_syntax")
finish
endif
" Include Python syntax
runtime! syntax/python.vim
unlet b:current_syntax
syn include @SQL syntax/sql.vim
syn region SQLEmbedded start=+'+ end=+'+ contains=@SQL
syn region SQLEmbedded start=+%+ end=+%+ contains=@SQL
let b:current_syntax = "pysql"
I added syntax for percent signs to check if it works.
Load the following python file (ignoring the fact the first line is not valid in Python):
a = %select * from mytab%
b = 'select * from mytab'
And run vim command set syntax=pysql. It works for SQL queries inside percent signs, but not work for quotes. Strangely, any words following the string literal is highlighted as SQL. For instance, select * from mytab in '1' select * from mytab '2' is highlighted.
Could you find the error in my syntax file?
select * from mytabin'1' select * from mytab '2'is ignoring the1in'1'and the2as they aren't SQL matching theselect ...using the'from after the 1 and before the 2'select * from mytab' select * from mytab 'select * from mytab', the middle SQL query (outside of string literals) is highlighted.