Sublime Forum

Snippet command to codefold

#1

Hello, (No clue which thread is the best for this one)
I’ve been developing snippets lately and I was wondering if there was any support for application commands.
I am currently creating an “import header” snippet in Java but I would like for it to automatically fold the code so instead of
//start import {
import java.lang.Math;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;
import…
//End import }
I would just end up with
//start import {
//end import }
Anything like this in dev yet? It would be a cool feature.
Thanks.

0 Likes

#2

Package Control lists a package called Auto Fold that seems like it would fit what you want to do.

Sublime Text 3 plugin for automatically folding tags, attributes or any configurable string matching a regular expression

1 Like

#3

That looks like a good package, I’ll put it to use in another context, however, my main concern for this is the fact that I will be distributing my snippets.So I really would like something that I can reference inside the snippet that causes it to fold the code since I don’t know if the users of the snippets will have the packages that I have, nor do I know if they have the package preferences that I do. There’s probably not an option for this [yet].
Kind of like the environment variables that can be listed out in one.
I guess to put it into pseudocode would be

Get starting line number [set to begin]
Insert text from snippet content
(Snippet ending would be set to the end of the last line using $0)
Get ending line number [set to end]
Send command to fold from begin to end

Obviously, this would not be useful in very many cases since you still would have to edit certain things so it’s definitely no big deal. But for what I am coding in if it just closes to two lines on insertion instead of around 13 for everyone that gets my distributed snips, it’s just better than staring at 13 lines of imports.

0 Likes

#4

Ahh, I get you. For distribution that is less than desirable I would agree, unless you leave it up to the person using the snippets to set up their own environment the way they want it to be. I haven’t done much Java in sublime yet or I probably would have already worked on something similar to this. :slight_smile:

Snippets can’t invoke commands (that I know of), but commands can invoke snippets. So you could potentially distribute your snippets in a package along with a folding plugin, a custom command that invokes the snippet and then folds, and a key binding to enable it.

Another option might be an on_modified event handler that catches the snippet insertion and autofolds it.

Either way I think you’d be in for distributing a package of some sort and not just a snippet.

0 Likes

#5

More than likely you are right about making a package.
I’ll see if I can come up with anything, I just started messing with it.
Thanks for your input.

0 Likes