Sublime Forum

Dynamic snippets

#1

Hello.
I wrote the following snippets in order to to auto-complete common idioms of python unittests:

ae    self.assertEqual(a, b)
at    self.assertTrue(x)
ar    self.assertRaises(exc, fun)

In some projects though I use pytest rather than unittest, so I would like to produce this result instead:

ae    assert a == 0
at    assert x
ar    with pytest.raises(exc): fun()

It seems to me there is no way to use snippets in such a way, correct? What could be a strategy to make snippets a bit less static and more dynamic? I could imagine tons of other uses cases other than just this one.

0 Likes

#2

An example of how to do this can be found in the (currently unreleased) package linked below, which augments built in snippets with a custom command that can be invoked via autocomplete.

It allows snippets to include a glob key so that you can make snippets apply not only by scope but also by filename; so if you happen to use a different filename or path, that would be one way.

it also allows you to specify your own programmatic variables (the defaults are DATE, CLIPBOARD and BUZZWORD, so you could use that to make a variable that expans out to different text based on contextual information that the plugin you write can gather.

It also allows you to specify options for a variable expansion, so you could have it prompt you at expansion time (which is done via a quick panel in this example) to pick the style you want.

2 Likes