8

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?

Basj
  • 41,386
  • 99
  • 383
  • 673
Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
  • 1
    Is the `source.gs` scope being applied to the file? You can check this by entering `ctrl+shift+alt+p` (in Windows). The scopes at the cursor will be displayed in the status bar. – skuroda Jul 16 '13 at 04:03
  • @skuroda thanks for the reply; it says `text.plain`. How can I define a selector for _*.gs_ files? – Cristian Lupascu Jul 16 '13 at 06:17

4 Answers4

3

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>
Community
  • 1
  • 1
skuroda
  • 19,514
  • 4
  • 50
  • 34
  • Thanks! Now I see `source.gs` in the status bar when pressing CTRL+ALT+SHIFT+P in a _*.gs_ file. The build, however, does not work. I have it set to Automatic and works for Ruby, Python, etc., but not for GolfScript. – Cristian Lupascu Jul 17 '13 at 06:18
  • Alright, I got it to work; I replaced `"selector": ["source.gs"]` with `"selector": "source.gs"` in the _*.sublime-build_ file – Cristian Lupascu Jul 17 '13 at 06:22
  • Glad you got everything figured out. I only suggested the User folder to make it easier if you had to move machines (only one directory to copy). I don't think the actual name of the file matters. So long as it has a `.tmLanguage` extension. – skuroda Jul 18 '13 at 03:01
  • Actually I think the location of the `.tmLanguage` file matters. I triend to move it and Sublime kept complaining. Anyway, I greatly appreciate your help. I had been stuck on this for a very long time. – Cristian Lupascu Jul 18 '13 at 06:50
  • I have a bunch of custom defined highlighters in `User/Syntax/`. But so long as you have it working, it doesn't matter :) – skuroda Jul 19 '13 at 02:27
1

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.

OdatNurd
  • 21,371
  • 3
  • 50
  • 68
  • Thank you for your answer. It seems that `file_patterns` is not recognized by ST2, is it? – Basj Jan 20 '21 at 19:35
  • No, it was added in ST3. If you're using ST2 apart from what the other answers mention regarding making a fake syntax. Other options include custom key bindings, but ST2 also doesn't have the enhanced `build` command that would allow you to trigger it for a different build. – OdatNurd Jan 20 '21 at 19:41
  • Thank you for your answer @OdatNurd! – Basj Jan 20 '21 at 19:43
  • For ST2 @OdatNurd, instead of using a `.sublime-build` + `.tmLanguage`, is it possible to use only a `.sublime-build` and the method from https://stackoverflow.com/questions/8088475/how-to-customise-file-type-to-syntax-associations-in-sublime-text or from https://stackoverflow.com/questions/7574502/set-default-syntax-to-different-filetype-in-sublime-text-2? I haven't been able to do it yet. – Basj Jan 24 '21 at 09:11
  • @Basj Depends on what you're trying to do; it would associate an extension with a file type, but all features of that language would the be enabled. For example, `.stuff` could be made to be Python (`.py`), so Python builds would work, but it would also have Python snippets, be syntax highlighted as Python, etc. The idea of adding a stub language is meant to have a distinct `scope` so that you can trigger a build without all the other features of the language following. – OdatNurd Jan 24 '21 at 22:46
0

We can use @skuroda's method but simplified: it seems that patterns, uuid is not necessary.

Create a golfScript.tmLanguage:

<?xml version="1.0" encoding="UTF-8" ?>
<plist version="1.0">
    <dict>
        <key>name</key>
        <string>GolfScript</string>
        <key>scopeName</key>
        <string>source.gs</string>
        <key>fileTypes</key>
        <array>
            <string>gs</string>
        </array>
    </dict>
</plist>
Basj
  • 41,386
  • 99
  • 383
  • 673
-1

Selector may be wrong. Try using embedding.gs instead of source.gs

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