(lambda () (if (buffer-substring yas-snippet-beg yas-snippet-end)
yas-after-exit-snippet-hook runs in an environment where
yas-snippet-end are defined. These give the beginning and end points (i.e. region) of the inserted snippet.
buffer-substring returns the contents of a given region within the current buffer. We can use this with
yas-snippet-end. If the snippet worked, then there will be text in the region. We expect the snippet to either work or fail gracefully, not defining the snippet region or running the hook. Anything non-nil evaluates to
t. So, when we insert a snippet, the conditional will trigger and run the toggle.
Since it's unlikely we'd use this functionality anywhere else, there's no point defining an explicit function. The use of
lambda creates an anonymous function. This function is added to the hook. When the hook is called, our function is created, called, and destroyed.
Another possibility is to search for the string explicitly.
(lambda () (if (string-match-p (regexp-quote "import ipdb; ipdb.set_trace()") (thing-at-point 'line t))
This code checks for the
thing-at-point on the current line. A quirk is that
(thing-at-point 'line) returns a string with a newline at the end. So, instead of matching against the snippet "character-for-character", we can match it with a regex and accept any line containing the snippet.