# Extending existing syntax definitions

**URL:** <https://forum.sublimetext.com/t/extending-existing-syntax-definitions/19663>\
**Category:** Technical Support\
**Created:** [May 1, 2016, 1:18pm UTC](https://forum.sublimetext.com/t/extending-existing-syntax-definitions/19663 "2016-05-01T13:18:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg\_hoch](https://forum.sublimetext.com/letter_avatar_proxy/v4/letter/g/ce73a5/32.png) [@greg\_hoch](https://forum.sublimetext.com/u/greg_hoch)\
**Post date:** [May 1, 2016, 1:18pm UTC](https://forum.sublimetext.com/t/extending-existing-syntax-definitions/19663/1 "2016-05-01T13:18:02Z")

</div>

Hi,

what is the best way to extend an existing syntax definition? Say I am working on a Javascript file with the promise library [Bluebird](http://bluebirdjs.com/docs/getting-started.html) and I use [coroutine](http://bluebirdjs.com/docs/api/promise.coroutine.html) for my function definitions:

```auto
var myFunction = Promise.coroutine(function* (x + y) {

})

```

The default syntax doesn’t recognize that as a function definition, it doesn’t get indexed as a symbol etc. What is the best way to extend the existing syntax definition? It would be great if I could do that without overwriting the existing definition so that updates to the ST javascript syntax don’t require any changes for my extended definition. It would even be better if I could extend an existing syntax without creating a new one that imports ST’s definition.

Thanks!

---

<div class="post-metadata">

**Author:** ![borela](https://forum.sublimetext.com/user_avatar/forum.sublimetext.com/borela/32/51707_2.png) [@borela](https://forum.sublimetext.com/u/borela)\
**Post date:** [May 1, 2016, 3:00pm UTC](https://forum.sublimetext.com/t/extending-existing-syntax-definitions/19663/2 "2016-05-01T15:00:15Z")

</div>

Create a syntax file (must end with .sublime-syntax) with something like this:

```
%YAML1.2
---

name: My Custom Javascript

file_extensions:
  - js

scope: source.js

contexts:
  main:
    - match: |
        (?xi)
        \b
        (?> coroutine
          | otherFunction
          | anotherOne
        )\b
      scope: support.function.js
    # Normal javascript...
    - include: JavaScript.sublime-syntax

```

The xi means:

```
x : Ignore spaces
i : Case insenstive

```

I normally use [rubular](http://rubular.com/) to test the regexes, there you can learn more about how it works.

PS: I got the name for the javascript file from the [github](https://github.com/sublimehq/Packages/tree/master/JavaScript) repository and I really recommend that you read the [official documentation](http://www.sublimetext.com/docs/3/syntax.html) to really understand how the syntax files works.

---

<div class="post-metadata">

**Author:** ![FichteFoll](https://forum.sublimetext.com/user_avatar/forum.sublimetext.com/fichtefoll/32/43583_2.png) [@FichteFoll](https://forum.sublimetext.com/u/FichteFoll)\
**Post date:** [May 1, 2016, 3:30pm UTC](https://forum.sublimetext.com/t/extending-existing-syntax-definitions/19663/3 "2016-05-01T15:30:22Z")

</div>

Unfortunately that is a really poor “integration” that will accomplish almost nothing considering how the current JavaScript.sublime-syntax is structured. Most importantly, it doesn’t add to the symbol indexer and thus cannot be searched for.

Back on topic, the problem is rather complex and non-trivial with how deeply intertwined modification to support your syntax and the original would have to be. Thus, I can not provide any help for this unfortunately.

It would probably be best to look at the default syntax and modify it in-place, inspired by current prototype function matching.

---

<div class="post-metadata">

**Author:** ![borela](https://forum.sublimetext.com/user_avatar/forum.sublimetext.com/borela/32/51707_2.png) [@borela](https://forum.sublimetext.com/u/borela)\
**Post date:** [May 1, 2016, 4:24pm UTC](https://forum.sublimetext.com/t/extending-existing-syntax-definitions/19663/4 "2016-05-01T16:24:31Z")

</div>

Yeah, I know, it’s just a “hack” that will work on some cases but I agree that is impossible to extend the core syntax without making changes to it.

---

<div class="post-metadata">

**Author:** ![greg\_hoch](https://forum.sublimetext.com/letter_avatar_proxy/v4/letter/g/ce73a5/32.png) [@greg\_hoch](https://forum.sublimetext.com/u/greg_hoch)\
**Post date:** [June 23, 2016, 2:03pm UTC](https://forum.sublimetext.com/t/extending-existing-syntax-definitions/19663/5 "2016-06-23T14:03:15Z")

</div>

Thanks! Just a quick follow-up:

1. What is the best way to modify the default syntax? Can I use `PackageResourceViewer: Open Resource -> JavaScript -> JavaScript.sublime-syntax`? When I save the file that opens, it’s placed in the Package folder and overwrites the zipped Javascript stuff package, no? What happens when ST updates the default javascript syntax?
2. Can you point me at a current prototype function matching?

---

<div class="post-metadata">

**Author:** ![FichteFoll](https://forum.sublimetext.com/user_avatar/forum.sublimetext.com/fichtefoll/32/43583_2.png) [@FichteFoll](https://forum.sublimetext.com/u/FichteFoll)\
**Post date:** [June 23, 2016, 2:23pm UTC](https://forum.sublimetext.com/t/extending-existing-syntax-definitions/19663/6 "2016-06-23T14:23:47Z")

</div>

1. Your modified version will _always_ override the default one. As such, I’d recommend saving the modified syntax under a different name in your user folder and just selecting it when needed (or making all .js files open with that syntax).

2. I wnated to refer to [this](https://github.com/sublimehq/Packages/blob/211a27c8c6b5fb42a85a5139028ca5efc5470e58/JavaScript/JavaScript.sublime-syntax#L718), but I realize that this wouldn’t really help.  
Anyway, what you need to do is match the line in a look-ahead pattern `(var)\s+({{identifier}})(?=\s*=\s*Promise\.coroutine\()`, then apply the `entity.name.function` scope to the identifier and take care of setting/pushing/not changing a context that takes care of matching the rest of this assignment.  
Make sure to arrange this match before the literal `var` keyword is matched: [https://github.com/sublimehq/Packages/blob/211a27c8c6b5fb42a85a5139028ca5efc5470e58/JavaScript/JavaScript.sublime-syntax#L703](https://github.com/sublimehq/Packages/blob/211a27c8c6b5fb42a85a5139028ca5efc5470e58/JavaScript/JavaScript.sublime-syntax#L703)
