Sublime Forum

Python highlighting in Postgres code blocks

#1

In Postgres there are code blocks in which other languages like Python can be used:

DO $$
    # PL/Python code
$$ LANGUAGE plpythonu;

I’d like to have Python highlighting inside that block and tried this:

- match: '(?i)(\$plpython3?u\$)$'
  push: Packages/Python/Python.sublime-syntax
  with_prototype:
    - match: \1
      pop: true

I want the Python highlighting to completely take over control in that block, but only the keywords are recognized. What am I getting wrong here?

1 Like

#2

As this syntax is a new one, I’d suggest to develop it against the current stable ST release 3176.

Hence, you should avoid using with_prototype to embed a 3rd party syntax, if you don’t need to inject some prototypes/rules into it. A better solution was to use the embed function to do so.

You should also avoid to point to 3rd-party syntaxes via path or syntax file like Packages/Python/Python.sublime-syntax as it would point to the wrong syntax in case a user uses a different package. Hence it is better to point to the main scope scope:source.python instead.

Your example would then look as follows:

%YAML 1.2
---
# See http://www.sublimetext.com/docs/3/syntax.html
file_extensions:
  - postgree
scope: source.postgree
contexts:
  main:
    - match: '(?i)(\$plpython3?u\$)$'
      scope: keyword.control.context.postgree
      embed: scope:source.python
      escape: '\1'
      escape_captures:
        0: keyword.control.context.postgree

6 Likes

#3

The documentation, at least the part you are linking to, hasn’t got such an example. And the one about “Including Other Files” only has one using with_prototype.

But it works and really makes me happy. Thank you!

0 Likes