Skip to content
MiniApolloPublic

About

VSCode/LSP snippet support for Emacs with Yasnippet.

Topics

Resources

Stars

15 stars

Watchers

0 watching

Forks

Latest commit

 

History

234 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snippy.el

MELPA MELPA Stable Tag License: GPL-3.0-or-later

VSCode/LSP snippet support for Emacs with Yasnippet.

main-demo.mp4

Table of Contents

Introduction

Snippy is a snippet translation and completion engine that uses YASnippet under the hood.

This package translates LSP/VSCode-style snippets into YASnippet format, allowing you to use all modern, external snippets seamlessly, since YASnippet doesn't fully support the entire LSP specification.

It can optionally fix LSP snippets that Eglot retrieves from servers (e.g., Java), working alongside YASnippet so you can use both simultaneously.

The Default Snippets collection is Friendly Snippets.

Features

Specification

Automatic mode detection

autodetection.mp4

Tabstops

tabstops.mp4

Placeholders

placeholders.mp4

Choices

choices.mp4

Variables

vars.mp4

All variables of the specification are supported, including defaults (${TM_SELECTED_TEXT:default}):

  • Selection, line and word: TM_SELECTED_TEXT, TM_CURRENT_LINE, TM_CURRENT_WORD, TM_LINE_INDEX, TM_LINE_NUMBER
  • Files: TM_FILENAME, TM_FILENAME_BASE, TM_DIRECTORY, TM_DIRECTORY_BASE, TM_FILEPATH, RELATIVE_FILEPATH
  • Workspace (uses project.el): WORKSPACE_NAME, WORKSPACE_FOLDER
  • Clipboard (kill ring): CLIPBOARD
  • Dates: CURRENT_YEAR, CURRENT_YEAR_SHORT, CURRENT_MONTH, CURRENT_MONTH_NAME, CURRENT_MONTH_NAME_SHORT, CURRENT_DATE, CURRENT_DAY_NAME, CURRENT_DAY_NAME_SHORT, CURRENT_HOUR, CURRENT_MINUTE, CURRENT_SECOND, CURRENT_SECONDS_UNIX, CURRENT_TIMEZONE_OFFSET
  • Random: RANDOM, RANDOM_HEX, UUID
  • Comments (from the buffer's comment syntax): LINE_COMMENT, BLOCK_COMMENT_START, BLOCK_COMMENT_END

Variable values are inserted as plain text, and backticks in snippets stay literal (they are not evaluated as Elisp like in Yasnippet).

Fix LSP server snippets

Enable: snippy-fix-lsp-snippet-mode

Works with Eglot. Snippets that Eglot receives from the server are translated before Yasnippet expands them. Snippy's own snippets and your Yasnippet snippets are not affected.

Installation

Requirements:

  • Emacs 30.1 or newer
  • Yasnippet 0.14.0 or newer
  • git (for installing and updating snippets)

Snippy is available on MELPA.

(use-package snippy
  :ensure t
  :hook (after-init . global-snippy-minor-mode)
  :custom
  (snippy-global-languages '("global")) ; Recommended
  ;; Optional
  ;; (snippy-install-dir (expand-file-name <Your location>))
  ;; Use different snippet collections
  ;; (snippy-source '("Your git repo" . "my-snippets-dir"))
  :config
  (snippy-install-or-update-snippets) ; Autoupdate git repo
  ;; (snippy-fix-lsp-snippet-mode) ; Fixes lsp server snippets (e.g java lsp)
  (add-hook 'completion-at-point-functions #'snippy-capf)) ; Or merge them with Yasnippet or eglot capf

Using special characters for snippets like !,@

Prefix characters like !, @ don't work with yasnippet out of the box.

With Snippy it does work, but if you are merging completion backends make sure snippy is the first capf that dictates boundaries for the completion.

Other completion backends do not include special characters in the prefix.

If you want to use special characters in Yasnippet-snippets use this to fix it:

;; This is not needed for Snippy.el
(setq yas-key-syntaxes '("w_" "w_." "^ ")) ; Fixes Yasnippet-snippets special character usage.

Now you can use snippets like !cdr which is defined in yasnippet-snippets package.

Configuration

Package Variables

  • snippy-install-dir: Directory where to install/clone snippets.
  • snippy-source: The source details for downloading snippets (Both needed).
    • Repository URL
    • Directory Name (Cloned repo dir name)
  • snippy-global-languages: List of languages to enable globally across all major modes.

You can use any language you like.

;; Global Variable names
(setq snippy-global-languages '("global" "license"))

snippy-emacs-to-vscode-lang-alist:

Alist mapping for Emacs major modes to VSCode language names.

You can see all the languages inside the package.json file in the Friendly Snippets repository. There are some language snippets that are optional. To use them add it to the list.

You can also add more remaps if any language is missing from the list (or you can just make a pull request). When adding language remaps the first is the only one that is used in the list. The lookup uses the exact major-mode, so derived modes need their own entry.

You also need to include all vscode snippet names not just the new one's name.

Optional remap names:

  • c++-mode, c++-ts-mode: "unreal"
  • csharp-mode, csharp-ts-mode: "unity"
  • html-mode: "djangohtml", "htmldjango"
  • web-mode: "angular", "twig"
  • python-mode, python-ts-mode: "django", "django-rest"

Modes that don't have a specific major-mode:

  • blade
  • eelixir
  • rmd
;; Choose the remaps you want to use
(add-to-list 'snippy-emacs-to-vscode-lang-alist '(super-cool-mode "coolang" "cool-doc")) ;  Example new dummy-mode

;; Add to an existing mode (You need to include all vscode snippet names not just the new one)
(add-to-list 'snippy-emacs-to-vscode-lang-alist '(c++-mode "cpp" "cppdoc" "unreal"))
(add-to-list 'snippy-emacs-to-vscode-lang-alist '(c++-ts-mode "cpp" "cppdoc" "unreal"))

Functions

  • snippy-install-or-update-snippets: Install (git clone) or update (git pull, runs in the background) the snippet repo in snippy-install-dir. Cached snippets are cleared when it finishes.
  • snippy-refresh-snippets: Force an update on the snippets for the current buffer. Called interactively it clears the cache and rereads the snippet files from disk.
  • snippy-expand: Expand snippet by prefix (needs snippy-minor-mode in the buffer).
  • snippy-expand-snippet: Expand VSCode snippet body.
  • snippy-transform-snippet-body: Convert a VSCode snippet body string to Yasnippet syntax without expanding it.
  • snippy-capf: Complete with snippy at point. Called interactively (e.g. bound to a key) it completes with snippy's snippets only.
;; Example: complete only snippets with a key
(keymap-global-set "C-c s" #'snippy-capf)

Modes

  • snippy-minor-mode: Toggle snippy in the current buffer.
  • global-snippy-minor-mode: Toggle snippy in all buffers.
  • snippy-fix-lsp-snippet-mode: Toggle LSP server snippet fix globally.

global-snippy-minor-mode enables snippy in buffers derived from prog-mode, text-mode or conf-mode, and in every mode listed in snippy-emacs-to-vscode-lang-alist.

Known limitations

  • Transforms (${1/regex/format/flags}, ${TM_FILENAME/regex/format/}) are not applied. The transform is dropped and the plain field or variable value is used.
  • An unknown variable without a default is left as text ($FOO) instead of becoming a placeholder.
  • A choice field stays empty until you move to it; VS Code shows the first option right away.
  • A variable inside a variable's default is not resolved: ${TM_SELECTED_TEXT:$TM_LINE_NUMBER} inserts the text $TM_LINE_NUMBER instead of the line number.

Development

Run the tests (ERT) with Yasnippet on the load path:

emacs -batch -L /path/to/yasnippet -L . -l test/snippy-tests.el -f ert-run-tests-batch-and-exit

The tests use temporary directories and never touch your installed snippets.

About

VSCode/LSP snippet support for Emacs with Yasnippet.

Topics

Resources

Stars

15 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages