snippet-converter.nvim icon indicating copy to clipboard operation
snippet-converter.nvim copied to clipboard

Add yasnippet converter

Open IllustratedMan-code opened this issue 3 years ago • 4 comments

I made an issue on the luasnip repo and was directed here.

Add support for yasnippet syntax. This would be great for snippet compatibility with emacs. Yasnippet collections exist and could provide a large number of snippets.

Syntax appears to be similar to textmate/lsp. Only problem I can see is that yasnippet uses emacs modes which would have to be converted to vim buffer types.

IllustratedMan-code avatar Oct 11 '22 18:10 IllustratedMan-code

That seems like a good snippet format to support in this plugin! Would you be interested in making a PR for the implementation? Also can you give an example of "emacs modes" in a snippet definition?

smjonas avatar Oct 12 '22 08:10 smjonas

An emacs major mode is just the terminology used for different languages. Major modes are analoguos to the &filetype variable in vim/neovim. This isn't actually part of the snippet, but a part of the directory structure of a snippet directory.

snippet_dir
|
|___ python-mode
|      |____ mysnip
|
|___ ess-mode 
       |____ anothersnip

Generally, the mode name is just the language followed by "mode", but in some cases like ess-mode (the mode name for the r language), the mode name won't line up with the language. This is furthur complicated by .yas-parents files which can allow snippets to be shared between modes.

A yasnippet generally looks something like this:

# -*- mode: snippet -*-
# name: function
# key: f
# group: definitions
# --
def ${1:fun}(${2:args}):
    $0

The first line is a mode annotation for emacs so emacs knows how to do syntax highlighting for the snippet file; this line isn't always present, but should be ignored. Some other "directives" aren't relavent to vim, like # group. Some are analogous to other snippet formats (key: f is identical to "prefix": "f" in vscode style snippets. The actual snippet begins after the # -- line.

Similar to how luasnip snippets can be written in pure lua, yasnippets can be written in elisp (a variant of lisp). These are defined when # type: command is in the header. I think trying to convert these is beyond the scope of snippet-converter.nvim. These snippets should just be ignored (maybe with a warning?).

In terms of a PR, I'm not the most confident in lua and I'm a little intimidated by your parser code to be honest. It would definitely be slow going. I can try to give it a go, but I would love to hear your development/debugging workflow. I've developed one neovim extension before and debugging was pretty awful.

I'm very much happy to test though!

IllustratedMan-code avatar Oct 12 '22 13:10 IllustratedMan-code

Thanks for your good explanation, that really helped me during the implementation!

These are defined when # type: command is in the header. I think trying to convert these is beyond the scope of snippet-converter.nvim. These snippets should just be ignored (maybe with a warning?).

That's a very good idea, I'll add this.

smjonas avatar Oct 12 '22 22:10 smjonas

Could you help me with the following:

  • can there only ever be one snippet per file? That would make the implementation simpler because I don't have to determine whether a line like # -*- mode: snippet -*- is part of the snippet body or part of the header of the next snippet

smjonas avatar Oct 14 '22 12:10 smjonas