question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Highlighting of SQL inside strings

See original GitHub issue

The stock Python package performs syntax highlighting inside strings containing SQL commands. That’s rather useful, even though the implementation might need some more work. In this example:

create = 'CREATE TABLE test (id INT, name TEXT);'
insert = 'INSERT INTO test VALUES (1, "Bob")'

in the second line, words recognized as SQL are highlighted, but not in the first one, where “CREATE TABLE” has the same color as the non-reserved names.

(Actually, as you can see, even Github’s Markdown does it, with exactly the same behavior, so perhaps something is wrong with the first line, although it does execute properly)

Do you consider this a useful addition?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:105
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

12reactions
movalexcommented, Sep 3, 2018

Sublime uses not very sophisticated SQL indicator for triple quote highlighting: \s*(?:SELECT|INSERT|UPDATE|DELETE|CREATE|REPLACE|ALTER|WITH)\b

Is it possible to implement this in MagicPython? Seems like a good feature, and used both in Atom and Sublime natively (does not work in VSCode).

    # Triple-quoted raw string, unicode or not, will detect SQL, otherwise regex
    - match: '([uU]?r)(""")'
      captures:
        1: storage.type.string.python
        2: meta.string.python string.quoted.double.block.python punctuation.definition.string.begin.python
      push:
        - meta_content_scope: meta.string.python string.quoted.double.block.python
        - match: '(?={{sql_indicator}})'
          set:
            - meta_scope: meta.string.python string.quoted.double.block.python
            - match: '"""'
              scope: punctuation.definition.string.end.python
              set: after-expression
            - match: ''
              push: scope:source.sql
              with_prototype:
                - match: '(?=""")'
                  pop: true
                - include: escaped-unicode-char
                - include: constant-placeholder
        - match: '(?=\S)'
          set:
            - meta_scope: meta.string.python string.quoted.double.block.python
            - match: '"""'
              scope: punctuation.definition.string.end.python
              set: after-expression
            - match: ''
              push: scope:source.regexp.python
              with_prototype:
                - match: '(?=""")'
                  pop: true
                - include: escaped-unicode-char
8reactions
1st1commented, Sep 3, 2019

Well, we have no plans for adding SQL highlighting. There are many cons/pros about this feature, see the above discussion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Highlight String Code - Visual Studio Marketplace
Extension for Visual Studio Code - Highlight string as SQL, HTML, CSS or JS in most languages. 高亮内嵌的SQL,HTML, CSS或者JS.
Read more >
Is it possible to have SQL syntax highlighting on strings ...
Hello, I'm trying to understand if it is possible to have (pl/PG)SQL syntax highlighting for queries that are on strings embedded in source...
Read more >
Visual Studio Code SQL Syntax Highlighting in .py Files
It activates sql highlighting when sees one of the three starting keywords in multiline string (multiline starting with """ or f""" ).
Read more >
Syntax Highlighting for SQL in Tagged Template Literals
In @databases you will use a lot of SQL strings inline within your TypeScript or JavaScript code. These strings are always "tagged" with...
Read more >
Vim syntax highlighting for SQL strings inside Python code
In Python, I had to get slightly more clever: this will treat any triple quotes-delimited string as SQL if it starts with a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found