]> code.delx.au - gnu-emacs-elpa/blob - company-yasnippet.el
Support the keyword :with
[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 should be used with care, because as long as there are
57 snippets defined for the current major mode, this back-end will always
58 shadow back-ends that come after it. Recommended usages:
59
60 * In a buffer-local value of `company-backends', grouped with a back-end or
61 several that provide actual text completions.
62
63 (add-hook 'js-mode-hook
64 (lambda ()
65 (set (make-local-variable 'company-backends)
66 '((company-dabbrev-code company-yasnippet)))))
67
68 * After keyword `:with', grouped with other back-ends.
69
70 (push '(company-semantic :with company-yasnippet) company-backends)
71
72 * Not in `company-backends', just bound to a key.
73
74 (global-set-key (kbd \"C-c y\") 'company-yasnippet)
75 "
76 (interactive (list 'interactive))
77 (case command
78 (interactive (company-begin-backend 'company-yasnippet))
79 (prefix
80 ;; Should probably use `yas--current-key', but that's bound to be slower.
81 ;; How many trigger keys start with non-symbol characters anyway?
82 (and yas-minor-mode
83 (company-grab-symbol)))
84 (annotation (concat " -> " (get-text-property 0 'yas-annotation arg)))
85 (candidates (company-yasnippet--candidates arg))
86 (post-completion
87 (let ((template (get-text-property 0 'yas-template arg)))
88 (yas-expand-snippet (yas--template-content template)
89 (- (point) (length arg))
90 (point)
91 (yas--template-expand-env template))))))
92
93 (provide 'company-yasnippet)
94 ;;; company-yasnippet.el ends here