João | 12 May 17:54 2010
Picon

Re: Dynamic snippet creation depending on field value

As always a little bit more work than I expected. update your SVN copy 
of yasnippet and try this snippet.

-----------
# -*- mode: snippet -*-
# name: format string
# key: str
# type: command
# --
(yas/expand-snippet (concat "\"${1:hello world, %s}\" 
${0:${1:$(yas/python-make-format-args yas/text 
'no-fields)}$(yas/python-format-string-snippet-2)}"))
---------

Then you need to add the following code to a (new) 
snippets/python-mode/.yas-setup.el file, or have it eval'ed somewhere 
before you expand the snippet.

---------
(defvar yas/python-format-string-last-string nil)
(defun yas/python-make-format-args (text &optional dont-make-fields)
   (let ((output)
         (nr 1))
     (set-match-data nil)
     (while (string-match "%." text (match-end 0))
       (setq output (if (eq nr 1) " % (" (concat output ", ")))
       (setq output (concat output
                            (unless dont-make-fields (format "${%d:" nr))
                            (format "arg%d" nr)
                            (unless dont-make-fields "}")))
       (incf nr))
     (setq yas/python-format-string-last-string text)
     (if output (setq output (concat output ")")))))

(defun yas/python-format-string-snippet-2 ()
   (yas/expand-snippet (yas/python-make-format-args 
yas/python-format-string-last-string))
   (deactivate-mark))
--------

Do consider that this is all in the trunk, and might be unstable, 
especially with when attempting undo/redo.

Check back here if it worked for you,
João

On 05/12/2010 01:38 AM, João wrote:
> I think what Luke is looking for is a snippet that dinamically adds
> fields to itself as you enter text in one of the mirros. This is not
> possible currently, unfortunately, and it would take considerable effort
> to do so.
>
> Nevertheless this pydox snippet is very close to solution I'm looking
> into which is having one snippet expand the string and mirror the fields
> in the end. When you exit that snippet another, second snippet is
> expanded. This second snippet is built much like your python-doxygen
> snippet.
>
> For this to work nicely, a small feature needs to be added, which is
> per-snippet exit hooks. I'll get back to this tomorrow.
>
> Joao
>
> PS: Any more interesting python snippets like this one that you might
> consider contributing?
>
> On 05/12/2010 12:54 AM, jpkotta wrote:
>>
>>
>> On May 11, 10:44 am, Luke<mmmasterl...@...> wrote:
>>> Hello,
>>>
>>> congratulations on YASnippet, I love it!
>>>
>>> I read in [1] how to create snippets dynamically based on user input
>>> before the snippet is created.
>>> Is it possible to create snippets based on a field value of a snippet
>>> AFTER the expansion has started?
>>>
>>> What I have in mind are formatted Python strings:
>>>
>>> 'I would like %d %s(s), please' % (nr, item)
>>>
>>> I'd like a snippet that counts the number of "%" characters while I'm
>>> typing a string and then adds the correct number of tuple elements
>>> behind the string. Each element should be a field and the fields
>>> should be separated by commas.
>>>
>>> For example, when I type
>>>
>>> 'the avlue of %s is between %d and %d'
>>>
>>> I'd like
>>>
>>> "% (arg1, arg2, arg3)"
>>>
>>> to be inserted with argN being a field so I can move around with TAB
>>> and S-TAB without worrying about the commas.
>>>
>>> Any suggestions?
>>>
>>> Luke
>>>
>>> [1]http://groups.google.com/group/smart-snippet/browse_thread/thread/f0e...
>>>
>>>
>>
>> It's possible. Here's a snippet for generating a Doxygen comment
>> based on the arguments of a (python) function. doxymacs-find-next-
>> func returns an alist of data about the function definition. You'd
>> have to write a similar function to find and parse your string.
>>
>> ,----
>> | # -*- mode: snippet -*-
>> | # name: python-doxygen
>> | # key: pydox
>> | # type: command
>> | # --
>> | (let* ((next-func-alist (doxymacs-find-next-func))
>> | (func-name (cdr (assoc 'func next-func-alist)))
>> | (params-list (cdr (assoc 'args next-func-alist)))
>> | (return-name (cdr (assoc 'return next-func-alist))) ;; broken
>> in python
>> | (snippet-text "")
>> | (idx 1))
>> | (setq snippet-text (format "## ${1:%s}\n" func-name))
>> | (setq idx 2)
>> | (dolist (param params-list)
>> | (unless (string= param "self")
>> | (setq snippet-text (concat snippet-text
>> | (format "# \\param %s ${%d:
>> (optional, default=${%d:None}) ${%d:?}}\n"
>> | param idx (1+ idx) (+ 2
>> idx))))
>> | (setq idx (+ 3 idx))))
>> | (setq snippet-text (concat snippet-text
>> | (format "# \\return ${%d:%s}"
>> | idx "None")))
>> | (yas/expand-snippet snippet-text))
>> `----
>>
>> It expands like this:
>>
>> ,----
>> | ## foo
>> | # \param a (optional, default=None) ?
>> | # \param b (optional, default=None) ?
>> | # \param c (optional, default=None) ?
>> | # \return None
>> | def foo(a, b, c):
>> | pass
>> `----
>>

--

-- 
You received this message because you are subscribed to the Google Groups "smart-snippet and YASnippet" group.
To post to this group, send email to smart-snippet@...
To unsubscribe from this group, send email to smart-snippet+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/smart-snippet?hl=en.


Gmane