One of the code conventions at work is to include a padding of one space inside square brackets and parenthesis. So list[ index ]
, not list[index]
. Adding those spaces can be annoying, so I tried writing a snippet:
# name: Bracket
# key: [
# --
[ ${1:exp} ]
This works when the opening square bracket is preceded by a space or beginning of a line, but not when its preceded by an identifier. I think one way to do this would be to have the trigger be a regular expression:
# key: "[:ascii:]"[
Or something like that. Is this even possible? Or is there some other clever way to make this work? I'm writing this for python, but I think that shouldn't matter.
Thanks!
Regex triggers are a powerful alternative to regular ones, with a few extra features: They let you define triggers using a full-blown regex syntax, enabling use-cases that would be inconvenient or even impossible with regular triggers.
$ means "Match the end of the string" (the position after the last character in the string).
The "matches regex" option allows you to implement regular expressions in Google Tag Manager for matching text patterns. Regex is useful for expressing more complex types of rules such as "fire the tag when the URL starts with A and ends in B or C."
I don't know about YASnippet, but what you want can easily be achieved using autopair (which can be installed via the packaging system):
(defun autopair-space-after-bracket (action pair pos-before)
(when (and (eq action 'opening)
(eq pair ?\]))
(insert " ")
(backward-char)))
(setq autopair-handle-action-fns (list #'autopair-default-handle-action
#'autopair-space-after-bracket))
Using this, and starting from the following situation (|
marks point position):
list|
then inserting [
yields:
list[ | ]
I too have had to deal with such a coding standard. My initial solution was identical to @Francesco's based on autopair.
However, I actually wanted to remove this padding in some cases, so made this minor-mode to be more flexible. I definitely recommend using it along with some kind of paired delimiter insertion tool (my favorite is autopair).
See: delim-pad
Going down your original track of using yasnippet ... ( BTW autopair and yasnippet are both written by João Távora, both are very powerful and flexible. kudos to that guy! )
Even if you got "[" recognized as a key, you would still have to keep pressing the trigger key to expand the snippet. That can get tired pretty quickly.
Yasnippet also allows you to bind snippets to keys directly, so this will work:
# -*- mode: snippet -*-
# name: beginsquare
# binding: [
# --
[ $0 ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With