Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Build System to be automatically chosen based on file extension

I have created a new Build System to run GolfScript programs. The definition is the following:

{
    "cmd": ["ruby", "D:\\w\\sublime\\golfscript.rb", "$file"]
}

This works, but I have to manually switch the Build System from "Automatic" to "golfscript" whenever I need to use this and then switch it back to be able to run Ruby, Python, etc.

I'd like to make my Build System be automatically applied when I have a *.gs file open.

I have read some docs and got the idea that I can use a selector in order to achieve this, so I added a selector to the existing configuration:

{
    "cmd": ["ruby", "D:\\w\\sublime\\golfscript.rb", "$file"],
    "selector": "source.gs"
}

After reading even more docs/examples, I could not figure out how to tell sublime what the selector is actually about.

How can I configure the source.gs selector to point to *.gs files?

like image 324
Cristian Lupascu Avatar asked Jul 15 '13 13:07

Cristian Lupascu


People also ask

How do I change the Build System in Sublime Text 3?

Select it, hit Ctrl B to build, and then hit Ctrl Shift B to run the resulting program. Or you can use a Build and Run option and call it by hitting Ctrl B , then selecting that option.

What is Build System in sublime?

Sublime Text provides build systems to allow users to run external programs. Examples of common uses for build systems include: compiling, transpiling, linting, and executing tests. Build systems are specified via JSON and saved in a file with the extension .sublime-build.

How do I open builds in Sublime Text 3?

You can select it in Sublime Text editor by going to Tools > Build System > py and building with Ctrl + b .


2 Answers

You need to create a syntax file for GolfScript.

Save the following XML as golfScript.tmLanguage and put it in the Packages/Golfscript folder as described here.

You may need to restart ST.

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
    <key>fileTypes</key>
    <array>
        <string>gs</string>
    </array>
    <key>name</key>
    <string>GolfScript</string>
    <key>patterns</key>
    <array>
    </array>
    <key>scopeName</key>
    <string>source.gs</string>
    <key>uuid</key>
    <string>c4c7fc10-d937-4f5d-9cb7-4316026457e5</string>
</dict>
</plist>
like image 158
skuroda Avatar answered Sep 22 '22 21:09

skuroda


The generally appropriate thing to do to associate a build with a particular type of file is indeed to use a selector that tells Sublime how to choose the build based on the type of file in question.

However, for cases where the extension of the language doesn't track with normal extensions for that language, or where there isn't a syntax definition available to provide the appropriate scope (and thus also syntax highlighting for that language), you can give hints about what build to use via the file_patterns key in the sublime-build file, as outlined in the documentation on build system options.

For example, presuming that GolfScript files have an extension of gs:

{
    "cmd": ["ruby", "D:\\w\\sublime\\golfscript.rb", "$file"],
    "file_patterns": ["*.gs"]
}

This would indicate to Sublime that for any file with this extension, this build is applicable and will be considered as a potential build system for the current file.

Depending on the file type in question, you may get asked by Sublime to choose the correct build the first time you run the build, similar to what happens if a build has multiple variants. In such a case, you get prompted to select the build, and then it will be remembered for future builds and you need to use Build With to select again.

For example, presuming that .gs files were associated with the syntax for Ruby (for example), both the build above and also the build for Ruby files (chosen based on the scope selector from that build system) would be candidates, requiring you to disambiguate on the first build.

like image 36
OdatNurd Avatar answered Sep 24 '22 21:09

OdatNurd