Klimi's new dotfiles with stow.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

147 rader
5.4 KiB

5 år sedan
  1. ;;; company-yasnippet.el --- company-mode completion backend for Yasnippet
  2. ;; Copyright (C) 2014, 2015 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. (defun company-yasnippet--key-prefixes ()
  27. ;; Mostly copied from `yas--templates-for-key-at-point'.
  28. (defvar yas-key-syntaxes)
  29. (save-excursion
  30. (let ((original (point))
  31. (methods yas-key-syntaxes)
  32. prefixes
  33. method)
  34. (while methods
  35. (unless (eq method (car methods))
  36. (goto-char original))
  37. (setq method (car methods))
  38. (cond ((stringp method)
  39. (skip-syntax-backward method)
  40. (setq methods (cdr methods)))
  41. ((functionp method)
  42. (unless (eq (funcall method original)
  43. 'again)
  44. (setq methods (cdr methods))))
  45. (t
  46. (setq methods (cdr methods))
  47. (yas--warning "Invalid element `%s' in `yas-key-syntaxes'" method)))
  48. (let ((prefix (buffer-substring-no-properties (point) original)))
  49. (unless (equal prefix (car prefixes))
  50. (push prefix prefixes))))
  51. prefixes)))
  52. (defun company-yasnippet--candidates (prefix)
  53. ;; Process the prefixes in reverse: unlike Yasnippet, we look for prefix
  54. ;; matches, so the longest prefix with any matches should be the most useful.
  55. (cl-loop with tables = (yas--get-snippet-tables)
  56. for key-prefix in (company-yasnippet--key-prefixes)
  57. ;; Only consider keys at least as long as the symbol at point.
  58. when (>= (length key-prefix) (length prefix))
  59. thereis (company-yasnippet--completions-for-prefix prefix
  60. key-prefix
  61. tables)))
  62. (defun company-yasnippet--completions-for-prefix (prefix key-prefix tables)
  63. (cl-mapcan
  64. (lambda (table)
  65. (let ((keyhash (yas--table-hash table))
  66. res)
  67. (when keyhash
  68. (maphash
  69. (lambda (key value)
  70. (when (and (stringp key)
  71. (string-prefix-p key-prefix key))
  72. (maphash
  73. (lambda (name template)
  74. (push
  75. (propertize key
  76. 'yas-annotation name
  77. 'yas-template template
  78. 'yas-prefix-offset (- (length key-prefix)
  79. (length prefix)))
  80. res))
  81. value)))
  82. keyhash))
  83. res))
  84. tables))
  85. ;;;###autoload
  86. (defun company-yasnippet (command &optional arg &rest ignore)
  87. "`company-mode' backend for `yasnippet'.
  88. This backend should be used with care, because as long as there are
  89. snippets defined for the current major mode, this backend will always
  90. shadow backends that come after it. Recommended usages:
  91. * In a buffer-local value of `company-backends', grouped with a backend or
  92. several that provide actual text completions.
  93. (add-hook 'js-mode-hook
  94. (lambda ()
  95. (set (make-local-variable 'company-backends)
  96. '((company-dabbrev-code company-yasnippet)))))
  97. * After keyword `:with', grouped with other backends.
  98. (push '(company-semantic :with company-yasnippet) company-backends)
  99. * Not in `company-backends', just bound to a key.
  100. (global-set-key (kbd \"C-c y\") 'company-yasnippet)
  101. "
  102. (interactive (list 'interactive))
  103. (cl-case command
  104. (interactive (company-begin-backend 'company-yasnippet))
  105. (prefix
  106. ;; Should probably use `yas--current-key', but that's bound to be slower.
  107. ;; How many trigger keys start with non-symbol characters anyway?
  108. (and (bound-and-true-p yas-minor-mode)
  109. (company-grab-symbol)))
  110. (annotation
  111. (concat
  112. (unless company-tooltip-align-annotations " -> ")
  113. (get-text-property 0 'yas-annotation arg)))
  114. (candidates (company-yasnippet--candidates arg))
  115. (no-cache t)
  116. (post-completion
  117. (let ((template (get-text-property 0 'yas-template arg))
  118. (prefix-offset (get-text-property 0 'yas-prefix-offset arg)))
  119. (yas-expand-snippet (yas--template-content template)
  120. (- (point) (length arg) prefix-offset)
  121. (point)
  122. (yas--template-expand-env template))))))
  123. (provide 'company-yasnippet)
  124. ;;; company-yasnippet.el ends here