Refactor feature/file-templates (+ later yas-reload-all)

This commit is contained in:
Henrik Lissner 2017-02-28 17:58:52 -05:00
parent 27defb3bb6
commit 122ddc5a12
2 changed files with 37 additions and 46 deletions

View file

@ -7,30 +7,27 @@
"") "")
(def-package! autoinsert ; built-in (def-package! autoinsert ; built-in
:after yasnippet :defer 1
:init :init
(setq auto-insert-query nil ; Don't prompt before insertion (setq auto-insert-query nil ; Don't prompt before insertion
auto-insert-alist nil) ; Tabula rasa auto-insert-alist nil) ; Tabula rasa
(after! yasnippet
(push '+file-templates-dir yas-snippet-dirs))
:config :config
(auto-insert-mode 1) (auto-insert-mode 1)
(push '+file-templates-dir yas-snippet-dirs)
(defun +file-templates--expand (key &optional mode project-only) (defun +file-templates--expand (key &optional mode project-only)
"Auto insert a snippet of yasnippet into new file." "Auto insert a snippet of yasnippet into new file."
(interactive) (interactive)
(when (if project-only (doom-project-p) t) (when (if project-only (doom-project-p) t)
(require 'yasnippet) (require 'yasnippet)
(unless yas-minor-mode (unless yas-minor-mode (yas-minor-mode-on))
(yas-minor-mode-on)) (when (and yas-minor-mode
(let ((snippet (let ((template (cdar (cl-mapcan #'(lambda (table) (yas--fetch table key)) (yas-expand-snippet (yas-lookup-snippet key mode t))
(yas--get-snippet-tables mode))))) (and (featurep 'evil) evil-mode))
(if template (yas--template-content template) nil)))) (evil-initialize-state 'insert))))
(when (and yas-minor-mode snippet)
(yas-expand-snippet snippet)
(when (and (featurep 'evil) evil-mode)
(evil-initialize-state 'insert))))))
(defun +file-templates|add (regexp trigger mode &optional project-only-p) (defun +file-templates|add (regexp trigger mode &optional project-only-p)
(define-auto-insert (define-auto-insert
@ -42,26 +39,23 @@
'(("/\\.gitignore$" "__" gitignore-mode) '(("/\\.gitignore$" "__" gitignore-mode)
("/Dockerfile$" "__" dockerfile-mode) ("/Dockerfile$" "__" dockerfile-mode)
("/docker-compose.yml$" "__" yaml-mode) ("/docker-compose.yml$" "__" yaml-mode)
;; Org-mode
("\\.org$" "__" org-mode)
("/Work/.+\\.org$" "__project.org" org-mode)
("/Invoices/.+\\.org$" "__invoice.org" org-mode)
("/Contacts/.+\\.org$" "__contact.org" org-mode)
;; C/C++ ;; C/C++
("/Makefile$" "__" makefile-gmake-mode)
("/main\\.\\(cc\\|cpp\\)$" "__main.cpp" c++-mode)
("/win32_\\.\\(cc\\|cpp\\)$" "__winmain.cpp" c++-mode)
("\\.h\\(h\\|pp|xx\\)$" "__hpp" c++-mode)
("\\.\\(cc\\|cpp\\)$" "__cpp" c++-mode)
("\\.h$" "__h" c-mode) ("\\.h$" "__h" c-mode)
("\\.c$" "__c" c-mode) ("\\.c$" "__c" c-mode)
("\\.h\\(h\\|pp|xx\\)$" "__hpp" c++-mode)
("\\.\\(cc\\|cpp\\)$" "__cpp" c++-mode)
("/main\\.\\(cc\\|cpp\\)$" "__main.cpp" c++-mode)
("/win32_\\.\\(cc\\|cpp\\)$" "__winmain.cpp" c++-mode)
("/Makefile$" "__" makefile-gmake-mode)
;; Elisp ;; Elisp
("\\.el$" "__initfile" emacs-lisp-mode)
("-test\\.el$" "__" emacs-ert-mode) ("-test\\.el$" "__" emacs-ert-mode)
("/.+\\.el$" "__initfile" emacs-lisp-mode) ("/.emacs.d/.+\\.el$" "__doom-module" emacs-lisp-mode)
("/.emacs.d/.+/packages\\.el$" "__doom-packages" emacs-lisp-mode)
(snippet-mode "__" snippet-mode) (snippet-mode "__" snippet-mode)
;; Go ;; Go
("/main\\.go$" "__main.go" go-mode t)
("\\.go$" "__.go" go-mode) ("\\.go$" "__.go" go-mode)
("/main\\.go$" "__main.go" go-mode t)
;; HTML ;; HTML
("\\.html$" "__.html" web-mode) ("\\.html$" "__.html" web-mode)
;; java ;; java
@ -69,40 +63,40 @@
("/main\\.java$" "__main" java-mode) ("/main\\.java$" "__main" java-mode)
("/build\\.gradle$" "__build.gradle" android-mode) ("/build\\.gradle$" "__build.gradle" android-mode)
;; Javascript ;; Javascript
("\\.lbaction/.+/Info.plist$" "__Info.plst" lb6-mode) ("\\.\\(json\\|jshintrc\\)$" "__" json-mode)
("\\.lbaction/.+/\\(default\\|suggestions\\)\\.js$" "__default.js" lb6-mode)
("/package\\.json$" "__package.json" json-mode) ("/package\\.json$" "__package.json" json-mode)
("/bower\\.json$" "__bower.json" json-mode) ("/bower\\.json$" "__bower.json" json-mode)
("\\.\\(json\\|jshintrc\\)$" "__" json-mode)
("/gulpfile\\.js$" "__gulpfile.js" js-mode) ("/gulpfile\\.js$" "__gulpfile.js" js-mode)
("\\.lbaction/.+/Info.plist$" "__Info.plst" lb6-mode)
("\\.lbaction/.+/\\(default\\|suggestions\\)\\.js$" "__default.js" lb6-mode)
;; Lua ;; Lua
("/main\\.lua$" "__main.lua" love-mode) ("/main\\.lua$" "__main.lua" love-mode)
("/conf\\.lua$" "__conf.lua" love-mode) ("/conf\\.lua$" "__conf.lua" love-mode)
;; Markdown ;; Markdown
("\\.md$" "__" markdown-mode) ("\\.md$" "__" markdown-mode)
;; PHP ;; PHP
("\\.class\\.php$" "__.class.php" php-mode)
("\\.php$" "__" php-mode) ("\\.php$" "__" php-mode)
("\\.class\\.php$" "__.class.php" php-mode)
;; Python ;; Python
;;("tests?/test_.+\\.py$" "__" nose-mode) ;;("tests?/test_.+\\.py$" "__" nose-mode)
;;("/setup\\.py$" "__setup.py" python-mode) ;;("/setup\\.py$" "__setup.py" python-mode)
("\\.py$" "__" python-mode) ("\\.py$" "__" python-mode)
;; Ruby ;; Ruby
("/\\.rspec$" "__.rspec" rspec-mode) ("\\.rb$" "__" ruby-mode)
("/spec_helper\\.rb$" "__helper" rspec-mode t)
("_spec\\.rb$" "__" rspec-mode t)
("/Rakefile$" "__Rakefile" ruby-mode t) ("/Rakefile$" "__Rakefile" ruby-mode t)
("/Gemfile$" "__Gemfile" ruby-mode t) ("/Gemfile$" "__Gemfile" ruby-mode t)
("/\\.rspec$" "__.rspec" rspec-mode)
("\\.gemspec$" "__.gemspec" ruby-mode t) ("\\.gemspec$" "__.gemspec" ruby-mode t)
("/spec_helper\\.rb$" "__helper" rspec-mode t)
("/lib/.+\\.rb$" "__module" ruby-mode t) ("/lib/.+\\.rb$" "__module" ruby-mode t)
("\\.rb$" "__" ruby-mode) ("_spec\\.rb$" "__" rspec-mode t)
;; Rust ;; Rust
("/Cargo.toml$" "__Cargo.toml" rust-mode)
("/main\\.rs$" "__main.rs" rust-mode) ("/main\\.rs$" "__main.rs" rust-mode)
("/Cargo.toml$" "__Cargo.toml" rust-mode)
;; SCSS ;; SCSS
("\\.scss$" "__" scss-mode)
("/master\\.scss$" "__master.scss" scss-mode) ("/master\\.scss$" "__master.scss" scss-mode)
("/normalize\\.scss$" "__normalize.scss" scss-mode) ("/normalize\\.scss$" "__normalize.scss" scss-mode)
("\\.scss$" "__" scss-mode)
;; Slim ;; Slim
("/\\(index\\|main\\)\\.slim$" "__" slim-mode) ("/\\(index\\|main\\)\\.slim$" "__" slim-mode)
;; Shell scripts ;; Shell scripts

View file

@ -4,16 +4,20 @@
;; behave together. ;; behave together.
(def-package! yasnippet (def-package! yasnippet
:commands (yas-minor-mode :commands (yas-minor-mode yas-minor-mode-on yas-expand yas-expand-snippet
yas-minor-mode-on yas-lookup-snippet yas-insert-snippet yas-new-snippet
yas-expand
yas-insert-snippet
yas-new-snippet
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
;; Ensure `yas-reload-all' is called as late as possible. Other modules could
;; have additional configuration for yasnippet. For example, file-templates.
(add-hook 'yas-minor-mode-hook '+snippets|load)
(defun +snippets|load (&rest _)
(yas-reload-all)
(remove-hook 'yas-minor-mode-hook '+snippets|load))
(add-hook! (text-mode prog-mode snippet-mode markdown-mode org-mode) (add-hook! (text-mode prog-mode snippet-mode markdown-mode org-mode)
'yas-minor-mode-on) 'yas-minor-mode-on)
@ -24,13 +28,6 @@
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))
;; 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)
;; 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) (advice-add 'yas-expand :before 'sp-remove-active-pair-overlay)