Search code examples
sublimetext3sublime-text-plugin

Ship default sublime-settings with package


In my SublimeText package, I include a file BlameHighlight.sublime-settings. During testing, I link from ~/Library/Application\ Support/Sublime\ Text\ 3/Packages to this directory, and the changes to this file takes effect.

I also include a Menu item that points to ${packages}/User/BlameHighlight.sublime-settings. When I use the menu, I see a completely blank file.

How can I use my version of BlameHighlight.sublime-settings as the default template for ${packages}/User/BlameHighlight.sublime-settings?


Solution

  • I would recommend to use the same strategy as every other package and not automatically create a copy of the default settings file. Not because I think its better, but because I think the user experience should not differ between the packages.

    However as MattDMo stated you have to write our own plugin for this. At least for ST3 this is pretty straight forward:

    import os, sublime_plugin, sublime
    
    class CopyUserSettingsCommand(sublime_plugin.WindowCommand):
        def run(self, package_name, settings_name):
            file_path = os.path.join(sublime.packages_path(), "User", settings_name)
            if not os.path.exists(file_path):
                try:
                    content = sublime.load_resource("Packages/{0}/{1}".format(package_name, settings_name))
                    with open(file_path, "w") as f:
                        f.write(content)
                except:
                    print("Error copying default settings.")
            self.window.open_file(file_path)
    

    Just copy this into a python file in your package and insert in the menu entry:

    // ...
    {
        "command": "copy_user_settings",
        "args": {
            "package_name": "BlameHighlight",
            "settings_name": "BlameHighlight.sublime-settings"
        },
        "caption": "Settings – User"
    },
    // ...