Lazy-load yasnippet harder for faster first-file load time

+ Rather than waiting for the first "interactive" major mode to be
  visited to activate yas-minor-mode, we wait until the first time the
  user invokes a snippet command to activate yas-global-mode.
+ yas-reload-all is one of the bottlenecks when loading a file for the
  first time. Deferring it further should help with this.
+ yas-global-mode reaches more major modes than our former list of
  hooks (fixes #5140).

Closes #5140
This commit is contained in:
Henrik Lissner 2021-06-05 12:49:40 -04:00
parent 5038ba9826
commit 60b595321e
4 changed files with 10 additions and 17 deletions

View file

@ -39,13 +39,12 @@
;; Smart tab, these will only work in GUI Emacs ;; Smart tab, these will only work in GUI Emacs
(map! :i [tab] (cmds! (and (featurep! :editor snippets) (map! :i [tab] (cmds! (and (featurep! :editor snippets)
(bound-and-true-p yas-minor-mode)
(yas-maybe-expand-abbrev-key-filter 'yas-expand)) (yas-maybe-expand-abbrev-key-filter 'yas-expand))
#'yas-expand #'yas-expand
(and (bound-and-true-p company-mode) (and (bound-and-true-p company-mode)
(featurep! :completion company +tng)) (featurep! :completion company +tng))
#'company-indent-or-complete-common) #'company-indent-or-complete-common)
:m [tab] (cmds! (and (bound-and-true-p yas-minor-mode) :m [tab] (cmds! (and (featurep! :editor snippets)
(evil-visual-state-p) (evil-visual-state-p)
(or (eq evil-visual-selection 'line) (or (eq evil-visual-selection 'line)
(not (memq (char-after) (list ?\( ?\[ ?\{ ?\} ?\] ?\)))))) (not (memq (char-after) (list ?\( ?\[ ?\{ ?\} ?\] ?\))))))

View file

@ -17,22 +17,14 @@
yas-new-snippet yas-new-snippet
yas-visit-snippet-file yas-visit-snippet-file
yas-activate-extra-mode yas-activate-extra-mode
yas-deactivate-extra-mode) yas-deactivate-extra-mode
yas-maybe-expand-abbrev-key-filter)
:init :init
;; Remove default ~/.emacs.d/snippets ;; Remove default ~/.emacs.d/snippets
(defvar yas-snippet-dirs nil) (defvar yas-snippet-dirs nil)
(unless (daemonp) ;; Lazy load yasnippet until it is needed
;; Ensure `yas-reload-all' is called as late as possible. Other modules (add-transient-hook! #'company-yasnippet (require 'yasnippet))
;; could have additional configuration for yasnippet. For example,
;; file-templates.
(add-transient-hook! 'yas-minor-mode-hook (yas-reload-all)))
(add-hook! '(text-mode-hook
prog-mode-hook
conf-mode-hook
snippet-mode-hook)
#'yas-minor-mode-on)
:config :config
(add-to-list 'doom-debug-variables '(yas-verbosity . 3)) (add-to-list 'doom-debug-variables '(yas-verbosity . 3))
@ -125,7 +117,7 @@
(smartparens-mode 1))))) (smartparens-mode 1)))))
;; If in a daemon session, front-load this expensive work: ;; If in a daemon session, front-load this expensive work:
(if (daemonp) (yas-reload-all))) (yas-global-mode +1))
(use-package! auto-yasnippet (use-package! auto-yasnippet

View file

@ -417,7 +417,8 @@ Made for `org-tab-first-hook' in evil-mode."
(defun +org-yas-expand-maybe-h () (defun +org-yas-expand-maybe-h ()
"Expand a yasnippet snippet, if trigger exists at point or region is active. "Expand a yasnippet snippet, if trigger exists at point or region is active.
Made for `org-tab-first-hook'." Made for `org-tab-first-hook'."
(when (bound-and-true-p yas-minor-mode) (when (featurep! :editor snippets)
(require 'yasnippet)
(and (let ((major-mode (if (org-in-src-block-p t) (and (let ((major-mode (if (org-in-src-block-p t)
(org-src-get-lang-mode (org-eldoc-get-src-lang)) (org-src-get-lang-mode (org-eldoc-get-src-lang))
major-mode)) major-mode))

View file

@ -127,7 +127,8 @@ snippet, or `emmet-expand-yas'/`emmet-expand-line', depending on whether
(not (or (memq (char-after) (list ?\n ?\s ?\t)) (not (or (memq (char-after) (list ?\n ?\s ?\t))
(eobp)))) (eobp))))
#'indent-for-tab-command) #'indent-for-tab-command)
((bound-and-true-p yas-minor-mode) ((featurep! :editor snippets)
(require 'yasnippet)
(if (yas--templates-for-key-at-point) (if (yas--templates-for-key-at-point)
#'yas-expand #'yas-expand
#'emmet-expand-yas)) #'emmet-expand-yas))