my-emacs-d/elpa/company-20160730.1516/company-yasnippet.el

148 lines
5.4 KiB
EmacsLisp
Raw Normal View History

2016-04-21 21:27:19 +00:00
;;; company-yasnippet.el --- company-mode completion backend for Yasnippet
2014-10-05 12:57:41 +00:00
2016-04-21 21:27:19 +00:00
;; Copyright (C) 2014, 2015 Free Software Foundation, Inc.
2014-10-05 12:57:41 +00:00
;; Author: Dmitry Gutov
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
2015-05-04 09:52:24 +00:00
(require 'company)
2014-10-05 12:57:41 +00:00
(require 'cl-lib)
2015-05-04 09:52:24 +00:00
(declare-function yas--table-hash "yasnippet")
(declare-function yas--get-snippet-tables "yasnippet")
(declare-function yas-expand-snippet "yasnippet")
(declare-function yas--template-content "yasnippet")
(declare-function yas--template-expand-env "yasnippet")
2016-04-21 21:27:19 +00:00
(declare-function yas--warning "yasnippet")
(defun company-yasnippet--key-prefixes ()
;; Mostly copied from `yas--templates-for-key-at-point'.
(defvar yas-key-syntaxes)
(save-excursion
(let ((original (point))
(methods yas-key-syntaxes)
prefixes
method)
(while methods
(unless (eq method (car methods))
(goto-char original))
(setq method (car methods))
(cond ((stringp method)
(skip-syntax-backward method)
(setq methods (cdr methods)))
((functionp method)
(unless (eq (funcall method original)
'again)
(setq methods (cdr methods))))
(t
(setq methods (cdr methods))
(yas--warning "Invalid element `%s' in `yas-key-syntaxes'" method)))
(let ((prefix (buffer-substring-no-properties (point) original)))
(unless (equal prefix (car prefixes))
(push prefix prefixes))))
prefixes)))
2014-10-05 12:57:41 +00:00
(defun company-yasnippet--candidates (prefix)
2016-04-21 21:27:19 +00:00
;; Process the prefixes in reverse: unlike Yasnippet, we look for prefix
;; matches, so the longest prefix with any matches should be the most useful.
(cl-loop with tables = (yas--get-snippet-tables)
for key-prefix in (company-yasnippet--key-prefixes)
;; Only consider keys at least as long as the symbol at point.
when (>= (length key-prefix) (length prefix))
thereis (company-yasnippet--completions-for-prefix prefix
key-prefix
tables)))
(defun company-yasnippet--completions-for-prefix (prefix key-prefix tables)
2015-05-04 09:52:24 +00:00
(cl-mapcan
2014-10-05 12:57:41 +00:00
(lambda (table)
(let ((keyhash (yas--table-hash table))
res)
(when keyhash
(maphash
(lambda (key value)
(when (and (stringp key)
2016-04-21 21:27:19 +00:00
(string-prefix-p key-prefix key))
2014-10-05 12:57:41 +00:00
(maphash
(lambda (name template)
(push
(propertize key
'yas-annotation name
2016-04-21 21:27:19 +00:00
'yas-template template
'yas-prefix-offset (- (length key-prefix)
(length prefix)))
2014-10-05 12:57:41 +00:00
res))
value)))
keyhash))
res))
2016-04-21 21:27:19 +00:00
tables))
2014-10-05 12:57:41 +00:00
;;;###autoload
(defun company-yasnippet (command &optional arg &rest ignore)
2016-04-21 21:27:19 +00:00
"`company-mode' backend for `yasnippet'.
2014-10-05 12:57:41 +00:00
2016-04-21 21:27:19 +00:00
This backend should be used with care, because as long as there are
snippets defined for the current major mode, this backend will always
shadow backends that come after it. Recommended usages:
2014-10-05 12:57:41 +00:00
2016-04-21 21:27:19 +00:00
* In a buffer-local value of `company-backends', grouped with a backend or
2014-10-05 12:57:41 +00:00
several that provide actual text completions.
(add-hook 'js-mode-hook
(lambda ()
(set (make-local-variable 'company-backends)
'((company-dabbrev-code company-yasnippet)))))
2016-04-21 21:27:19 +00:00
* After keyword `:with', grouped with other backends.
2014-10-05 12:57:41 +00:00
(push '(company-semantic :with company-yasnippet) company-backends)
* Not in `company-backends', just bound to a key.
(global-set-key (kbd \"C-c y\") 'company-yasnippet)
"
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'company-yasnippet))
(prefix
;; Should probably use `yas--current-key', but that's bound to be slower.
;; How many trigger keys start with non-symbol characters anyway?
2015-05-04 09:52:24 +00:00
(and (bound-and-true-p yas-minor-mode)
2014-10-05 12:57:41 +00:00
(company-grab-symbol)))
(annotation
(concat
(unless company-tooltip-align-annotations " -> ")
(get-text-property 0 'yas-annotation arg)))
(candidates (company-yasnippet--candidates arg))
2016-04-21 21:27:19 +00:00
(no-cache t)
2014-10-05 12:57:41 +00:00
(post-completion
2016-04-21 21:27:19 +00:00
(let ((template (get-text-property 0 'yas-template arg))
(prefix-offset (get-text-property 0 'yas-prefix-offset arg)))
2014-10-05 12:57:41 +00:00
(yas-expand-snippet (yas--template-content template)
2016-04-21 21:27:19 +00:00
(- (point) (length arg) prefix-offset)
2014-10-05 12:57:41 +00:00
(point)
(yas--template-expand-env template))))))
(provide 'company-yasnippet)
;;; company-yasnippet.el ends here