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-10-23 20:07:54 +02:00
|
|
|
(setq yas-snippet-dirs '(yas-installed-snippets-dir))
|
|
|
|
|
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-02-23 00:06:12 -05:00
|
|
|
(after! evil
|
2017-02-19 18:32:09 -05:00
|
|
|
;; Exit snippets on ESC in normal mode
|
2017-05-07 19:12:48 +02:00
|
|
|
(add-hook '+evil-esc-hook #'yas-exit-all-snippets)
|
2017-02-19 18:32:09 -05:00
|
|
|
;; Once you're in normal mode, you're out
|
2017-04-17 02:17:10 -04:00
|
|
|
(add-hook 'evil-normal-state-entry-hook #'yas-abort-snippet)
|
2017-02-19 18:32:09 -05:00
|
|
|
;; Strip out whitespace before a line selection
|
|
|
|
(defun +snippets|yas-before-expand ()
|
|
|
|
"Strip out the shitespace before a line selection."
|
|
|
|
(when (and (evil-visual-state-p)
|
|
|
|
(eq (evil-visual-type) 'line))
|
2017-03-22 17:42:51 -04:00
|
|
|
(setq yas-selected-text
|
|
|
|
(replace-regexp-in-string
|
|
|
|
"\\(^\\s-*\\|\n? $\\)" ""
|
|
|
|
(buffer-substring-no-properties evil-visual-beginning
|
|
|
|
evil-visual-end)))))
|
2017-04-17 02:17:10 -04:00
|
|
|
(add-hook 'yas-before-expand-snippet-hook #'+snippets|yas-before-expand)
|
2017-02-11 06:59:49 -05:00
|
|
|
|
2017-02-19 18:32:09 -05:00
|
|
|
(defun +snippets|yas-after-expand ()
|
|
|
|
"Fix previous hook persisting yas-selected-text between expansions."
|
|
|
|
(setq yas-selected-text nil))
|
2017-04-17 02:17:10 -04:00
|
|
|
(add-hook 'yas-after-exit-snippet-hook #'+snippets|yas-after-expand)))
|
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/")))
|
|
|
|
|