]> code.delx.au - gnu-emacs-elpa/blob - company-yasnippet.el
Merge pull request #531 from juergenhoetzel/master
[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 ;; Only consider keys at least as long as the symbol at point.
71 when (>= (length key-prefix) (length prefix))
72 thereis (company-yasnippet--completions-for-prefix prefix
73 key-prefix
74 tables)))
75
76 (defun company-yasnippet--completions-for-prefix (prefix key-prefix tables)
77 (cl-mapcan
78 (lambda (table)
79 (let ((keyhash (yas--table-hash table))
80 res)
81 (when keyhash
82 (maphash
83 (lambda (key value)
84 (when (and (stringp key)
85 (string-prefix-p key-prefix key))
86 (maphash
87 (lambda (name template)
88 (push
89 (propertize key
90 'yas-annotation name
91 'yas-template template
92 'yas-prefix-offset (- (length key-prefix)
93 (length prefix)))
94 res))
95 value)))
96 keyhash))
97 res))
98 tables))
99
100 ;;;###autoload
101 (defun company-yasnippet (command &optional arg &rest ignore)
102 "`company-mode' backend for `yasnippet'.
103
104 This backend should be used with care, because as long as there are
105 snippets defined for the current major mode, this backend will always
106 shadow backends that come after it. Recommended usages:
107
108 * In a buffer-local value of `company-backends', grouped with a backend or
109 several that provide actual text completions.
110
111 (add-hook 'js-mode-hook
112 (lambda ()
113 (set (make-local-variable 'company-backends)
114 '((company-dabbrev-code company-yasnippet)))))
115
116 * After keyword `:with', grouped with other backends.
117
118 (push '(company-semantic :with company-yasnippet) company-backends)
119
120 * Not in `company-backends', just bound to a key.
121
122 (global-set-key (kbd \"C-c y\") 'company-yasnippet)
123 "
124 (interactive (list 'interactive))
125 (cl-case command
126 (interactive (company-begin-backend 'company-yasnippet))
127 (prefix
128 ;; Should probably use `yas--current-key', but that's bound to be slower.
129 ;; How many trigger keys start with non-symbol characters anyway?
130 (and (bound-and-true-p yas-minor-mode)
131 (company-grab-symbol)))
132 (annotation
133 (concat
134 (unless company-tooltip-align-annotations " -> ")
135 (get-text-property 0 'yas-annotation arg)))
136 (candidates (company-yasnippet--candidates arg))
137 (no-cache t)
138 (post-completion
139 (let ((template (get-text-property 0 'yas-template arg))
140 (prefix-offset (get-text-property 0 'yas-prefix-offset arg)))
141 (yas-expand-snippet (yas--template-content template)
142 (- (point) (length arg) prefix-offset)
143 (point)
144 (yas--template-expand-env template))))))
145
146 (provide 'company-yasnippet)
147 ;;; company-yasnippet.el ends here