Millet Porridge

English version of https://corvo.myseu.cn

0%

Vsnips: Allows you to use Ultisnips in vscode

(news)2022-04-12, we support vscode web extension, and you can install it when you use vscode.dev or github.dev.

Here is the project: https://github.com/corvofeng/Vsnips

1
ext install corvofeng.Vsnips

Vsnips

Allows you to use Ultisnips in vscode.

It is under development, although I can’t guarantee that all ultisnips functions can be used in vscode, I will try my best. Please feel free to give an issue or pull requests.

Please: refer to the docs: https://vsnips.corvo.fun/

UltiSnips

For details of Ultisnips, please refer here.

ultisnips.

It’s easy to write an UltiSnips snippets, for example, in Python, I usually use import IPython; IPython.embed() for debugging. And you could write snippts like this:

python.snippets

1
2
3
4
5
6
snippet dbg "Use IPython to debug"
# ---------- XXX: Can't GIT add [START] ---------- #
import IPython
IPython.embed(using=False)
# ---------- XXX: Can't GIT add [END] ---------- #
endsnippet

The whole snippst is surrounded with snippet and endsnippet, while the dbg is the triger, and "Use IPython to debug" is description for this snippet. This is what it looks like when you use the snippet:

3

VSCode snippets

VSCode has it’s own implementation for snippets which lets you create your own. But I found it difficult to use and lacking in feature set, for example:

  1. If there is a multi-line snippet, json is not very well for storing.
  2. It’s hard for many PC sync the snippets. Because of this, there is already too many snippet extensions for different languages, like, C/C++ Snippets, Bootstrap 3 Snippets.

I do not mean that it’s bad for different language having their own extension. But you must admit that if you want to add snippets for a language, you need to learn how to write an extension and how to add snippets. and what’s most important is that if you are an vimmer, you had to write snippets for these two editors, which bothers me.

How to use Vsnips

This plugin based on the snippet api in VSCode, whether you have used UltiSnips or not, it’s easy to start. I have adapted some UltiSnips snippets to VSCode and used it by default.

If you have already use UltiSnips, and even have your own snippets, you can add own snippets dir in settings, and use the variable in VarFiles to set that.

1
2
3
4
5
6
7
8
9
{
"Vsnips.VarFiles": [
"/home/corvo/.vimrc",
"/home/corvo/.vim/common.vim",
],
"Vsnips.SnipsDir": [
"/home/corvo/.vim/UltiSnips"
]
}

When you give the variable like let g:snips_author="corvo", you could use it like snippets below.

1
2
3
4
5
6
7
8
9
10
11
12
snippet full_title "Python title fully"
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ts=4 sw=4 tw=99 et:

"""
@Date : `!v strftime("%B %d, %Y")`
@Author : `!v g:snips_author`

"""

endsnippet

Completed Tasks

  • Auto download the Ultisnips
  • Multi language completions support
  • Allow user add their own Ultisnips.
    • From Ultisnips to VSCode snippets.
    • Support strftime
    • Allow user define variables
    • Allow some functions(Rewrite by javascript)
  • Syntax highlight for snippets
  • Allow user define their own functions
  • Support autoDocstring for Python and TypeScript.
  • Add support for golang function comments.

Work in Progress

  • Support extends and priority in Ultisnips

May not work

The UltiSnips in vim could adjust the doc according to the python function argument. But the Vsnips is based on VSCode CompletionItemProvider, it may be hard to support this feature.

So, I change to another solution, after the user triggers the keys like v-doc, Vsnips could auto-generate the doc for the functions, Which will refer to autoDocstring.