Regular expression as a trigger
Asked Answered
A

3

6

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!

Asperity answered 4/1, 2013 at 0:32 Comment(1)
Just a wild guess, bout would removing the space in key: [ do anything? (So, make it: key:[)?Mutazilite
E
1

Minor mode solution

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

Using yasnippet

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 ]
Eckel answered 4/1, 2013 at 14:33 Comment(2)
Isn't this kind of a duplicate of my answer? Except for the binding part.Melda
Thank you so much for your answer. I've been using delim-pad + autopair for the past few days and its been working great!Asperity
N
4

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[ | ]

Napiform answered 4/1, 2013 at 7:46 Comment(2)
Considering the situation, I also believe this should/could be accomplished with autopair, and would be more logical than the bracket (where you have to press TAB).Melda
I agree with the usage of something other than YASnippet for this - and it definitely seems easier to do with autopair than electric pair.Omnidirectional
E
1

Minor mode solution

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

Using yasnippet

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 ]
Eckel answered 4/1, 2013 at 14:33 Comment(2)
Isn't this kind of a duplicate of my answer? Except for the binding part.Melda
Thank you so much for your answer. I've been using delim-pad + autopair for the past few days and its been working great!Asperity
M
0

The reason why this is not working, is because you are trying to expand on list[ as opposed to [ when something is before this square bracket. Yasnippet is "not your guy" I would say for creating an expansion on [. I'll show you at the end the more common use of yasnippet.

Considering that I would not really like to press the activation key (e.g. TAB) whenever I would type a bracket, I would go with the above answer from Francesco. If it helps, I also use autopair.

Furthermore, a good, or at least better way to use yasnippet is if you were indeed interested in using this only for list, or some situations which you are willing to define beforehand. Consider creating many snippets for code you use often and you'll be producing code really fast. Anyway, here is the example that I think suits your need for this situation:

list[ $0 ]

Bind it to either list or even shorter to l, whatever floats your boat.

Personally I would go with a combination of both autopair and yasnippet, they are both great. Good luck!

Copy paste:

# name: List
# key: list
# --
list[ $0 ]
Melda answered 4/1, 2013 at 9:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.