]> code.delx.au - gnu-emacs-elpa/blob - company-yasnippet.el
93e6eb299e250d4b2a9df4298be64a637752bd63
[gnu-emacs-elpa] / company-yasnippet.el
1 ;;; company-yasnippet.el --- company-mode completion backend for Yasnippet
2
3 ;; Copyright (C) 2014, 2015 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 (declare-function yas--warning "yasnippet")
37
38 (defun company-yasnippet--key-prefixes ()
39 ;; Mostly copied from `yas--templates-for-key-at-point'.
40 (defvar yas-key-syntaxes)
41 (save-excursion
42 (let ((original (point))
43 (methods yas-key-syntaxes)
44 prefixes
45 method)
46 (while methods
47 (unless (eq method (car methods))
48 (goto-char original))
49 (setq method (car methods))
50 (cond ((stringp method)
51 (skip-syntax-backward method)
52 (setq methods (cdr methods)))
53 ((functionp method)
54 (unless (eq (funcall method original)
55 'again)
56 (setq methods (cdr methods))))
57 (t
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))))
63 prefixes)))
64
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
71 key-prefix
72 tables)))
73
74 (defun company-yasnippet--completions-for-prefix (prefix key-prefix tables)
75 (cl-mapcan
76 (lambda (table)
77 (let ((keyhash (yas--table-hash table))
78 res)
79 (when keyhash
80 (maphash
81 (lambda (key value)
82 (when (and (stringp key)
83 (string-prefix-p key-prefix key))
84 (maphash
85 (lambda (name template)
86 (push
87 (propertize key
88 'yas-annotation name
89 'yas-template template
90 'yas-prefix-offset
91 (let ((pl (length prefix))
92 (kpl (length key-prefix)))
93 (if (> kpl pl)
94 (- kpl pl)
95 0)))
96 res))
97 value)))
98 keyhash))
99 res))
100 tables))
101
102 ;;;###autoload
103 (defun company-yasnippet (command &optional arg &rest ignore)
104 "`company-mode' backend for `yasnippet'.
105
106 This backend should be used with care, because as long as there are
107 snippets defined for the current major mode, this backend will always
108 shadow backends that come after it. Recommended usages:
109
110 * In a buffer-local value of `company-backends', grouped with a backend or
111 several that provide actual text completions.
112
113 (add-hook 'js-mode-hook
114 (lambda ()
115 (set (make-local-variable 'company-backends)
116 '((company-dabbrev-code company-yasnippet)))))
117
118 * After keyword `:with', grouped with other backends.
119
120 (push '(company-semantic :with company-yasnippet) company-backends)
121
122 * Not in `company-backends', just bound to a key.
123
124 (global-set-key (kbd \"C-c y\") 'company-yasnippet)
125 "
126 (interactive (list 'interactive))
127 (cl-case command
128 (interactive (company-begin-backend 'company-yasnippet))
129 (prefix
130 ;; Should probably use `yas--current-key', but that's bound to be slower.
131 ;; How many trigger keys start with non-symbol characters anyway?
132 (and (bound-and-true-p yas-minor-mode)
133 (company-grab-symbol)))
134 (annotation
135 (concat
136 (unless company-tooltip-align-annotations " -> ")
137 (get-text-property 0 'yas-annotation arg)))
138 (candidates (company-yasnippet--candidates arg))
139 (no-cache t)
140 (post-completion
141 (let ((template (get-text-property 0 'yas-template arg))
142 (prefix-offset (get-text-property 0 'yas-prefix-offset arg)))
143 (yas-expand-snippet (yas--template-content template)
144 (- (point) (length arg) prefix-offset)
145 (point)
146 (yas--template-expand-env template))))))
147
148 (provide 'company-yasnippet)
149 ;;; company-yasnippet.el ends here