Skip to content
Advertisement

Paste a formatted and higlighted multiline text (SQL) into string literal in PyCharm

Is there a way in PyCharm to paste multiline text (SQL) into string literal? E.g.:

SELECT column1
     , column2
FROM table1
WHERE column3 IN
(
    SELECT TOP(1) column4
    FROM table2
    INNER JOIN table3
    ON table2.column1 = table3.column1
)

Into:

sql = (
    "SELECT column1"
    "     , column2"
    "FROM table1"
    "..."
)

And ideally tell PyCharm it is text in SQL language to highlight syntax?

Advertisement

Answer

Is there a way in PyCharm to paste multiline text (SQL) into string literal?

Yes. First you’ll have to “Create a live template” and since you want to paste the right function to use would be clipboard(). You can do this by going to File > Settings > Editor > Live Templates as shown in the screenshot.

enter image description here

And ideally tell PyCharm it is text in SQL language to highlight syntax?

Yes. You can do this by using “language injection” in the code segment. If you have it preconfiguered like shown in the following screenshot PyCharm should autodetect SQL and apply the syntax highlight. It can be set at File > Settings > Editor > Intention.

enter image description here

If you don’t have the intention set to autodetect you can also do it manually by right-clicking and choosing “Show Context Actions” or pressing the light-bulb in the code (this is made easier by putting the SQL code inside a string).

enter image description here

After choosing the SQL dialect you prefer, you can also fine-tune syntax highlight and syntax checks of the injected language.

Functionalities IDE path to dialogue
Syntax checks Settings > Editor > Inspections > SQL
Syntax highlights Settings > Editor > Color Sheme > SQL
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement