Update modules/feature/snippets

This commit is contained in:
Henrik Lissner 2017-02-19 18:32:09 -05:00
parent 2f87987803
commit aaf2314d9d
3 changed files with 71 additions and 64 deletions

View file

@ -0,0 +1,18 @@
;;; feature/snippets/autoload/evil.el
;;;###autoload
(defun +snippets/expand-on-region ()
"Only use this with `evil-mode'. Expands a snippet around a selected region
and switches to insert mode if there are editable fields."
(interactive)
(when (evil-visual-state-p)
(let ((end (region-end)))
(evil-visual-select
(region-beginning)
(if (eq evil-this-type 'line) end (1+ end))
'inclusive)))
(yas-insert-snippet)
(let* ((snippet (first (yas--snippets-at-point)))
(fields (yas--snippet-fields snippet)))
(evil-insert-state +1)
(unless fields (evil-change-state 'normal))))

View file

@ -1,21 +1,4 @@
;;; autoload.el
;;;###autoload
(defun +snippets/expand-on-region ()
"Switch to insert mode when expanding a template via region selection, or go
back to normal mode if there are no fields."
(interactive)
(when (evil-visual-state-p)
(let ((end (region-end)))
(evil-visual-select
(region-beginning)
(if (eq evil-this-type 'line) end (1+ end))
'inclusive)))
(yas-insert-snippet)
(let* ((snippet (first (yas--snippets-at-point)))
(fields (yas--snippet-fields snippet)))
(evil-insert-state +1)
(unless fields (evil-change-state 'normal))))
;;; feature/snippets/autoload/snippets.el
;;;###autoload
(defun +snippets/goto-start-of-field ()
@ -74,12 +57,6 @@ buggy behavior when <delete> is pressed in an empty field."
(when (and field (> (point) sof))
(delete-region sof (point)))))
;;;###autoload
(defun +snippets/find-file ()
"Browse through snippets folder"
(interactive)
(projectile-find-file-in-directory (car yas-snippet-dirs)))
;; TODO move this to ivy
;;;###autoload
(defun +snippets/ivy-prompt (prompt choices &optional display-fn)

View file

@ -1,5 +1,8 @@
;;; feature/snippets/config.el
;; Snippets! I've thrown together a few hacks to make `yasnippet' and `evil'
;; behave together.
(@def-package yasnippet
:commands (yas-minor-mode
yas-minor-mode-on
@ -9,18 +12,29 @@
yas-visit-snippet-file)
:preface
(defvar yas-minor-mode-map (make-sparse-keymap))
:init
(@add-hook (text-mode prog-mode snippet-mode markdown-mode org-mode)
'yas-minor-mode-on)
:config
(setq yas-verbosity 0
yas-indent-line 'auto
yas-also-auto-indent-first-line t
yas-prompt-functions '(yas-completing-prompt yas-ido-prompt yas-no-prompt)
yas-snippet-dirs '(yas-installed-snippets-dir))
(@add-hook (text-mode prog-mode snippet-mode markdown-mode org-mode)
'yas-minor-mode-on)
:config
;; Ensure `yas-reload-all' is called as late as possible. Other modules could
;; have additional configuration for yasnippet. For example, file-templates.
(defun +snippets|load (&rest _)
(yas-reload-all)
(advice-remove 'yas-expand '+snippets|load))
(advice-add 'yas-expand :before '+snippets|load)
;; fix an error caused by smartparens interfering with yasnippet bindings
(advice-add 'yas-expand :before 'sp-remove-active-pair-overlay)
(@after evil
(@map (:map yas-keymap
"C-e" '+snippets/goto-end-of-field
"C-a" '+snippets/goto-start-of-field
@ -36,15 +50,10 @@
:i [tab] 'yas-expand
:v [tab] '+snippets/expand-on-region))
;; Fix an error caused by smartparens interfering with yasnippet bindings
(advice-add 'yas-expand :before 'sp-remove-active-pair-overlay)
;; Exit snippets on ESC in normal mode
(advice-add 'evil-force-normal-state :before 'yas-exit-all-snippets)
;; Once you're in normal mode, you're out
(add-hook 'evil-normal-state-entry-hook 'yas-abort-snippet)
;; Strip out whitespace before a line selection
(defun +snippets|yas-before-expand ()
"Strip out the shitespace before a line selection."
@ -61,11 +70,14 @@
(defun +snippets|yas-after-expand ()
"Fix previous hook persisting yas-selected-text between expansions."
(setq yas-selected-text nil))
(add-hook 'yas-after-exit-snippet-hook '+snippets|yas-after-expand))
(add-hook 'yas-after-exit-snippet-hook '+snippets|yas-after-expand)))
(@def-package auto-yasnippet
:commands (aya-create aya-expand aya-open-line aya-persist-snippet)
:init
(@map :i [C-tab] 'aya-expand
:nv [C-tab] 'aya-create)
:config
(setq aya-persist-snippets-dir (concat doom-local-dir "auto-snippets/")))