]> code.delx.au - gnu-emacs-elpa/blob - company-yasnippet.el
Spell "backend" and "frontend" without hyphens
[gnu-emacs-elpa] / company-yasnippet.el
1 ;;; company-yasnippet.el --- company-mode completion backend 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 'company)
29 (require 'cl-lib)
30
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
37 (defun company-yasnippet--candidates (prefix)
38 (cl-mapcan
39 (lambda (table)
40 (let ((keyhash (yas--table-hash table))
41 res)
42 (when keyhash
43 (maphash
44 (lambda (key value)
45 (when (and (stringp key)
46 (string-prefix-p prefix key))
47 (maphash
48 (lambda (name template)
49 (push
50 (propertize key
51 'yas-annotation name
52 'yas-template template)
53 res))
54 value)))
55 keyhash))
56 res))
57 (yas--get-snippet-tables)))
58
59 ;;;###autoload
60 (defun company-yasnippet (command &optional arg &rest ignore)
61 "`company-mode' backend for `yasnippet'.
62
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:
66
67 * In a buffer-local value of `company-backends', grouped with a backend or
68 several that provide actual text completions.
69
70 (add-hook 'js-mode-hook
71 (lambda ()
72 (set (make-local-variable 'company-backends)
73 '((company-dabbrev-code company-yasnippet)))))
74
75 * After keyword `:with', grouped with other backends.
76
77 (push '(company-semantic :with company-yasnippet) company-backends)
78
79 * Not in `company-backends', just bound to a key.
80
81 (global-set-key (kbd \"C-c y\") 'company-yasnippet)
82 "
83 (interactive (list 'interactive))
84 (cl-case command
85 (interactive (company-begin-backend 'company-yasnippet))
86 (prefix
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)))
91 (annotation
92 (concat
93 (unless company-tooltip-align-annotations " -> ")
94 (get-text-property 0 'yas-annotation arg)))
95 (candidates (company-yasnippet--candidates arg))
96 (post-completion
97 (let ((template (get-text-property 0 'yas-template arg)))
98 (yas-expand-snippet (yas--template-content template)
99 (- (point) (length arg))
100 (point)
101 (yas--template-expand-env template))))))
102
103 (provide 'company-yasnippet)
104 ;;; company-yasnippet.el ends here