Skip to content

Commit

Permalink
Closes #318: correctly implement YAS--MODES-TO-ACTIVATE
Browse files Browse the repository at this point in the history
- simpler and got rid of YAS--ALL-PARENTS
- also got rid of YAS/MODE-SYMBOL backward compatibility hack
  • Loading branch information
joaotavora committed Aug 31, 2013
1 parent ddaf9a9 commit a7d40ed
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 46 deletions.
24 changes: 18 additions & 6 deletions yasnippet-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ TODO: correct this bug!"
(funcall ,saved-sym))))
,@body))))


(defmacro yas-with-some-interesting-snippet-dirs (&rest body)
`(yas-saving-variables
(yas-with-overriden-buffer-list
Expand All @@ -327,6 +328,7 @@ TODO: correct this bug!"
("lisp-interaction-mode" ("sc" . "brother from another mother"))))
,@body))))


(ert-deftest basic-jit-loading ()
"Test basic loading and expansion of snippets"
(yas-with-some-interesting-snippet-dirs
Expand All @@ -351,14 +353,24 @@ TODO: correct this bug!"
("c-mode"
(".yas-parents" . "cc-mode"))
("cc-mode"
(".yas-parents" . "yet-another-c-mode"))
(".yas-parents" . "yet-another-c-mode and-that-one"))
("yet-another-c-mode"
(".yas-parents" . "c-mode"))))
(".yas-parents" . "c-mode and-also-this-one lisp-interaction-mode"))))
(yas-reload-all)
(condition-case nil
(yas--all-parents 'c-mode)
(error
(ert-fail "cyclic parenthood test failed"))))))
(with-temp-buffer
(let* ((major-mode 'c-mode)
(expected '(c-mode
cc-mode
yet-another-c-mode
and-also-this-one
and-that-one
prog-mode
emacs-lisp-mode
lisp-interaction-mode))
(observed (yas--modes-to-activate)))
(should (null (cl-set-exclusive-or expected observed)))
(should (= (length expected)
(length observed))))))))

(defun yas--basic-jit-loading-1 ()
(with-temp-buffer
Expand Down
53 changes: 13 additions & 40 deletions yasnippet.el
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@
;; `yas-snippet-dirs' and is used for deciding which
;; snippets to consider for the active buffer.
;;
;; Deprecated `yas/mode-symbol' aliases this variable for
;; backward-compatibility.
;;
;; Major commands are:
;;
;; M-x yas-expand
Expand Down Expand Up @@ -674,11 +671,6 @@ There might be additional parenting information stored in the
`derived-mode-parent' property of some mode symbols, but that is
not recorded here.")

(defvar yas--ancestors (make-hash-table)
"A hash table of mode symbols do lists of all parent mode symbols.
A cache managed by `yas--all-parents'")

(defvar yas--direct-keymaps (list)
"Keymap alist supporting direct snippet keybindings.
Expand All @@ -704,17 +696,19 @@ defined direct keybindings to the command
(defun yas--modes-to-activate ()
"Compute list of mode symbols that are active for `yas-expand'
and friends."
(let ((modes-to-activate (list major-mode))
(mode major-mode))
(while (setq mode (get mode 'derived-mode-parent))
(push mode modes-to-activate))
(dolist (mode (yas-extra-modes))
(push mode modes-to-activate))
(remove-duplicates
(append modes-to-activate
(mapcan #'(lambda (mode)
(yas--all-parents mode))
modes-to-activate)))))
(cl-labels

This comment has been minimized.

Copy link
@syohex

syohex Sep 2, 2013

Contributor

cl-lables is introduced at Emacs 24.3. Other version users need to load cl-lib.el((require 'cl)).
cl-lib.el is not a default package for Emacs 24.2 or lower users. If you use cl-lables macro,
please add Package-Requires directive like following URL.

https://github.com/magit/magit/blob/master/magit.el#L20

This comment has been minimized.

Copy link
@joaotavora

joaotavora Sep 2, 2013

Author Owner

Alright, replaced with (let ((dfs (lambda ...)))) in ce50b3d

((dfs (mode &optional explored)
(push mode explored)
(cons mode
(loop for neighbour
in (remove nil (cons (get mode
'derived-mode-parent)
(gethash mode yas--parents)))

unless (memq neighbour explored)
append (dfs neighbour explored)))))
(remove-duplicates (append yas-extra-modes
(dfs major-mode)))))

(defvar yas-minor-mode-hook nil
"Hook run when `yas-minor-mode' is turned on.")
Expand Down Expand Up @@ -1162,24 +1156,6 @@ conditions to filter out potential expansions."
(t
(eq requirement result)))))

(defun yas--all-parents (mode)
"Return a list of all parent modes of MODE."
(or (gethash mode yas--ancestors)
(let ((seen '()))
(labels ((yas--all-parents-1
(m)
(cond ((memq m seen)
(yas--message 1
"Cyclic parenthood: mode %s has already seen as a parent of mode %s"
m mode)
nil)
(t
(let* ((parents (gethash m yas--parents)))
(setq seen (append seen parents))
(append parents (mapcan #'yas--all-parents-1 parents)))))))
(puthash mode (yas--all-parents-1 mode)
yas--ancestors)))))

(defun yas--table-templates (table)
(when table
(let ((acc (list)))
Expand Down Expand Up @@ -1301,9 +1277,6 @@ Can be a symbol or a list of symbols.
This variable probably makes more sense as buffer-local, so
ensure your use `make-local-variable' when you set it.")
(defun yas-extra-modes ()
(if (listp yas-extra-modes) yas-extra-modes (list yas-extra-modes)))
(defvaralias 'yas/mode-symbol 'yas-extra-modes)

(defun yas--table-get-create (mode)
"Get or create the snippet table corresponding to MODE."
Expand Down

0 comments on commit a7d40ed

Please sign in to comment.