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")
36 (declare-function yas--warning "yasnippet")
38 (defun company-yasnippet--key-prefixes ()
39 ;; Mostly copied from `yas--templates-for-key-at-point'.
40 (defvar yas-key-syntaxes)
42 (let ((original (point))
43 (methods yas-key-syntaxes)
47 (unless (eq method (car methods))
49 (setq method (car methods))
50 (cond ((stringp method)
51 (skip-syntax-backward method)
52 (setq methods (cdr methods)))
54 (unless (eq (funcall method original)
56 (setq methods (cdr methods))))
58 (setq methods (cdr methods))
59 (yas--warning "Invalid element `%s' in `yas-key-syntaxes'" method)))
60 (let ((prefix (buffer-substring-no-properties (point) original)))
61 (unless (equal prefix (car prefixes))
62 (push prefix prefixes))))
65 (defun company-yasnippet--candidates (prefix)
66 ;; Process the prefixes in reverse: unlike Yasnippet, we look for prefix
67 ;; matches, so the longest prefix with any matches should be the most useful.
68 (cl-loop with tables = (yas--get-snippet-tables)
69 for key-prefix in (company-yasnippet--key-prefixes)
70 thereis (company-yasnippet--completions-for-prefix prefix
74 (defun company-yasnippet--completions-for-prefix (prefix key-prefix tables)
77 (let ((keyhash (yas--table-hash table))
82 (when (and (stringp key)
83 (string-prefix-p key-prefix key))
85 (lambda (name template)
87 (propertize (company-yasnippet--adjust-key key prefix key-prefix)
89 'yas-template template
90 'yas-prefix-offset (- (length key-prefix)
98 (defun company-yasnippet--adjust-key (key prefix key-prefix)
99 (let ((pl (length prefix))
100 (kpl (length key-prefix)))
105 (concat (substring prefix 0 (- pl kpl)) key))
107 (substring key (- kpl pl))))))
110 (defun company-yasnippet (command &optional arg &rest ignore)
111 "`company-mode' backend for `yasnippet'.
113 This backend should be used with care, because as long as there are
114 snippets defined for the current major mode, this backend will always
115 shadow backends that come after it. Recommended usages:
117 * In a buffer-local value of `company-backends', grouped with a backend or
118 several that provide actual text completions.
120 (add-hook 'js-mode-hook
122 (set (make-local-variable 'company-backends)
123 '((company-dabbrev-code company-yasnippet)))))
125 * After keyword `:with', grouped with other backends.
127 (push '(company-semantic :with company-yasnippet) company-backends)
129 * Not in `company-backends', just bound to a key.
131 (global-set-key (kbd \"C-c y\") 'company-yasnippet)
133 (interactive (list 'interactive))
135 (interactive (company-begin-backend 'company-yasnippet))
137 ;; Should probably use `yas--current-key', but that's bound to be slower.
138 ;; How many trigger keys start with non-symbol characters anyway?
139 (and (bound-and-true-p yas-minor-mode)
140 (company-grab-symbol)))
143 (unless company-tooltip-align-annotations " -> ")
144 (get-text-property 0 'yas-annotation arg)))
145 (candidates (company-yasnippet--candidates arg))
147 (let ((template (get-text-property 0 'yas-template arg))
148 (prefix-offset (get-text-property 0 'yas-prefix-offset arg)))
149 (yas-expand-snippet (yas--template-content template)
150 (- (point) (length arg) prefix-offset)
152 (yas--template-expand-env template))))))
154 (provide 'company-yasnippet)
155 ;;; company-yasnippet.el ends here