1 ;;; company-yasnippet.el --- company-mode completion back-end for Yasnippet
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
5 ;; Author: Dmitry Gutov
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 (defun company-yasnippet--candidates (prefix)
33 (let ((keyhash (yas--table-hash table))
38 (when (and (stringp key)
39 (string-prefix-p prefix key))
41 (lambda (name template)
45 'yas-template template)
50 (yas--get-snippet-tables)))
53 (defun company-yasnippet (command &optional arg &rest ignore)
54 "`company-mode' back-end for `yasnippet'.
56 This back-end is supposed to be used in a particular way:
58 * In a buffer-local value of `company-backends'.
59 * Grouped with a backend or several that provide actual text completions.
61 Neither condition is mandatory, but as long as there are snippets defined
62 for the current major mode, this back-end will always shadow back-ends that
63 come after it. So any other back-ends intended to be used in the current
64 buffer should be grouped with it. Example config:
66 (add-hook 'js-mode-hook
68 (set (make-local-variable 'company-backends)
69 '((company-dabbrev-code company-yasnippet))))"
70 (interactive (list 'interactive))
72 (interactive (company-begin-backend 'company-yasnippet))
74 ;; Should probably use `yas--current-key', but that's bound to be slower.
75 ;; How many trigger keys start with non-symbol characters anyway?
77 (company-grab-symbol)))
78 (annotation (concat " -> " (get-text-property 0 'yas-annotation arg)))
79 (candidates (company-yasnippet--candidates arg))
81 (let ((template (get-text-property 0 'yas-template arg)))
82 (yas-expand-snippet (yas--template-content template)
83 (- (point) (length arg))
85 (yas--template-expand-env template))))))
87 (provide 'company-yasnippet)
88 ;;; company-yasnippet.el ends here