Home Download Buy Blog Forum Support

Syntax Definition referencing other syntax definitions

Syntax Definition referencing other syntax definitions

Postby cfrolik on Wed Jul 30, 2014 3:23 pm

I have my own custom file format that looks something like this... (oversimplified)

@module{module-name}

@section{section1}
<text goes here>
@section{section2}
<text goes here>
@code
<code goes here>
@end

I've successfully made a .tmLanguage file for this, since it is very simple.
However, I want the <text goes here> parts to highlight as Markdown, and I want the <code goes here> parts to highlight as JavaScript.
But, I don't want to copy/paste the .tmLanguage content for Markdown and JavaScript into my .tmLanguage file; instead, I just want to "include" them within a begin/end pattern.

Is this possible?
cfrolik
 
Posts: 2
Joined: Wed Oct 24, 2012 2:00 pm

Re: Syntax Definition referencing other syntax definitions

Postby sapphirehamster on Thu Jul 31, 2014 9:46 pm

Use "include" to include other syntaxes. For example with javascript in the patterns list:

Code: Select all
         <key>patterns</key>
         <array>
            <dict>
               <key>include</key>
               <string>source.js</string>
            </dict>
         </array>


See https://sublime-text-unofficial-documen ... xdefs.html
and http://manual.macromates.com/en/language_grammars
for more information on using "include" and grammars in general.

Also look at all the tmLanguage files that ship with Sublime. Many of them use includes to embed other languages.
sapphirehamster
 
Posts: 96
Joined: Sun Jul 01, 2012 11:19 pm

Re: Syntax Definition referencing other syntax definitions

Postby Oddwel on Thu Mar 05, 2015 10:40 am

Hello.

I might be a little late on this one, but I wouldn't want to start a new thread when this one already exists.

I'm working on a BBcode syntax based on the package from chipotle. I have this little bit of syntax definition:
Code: Select all
<dict>
   <key>begin</key>
   <string>\[code=html\]</string>
   <key>captures</key>
   <dict>
      <key>0</key>
      <dict>
         <key>name</key>
         <string>meta.tag.code.html.bb.azuha</string>
      </dict>
   </dict>
   <key>end</key>
   <string>\[/code\]</string>
   <key>name</key>
   <string>text.html.embedded.bb.azuha</string>
   <key>patterns</key>
   <array>
      <dict>
         <key>include</key>
         <string>text.html.basic</string>
      </dict>
   </array>
</dict>
<dict>
   <key>name</key>
   <string>text.url.embedded.bb.azuha</string>
   <key>begin</key>
   <string>\[url(=)</string>
   <key>beginCaptures</key>
   <dict>
      <key>0</key>
      <dict>
         <key>name</key>
         <string>meta.tag.any.bb.azuha</string>
      </dict>
      <key>1</key>
      <dict>
         <key>name</key>
         <string>keyword.operator</string>
      </dict>
   </dict>
   <key>end</key>
   <string>[^\]]+?(\])</string>
   <key>endCaptures</key>
   <dict>
      <key>1</key>
      <dict>
         <key>name</key>
         <string>meta.tag.any.bb.azuha</string>
      </dict>
   </dict>
   <key>patterns</key>
   <array>
      <dict>
         <key>include</key>
         <string>text.url</string>
      </dict>
   </array>
</dict>


The html part is from the original syntax, I didn't touch it. The one I wrote is the one below involving urls.
Its goal is basically to treat the url in a url markup with a custom syntax I wrote, which works perfectly on a raw file and the scopeName of which is strictly the same as the one I include.

I know for a fact that the regexp are correct, the scopes are correctly computed outside of the url I want to parse. I also tried the html embedding and that works too.

The problem I have is the following :

it does not include.

My syntax exists. I tried putting it in the same folder as the html one. No success.

am I missing something ?
Is there something I don't know about including syntaxes ? I searched but didn't find, which is odd, I've been coding for 5 years and I've never been the only one with a problem before.
Oddwel
 
Posts: 1
Joined: Thu Mar 05, 2015 10:27 am

Re: Syntax Definition referencing other syntax definitions

Postby FichteFoll on Fri Mar 06, 2015 2:47 am

Normally I'd request you to upload the other syntax definition, but I think the issue is somewhere else – in your "end" pattern.

So your end pattern is currently this:
Code: Select all
end: '[^\]]+?(\])'


This will match ... basically everything until the first closing bracket is found. The problem is that any patterns *inside* this begin-end match are only applied to the text *within* the matches. Since your end match will start matching at the very first character (of a new line) however, the include obviously won't find anything since there is nothing to find.

Since simple matches like these ("begin", "end", "match") will not cover multiple lines for the sake of efficiency in the algorithm, you should be able to confirm this by adding a newline right before the closing bracket. Such as:
Code: Select all
[url=http://www.sublimetext.com/forum/posting.php?mode=reply&f=6&t=16560
]  blabla I'm not highlighted either way[/url]
FichteFoll
 
Posts: 653
Joined: Fri Mar 16, 2012 11:49 pm
Location: Germany


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 8 guests