2017-06-08 11:47:56 +02:00
|
|
|
;;; feature/snippets/config.el -*- lexical-binding: t; -*-
|
2017-02-11 06:59:49 -05:00
|
|
|
|
2017-02-19 18:32:09 -05:00
|
|
|
;; Snippets! I've thrown together a few hacks to make `yasnippet' and `evil'
|
|
|
|
;; behave together.
|
|
|
|
|
2017-02-23 00:06:12 -05:00
|
|
|
(def-package! yasnippet
|
2017-02-28 17:58:52 -05:00
|
|
|
:commands (yas-minor-mode yas-minor-mode-on yas-expand yas-expand-snippet
|
|
|
|
yas-lookup-snippet yas-insert-snippet yas-new-snippet
|
2017-03-23 00:29:14 -04:00
|
|
|
yas-visit-snippet-file snippet-mode)
|
2017-02-11 06:59:49 -05:00
|
|
|
:preface
|
|
|
|
(defvar yas-minor-mode-map (make-sparse-keymap))
|
2017-02-19 18:32:09 -05:00
|
|
|
|
2017-02-11 06:59:49 -05:00
|
|
|
:init
|
2017-02-28 17:58:52 -05:00
|
|
|
;; Ensure `yas-reload-all' is called as late as possible. Other modules could
|
|
|
|
;; have additional configuration for yasnippet. For example, file-templates.
|
2017-06-05 16:41:39 +02:00
|
|
|
(add-transient-hook! 'yas-minor-mode-hook (yas-reload-all))
|
2017-02-28 17:58:52 -05:00
|
|
|
|
2017-04-05 15:54:54 -04:00
|
|
|
(add-hook! (text-mode prog-mode snippet-mode)
|
2017-04-17 02:17:10 -04:00
|
|
|
#'yas-minor-mode-on)
|
2017-02-19 18:32:09 -05:00
|
|
|
|
|
|
|
:config
|
2017-12-27 18:19:56 -05:00
|
|
|
(setq yas-verbosity (if doom-debug-mode 3 0)
|
2017-02-11 06:59:49 -05:00
|
|
|
yas-also-auto-indent-first-line t
|
2017-12-27 18:19:56 -05:00
|
|
|
yas-prompt-functions (delq 'yas-dropdown-prompt yas-prompt-functions)
|
2017-03-23 15:48:05 -04:00
|
|
|
;; Allow nested snippets
|
|
|
|
yas-triggers-in-field t)
|
2017-02-11 06:59:49 -05:00
|
|
|
|
2017-03-02 18:17:40 -05:00
|
|
|
;; Allows project-specific snippets
|
|
|
|
(defun +snippets|enable-project-modes (mode &rest _)
|
|
|
|
"Enable snippets for project modes."
|
|
|
|
(if (symbol-value mode)
|
|
|
|
(yas-activate-extra-mode mode)
|
|
|
|
(yas-deactivate-extra-mode mode)))
|
2017-04-17 02:17:10 -04:00
|
|
|
(add-hook 'doom-project-hook #'+snippets|enable-project-modes)
|
2017-03-02 18:17:40 -05:00
|
|
|
|
2017-02-19 18:32:09 -05:00
|
|
|
;; fix an error caused by smartparens interfering with yasnippet bindings
|
2017-04-17 02:17:10 -04:00
|
|
|
(advice-add #'yas-expand :before #'sp-remove-active-pair-overlay)
|
2017-02-11 06:59:49 -05:00
|
|
|
|
2017-12-27 18:20:14 -05:00
|
|
|
;; Exit snippets on ESC from normal mode
|
2018-01-02 20:36:15 -05:00
|
|
|
(add-hook '+evil-esc-hook #'yas-abort-snippet)
|
|
|
|
|
|
|
|
;; Monkey patch yas-expand-snippet until #883 is resolved. See
|
|
|
|
;; https://github.com/joaotavora/yasnippet/issues/883
|
|
|
|
(defun +snippets*yas-expand-snippet (content &optional start end expand-env)
|
|
|
|
"Expand snippet CONTENT at current point.
|
|
|
|
|
|
|
|
Text between START and END will be deleted before inserting
|
|
|
|
template. EXPAND-ENV is a list of (SYM VALUE) let-style dynamic bindings
|
|
|
|
considered when expanding the snippet."
|
|
|
|
(cl-assert (and yas-minor-mode (memq 'yas--post-command-handler post-command-hook))
|
|
|
|
nil "[yas] `yas-expand-snippet' needs properly setup `yas-minor-mode'")
|
|
|
|
(run-hooks 'yas-before-expand-snippet-hook)
|
|
|
|
(let* ((yas-selected-text (or yas-selected-text
|
|
|
|
(and (region-active-p)
|
|
|
|
(buffer-substring-no-properties (region-beginning)
|
|
|
|
(region-end)))))
|
|
|
|
(start (or start (and (region-active-p) (region-beginning)) (point)))
|
|
|
|
(end (or end (and (region-active-p) (region-end)) (point)))
|
|
|
|
(to-delete (and start end (buffer-substring-no-properties start end)))
|
|
|
|
snippet)
|
|
|
|
(goto-char start)
|
|
|
|
(setq yas--indent-original-column (current-column))
|
|
|
|
(when (and to-delete (> end start))
|
|
|
|
(delete-region start end))
|
|
|
|
(cond ((listp content) (yas--eval-for-effect content))
|
|
|
|
(t
|
|
|
|
(setq yas--start-column (current-column))
|
|
|
|
(let ((yas--inhibit-overlay-hooks t))
|
|
|
|
(setq snippet
|
|
|
|
(yas--snippet-create content expand-env start (point))))
|
|
|
|
(let ((existing-field (and yas--active-field-overlay
|
|
|
|
(overlay-buffer yas--active-field-overlay)
|
|
|
|
(overlay-get yas--active-field-overlay 'yas--field))))
|
|
|
|
(when existing-field
|
|
|
|
(setf (yas--snippet-previous-active-field snippet) existing-field)
|
|
|
|
(yas--advance-end-maybe existing-field (overlay-end yas--active-field-overlay))))
|
|
|
|
(unless (yas--snippet-fields snippet)
|
|
|
|
(yas-exit-snippet snippet))
|
|
|
|
(let ((first-field (car (yas--snippet-fields snippet))))
|
|
|
|
(when first-field
|
|
|
|
(sit-for 0)
|
|
|
|
(yas--letenv (yas--snippet-expand-env snippet)
|
|
|
|
(yas--move-to-field snippet first-field))
|
|
|
|
(when (and (eq (yas--field-number first-field) 0)
|
|
|
|
(> (length (yas--field-text-for-display
|
|
|
|
first-field))
|
|
|
|
0))
|
|
|
|
(setq deactivate-mark nil))))
|
|
|
|
(yas--message 4 "snippet %d expanded." (yas--snippet-id snippet))
|
|
|
|
t))))
|
|
|
|
(advice-add #'yas-expand-snippet :override #'+snippets*yas-expand-snippet))
|
2017-02-11 06:59:49 -05:00
|
|
|
|
|
|
|
|
2017-02-23 00:06:12 -05:00
|
|
|
(def-package! auto-yasnippet
|
2017-02-11 06:59:49 -05:00
|
|
|
:commands (aya-create aya-expand aya-open-line aya-persist-snippet)
|
|
|
|
:config
|
|
|
|
(setq aya-persist-snippets-dir (concat doom-local-dir "auto-snippets/")))
|
|
|
|
|