diff --git a/modules/editor/snippets/autoload/evil.el b/modules/editor/snippets/autoload/evil.el deleted file mode 100644 index 8cb8ccd9c..000000000 --- a/modules/editor/snippets/autoload/evil.el +++ /dev/null @@ -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))) - diff --git a/modules/editor/snippets/autoload/snippets.el b/modules/editor/snippets/autoload/snippets.el index 062bdc8cd..15a7c0b63 100644 --- a/modules/editor/snippets/autoload/snippets.el +++ b/modules/editor/snippets/autoload/snippets.el @@ -110,6 +110,26 @@ buggy behavior when is pressed in an empty field." (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 diff --git a/modules/editor/snippets/config.el b/modules/editor/snippets/config.el index b12d030bf..81b89c166 100644 --- a/modules/editor/snippets/config.el +++ b/modules/editor/snippets/config.el @@ -48,9 +48,9 @@ ;; Enable `read-only-mode' for built-in snippets (in `doom-local-dir') (add-hook 'snippet-mode-hook #'+snippets|read-only-maybe) - (when (featurep! :editor evil) - ;; evil visual-mode integration for `yas-insert-snippet' - (define-key yas-minor-mode-map [remap yas-insert-snippet] #'+snippets/expand-on-region))) + ;; (Evil only) fix off-by-one issue with visual-mode selections in + ;; `yas-insert-snippet', and switches to insert mode afterwards. + (advice-add #'yas-insert-snippet :around #'+snippets*expand-on-region)) ;; `auto-yasnippet'