Update modules/feature/snippets
This commit is contained in:
parent
2f87987803
commit
aaf2314d9d
3 changed files with 71 additions and 64 deletions
18
modules/feature/snippets/autoload/evil.el
Normal file
18
modules/feature/snippets/autoload/evil.el
Normal 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))))
|
|
@ -1,21 +1,4 @@
|
||||||
;;; autoload.el
|
;;; feature/snippets/autoload/snippets.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))))
|
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +snippets/goto-start-of-field ()
|
(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))
|
(when (and field (> (point) sof))
|
||||||
(delete-region sof (point)))))
|
(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
|
;; TODO move this to ivy
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +snippets/ivy-prompt (prompt choices &optional display-fn)
|
(defun +snippets/ivy-prompt (prompt choices &optional display-fn)
|
|
@ -1,5 +1,8 @@
|
||||||
;;; feature/snippets/config.el
|
;;; feature/snippets/config.el
|
||||||
|
|
||||||
|
;; Snippets! I've thrown together a few hacks to make `yasnippet' and `evil'
|
||||||
|
;; behave together.
|
||||||
|
|
||||||
(@def-package yasnippet
|
(@def-package yasnippet
|
||||||
:commands (yas-minor-mode
|
:commands (yas-minor-mode
|
||||||
yas-minor-mode-on
|
yas-minor-mode-on
|
||||||
|
@ -9,63 +12,72 @@
|
||||||
yas-visit-snippet-file)
|
yas-visit-snippet-file)
|
||||||
:preface
|
:preface
|
||||||
(defvar yas-minor-mode-map (make-sparse-keymap))
|
(defvar yas-minor-mode-map (make-sparse-keymap))
|
||||||
|
|
||||||
:init
|
:init
|
||||||
|
(@add-hook (text-mode prog-mode snippet-mode markdown-mode org-mode)
|
||||||
|
'yas-minor-mode-on)
|
||||||
|
|
||||||
|
:config
|
||||||
(setq yas-verbosity 0
|
(setq yas-verbosity 0
|
||||||
yas-indent-line 'auto
|
yas-indent-line 'auto
|
||||||
yas-also-auto-indent-first-line t
|
yas-also-auto-indent-first-line t
|
||||||
yas-prompt-functions '(yas-completing-prompt yas-ido-prompt yas-no-prompt)
|
yas-prompt-functions '(yas-completing-prompt yas-ido-prompt yas-no-prompt)
|
||||||
yas-snippet-dirs '(yas-installed-snippets-dir))
|
yas-snippet-dirs '(yas-installed-snippets-dir))
|
||||||
|
|
||||||
(@add-hook (text-mode prog-mode snippet-mode markdown-mode org-mode)
|
;; Ensure `yas-reload-all' is called as late as possible. Other modules could
|
||||||
'yas-minor-mode-on)
|
;; 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
|
;; fix an error caused by smartparens interfering with yasnippet bindings
|
||||||
(yas-reload-all)
|
|
||||||
(@map (:map yas-keymap
|
|
||||||
"C-e" '+snippets/goto-end-of-field
|
|
||||||
"C-a" '+snippets/goto-start-of-field
|
|
||||||
"<M-right>" '+snippets/goto-end-of-field
|
|
||||||
"<M-left>" '+snippets/goto-start-of-field
|
|
||||||
"<S-tab>" 'yas-prev-field
|
|
||||||
"<M-backspace>" '+snippets/delete-to-start-of-field
|
|
||||||
"<escape>" 'evil-normal-state
|
|
||||||
[backspace] '+snippets/delete-backward-char
|
|
||||||
"<delete>" '+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
|
|
||||||
(advice-add 'yas-expand :before 'sp-remove-active-pair-overlay)
|
(advice-add 'yas-expand :before 'sp-remove-active-pair-overlay)
|
||||||
|
|
||||||
;; Exit snippets on ESC in normal mode
|
(@after evil
|
||||||
(advice-add 'evil-force-normal-state :before 'yas-exit-all-snippets)
|
(@map (:map yas-keymap
|
||||||
|
"C-e" '+snippets/goto-end-of-field
|
||||||
|
"C-a" '+snippets/goto-start-of-field
|
||||||
|
"<M-right>" '+snippets/goto-end-of-field
|
||||||
|
"<M-left>" '+snippets/goto-start-of-field
|
||||||
|
"<S-tab>" 'yas-prev-field
|
||||||
|
"<M-backspace>" '+snippets/delete-to-start-of-field
|
||||||
|
"<escape>" 'evil-normal-state
|
||||||
|
[backspace] '+snippets/delete-backward-char
|
||||||
|
"<delete>" '+snippets/delete-forward-char-or-field)
|
||||||
|
|
||||||
;; Once you're in normal mode, you're out
|
(:map yas-minor-mode-map
|
||||||
(add-hook 'evil-normal-state-entry-hook 'yas-abort-snippet)
|
:i [tab] 'yas-expand
|
||||||
|
:v [tab] '+snippets/expand-on-region))
|
||||||
|
|
||||||
;; Strip out whitespace before a line selection
|
;; Exit snippets on ESC in normal mode
|
||||||
(defun +snippets|yas-before-expand ()
|
(advice-add 'evil-force-normal-state :before 'yas-exit-all-snippets)
|
||||||
"Strip out the shitespace before a line selection."
|
;; Once you're in normal mode, you're out
|
||||||
(when (and (evil-visual-state-p)
|
(add-hook 'evil-normal-state-entry-hook 'yas-abort-snippet)
|
||||||
(eq (evil-visual-type) 'line))
|
;; Strip out whitespace before a line selection
|
||||||
(setq-local
|
(defun +snippets|yas-before-expand ()
|
||||||
yas-selected-text
|
"Strip out the shitespace before a line selection."
|
||||||
(replace-regexp-in-string
|
(when (and (evil-visual-state-p)
|
||||||
"\\(^ *\\|\n? $\\)" ""
|
(eq (evil-visual-type) 'line))
|
||||||
(buffer-substring-no-properties (region-beginning)
|
(setq-local
|
||||||
(1- (region-end)))))))
|
yas-selected-text
|
||||||
(add-hook 'yas-before-expand-snippet-hook '+snippets|yas-before-expand)
|
(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 ()
|
(defun +snippets|yas-after-expand ()
|
||||||
"Fix previous hook persisting yas-selected-text between expansions."
|
"Fix previous hook persisting yas-selected-text between expansions."
|
||||||
(setq yas-selected-text nil))
|
(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
|
(@def-package auto-yasnippet
|
||||||
:commands (aya-create aya-expand aya-open-line aya-persist-snippet)
|
:commands (aya-create aya-expand aya-open-line aya-persist-snippet)
|
||||||
|
:init
|
||||||
|
(@map :i [C-tab] 'aya-expand
|
||||||
|
:nv [C-tab] 'aya-create)
|
||||||
:config
|
:config
|
||||||
(setq aya-persist-snippets-dir (concat doom-local-dir "auto-snippets/")))
|
(setq aya-persist-snippets-dir (concat doom-local-dir "auto-snippets/")))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue