smjonas / snippet-converter.nvim

Bundle snippets from multiple sources and convert them to your format of choice.
Mozilla Public License 2.0
174 stars 4 forks source link

Add yasnippet converter #10

Closed IllustratedMan-code closed 1 year ago

IllustratedMan-code commented 2 years ago

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.

smjonas commented 2 years ago

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?

IllustratedMan-code commented 2 years ago

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!

smjonas commented 2 years ago

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 commented 2 years ago

Could you help me with the following: