Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
6.4 KiB

  1. ;;; company-yasnippet.el --- company-mode completion backend for Yasnippet
  2. ;; Copyright (C) 2014, 2015, 2020 Free Software Foundation, Inc.
  3. ;; Author: Dmitry Gutov
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;;; Code:
  18. (require 'company)
  19. (require 'cl-lib)
  20. (declare-function yas--table-hash "yasnippet")
  21. (declare-function yas--get-snippet-tables "yasnippet")
  22. (declare-function yas-expand-snippet "yasnippet")
  23. (declare-function yas--template-content "yasnippet")
  24. (declare-function yas--template-expand-env "yasnippet")
  25. (declare-function yas--warning "yasnippet")
  26. (defvar company-yasnippet-annotation-fn
  27. (lambda (name)
  28. (concat
  29. (unless company-tooltip-align-annotations " -> ")
  30. name))
  31. "Function to format completion annotation.
  32. It has to accept one argument: the snippet's name.")
  33. (defun company-yasnippet--key-prefixes ()
  34. ;; Mostly copied from `yas--templates-for-key-at-point'.
  35. (defvar yas-key-syntaxes)
  36. (save-excursion
  37. (let ((original (point))
  38. (methods yas-key-syntaxes)
  39. prefixes
  40. method)
  41. (while methods
  42. (unless (eq method (car methods))
  43. (goto-char original))
  44. (setq method (car methods))
  45. (cond ((stringp method)
  46. (skip-syntax-backward method)
  47. (setq methods (cdr methods)))
  48. ((functionp method)
  49. (unless (eq (funcall method original)
  50. 'again)
  51. (setq methods (cdr methods))))
  52. (t
  53. (setq methods (cdr methods))
  54. (yas--warning "Invalid element `%s' in `yas-key-syntaxes'" method)))
  55. (let ((prefix (buffer-substring-no-properties (point) original)))
  56. (unless (equal prefix (car prefixes))
  57. (push prefix prefixes))))
  58. prefixes)))
  59. (defun company-yasnippet--candidates (prefix)
  60. ;; Process the prefixes in reverse: unlike Yasnippet, we look for prefix
  61. ;; matches, so the longest prefix with any matches should be the most useful.
  62. (cl-loop with tables = (yas--get-snippet-tables)
  63. for key-prefix in (company-yasnippet--key-prefixes)
  64. ;; Only consider keys at least as long as the symbol at point.
  65. when (>= (length key-prefix) (length prefix))
  66. thereis (company-yasnippet--completions-for-prefix prefix
  67. key-prefix
  68. tables)))
  69. (defun company-yasnippet--completions-for-prefix (prefix key-prefix tables)
  70. (cl-mapcan
  71. (lambda (table)
  72. (let ((keyhash (yas--table-hash table))
  73. res)
  74. (when keyhash
  75. (maphash
  76. (lambda (key value)
  77. (when (and (stringp key)
  78. (string-prefix-p key-prefix key))
  79. (maphash
  80. (lambda (name template)
  81. (push
  82. (propertize key
  83. 'yas-annotation name
  84. 'yas-template template
  85. 'yas-prefix-offset (- (length key-prefix)
  86. (length prefix)))
  87. res))
  88. value)))
  89. keyhash))
  90. res))
  91. tables))
  92. (defun company-yasnippet--doc (arg)
  93. (let ((template (get-text-property 0 'yas-template arg))
  94. (mode major-mode)
  95. (file-name (buffer-file-name)))
  96. (with-current-buffer (company-doc-buffer)
  97. (let ((buffer-file-name file-name))
  98. (yas-minor-mode 1)
  99. (condition-case error
  100. (yas-expand-snippet (yas--template-content template))
  101. (error
  102. (message "%s" (error-message-string error))))
  103. (delay-mode-hooks
  104. (let ((inhibit-message t))
  105. (if (eq mode 'web-mode)
  106. (progn
  107. (setq mode 'html-mode)
  108. (funcall mode))
  109. (funcall mode)))
  110. (ignore-errors (font-lock-ensure))))
  111. (current-buffer))))
  112. ;;;###autoload
  113. (defun company-yasnippet (command &optional arg &rest ignore)
  114. "`company-mode' backend for `yasnippet'.
  115. This backend should be used with care, because as long as there are
  116. snippets defined for the current major mode, this backend will always
  117. shadow backends that come after it. Recommended usages:
  118. * In a buffer-local value of `company-backends', grouped with a backend or
  119. several that provide actual text completions.
  120. (add-hook 'js-mode-hook
  121. (lambda ()
  122. (set (make-local-variable 'company-backends)
  123. '((company-dabbrev-code company-yasnippet)))))
  124. * After keyword `:with', grouped with other backends.
  125. (push '(company-semantic :with company-yasnippet) company-backends)
  126. * Not in `company-backends', just bound to a key.
  127. (global-set-key (kbd \"C-c y\") 'company-yasnippet)
  128. "
  129. (interactive (list 'interactive))
  130. (cl-case command
  131. (interactive (company-begin-backend 'company-yasnippet))
  132. (prefix
  133. ;; Should probably use `yas--current-key', but that's bound to be slower.
  134. ;; How many trigger keys start with non-symbol characters anyway?
  135. (and (bound-and-true-p yas-minor-mode)
  136. (company-grab-symbol)))
  137. (annotation
  138. (funcall company-yasnippet-annotation-fn
  139. (get-text-property 0 'yas-annotation arg)))
  140. (candidates (company-yasnippet--candidates arg))
  141. (doc-buffer (company-yasnippet--doc arg))
  142. (no-cache t)
  143. (post-completion
  144. (let ((template (get-text-property 0 'yas-template arg))
  145. (prefix-offset (get-text-property 0 'yas-prefix-offset arg)))
  146. (yas-expand-snippet (yas--template-content template)
  147. (- (point) (length arg) prefix-offset)
  148. (point)
  149. (yas--template-expand-env template))))))
  150. (provide 'company-yasnippet)
  151. ;;; company-yasnippet.el ends here