Sublime Forum

Calling plugins or other snippets from a snippet

#1

Is there any way to have the output of a plugin inserted into a snippet? What would the syntax of a plugin call be in a snippet?

Is there a way to have a different snippet included? Like having a license snippet, and then a class skeleton snippet that includes the license without having to duplicate the license portion.

I’ve searched the docs for this sort of thing, and come up empty. Please point me in the right direction if I’m missing something.

MPEDrummer

0 Likes

#2

I’m not entirely clear, but… It’s possible to use one snippet/ completion, while within a different snippet by setting

// Controls if auto complete is shown when snippet fields are active. // Only relevant if auto_complete_commit_on_tab is true. "auto_complete_with_fields": false,
to true.

A snippet can refer to selected text as $SELECTION, or can read TM_CURRENT_LINE and TM_CURRENT_WORD. I understand that it should also be possible to use clipboard content and/or system variables, but I haven’t pursued this. Perhaps setting the license text as a sys variable is something to explore?

Otherwise, it may require a (Python) TextCommand that inserts two snippets… :question:

0 Likes

#3

Docs: readthedocs.org/docs/sublime-tex … ppets.html

0 Likes

#4

Searching about ST environment variables is pointing me towards using ‘.sublime-options’ to create custom variables. But I figure this must refer to ‘.sublime-settings’ files :question:. So in, perhaps,

Packages\Default\Base File.sublime-settings

you might add a line

"THE_LICENSE": "You cannot use this.",

and then refer to this in a snippet as $THE_LICENSE. But I’m guessing :wink:

0 Likes

#5

Often thought it would be great if snippets were a template language with if/else/for/ include directives etc

0 Likes

#6

Apparently TextMate snippets can include “interpoloated shell code” (bash code):

<a href="` if $(pbpaste|wc -l) -eq 0 ]] then pbpaste else echo http://example.com/ fi `">$TM_SELECTED_TEXT</a>
but I guess ST doesn’t do this?

0 Likes

#7

Nope, it doesn’t support interpolated shell code. That would fix all the troubles.

I’ll have to check out that setting.

Thanks!

0 Likes

#8

Yeah, but that’s for generation of a static snippet.

I’m talking about being able to reference the tabstops from inside the template. MVC snippets.

Similar to how you can use regex transformations. But look who I’m talking to :smile:

0 Likes

#9

@castles Hi.

Could you elaborate? I’m curious, as it’s possible to read, and effectively count, the number of tab stops on the current line.

0 Likes

#10

@Castles has but a simple dream: to have dynamic snippets created/controlled by python. Is that too much to ask for?

0 Likes

#11

I’m saying the directives, where applicable, get evaluated as you type.

Instead of using twiddly, hard to read/tweak regexes you can use some templating language. I find that unless something is super easy to use, in heat of the moment I won’t use it.

0 Likes

#12

And snippets for input chained together via co-routines to create wizards with narry a damn dialogue or input panel in sight.

data = yield view.insert_snippet(a)
yield scheduled.pause
do_stuff_with(data)

0 Likes

#13

Could create a ‘sublime_framework’. Use ‘on_modified’ and ‘on_query_completions’. Could call snippets, but then use ‘on_modified’ to discard the text, but keep the variables.

[code]import sublime_framework

def MySublime(sublime_framework.KeyBinding):
def run(self, view, window):
char = view.cursor(-1)
some_text = view.curr_line
stuff = view.snip(“do_this”, discard = True)[/code]

0 Likes

#14

[quote=“mpedrummer”]I’ll have to check out that setting.

Thanks![/quote]

I also use the following key bindings:

[code] { “keys”: “ctrl+alt+right”], “command”: “insert_best_completion”, “args”: {“default”: “”, “exact”: true} }, // deleted “\t”
{ “keys”: “ctrl+alt+right”], “command”: “insert_best_completion”, “args”: {“default”: “”, “exact”: false}, // deleted “\t”
“context”:

		{ "key": "setting.tab_completion", "operator": "equal", "operand": true }
	]
},
{ "keys": "ctrl+alt+right"], "command": "replace_completion_with_next_completion", "context":
	
		{ "key": "last_command", "operator": "equal", "operand": "insert_best_completion" },
		{ "key": "setting.tab_completion", "operator": "equal", "operand": true }
	]
},
{ "keys": "ctrl+alt+left"], "command": "replace_completion_with_prev_completion", "context":
	
		{ "key": "last_command", "operator": "equal", "operand": "insert_best_completion" },
		{ "key": "setting.tab_completion", "operator": "equal", "operand": true }
	]
}[/code]

This allows me to use Tab within a Snippet, but use Ctrl-Alt-Right to trigger another Snippet/completion within it.

0 Likes

#15

Huh. I seem to be missing something (shocker!)

If I have snippet 1, triggered by mpedrummer-snippet-1, and I want to have it be part of snippet 2, mpedrummer-snippet-2, what should that inclusion look like? Changing the setting really didn’t alter the behavior in any way I noticed, which probably means we’re talking about different stuff and I don’t know enough to ask the right question.

0 Likes