Sublime Forum

Any plugins for changing per-project settings by extension?

#1

Howdy, I’m a new user to Sublime. I’ve only been using it for a couple of days but I’m really digging how everything fits together. Currently I’m working through getting things set up (snippets, etc).

The crux of my “problem” (such as it is) is that I work with both Objective-C projects as well as C/C++ projects. I’m sure you know where this is heading (pun!); By default *.h is associated with C++, which makes the syntax coloring not look right for Objective-C. My searches and what I’ve learned about how settings work would indicate that this isn’t a setting that can be overridden on a per-project basis because it needs to exist in a file with a specific name.

However, from looking at the API reference it looks like the on_new()/on_load()/on_post_save() events might be useful along with a per-project setting that would tell them to examine the extension of the file being created/loaded/saved and, if there is an appropriate project setting, change the syntax.

Is that something that’s actually viable? If so, it seems like something that might already have been written, but so far my searches have come up empty, so any help there would be appreciated as well.

0 Likes

#2

I’ve yet to find a plugin that’s already made that does something similar to this (my google-fu may be failing me due to lack of correct terminology, though).

However, I got my feet wet with some plugin stuff for some other features I needed to include (e.g. something that inserts a pre-formatted header comment that includes information fished out of several files) so I figured I’d give this one a go as well.

For all of the projects that require this functionality, I include the following in my project file:

"settings":
{
    "datamill": true
}

I’m using this as part of the context for several key bindings so that they’re only active while projects of this variety are active. As such, I reused that key and came up with the following plugin code (excuse the poor style, Python is not usually my jam):

import sublime, sublime_plugin

class ObjCSyntaxSwap (sublime_plugin.EventListener):
    # Check the provided view; if the file there is a header file that is not 
    # already marked as Objective-C AND the setting that indicates that we want 
    # to open headers as Objective-C is set, then set the syntax for the 
    # buffer; otherwise do nothing.
    def make_objc (self, view):
        isDataMill = view.settings ().get ('datamill', False)
        syntax     = view.settings ().get ('syntax')
        extension  = view.window ().extract_variables ()['file_extension']

        if isDataMill == True and extension == 'h' and "Objective-C" not in syntax:
            view.set_syntax_file ('Packages/Objective-C/Objective-C.sublime-syntax')

    # A file has been loaded
    def on_load (self, view):
        self.make_objc (view)

    # A file has been saved
    def on_post_save (self, view):
        self.make_objc (view)

This seems to do the business nicely. I thought I’d leave this here for reference in case it’s useful to anyone else that wants to do the something similar.

1 Like