joaotavora / yasnippet

A template system for Emacs
http://joaotavora.github.io/yasnippet/
2.78k stars 313 forks source link

Adding support for regex as keys when expanding snippets #988

Open Zetagon opened 5 years ago

Zetagon commented 5 years ago

I read https://castel.dev/post/lecture-notes-1/ and wanted to replicate at least the simpler parts of the fraction snippet. Is it possible to easily write such snippets in yasnippet where the key is a regex?

In case it is not currently possible I have hacked together some elisp code using the interface provided that can do this but it is not very integrated into yasnippet. I have basically made a new keybinding that checks if a regex has matched. If it matched a corresponding snippet is expanded using yas-lookup-snippet and yas-expand-snippet. The configuration still happens in elisp code and not in a snippet buffer which is suboptimal.

Is anyone else interested in using regex as keys? If so I could with some help integrate this more into yasnippet and merge it.

Zetagon commented 2 years ago

I'm not working on this anymore, and don't intend to in the future. If anybody wants to work on it, feel free to do so.

rickalex21 commented 3 months ago

I needed this for react to simulate the way they do it in vscode with emmet. I ended up doing this:

# -*- mode: snippet -*-
# name: class name expansion
# key: .
# --
<div className="$1">$0</div>

Ideally I would like to do this as the key .someName and automatically expand to this:

<div className="someName">cursor-here</div>

I know this is how it works in ultisnips, given the power of elisp and yasnippet IDKY this is not a thing.