Sublime Forum

Possible to achieve with the new sublime-syntax definitions?

#1

Previously, with tmLanguage syntax definitions, I’d manually edit my syntax definitions for all the languages I used to capture TODOs (comment.line.todo) and then define an obnoxious colour in my chosen theme to make sure they got done, e.g. for Python, I’d add:

<key>begin</key> <string>(?&lt;=^|;)\s*((#))\s*((?i)todo)</string> <key>beginCaptures</key> <dict> <key>1</key> <dict> <key>name</key> <string>comment.line.number-sign</string> </dict> <key>2</key> <dict> <key>name</key> <string>comment.line.number-sign</string> </dict> <key>3</key> <dict> <key>name</key> <string>comment.line.todo</string> </dict> </dict>

And something similar for Perl, Ruby, JS, etc.

What’s the preferred way of doing this with the new sublime-syntax files? Is there a way to define an isolated syntax for TODOs and then include that within all the other sublime-syntaxes that I’m interested in?

I also did something similar for highlighting trailing whitespace and mixed tabs/spaces to avoid having to install addons to do it for me. It worked really well.

Interested to hear the community’s thoughts on this.

Thanks

0 Likes

#2

I suggest using a dedicated plugin for that. Personally, I’m using sublimelinter-annotation.

0 Likes

#3

Hey FichteFoll - thanks for the suggestion, I’ll install and have a play around to solve the TODO thing.

With that said, I’m not opposed to using an addon to achieve this, but I like the flexibility tmLanguages/regexes give me to define exactly what I want and then being able to define the colours and so on.

For example, my company defines strict whitespace rules (no mixed tabs/spaces, indent with tabs, no trailing whitespace), and I have written careful regexes to identify occurrences in the code I’m working on and it’s worked really well so far (apart from the fact that I’ve had to manually merge any changes to tmLanguages as Sublime has evolved!).

I tend to work on very large files from time to time and have found all the “whitespace highlight” addons to be far less performant than the tmLanguage/theme approach.

0 Likes

#4

If you read through sublimetext.com/docs/3/syntax.html, you will notice two features that could help you with your quest, notably “Including other files”.

0 Likes

#5

Thanks. I had read through that prior to making this thread, but didn’t fully grasp whether or not what I wanted was possible – it seems the second use-case (i.e. the Jinja2 one) is relevant. I’ll fiddle around and post my results if anyone else is interested.

0 Likes

#6

If you were to edit an existing sublime-syntax theme (which, like others have said, seems like an iffy solution), you’d probably want to add this rule at the start of “prototype”, which is the set of common matches that can occur (almost) anywhere. Otherwise you would potentially need to add “TODO” matches in multiple contexts.

0 Likes

#7

I just added this functionality to my Scala syntax.

Code before:

  prototype:
    - include: 'comments'

  # comments
  comments:
    - match: '//'
      push:
        - meta_scope: comment.line.double-slash.scala
        - match: $
          pop: true
    - match: '/\*^*]'
      push:
        - meta_scope: comment.block.scala
        - match: '\*/'
          pop: true

Code with Todo detection:

   prototype:
    - include: 'comments'

  # comments
  comments:
    - match: '//'
      push:
        - meta_scope: comment.line.double-slash.scala
        - match: $
          pop: true
        - include: comments_in
    - match: '/\*^*]'
      push:
        - meta_scope: comment.block.scala
        - match: '\*/'
          pop: true
        - include: comments_in

  comments_in:
    - match: (?i:todo|hack)
      scope: comment.line.todo
0 Likes

#8

bathos: Huge thanks for the tip about ‘prototype’ - I was indeed going through and adding it to all contexts! Made my life a whole lot easier. :smile:

gwenzek: Appreciate your example as well - this has helped me rewrite my definitions to be more concise. :smile:

0 Likes

#9

The comment.line.todo scope is now in Ecmascript Syntax too.

0 Likes