Snippets | Quick, Powerful and Customizable Code Templates

Snippets

Customizable shortcuts inspired by VS Code Snippets for Roblox Studio


Plugin | Demo | User Guide (soon)

(Plugin contains some documentation out of the box.)


Features

  • Easy to use snippet editor
  • Built-in example templates with instructions
  • DockWidget UI
  • Simplistic user interface
  • Lookbehinds/Lookaheads for arguments passed to the snippet
  • Whitespace preservation for snippets in the middle of code blocks

FAQ

Click to view FAQ

Can I make arguments to snippets optional?

Unfortunately, it is currently not possible to make optional arguments with user defined snippets. It is possible to do internally, but unless you know what you are doing I do not recommend trying to implement it yourself. The internals do not have as much documentation.

Why does my cursor position get reset when I input a snippet?

Due to the limitations of Roblox Studio, there is no way for a plugin to set the position of the cursor in the script editor. We would love to make the cursor position properly but it is not possible at the moment.

Can I edit the default snippets?

For the most part, yes! The only exception to this rule is you cannot edit special snippets like eval or go because they are implemented differently internally.

Can I disable the use of snippets in my script?

Indeed you can. If you wish to disable all snippets for any specific script, just add an attribute named "snippetLocked" to the script and set the value to anything that isnt falsey.

Why Snippets?

While I was researching other implementations of similar modules, I came across this plugin by A1rfoil. To be clear, I think the plugin is good but it has some pretty obvious issues which are solved by Snippets.

The worst issue with that plugin in my opinion is the lack of arguments to the snippet code. It supports basic syntax for things like the time but its not flexible enough to be very useful and as far as I’m aware there is no way to define your own replacements.

Snippets completely solves this issue with the use of Lookaheads and Lookbehinds. Your snippet can take any number of arguments and you can use them anywhere in the snippet via $kn where n is the index of the argument. This allows for some pretty interesting templates like the included class template. The only limitation is that you cannot make optional arguments.

Snippets also implements some non standard snippets that don’t follow the simplistic find → replace idea behind the rest. They have much more utility. They aren’t user creatable (mainly because it would be a pain to serialize user functions + limitations with upvalues) but as a result, they are much higher level. As an example, if I use the snippet:

--(this just uses loadstring internally so you can put any valid lua code here)
--@eval {2 + 2}

It would actually output 4 to the console.

While the utility snippets aren’t exactly the most useful things all the time I think they are pretty fun and can speed up how fast you navigate Studio.


Credits

  • @Fifkee UI / Programming
  • @ArtBlart Additional Programming
  • @ZappyArt Thumbnail / Banner Art

  • Honorable Mentions
    • @cowsoncows Didn’t Contribute But I Like His Name
    • @kisty1 “My name’s Deez”
    • @Raccoonyz “dont add my devforum username”
    • @Jiramide Spelunky 2 Caveman Enthusiast
29 Likes

This reminds me of UserSnippits in VSCode. Looks very useful; thank you for the plugin!

3 Likes

There’s also Code Templates which seems to do a similar job.

2 Likes

Ooh, thanks for bringing this to my attention. After doing some surface research into the plugin it seems to have the same shortcomings as the one I linked before. It has some amount of variables using $VARNAME$ syntax but is too rigid to be useful for snippets. It mostly seems useful for watermarking scripts and basic find and replace stuff.

1 Like

Really excited about this! Snippets was the biggest thing I wanted from vs code so psyched to see this plugin. Thanks so much for making!

I’ve made some notes from initial use for feedback. I only ran into two major issues:
– Snippets don’t save once a place is closed.
– Snippets broke when I opened a new place with a current place/snippets already open.
– Edit: I added several of my own snippets directly into the json file to get them to save. The plugin loads them into the gui correctly but has otherwise stopped working entirely.

Some other notes from initial use:

  • Snippets don’t replicate across places.
  • Using the plugin generates a ton of discarded scripts in Drafts when Team Create is enabled. (Not a huge deal just noting it)
  • Snippets don’t work unless the plugin is actively toggled on
  • The plugin automatically closes when you edit a snippet, which then disables snippet functionality. Spent quite a while confused on this :smiley:

Feature Suggestion: Ability to edit snippet trigger (–@)

2 Likes

The fact that user created snippets aren’t saving for you is incredibly weird. I’ve never run into this issue which makes me think that its an issue with team create? The plugin also works for me when not actively selected. I’ll look into fixing compatibility issues in v1.2. In the meantime, can you provide any error information if possible? You might be able to try deleting settings.json entirely and relaunching the plugin.

