Convert +snippets/expand-on-region to advice
It's meant to be corrective, not alternative behavior.
This commit is contained in:
parent
ff25e3c08f
commit
dcdebdb283
3 changed files with 23 additions and 19 deletions
|
@ -1,16 +0,0 @@
|
||||||
;;; editor/snippets/autoload/evil.el -*- lexical-binding: t; -*-
|
|
||||||
;;;###if (featurep! :editor evil)
|
|
||||||
|
|
||||||
;;;###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)
|
|
||||||
(evil-visual-select evil-visual-beginning evil-visual-end 'inclusive))
|
|
||||||
(cl-letf (((symbol-function 'region-beginning) (lambda () evil-visual-beginning))
|
|
||||||
((symbol-function 'region-end) (lambda () evil-visual-end)))
|
|
||||||
(yas-insert-snippet))
|
|
||||||
(when (yas-active-snippets)
|
|
||||||
(evil-insert-state +1)))
|
|
||||||
|
|
|
@ -110,6 +110,26 @@ buggy behavior when <delete> is pressed in an empty field."
|
||||||
(message "This is a built-in snippet, enabling read only mode. Use `yas-new-snippet' to redefine snippets")))
|
(message "This is a built-in snippet, enabling read only mode. Use `yas-new-snippet' to redefine snippets")))
|
||||||
|
|
||||||
|
|
||||||
|
;;
|
||||||
|
;;; Advice
|
||||||
|
|
||||||
|
;;;###autoload
|
||||||
|
(defun +snippets*expand-on-region (orig-fn &optional no-condition)
|
||||||
|
"Fix off-by-one issue with expanding snippets on an evil visual region, and
|
||||||
|
switches to insert mode.
|
||||||
|
|
||||||
|
If evil-local-mode isn't enabled, run ORIG-FN as is."
|
||||||
|
(if (not (and (bound-and-true-p evil-local-mode)
|
||||||
|
(evil-visual-state-p)))
|
||||||
|
(funcall orig-fn no-condition)
|
||||||
|
(evil-visual-select evil-visual-beginning evil-visual-end 'inclusive)
|
||||||
|
(cl-letf (((symbol-function 'region-beginning) (lambda () evil-visual-beginning))
|
||||||
|
((symbol-function 'region-end) (lambda () evil-visual-end)))
|
||||||
|
(funcall orig-fn no-condition))
|
||||||
|
(when (yas-active-snippets)
|
||||||
|
(evil-insert-state +1))))
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; Commands
|
;; Commands
|
||||||
|
|
||||||
|
|
|
@ -48,9 +48,9 @@
|
||||||
;; Enable `read-only-mode' for built-in snippets (in `doom-local-dir')
|
;; Enable `read-only-mode' for built-in snippets (in `doom-local-dir')
|
||||||
(add-hook 'snippet-mode-hook #'+snippets|read-only-maybe)
|
(add-hook 'snippet-mode-hook #'+snippets|read-only-maybe)
|
||||||
|
|
||||||
(when (featurep! :editor evil)
|
;; (Evil only) fix off-by-one issue with visual-mode selections in
|
||||||
;; evil visual-mode integration for `yas-insert-snippet'
|
;; `yas-insert-snippet', and switches to insert mode afterwards.
|
||||||
(define-key yas-minor-mode-map [remap yas-insert-snippet] #'+snippets/expand-on-region)))
|
(advice-add #'yas-insert-snippet :around #'+snippets*expand-on-region))
|
||||||
|
|
||||||
|
|
||||||
;; `auto-yasnippet'
|
;; `auto-yasnippet'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue