Let snippets expand in strings/comments by default

* yasnippet.el (yas-buffer-local-condition): Change default to t.
* doc/snippet-expansion.org: Update documentation.
This commit is contained in:
Noam Postavsky 2017-02-04 11:23:50 -05:00
parent c87afe0901
commit 4f37afd493
2 changed files with 18 additions and 32 deletions

View File

@ -178,32 +178,29 @@ In particular, the following things matter:
(yas-activate-extra-mode 'rails-mode))) (yas-activate-extra-mode 'rails-mode)))
#+END_SRC #+END_SRC
- Buffer-local - Buffer-local [[sym:yas-buffer-local-condition][=yas-buffer-local-condition=]] variable
[[sym:yas-buffer-local-condition][=yas-buffer-local-condition=]]
variable
This variable provides finer grained control over what snippets can This variable provides finer grained control over what snippets can
be expanded in the current buffer. The default value won't let you be expanded in the current buffer. You could disable snippet
expand snippets inside comments or string literals for example. See expansion inside comments or string literals for example. See [[condition-system][the
The condition system\_ for more info. condition system]] for more info.
** The condition system ** The condition system <<condition-system>>
Consider this scenario: you are an old Emacs hacker. You like the Consider this scenario: you are an old Emacs hacker. You like the
abbrev-way and bind [[sym:yas-expand][=yas-expand=]] to =SPC=. However, you don't want abbrev-way and bind [[sym:yas-expand][=yas-expand=]] to =SPC=. However, you don't want
=if= to be expanded as a snippet when you are typing in a comment =if= to be expanded as a snippet when you are typing in a comment
block or a string (e.g. in =python-mode=). block or a string (e.g. in =python-mode=).
If you use the =# condition := directive (see If you use the =# condition := directive (see [[./snippet-development.org][Writing Snippets]]) you
[[./snippet-development.org][Writing Snippets]]) you could just specify could just specify the condition for =if= to be =(not
the condition for =if= to be =(not (python-syntax-comment-or-string-p))=. But how (python-syntax-comment-or-string-p))=. But how about =while=, =for=,
about =while=, =for=, etc. ? Writing the same condition for all the etc? Writing the same condition for all the snippets is just boring.
snippets is just boring. So has a buffer local variable So you can instead set [[sym:yas-buffer-local-condition][=yas-buffer-local-condition=]] to =(not
[[sym:yas-buffer-local-condition][=yas-buffer-local-condition=]]. You can set this variable to (python-syntax-comment-or-string-p))= in =python-mode-hook=.
=(not (python-syntax-comment-or-string-p))= in =python-mode-hook=.
Then, what if you really want some particular snippet to expand even Then, what if you really want some particular snippet to expand even
inside a comment? Set [[sym:yas-buffer-local-condition][=yas-buffer-local-condition=]] like this inside a comment? Set [[sym:yas-buffer-local-condition][=yas-buffer-local-condition=]] like this
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp
(add-hook 'python-mode-hook (add-hook 'python-mode-hook
@ -214,10 +211,10 @@ inside a comment? Set [[sym:yas-buffer-local-condition][=yas-buffer-local-condit
t)))) t))))
#+END_SRC #+END_SRC
... and specify the condition for a snippet that you're going to expand ... and for a snippet that you want to expand in comments, specify a
in comment to be evaluated to the symbol =force-in-comment=. Then it can condition which evaluates to the symbol =force-in-comment=. Then it
be expanded as you expected, while other snippets like =if= still can't can be expanded as you expected, while other snippets like =if= still
expanded in comment. can't expanded in comments.
For the full set of possible conditions, see the documentation for For the full set of possible conditions, see the documentation for
[[sym:yas-buffer-local-condition][=yas-buffer-local-condition=]]. [[sym:yas-buffer-local-condition][=yas-buffer-local-condition=]].

View File

@ -456,13 +456,7 @@ Attention: These hooks are not run when exiting nested/stacked snippet expansion
'() '()
"Hooks to run just before expanding a snippet.") "Hooks to run just before expanding a snippet.")
(defvar yas-buffer-local-condition (defvar yas-buffer-local-condition t
'(if (and (let ((ppss (syntax-ppss)))
(or (nth 3 ppss) (nth 4 ppss)))
(memq this-command '(yas-expand yas-expand-from-trigger-key
yas-expand-from-keymap)))
'(require-snippet-condition . force-in-comment)
t)
"Snippet expanding condition. "Snippet expanding condition.
This variable is a Lisp form which is evaluated every time a This variable is a Lisp form which is evaluated every time a
@ -509,12 +503,7 @@ conditions.
(setq yas-buffer-local-condition (setq yas-buffer-local-condition
\\='(if (python-syntax-comment-or-string-p) \\='(if (python-syntax-comment-or-string-p)
\\='(require-snippet-condition . force-in-comment) \\='(require-snippet-condition . force-in-comment)
t)))) t))))")
The default value is similar, it filters out potential snippet
expansions inside comments and string literals, unless the
snippet itself contains a condition that returns the symbol
`force-in-comment'.")
;;; Internal variables ;;; Internal variables