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