joaotavora / yasnippet

A template system for Emacs
http://joaotavora.github.io/yasnippet/
2.78k stars 311 forks source link

Suggestion IDO discover keys #174

Closed joaotavora closed 12 years ago

joaotavora commented 12 years ago

When new yasnippet user wants to try out, he needs to find out what keys exist. Menu is fine, but some users prefer not to have any menu in emacs (really).

I created an IDO-based function that allow to pick possible templates. This also allows to type just part of the key, or nothing at all, to get to an IDO menu of possible choices. File attached. Feel welcome to copy from it.

Also hippie-expand can expand to key by prefix.

Google Code Info: Issue #: 172 Author: tibokr...@gmail.com Created On: 2011-04-09T17:03:09.000Z Closed On: 2011-05-08T02:59:53.000Z

joaotavora commented 12 years ago

interesting, but in what way is this different from yas/insert snippet, with yas/prompt-functions being lead by `yas/ido-prompt'? It does seems to provide some provide some extra functionality, but I would rather see it integrated into the existing yasnippet.el logic.

Also to describe the currently active snippet tables, `yas/describe-tables' is a great non-menu way to find out what keys exist. this has been in the svn trunk of yasnippet for a while.

Marking this wontfix, may change it if you make a convincing case :-D

Google Code Info: Author: joaotav...@gmail.com Created On: 2011-05-08T02:59:53.000Z

joaotavora commented 12 years ago

Thanks for the feedback. The difference to yas/insert snippet is in what the choices are.

yas/ido-prompt offers something like: "Choose a snippet: {class ... { ... } | namespace ... | template <typename ...> | ...}"

My function offers in comparison: "Select template: {class | ns | template | ...}"

This allows to learn the template key while working, not as an extra activity.

And mine is integrated as a replacement to yas/expand, using also the prefix, such that cursor after "cla" cla|

offers "Select template: cla| [class]"

This makes it possible to use ido on the yas trigger key. I would also indeed suggest integrating it with that. yas/expand is not interactive that way now, and offering an interactive yas/insert-snippet does not make yas/expand more interactive.

I refer to Eclipse, where C-Space offers similar functionality. Eclipse does equally not require users to call a different function to find out what template triggers exist for C-Space.

Take this scenario. In an emacs-lisp file,the user want to write (yes-or-no-p "PROMPT ") to do so, the user types (yesno|) presses the yas trigger, but nothing happens, as this is not the right key, the user remembered or guessed wrongly. The user could now try to remember the right key, or call other function and lookup the right key, but the time that takes, he will have typed the expression quicker. IDO philosophy is that while the bit the user typed is wrong, it is sufficient for emacs to offer a useful choice, and that's what my function does: "Select template: yesno| [yes-or-no-p]"

Google Code Info: Author: tibokr...@gmail.com Created On: 2011-05-08T09:28:25.000Z

joaotavora commented 12 years ago

There is a variable yas/choose-keys-first that controls just that: when prompting for snippets, prompt for the keys first, then the names (should the smae key reference two different snippets).

Unfortunately, in the svn version of yasnippet, it's broken. I'm working on that. But do try it out in yasnippet 0.6.

Google Code Info: Author: joaotav...@gmail.com Created On: 2011-05-08T14:11:32.000Z

joaotavora commented 12 years ago

`yas/choose-keys-first' should now be working in the svn trunk, rev 665. Try it out and and tell me what you think is still missing.

Google Code Info: Author: joaotav...@gmail.com Created On: 2011-05-08T14:30:23.000Z

joaotavora commented 12 years ago

I tried yas/choose-keys-first from 665 and it works very similarly to what I provide except that it does not work on yas/expand and a present prefix is not considered.

So a user either has to use yas/expand or yas/insert, I don't know why there are those two to start with, but I guess some users will prefer expand, others will prefer insert. Those using expand should also benefit from IDO, IMHO.

Google Code Info: Author: tibokr...@gmail.com Created On: 2011-05-08T15:15:32.000Z

joaotavora commented 12 years ago

`yas/expand' expands an abbreviation into a snippet. if the text preceding point is not an abbreviation, TAB (or whatever yas/trigger-key is) reverts to its default binding.

YASnippet is not a code completing package, and, as I've said before, is aimed at working closely to textmate's snippet system.

The "philosophy" if you will, when using yas/expand is not to break the workflow too much with dialogs and stuff: if and only if an valid key is found a snippet is expanded, otherwise the yas/expand-key does nothing, or indents, and the user isn't bothered.

Doing the proposed fuzzy search on yas/expand would make many more false expansions, bits of text that aren't snippet keys but can be parts of snippet keys.

That said, there is nothing stopping you from binding `yas/insert-snippet' to some keybinding (it's currently C-c & C-s) and insert snippets from there. Also, there is nothing stopping you from doing

(setq yas/prompt-functions (list 'yas/my-ido-prompt-with-initial-input))

(defun yas/my-ido-prompt-with-initial-input (prompt choices &optional display-fn) (let (formatted-choices filtered-choices chosen d) (dolist (choice choices) (setq d (or (and display-fn (funcall display-fn choice)) choice)) (when (stringp d) (push d formatted-choices) (push choice filtered-choices))) (setq chosen (and formatted-choices (ido-completing-read prompt formatted-choices nil 'require-match (thing-at-point 'word) nil))) (when chosen (nth (position chosen formatted-choices :test #'string=) filtered-choices))))

which is basically copying the current yas/completing-read and adding the initial-input argument to the completing-read function. This could also be improved to return the only match if there is only one match.

Google Code Info: Author: joaotav...@gmail.com Created On: 2011-05-08T20:44:08.000Z

joaotavora commented 12 years ago

ok, I see. I don't know much about textmate, but I see that tab should not be interactive then (menus etc.). I will use insert-snippet instead as you suggest. sorry about my confusion.

Google Code Info: Author: tibokr...@gmail.com Created On: 2011-05-08T21:22:49.000Z