Sublime Forum

Strange behavior when matching syntax

#1

Hello! :slight_smile:

When working on improvements in syntax definition for Inno Setup, I noticed some peculiar behavior. Let’s start with a minimal syntax I managed to reduce the issue to:

%YAML 1.2
---
name: 'Test'
scope: base-scope

contexts:
  main:
    - match: ' *(?=#)'
      push:
        - clear_scopes: true
      with_prototype:
        - match: '(?= *$)'
          pop: true
    - match: '^ *a *$'
    - match: '^b$'
    - match: '^c'
    - match: 'd$'

And a simple test file (contains no whitespace apart from the line breaks):

a
b
c
d
e

Inspecting scopes reveals that the “base-scope” gets removed from a, b, c and d lines.

Now to the peculiarities:

  • Adding any meta_scope to context with clear_scopes fixes the issue (“base-scope” is present on all lines).
  • Adding any scope when a, b, c or d is matched fixes the issue on that line.
  • Adding “-match: e” fixes the issue.
  • Changing space to other character in ’ *(?=#)’ fixes the issue.
  • Changing # to other character fixes the issue
  • If spaces are added to lines where they would be matched, their scope is cleared as well.
  • Using capturing groups fixes syntax only for parts with scope assigned in the “captures:”.

If I write “#” into the test file, scopes for it behave as one would expect, but issue for other lines is still there. Check it out yourself :wink:

Do anyone have any clue what is going on? Is this behavior on purpose and I just don’t get something or maybe it is a bug?

I’m on Sublime Text 3.2.2 Build 3211.

1 Like

#2

ST 3211 on Windows:

I modified base scope to invalid.illegal to see it more obviously, but I don’t see it being removed anywhere.

grafik

1 Like

#3

Thanks for the feedback.

I created a brand new syntax file with the same content as above and then formatting is indeed fine.
But if I rename this new file as it was originally called, then this comes back:

image

I did the same with the complete syntax, for which the issue occurred originally. Then the scopes are still cleared incorrectly. Subsequently the minimal example also fails in this new file.

It seems like this is some combination of both syntax file and test file that causes this, and then it somehow persists.

If someone on the Sublime Text side would be interested in figuring this out, I could try to pinpoint the specific reason. Otherwise, in case someone else has this problem too, the simplest fix is to just put the whitespace inside lookahead, ie: match: '(?= *#)'.

1 Like

#4

Still works fine for me on Windows.

What OS are you using?

1 Like

#5

I’m on Windows too.

In the meanwhile I managed to come up with an example that fails consistently.
It would be great if you could check if you get the same result.

Test Minimal.txt:

  #this line with spaces in front is necessary
  scopes here are just fine
  [but_are_cleared_around_this]  

Syntax Minimal.sublime-syntax:

%YAML 1.2
---
name: 'Syntax Minimal'
scope: invalid.illegal

contexts:
  main:
    - include: preprocessor
    - include: section

  preprocessor:
    - match: '^\s*(?=#)'
      push:
        - clear_scopes: true
        - meta_content_scope: support.function
        - match: '(?=\s*$)'
          pop: true

  section:
    - match: '(?i)^\s*(\[\w*\])\s*$'
      captures:
        1: entity.name.namespace

You should see the scopes cleared around [ and ]:

image

Once the issue is triggered:

  1. You can freely modify the example and scopes around [Section] will always be cleared.
  2. If you get the example to a state that would not trigger the issue, re-saving the syntax fixes it.
  3. Now you can undo the changes in the example and highlighting will be correct!

I also get this on a portable version so it seems at least it’s not my particular setup that causes it.

0 Likes