vim-sleuth
vim-sleuth copied to clipboard
Indentation being inferred from multiline string literal in Python code when a different pydoc (also a multiline str literal) has quotes in it
trafficstars
I have a Python file where the Python is indented with 4 spaces and an inner multiline literal for sql is indented using 2 spaces:
def foobar():
"""Blah blah "something quoted"
"""
pass
class FooBar:
def run_query():
sql = """
SELECT e.foo, e.bar
FROM (
SELECT some_subquery, * FROM another_table
) e
WHERE e.blah
AND e.xyz
"""
yield from MyQueryObj(sql).rows()
And vim-sleuth infers sw=2 when I expect sw=4 for the Python file.
In producing this minimal repro, I've found that either of the following changes produces the expected sw=4:
- commenting out the entire
sql = """<SQL>"""multiline literal - Changing the
"something quoted"into'something quoted'infoobar()'s pydoc
Maybe relevant, I have the following loaded to SQL syntax highlight such literals, in $VIMDIR/after/syntax/python.vim:
" Syntax highlight SQL embedded inside Python triple-quotes
" https://stackoverflow.com/questions/29791199/highlight-sql-inside-python-string-literals
" Temporarily disable current syntax
unlet b:current_syntax
" Load SQL syntax
syn include @SQL syntax/sql.vim
" modified https://thegreata.pe/articles/2020/07/11/vim-syntax-highlighting-for-sql-strings-inside-python-code/
syntax region sqlPythonString
\ matchgroup=SpecialComment
\ start=+\v\C\z('''|""")\_s*(--|(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|COPY|WITH)>)@=+
\ end=+\z1+
\ contains=@SQL
syntax region sqlPythonRawString
\ matchgroup=SpecialComment
\ start=+\v\Cr\z('''|""")\_s*(--|(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|COPY|WITH)>)@=+
\ end=+\z1+
\ contains=@SQL
syntax region sqlPythonFString
\ matchgroup=SpecialComment
\ start=+\v\Cf\z('''|""")\_s*(--|(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|COPY|WITH)>)@=+
\ end=+\z1+
\ contains=@SQL,pythonString
syntax region sqlPythonRawFString
\ matchgroup=SpecialComment
\ start=+\v\C(fr|rf)\z('''|""")\_s*(--|(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|COPY|WITH)>)@=+
\ end=+\z1+
\ contains=@SQL,pythonString
" modified https://github.com/sheerun/vim-polyglot/blob/ce31cd1d2f4e8eee9fd91325e4599f15cb9566fd/syntax/python.vim#L273
syn region pythonStrInterpRegion
\ matchgroup=pythonStrFormat
\ start="{"
\ end="\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}"
\ extend contained containedin=pythonFString,pythonRawFString,sqlPythonFString,sqlPythonRawFString contains=pythonStrInterpRegion,@pythonExpression
" Restore current syntax
let b:current_syntax = 'python'
But commenting out out the entire syntax/python.vim file does not affect the repro above.