Merge: snippet-local exit hook; error handling improvements

This commit is contained in:
Noam Postavsky 2017-02-12 11:19:41 -05:00
commit c87afe0901
2 changed files with 85 additions and 49 deletions

View File

@ -553,6 +553,36 @@ TODO: correct this bug!"
"brother from another mother") ;; no newline should be here! "brother from another mother") ;; no newline should be here!
))) )))
(ert-deftest snippet-exit-hooks ()
(defvar yas--ran-exit-hook)
(with-temp-buffer
(yas-saving-variables
(let ((yas--ran-exit-hook nil)
(yas-triggers-in-field t))
(yas-with-snippet-dirs
'((".emacs.d/snippets"
("emacs-lisp-mode"
("foo" . "\
# expand-env: ((yas-after-exit-snippet-hook (lambda () (setq yas--ran-exit-hook t))))
# --
FOO ${1:f1} ${2:f2}")
("sub" . "\
# expand-env: ((yas-after-exit-snippet-hook (lambda () (setq yas--ran-exit-hook 'sub))))
# --
SUB"))))
(yas-reload-all)
(emacs-lisp-mode)
(yas-minor-mode +1)
(insert "foo")
(ert-simulate-command '(yas-expand))
(should-not yas--ran-exit-hook)
(yas-mock-insert "sub")
(ert-simulate-command '(yas-expand))
(ert-simulate-command '(yas-next-field))
(should-not yas--ran-exit-hook)
(ert-simulate-command '(yas-next-field))
(should (eq yas--ran-exit-hook t)))))))
(defvar yas--barbaz) (defvar yas--barbaz)
(defvar yas--foobarbaz) (defvar yas--foobarbaz)

View File

@ -340,9 +340,16 @@ per-snippet basis. A value of `cua' is considered equivalent to
(const cua))) ; backwards compat (const cua))) ; backwards compat
(defcustom yas-good-grace t (defcustom yas-good-grace t
"If non-nil, don't raise errors in inline elisp evaluation. "If non-nil, don't raise errors in elisp evaluation.
An error string \"[yas] error\" is returned instead." This affects both the inline elisp in snippets and the hook
variables such as `yas-after-exit-snippet-hook'.
If this variable's value is `inline', an error string \"[yas]
error\" is returned instead of raising the error. If this
variable's value is `hooks', a message is output to according to
`yas-verbosity-level'. If this variable's value is t, both are
active."
:type 'boolean) :type 'boolean)
(defcustom yas-visit-from-menu nil (defcustom yas-visit-from-menu nil
@ -1323,33 +1330,22 @@ Returns (TEMPLATES START END). This function respects
;;; Internal functions and macros: ;;; Internal functions and macros:
(defun yas--handle-error (err) (defun yas--eval-for-string (form)
"Handle error depending on value of `yas-good-grace'."
(let ((msg (yas--format "elisp error: %s" (error-message-string err))))
(if yas-good-grace msg
(error "%s" msg))))
(defun yas--eval-lisp (form)
"Evaluate FORM and convert the result to string." "Evaluate FORM and convert the result to string."
(let ((retval (catch 'yas--exception (let ((debug-on-error (and (not (memq yas-good-grace '(t inline)))
(condition-case err debug-on-error)))
(save-excursion (condition-case oops
(save-restriction (save-excursion
(save-match-data (save-restriction
(widen) (save-match-data
(let ((result (eval form))) (widen)
(when result (let ((result (eval form)))
(format "%s" result)))))) (when result
(error (yas--handle-error err)))))) (format "%s" result))))))
(when (and (consp retval) ((debug error) (cdr oops)))))
(eq 'yas--exception (car retval)))
(error (cdr retval)))
retval))
(defun yas--eval-lisp-no-saves (form) (defun yas--eval-for-effect (form)
(condition-case err (yas--safely-run-hook (apply-partially #'eval form)))
(eval form)
(error (message "%s" (yas--handle-error err)))))
(defun yas--read-lisp (string &optional nil-on-error) (defun yas--read-lisp (string &optional nil-on-error)
"Read STRING as a elisp expression and return it. "Read STRING as a elisp expression and return it.
@ -1665,7 +1661,7 @@ this is a snippet or a snippet-command.
CONDITION, EXPAND-ENV and KEYBINDING are Lisp forms, they have CONDITION, EXPAND-ENV and KEYBINDING are Lisp forms, they have
been `yas--read-lisp'-ed and will eventually be been `yas--read-lisp'-ed and will eventually be
`yas--eval-lisp'-ed. `yas--eval-for-string'-ed.
The remaining elements are strings. The remaining elements are strings.
@ -1758,8 +1754,7 @@ With prefix argument USE-JIT do jit-loading of snippets."
;; ;;
(yas--define-parents mode-sym parents) (yas--define-parents mode-sym parents)
(yas--menu-keymap-get-create mode-sym) (yas--menu-keymap-get-create mode-sym)
(let ((fun `(lambda () ;; FIXME: Simulating lexical-binding. (let ((fun (apply-partially #'yas--load-directory-1 dir mode-sym)))
(yas--load-directory-1 ',dir ',mode-sym))))
(if use-jit (if use-jit
(yas--schedule-jit mode-sym fun) (yas--schedule-jit mode-sym fun)
(funcall fun))) (funcall fun)))
@ -2854,16 +2849,16 @@ The last element of POSSIBILITIES may be a list of strings."
key))))) key)))))
(defun yas-throw (text) (defun yas-throw (text)
"Throw a yas--exception with TEXT as the reason." "Signal `yas-exception' with TEXT as the reason."
(throw 'yas--exception (cons 'yas--exception text))) (signal 'yas-exception (list text)))
(put 'yas-exception 'error-conditions '(error yas-exception))
(put 'yas-exception 'error-message "[yas] Exception")
(defun yas-verify-value (possibilities) (defun yas-verify-value (possibilities)
"Verify that the current field value is in POSSIBILITIES. "Verify that the current field value is in POSSIBILITIES.
Otherwise signal `yas-exception'."
Otherwise throw exception." (when (and yas-moving-away-p (cl-notany (lambda (pos) (string= pos yas-text)) possibilities))
(when (and yas-moving-away-p (yas-throw (format "Field only allows %s" possibilities))))
(cl-notany (lambda (pos) (string= pos yas-text)) possibilities))
(yas-throw (yas--format "Field only allows %s" possibilities))))
(defun yas-field-value (number) (defun yas-field-value (number)
"Get the string for field with NUMBER. "Get the string for field with NUMBER.
@ -3020,7 +3015,7 @@ string iff EMPTY-ON-NIL-P is true."
(transformed (and transform (transformed (and transform
(save-excursion (save-excursion
(goto-char start-point) (goto-char start-point)
(let ((ret (yas--eval-lisp transform))) (let ((ret (yas--eval-for-string transform)))
(or ret (and empty-on-nil-p ""))))))) (or ret (and empty-on-nil-p "")))))))
transformed)) transformed))
@ -3333,12 +3328,19 @@ This renders the snippet as ordinary text."
(yas--maybe-move-to-active-field snippet)) (yas--maybe-move-to-active-field snippet))
(setq yas--snippets-to-move nil)) (setq yas--snippets-to-move nil))
(defun yas--safely-run-hooks (hook-var) (defun yas--safely-call-fun (fun)
(condition-case error (condition-case error
(run-hooks hook-var) (funcall fun)
(error ((debug error)
(yas--message 2 "%s error: %s" hook-var (error-message-string error))))) (yas--message 2 "Error running %s: %s"
(if (symbolp fun) fun "a hook")
(error-message-string error)))))
(defun yas--safely-run-hook (hook)
(let ((debug-on-error (and (not (memq yas-good-grace '(t hooks)))
debug-on-error)))
(if (functionp hook) (yas--safely-call-fun hook)
(mapc #'yas--safely-call-fun hook))))
(defun yas--check-commit-snippet () (defun yas--check-commit-snippet ()
"Check if point exited the currently active field of the snippet. "Check if point exited the currently active field of the snippet.
@ -3346,15 +3348,19 @@ This renders the snippet as ordinary text."
If so cleans up the whole snippet up." If so cleans up the whole snippet up."
(let* ((snippets (yas-active-snippets 'all)) (let* ((snippets (yas-active-snippets 'all))
(snippets-left snippets) (snippets-left snippets)
(snippet-exit-transform)) (snippet-exit-transform nil)
(snippet-exit-hook yas-after-exit-snippet-hook))
(dolist (snippet snippets) (dolist (snippet snippets)
(let ((active-field (yas--snippet-active-field snippet))) (let ((active-field (yas--snippet-active-field snippet)))
(yas--letenv (yas--snippet-expand-env snippet) (yas--letenv (yas--snippet-expand-env snippet)
;; Note: the `force-exit' field could be a transform in case of
;; ${0: ...}, see `yas--move-to-field'.
(setq snippet-exit-transform (yas--snippet-force-exit snippet)) (setq snippet-exit-transform (yas--snippet-force-exit snippet))
(cond ((or snippet-exit-transform (cond ((or snippet-exit-transform
(not (and active-field (yas--field-contains-point-p active-field)))) (not (and active-field (yas--field-contains-point-p active-field))))
(setq snippets-left (delete snippet snippets-left)) (setq snippets-left (delete snippet snippets-left))
(setf (yas--snippet-force-exit snippet) nil) (setf (yas--snippet-force-exit snippet) nil)
(setq snippet-exit-hook yas-after-exit-snippet-hook)
(yas--commit-snippet snippet)) (yas--commit-snippet snippet))
((and active-field ((and active-field
(or (not yas--active-field-overlay) (or (not yas--active-field-overlay)
@ -3371,8 +3377,8 @@ If so cleans up the whole snippet up."
nil))))) nil)))))
(unless (or (null snippets) snippets-left) (unless (or (null snippets) snippets-left)
(if snippet-exit-transform (if snippet-exit-transform
(yas--eval-lisp-no-saves snippet-exit-transform)) (yas--eval-for-effect snippet-exit-transform))
(yas--safely-run-hooks 'yas-after-exit-snippet-hook)))) (yas--safely-run-hook snippet-exit-hook))))
;; Apropos markers-to-points: ;; Apropos markers-to-points:
;; ;;
@ -3648,7 +3654,7 @@ considered when expanding the snippet."
(cond ((listp content) (cond ((listp content)
;; x) This is a snippet-command ;; x) This is a snippet-command
;; ;;
(yas--eval-lisp-no-saves content)) (yas--eval-for-effect content))
(t (t
;; x) This is a snippet-snippet :-) ;; x) This is a snippet-snippet :-)
;; ;;
@ -4169,9 +4175,9 @@ with their evaluated value into `yas--backquote-markers-and-strings'."
(delete-region (match-beginning 0) (match-end 0))) (delete-region (match-beginning 0) (match-end 0)))
(let ((before-change-functions (let ((before-change-functions
(cons detect-change before-change-functions))) (cons detect-change before-change-functions)))
(setq transformed (yas--eval-lisp (yas--read-lisp (setq transformed (yas--eval-for-string (yas--read-lisp
(yas--restore-escapes (yas--restore-escapes
current-string '(?`)))))) current-string '(?`))))))
(goto-char (match-beginning 0)) (goto-char (match-beginning 0))
(when transformed (when transformed
(let ((marker (make-marker)) (let ((marker (make-marker))