Sublime Forum

Syntax include pop

#1

Hi,
Is there a way to to pop the current context if a include matched anything?
For example, I have a context to match variables and types:

types:
  - match: SomePattern
    scope: type.example

variables:
  - match: SomePattern
    scope: variables.example

Those variables may appear free or as function parameters and the type before the variable is not required, so I assumed:

parameter:
  - include: variables
    pop: true
  - include: types

The idea was that if it matched the variable, it would pop the current context as the type must never appear in front of it.

Is it possible to do something like this without having to rewrite the variables’ pattern over and over? The type and the variable may be in different lines, so a single pattern to match both would not work.

Thanks

0 Likes

#2

Unfortunately it isn’t possible to do it the way you would like, to pop the context off the stack when something in an included context matches.

However, you can do it like this:

types:
  - match: SomeTypePattern
    scope: type.example

variables:
  - match: SomeVariablePattern
    scope: variables.example

parameter: #optional_type_followed_by_variable
  - include: types
  - match: (?=.) # whether or not a type was matched, we now want to change context so we can match a variable
    set: parameter_variable

parameter_variable:
  - include: variables

also, you can define regex variables so that you don’t ever rewrite a pattern, you can just reuse it - see http://www.sublimetext.com/docs/3/syntax.html and search for Variables.

2 Likes

#3

Thanks kingkeith your solution helped.

I’ve been using lots of variables to prevent rewriting but this problem came up when multiple files were involved (the definition of types included builtin types and user types).

It would make the syntax files a lot cleaner if we could pop on include but I’m not sure how hard that would be for the devs to implement so I’ll stick with your solution.

0 Likes

#4

It would be easier to understand if you included some examples of the type of code you are trying to write syntax for. You probably want to avoid using the name “variables” which has special purpose in syntax files.

If you want to make sure there is no type in front of the variable, could you use a negative lookbehind?

(?<!{{type}})({{identifier}})

where identifier is the regex for a variable name.

0 Likes

#5

backreferences and lookbehinds (actually your example is a lookbehind) are not supported by ST3’s new regex engine, so it is recommended to avoid using them where possible in syntax definitions. This is for performance reasons, because the new engine is much faster.

0 Likes

#6

Oh wow, didn’t realise that. So if I’m using these it defaults back to the old engine?

0 Likes

#7

Correct, expressions containing lookbehinds will be matched with the old engine - see this helpful post for more details :slightly_smiling:

0 Likes

#8

There is no problem with using “variables” as a scope name since the variables for regular expressions are defined in a top-level key whereas all contexts are inside the “contexts” key.

1 Like

#9

In the end, I designed the entire syntax with patterns that matched a single time and pop the current context, this allows highlighting of sequences like the example I gave before, now I just push the type and variable contexts without worrying if they are going to match multiple times.

The source code is here: https://github.com/borela/naomi
I only added the highlighting for PHP 7 as I was needing it to help me in a project, it doesn’t have all the new features yet, but I’ll add them as soon as I need or if someone send a pull request.

Here’s what it can do for now:

1 Like