merge: pull request #7359 from theschmocker/snippet-command-fixes-and-enhancements
This commit is contained in:
commit
56187fc35a
1 changed files with 74 additions and 53 deletions
|
@ -33,7 +33,13 @@ ignored. This makes it easy to override built-in snippets with private ones."
|
||||||
(unless (file-directory-p dir)
|
(unless (file-directory-p dir)
|
||||||
(if (y-or-n-p (format "%S doesn't exist. Create it?" (abbreviate-file-name dir)))
|
(if (y-or-n-p (format "%S doesn't exist. Create it?" (abbreviate-file-name dir)))
|
||||||
(make-directory dir t)
|
(make-directory dir t)
|
||||||
(error "%S doesn't exist" (abbreviate-file-name dir)))))
|
(error "%S doesn't exist" (abbreviate-file-name dir))))
|
||||||
|
dir)
|
||||||
|
|
||||||
|
(defun +snippets--use-snippet-file-name-p (snippet-file-name)
|
||||||
|
(or (not (file-exists-p snippet-file-name))
|
||||||
|
(y-or-n-p (format "%s exists. Overwrite it?"
|
||||||
|
(abbreviate-file-name snippet-file-name)))))
|
||||||
|
|
||||||
(defun +snippet--get-template-by-uuid (uuid &optional mode)
|
(defun +snippet--get-template-by-uuid (uuid &optional mode)
|
||||||
"Look up the template by uuid in child-most to parent-most mode order.
|
"Look up the template by uuid in child-most to parent-most mode order.
|
||||||
|
@ -45,10 +51,9 @@ Finds correctly active snippets from parent modes (based on Yas' logic)."
|
||||||
return it))
|
return it))
|
||||||
|
|
||||||
(defun +snippet--completing-read-uuid (prompt all-snippets &rest args)
|
(defun +snippet--completing-read-uuid (prompt all-snippets &rest args)
|
||||||
(plist-get
|
(let* ((completion-uuid-alist
|
||||||
(text-properties-at
|
(cl-loop for (_ . tpl) in (mapcan #'yas--table-templates
|
||||||
0 (apply #'completing-read prompt
|
(if all-snippets
|
||||||
(cl-loop for (_ . tpl) in (mapcan #'yas--table-templates (if all-snippets
|
|
||||||
(hash-table-values yas--tables)
|
(hash-table-values yas--tables)
|
||||||
(yas--get-snippet-tables)))
|
(yas--get-snippet-tables)))
|
||||||
|
|
||||||
|
@ -57,13 +62,19 @@ Finds correctly active snippets from parent modes (based on Yas' logic)."
|
||||||
(yas--template-name tpl)
|
(yas--template-name tpl)
|
||||||
(abbreviate-file-name (yas--template-load-file tpl)))
|
(abbreviate-file-name (yas--template-load-file tpl)))
|
||||||
collect
|
collect
|
||||||
(progn
|
(cons txt (yas--template-uuid tpl))))
|
||||||
(set-text-properties 0 (length txt) `(uuid ,(yas--template-uuid tpl)
|
(completion (apply #'completing-read prompt completion-uuid-alist args)))
|
||||||
path ,(yas--template-load-file tpl))
|
(alist-get completion completion-uuid-alist nil nil #'string=)))
|
||||||
txt)
|
|
||||||
txt))
|
(defun +snippets--snippet-mode-name-completing-read (&optional all-modes)
|
||||||
args))
|
(cond (all-modes (completing-read
|
||||||
'uuid))
|
"Select snippet mode: "
|
||||||
|
obarray
|
||||||
|
(lambda (sym)
|
||||||
|
(string-match-p "-mode\\'" (symbol-name sym)))))
|
||||||
|
((not (null yas--extra-modes)) (completing-read "Select snippet mode: "
|
||||||
|
(cons major-mode yas--extra-modes)))
|
||||||
|
(t (symbol-name major-mode))))
|
||||||
|
|
||||||
(defun +snippet--abort ()
|
(defun +snippet--abort ()
|
||||||
(interactive)
|
(interactive)
|
||||||
|
@ -193,25 +204,30 @@ buggy behavior when <delete> is pressed in an empty field."
|
||||||
(user-error "Cannot find template with UUID %S" template-uuid)))
|
(user-error "Cannot find template with UUID %S" template-uuid)))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +snippets/new ()
|
(defun +snippets/new (&optional all-modes)
|
||||||
"Create a new snippet in `+snippets-dir'."
|
"Create a new snippet in `+snippets-dir'.
|
||||||
(interactive)
|
|
||||||
(let ((default-directory
|
If there are extra yasnippet modes active, or if ALL-MODES is non-nil, you will
|
||||||
(expand-file-name (symbol-name major-mode)
|
be prompted for the mode for which to create the snippet."
|
||||||
+snippets-dir)))
|
(interactive "P")
|
||||||
(+snippet--ensure-dir default-directory)
|
(let* ((mode (+snippets--snippet-mode-name-completing-read all-modes))
|
||||||
(with-current-buffer (switch-to-buffer "untitled-snippet")
|
(default-directory (+snippet--ensure-dir (expand-file-name mode +snippets-dir)))
|
||||||
|
(snippet-key (read-string "Enter a key for the snippet: "))
|
||||||
|
(snippet-file-name (expand-file-name snippet-key)))
|
||||||
|
(when (+snippets--use-snippet-file-name-p snippet-file-name)
|
||||||
|
(with-current-buffer (switch-to-buffer snippet-key)
|
||||||
(snippet-mode)
|
(snippet-mode)
|
||||||
(erase-buffer)
|
(erase-buffer)
|
||||||
|
(set-visited-file-name snippet-file-name)
|
||||||
(yas-expand-snippet (concat "# -*- mode: snippet -*-\n"
|
(yas-expand-snippet (concat "# -*- mode: snippet -*-\n"
|
||||||
"# name: $1\n"
|
"# name: $1\n"
|
||||||
"# uuid: $2\n"
|
"# uuid: $2\n"
|
||||||
"# key: ${3:trigger-key}${4:\n"
|
"# key: ${3:" snippet-key "}${4:\n"
|
||||||
"# condition: t}\n"
|
"# condition: t}\n"
|
||||||
"# --\n"
|
"# --\n"
|
||||||
"$0"))
|
"$0"))
|
||||||
(when (bound-and-true-p evil-local-mode)
|
(when (bound-and-true-p evil-local-mode)
|
||||||
(evil-insert-state)))))
|
(evil-insert-state))))))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +snippets/new-alias (template-uuid)
|
(defun +snippets/new-alias (template-uuid)
|
||||||
|
@ -224,21 +240,26 @@ You will be prompted for a snippet to alias."
|
||||||
current-prefix-arg)))
|
current-prefix-arg)))
|
||||||
(unless (require 'doom-snippets nil t)
|
(unless (require 'doom-snippets nil t)
|
||||||
(user-error "This command requires the `doom-snippets' library bundled with Doom Emacs"))
|
(user-error "This command requires the `doom-snippets' library bundled with Doom Emacs"))
|
||||||
(let ((default-directory (expand-file-name (symbol-name major-mode) +snippets-dir)))
|
(let* ((default-directory (+snippet--ensure-dir (expand-file-name
|
||||||
(+snippet--ensure-dir default-directory)
|
(symbol-name major-mode)
|
||||||
(with-current-buffer (switch-to-buffer "untitled-snippet")
|
+snippets-dir)))
|
||||||
|
(alias-key (read-string "Enter a key for the alias: "))
|
||||||
|
(alias-file-name (expand-file-name alias-key)))
|
||||||
|
(when (+snippets--use-snippet-file-name-p alias-file-name)
|
||||||
|
(with-current-buffer (switch-to-buffer alias-key)
|
||||||
(snippet-mode)
|
(snippet-mode)
|
||||||
(erase-buffer)
|
(erase-buffer)
|
||||||
|
(set-visited-file-name alias-file-name)
|
||||||
(yas-expand-snippet
|
(yas-expand-snippet
|
||||||
(concat "# -*- mode: snippet -*-\n"
|
(concat "# -*- mode: snippet -*-\n"
|
||||||
"# name: $1\n"
|
"# name: $1\n"
|
||||||
"# key: ${2:trigger-key}${3:\n"
|
"# key: ${2:" alias-key "}${3:\n"
|
||||||
"# condition: t}\n"
|
"# condition: t}\n"
|
||||||
"# type: command\n"
|
"# type: command\n"
|
||||||
"# --\n"
|
"# --\n"
|
||||||
"(%alias \"${4:" (or template-uuid "uuid") "}\")"))
|
"(doom-snippets-expand :uuid \"${4:" (or template-uuid "uuid") "}\")"))
|
||||||
(when (bound-and-true-p evil-local-mode)
|
(when (bound-and-true-p evil-local-mode)
|
||||||
(evil-insert-state)))))
|
(evil-insert-state))))))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +snippets/edit (template-uuid)
|
(defun +snippets/edit (template-uuid)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue