1 ;;; company-yasnippet.el --- company-mode completion backend 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/>.
31 (declare-function yas--table-hash "yasnippet")
32 (declare-function yas--get-snippet-tables "yasnippet")
33 (declare-function yas-expand-snippet "yasnippet")
34 (declare-function yas--template-content "yasnippet")
35 (declare-function yas--template-expand-env "yasnippet")
37 (defun company-yasnippet--candidates (prefix)
40 (let ((keyhash (yas--table-hash table))
45 (when (and (stringp key)
46 (string-prefix-p prefix key))
48 (lambda (name template)
52 'yas-template template)
57 (yas--get-snippet-tables)))
60 (defun company-yasnippet (command &optional arg &rest ignore)
61 "`company-mode' backend for `yasnippet'.
63 This backend should be used with care, because as long as there are
64 snippets defined for the current major mode, this backend will always
65 shadow backends that come after it. Recommended usages:
67 * In a buffer-local value of `company-backends', grouped with a backend or
68 several that provide actual text completions.
70 (add-hook 'js-mode-hook
72 (set (make-local-variable 'company-backends)
73 '((company-dabbrev-code company-yasnippet)))))
75 * After keyword `:with', grouped with other backends.
77 (push '(company-semantic :with company-yasnippet) company-backends)
79 * Not in `company-backends', just bound to a key.
81 (global-set-key (kbd \"C-c y\") 'company-yasnippet)
83 (interactive (list 'interactive))
85 (interactive (company-begin-backend 'company-yasnippet))
87 ;; Should probably use `yas--current-key', but that's bound to be slower.
88 ;; How many trigger keys start with non-symbol characters anyway?
89 (and (bound-and-true-p yas-minor-mode)
90 (company-grab-symbol)))
93 (unless company-tooltip-align-annotations " -> ")
94 (get-text-property 0 'yas-annotation arg)))
95 (candidates (company-yasnippet--candidates arg))
97 (let ((template (get-text-property 0 'yas-template arg)))
98 (yas-expand-snippet (yas--template-content template)
99 (- (point) (length arg))
101 (yas--template-expand-env template))))))
103 (provide 'company-yasnippet)
104 ;;; company-yasnippet.el ends here