As for the suggestion, I already have this on the roadmap. Hopefully this should be implemented by the next update but I mainly want to focus on fixing preexisting issues before adding new features.

Also, if you encounter issues where snippets seem to be replacing slowly just know this isn’t something I can remedy. Roblox released a major update to the editor 24 hours before I intended to release the plugin and I have been scrambling to fix things as I discover them. They specifically made it so script.Source only updates after you stop typing as opposed to after every keystroke.

Oh wow that’s a huge bummer on the release timing :(.
I’m a bit slammed right now but I’ll mess with it again when I get the chance and let you know what I find. I had updated the trigger in the module manually but that worked until I tried adding my own snippets to the json. I’ll try to delete the json and see if it fixes.

Hi Art - I tried reinstalling and still no saving. Perhaps I’m doing it wrong?

Feel free to reach out on Discord @TheAlchemist#2121 - interested in keeping this rolling

This plugin is great, there are some things annoying me, though. You have to re-create the snippet if you want to change something about the snippet, like add a new parameter.

eiHas5nfcS
I have a few pet peeves but unfortunately, I don’t know if it is possible to fix them. You can’t specify things to be pasted at the top of the script (like the table in this gif, I would need to manually paste it at the top). The only alternative to that is just to add another parameter to the snippet that contains the table (or variable), which works. The plugin is great after you get the hang of it a bit. Will documentation ever be released?

Edit: You also would need to add a space at the end of every snippet you make, otherwise the snippet would paste before you could type in the last parameter. Would be nice if the plugin automatically required a space at the end of the comment if there was no space specified at the end of the snippet command.

Hey there folks! It’s been a long time since my last major update to this plugin but I promise its not dead! I’ve been very busy with school, but I am close to graduation so my workload has dropped off significantly. I have some extra time on my hands and I decided to push some much needed updates to the plugin.

New Features / Changes

For starters, you can now define custom prefixes for newly created snippets! I saw this requested in a few places so I decided to implement it. At the current moment you can’t edit the prefixes of already created snippets, so you will need to recreate any snippets you want custom prefixes on. However, I will add a button in the main menu to do it eventually.

Another missing feature that I stupidly left out was the ability to open the plugin while in the script editor. Oops. It is now able to be toggled while you have a script open which means you can have it docked somewhere while the editor is open. Other QOL stuff includes escapable lookaheads / lookbehinds. All you have to do to escape them is prefix it with another “$”. As an example: $$k2. There is also some new built-ins.

These new built-ins include --:!rename {to} which renames the current script you have open to whatever you specified in to. --:!watermark {description}, which adds a “watermark” to the script which contains what you specified in description as well as your Roblox username and today’s date in dd/mm/yyyy format. Lastly, I added --:!lock which prevents snippets from working on the current script. You may have also noticed the prefix for built-ins has changed. It is now --:! as requested by a friend to reflect Vim’s shell command syntax. If this is a too much of a bother I am willing to revert the change.

I also fixed some long standing bugs with pattern matching and made some code slightly more efficient but there’s not much to talk about there.

Current Issues

As it stands, this is still unusable in Team Create when script drafts are on. This is an issue that I cannot bypass as the source of the script is not actually updated apparently so I can’t even hack my way around it if I wanted to.

There is also the issue of snippets not saving correctly. I think I have an idea of what is causing this and I am actively working on a fix! I hope to see any snippet loss completely squashed soon but we will see. Plugins and their documentation aren’t exactly reliable so a lot of this is trial and error. Let me know if you experience any progress loss and I will try to help you.

Future plans

For starters, I have no idea how far away the things on this roadmap will be. I will be busy with work and getting ready for college this summer so my free time will likely be limited. Despite this, I still want to be transparent about my goals for this plugin so here I am!

My immediate goals are to get rid of any bugs and polish up what I have currently. There is a lot of code that is everywhere because multiple people have worked on this project (Me and Noire) so a lot of the code is just plain weird.

In terms of features that I want to add, I am still open to suggestion and criticism. The plugin is in a state where it is usable and has some nice features but is also still buggy and underdeveloped in some places. I specifically want to clean up some UI and make the plugin more user friendly.

The biggest goal that I cannot put an ETA on is the user guide. I really want to get that done so I can showcase just how powerful snippets can be, but I am not great at writing. Also UX isn’t really my thing, so it may take a while. I do promise it will get done eventually though. I just don’t want to do a half job and leave you more confused after going through the user guide than before.

To wrap things up, I am really excited to keep working on this but as I stated before I will likely be short on time so I can make no promises on time frames. I’m glad to see some people getting some mileage out of this plugin though. Let me know what you think about Snippets v1.2.

1 Like