Sublime Forum

How can I create overridable Snippets in my Package?

#1

I am building a Package wherein I have 3 snippets that I want my users to be able to override by including an identically-named snippet in their User Package. Note that I am NOT using the normal trigger mechanism to insert it. Instead, I am using them from within a plugin (because there is about 25 different ways to trigger it, but I need there to be only ONE snippet file) by calling

self.view.run_command("insert_snippet", args)

One of the arguments in the args dictionary is

    "name":  "path/to/snippet-file"

for example

    "name":  "Packages/ProComment/c_header.sublime-snippet"

I want my users to be able to use their own snippet files with this. I tried copying c_header.sublime-snippet to my User Package and altering it, but the original one in the package directory was still being used. I tried

  • “${packages}/ProComment/c_header.sublime-snippet”
  • “${packages}/c_header.sublime-snippet”
  • “${packages_path}/ProComment/c_header.sublime-snippet”
  • “${packages_path}/c_header.sublime-snippet”

and none of them work to get it to use the one in the User package. Of course

    "name":  "Packages/User/c_header.sublime-snippet"

works, but then doesn’t go back to the default snippet when the one in the User package is removed.

Is there some magic variable or token to include in the Path string to get Sublime Text to detect that there is an override snippet in the User package? Or do I need to “manually” (in the plugin code) check for the existence of the snippet file in the User package and use it if it is there, else drop back to the default path?

P.S. I have figured out how to manually check whether there is a user-override Snippet file present by using sublime.packages_path(), but I’d much rather use a magic variable like ${packages} in a single path submitted to the insert_snippet command if that is possible.

0 Likes

#2

True overrides for a snippet should be placed in the same package as where the snippet is defined, as opposed to in the User package. Then your plugin wouldn’t need to do anything special. But if your plugin requires that it be unpacked (hopefully it doesn’t - it all depends on whether you added a file to force package control to unpack it on installation?), then the user’s override would get replaced whenever package control upgrades the package.
Tip: the OverrideAudit plugin is a good way for a user to see and manage overrides

1 Like

#3

Awesome, Keith! Thank you for the clarification about how it works!

Is there any place in the docs that clearly explains how file overriding works for every Package Resource file type?

0 Likes

#4

https://docs.sublimetext.io/guide/extensibility/packages.html#interactions-between-packages-with-the-same-name

2 Likes

#5

Thank you very much!!

0 Likes