]> code.delx.au - gnu-emacs-elpa/blob - company-yasnippet.el
company-yasnippet: autoload, to make it easy to use separately
[gnu-emacs-elpa] / company-yasnippet.el
1 ;;; company-yasnippet.el --- company-mode completion back-end for Yasnippet
2
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
4
5 ;; Author: Dmitry Gutov
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22
23 ;;; Commentary:
24 ;;
25
26 ;;; Code:
27
28 (require 'yasnippet)
29
30 (defun company-yasnippet--candidates (prefix)
31 (mapcan
32 (lambda (table)
33 (let ((keyhash (yas--table-hash table))
34 res)
35 (when keyhash
36 (maphash
37 (lambda (key value)
38 (when (and (stringp key)
39 (string-prefix-p prefix key))
40 (maphash
41 (lambda (name template)
42 (push
43 (propertize key
44 'yas-annotation name
45 'yas-template template)
46 res))
47 value)))
48 keyhash))
49 res))
50 (yas--get-snippet-tables)))
51
52 ;;;###autoload
53 (defun company-yasnippet (command &optional arg &rest ignore)
54 "`company-mode' back-end for `yasnippet'.
55
56 This back-end is supposed to be used in a particular way:
57
58 * In a buffer-local value of `company-backends'.
59 * Grouped with a backend or several that provide actual text completions.
60
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:
65
66 (add-hook 'js-mode-hook
67 (lambda ()
68 (set (make-local-variable 'company-backends)
69 '((company-dabbrev-code company-yasnippet))))"
70 (interactive (list 'interactive))
71 (case command
72 (interactive (company-begin-backend 'company-yasnippet))
73 (prefix
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?
76 (and yas-minor-mode
77 (company-grab-symbol)))
78 (annotation (concat " -> " (get-text-property 0 'yas-annotation arg)))
79 (candidates (company-yasnippet--candidates arg))
80 (post-completion
81 (let ((template (get-text-property 0 'yas-template arg)))
82 (yas-expand-snippet (yas--template-content template)
83 (- (point) (length arg))
84 (point)
85 (yas--template-expand-env template))))))
86
87 (provide 'company-yasnippet)
88 ;;; company-yasnippet.el ends here