From aaf2314d9d8184cd47c7e0fc2be8db698b22b189 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Sun, 19 Feb 2017 18:32:09 -0500 Subject: [PATCH] Update modules/feature/snippets --- modules/feature/snippets/autoload/evil.el | 18 ++++ .../{autoload.el => autoload/snippets.el} | 25 +---- modules/feature/snippets/config.el | 92 +++++++++++-------- 3 files changed, 71 insertions(+), 64 deletions(-) create mode 100644 modules/feature/snippets/autoload/evil.el rename modules/feature/snippets/{autoload.el => autoload/snippets.el} (77%) diff --git a/modules/feature/snippets/autoload/evil.el b/modules/feature/snippets/autoload/evil.el new file mode 100644 index 000000000..aa09b2870 --- /dev/null +++ b/modules/feature/snippets/autoload/evil.el @@ -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)))) diff --git a/modules/feature/snippets/autoload.el b/modules/feature/snippets/autoload/snippets.el similarity index 77% rename from modules/feature/snippets/autoload.el rename to modules/feature/snippets/autoload/snippets.el index 6cdd8f747..0567e3eed 100644 --- a/modules/feature/snippets/autoload.el +++ b/modules/feature/snippets/autoload/snippets.el @@ -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 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) diff --git a/modules/feature/snippets/config.el b/modules/feature/snippets/config.el index b486b473f..2fe3acaae 100644 --- a/modules/feature/snippets/config.el +++ b/modules/feature/snippets/config.el @@ -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,63 +12,72 @@ 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) + ;; 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) - :config - (yas-reload-all) - (@map (:map yas-keymap - "C-e" '+snippets/goto-end-of-field - "C-a" '+snippets/goto-start-of-field - "" '+snippets/goto-end-of-field - "" '+snippets/goto-start-of-field - "" 'yas-prev-field - "" '+snippets/delete-to-start-of-field - "" 'evil-normal-state - [backspace] '+snippets/delete-backward-char - "" '+snippets/delete-forward-char-or-field) - - (:map yas-minor-mode-map - :i [tab] 'yas-expand - :v [tab] '+snippets/expand-on-region)) - - ;; Fix an error caused by smartparens interfering with yasnippet bindings + ;; 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) + (@after evil + (@map (:map yas-keymap + "C-e" '+snippets/goto-end-of-field + "C-a" '+snippets/goto-start-of-field + "" '+snippets/goto-end-of-field + "" '+snippets/goto-start-of-field + "" 'yas-prev-field + "" '+snippets/delete-to-start-of-field + "" 'evil-normal-state + [backspace] '+snippets/delete-backward-char + "" '+snippets/delete-forward-char-or-field) - ;; Once you're in normal mode, you're out - (add-hook 'evil-normal-state-entry-hook 'yas-abort-snippet) + (:map yas-minor-mode-map + :i [tab] 'yas-expand + :v [tab] '+snippets/expand-on-region)) - ;; 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)) - (setq-local - yas-selected-text - (replace-regexp-in-string - "\\(^ *\\|\n? $\\)" "" - (buffer-substring-no-properties (region-beginning) - (1- (region-end))))))) - (add-hook 'yas-before-expand-snippet-hook '+snippets|yas-before-expand) + ;; 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." + (when (and (evil-visual-state-p) + (eq (evil-visual-type) 'line)) + (setq-local + yas-selected-text + (replace-regexp-in-string + "\\(^ *\\|\n? $\\)" "" + (buffer-substring-no-properties (region-beginning) + (1- (region-end))))))) + (add-hook 'yas-before-expand-snippet-hook '+snippets|yas-before-expand) - (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)) + (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))) (@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/")))