Add function do return a vim dictionary with current snippets

Bug #1171227 reported by Jorge
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
UltiSnips
Fix Committed
Undecided
Unassigned

Bug Description

Hi Sirver,

I wrote two small functions, one in vimL UltiSnips_SnippetDict which calls another function in python
list_snippets_dict which is similar to list_snippets but just returns a vim dictionary with the snippets and description.

The purpose of this is to be able to list the matching snippets via a pump instead of in the bottom of the window.
For this the user would need to install two plugins from Ingo Karkat: SnippetComplete and SnippetCompleteSnipMate.

I will email the author of those plugins to see if he can change one of them so it will support ultisnips
(the only change will be to replace the line

let l:snippets = GetSnipsInCurrentScope()
by the line
let l:snippets = UltiSnips_SnippetDict()

so I don't antecipate a big problem there.

I tried to record a short video showing the functionality:
http://youtu.be/Cm7lzcS7SWM

Please accept the patch or let me know issues you see with it.
If you decide to accept and once I hear from Ingo I will also add additional information to the documentation so the users can use this functionality.

Revision history for this message
Jorge (skeept) wrote :
Revision history for this message
SirVer (sirver) wrote :

take a look at the current v3.0 branch. I think what you want is already implemented there - though the code is not pretty.

Changed in ultisnips:
status: New → Incomplete
Revision history for this message
Jorge (skeept) wrote : Re: [Bug 1171227] Re: Add function do return a vim dictionary with current snippets

I have looked at Mark Webber's fork before, but for the some reason I
decided to stick with the Ultisnips main branch.

I will welcome the new release when it is done...

Regards,

Jorge

On Sun, Apr 21, 2013 at 3:48 PM, SirVer <email address hidden> wrote:

> take a look at the current v3.0 branch. I think what you want is already
> implemented there - though the code is not pretty.
>
>
> ** Changed in: ultisnips
> Status: New => Incomplete
>
> --
> You received this bug notification because you are subscribed to the bug
> report.
> https://bugs.launchpad.net/bugs/1171227
>
> Title:
> Add function do return a vim dictionary with current snippets
>
> Status in UltiSnips - The Ultimate Snippet Solution for Vim:
> Incomplete
>
> Bug description:
> Hi Sirver,
>
> I wrote two small functions, one in vimL UltiSnips_SnippetDict which
> calls another function in python
> list_snippets_dict which is similar to list_snippets but just returns a
> vim dictionary with the snippets and description.
>
> The purpose of this is to be able to list the matching snippets via a
> pump instead of in the bottom of the window.
> For this the user would need to install two plugins from Ingo Karkat:
> SnippetComplete and SnippetCompleteSnipMate.
>
> I will email the author of those plugins to see if he can change one of
> them so it will support ultisnips
> (the only change will be to replace the line
>
> let l:snippets = GetSnipsInCurrentScope()
> by the line
> let l:snippets = UltiSnips_SnippetDict()
>
> so I don't antecipate a big problem there.
>
> I tried to record a short video showing the functionality:
> http://youtu.be/Cm7lzcS7SWM
>
> Please accept the patch or let me know issues you see with it.
> If you decide to accept and once I hear from Ingo I will also add
> additional information to the documentation so the users can use this
> functionality.
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/ultisnips/+bug/1171227/+subscriptions
>

Revision history for this message
SirVer (sirver) wrote :

I am reluctant to apply this patch, mainly because it will likely break with v3.0 again - if you feel very strongly about it, please at least add a test for it, so there is a chance this will make it through.

Revision history for this message
Jorge (skeept) wrote :

I understand your reluctancy to apply the patch, and from my point of view
if you are working actively in the 3.0 branch I might as well switch to
that branch already.
Like I mentioned I tried it before and at the time I didn't have time to do
the change because some things changed slightly, but I will find time in
the near future.

Having said that, all the patch I suggest does it define a function that
returns a dictionary with the current snippets. The pump stuff is all done
outside ultisnips.
I think it will be usefull to ultisnips to have such a function returning
the current snippets so it can be integrated with other tools. I am
thinking about neocomlcache that provides completion from different
sources, but there might be other plugins doing similar things.

But again, there migth even be a way of getting the list of current
snippets without changing the code inside ultisnips, just defining an
external function... Is this possible at this point?

On Mon, Apr 22, 2013 at 12:26 AM, SirVer <email address hidden> wrote:

> I am reluctant to apply this patch, mainly because it will likely break
> with v3.0 again - if you feel very strongly about it, please at least
> add a test for it, so there is a chance this will make it through.
>
> --
> You received this bug notification because you are subscribed to the bug
> report.
> https://bugs.launchpad.net/bugs/1171227
>
> Title:
> Add function do return a vim dictionary with current snippets
>
> Status in UltiSnips - The Ultimate Snippet Solution for Vim:
> Incomplete
>
> Bug description:
> Hi Sirver,
>
> I wrote two small functions, one in vimL UltiSnips_SnippetDict which
> calls another function in python
> list_snippets_dict which is similar to list_snippets but just returns a
> vim dictionary with the snippets and description.
>
> The purpose of this is to be able to list the matching snippets via a
> pump instead of in the bottom of the window.
> For this the user would need to install two plugins from Ingo Karkat:
> SnippetComplete and SnippetCompleteSnipMate.
>
> I will email the author of those plugins to see if he can change one of
> them so it will support ultisnips
> (the only change will be to replace the line
>
> let l:snippets = GetSnipsInCurrentScope()
> by the line
> let l:snippets = UltiSnips_SnippetDict()
>
> so I don't antecipate a big problem there.
>
> I tried to record a short video showing the functionality:
> http://youtu.be/Cm7lzcS7SWM
>
> Please accept the patch or let me know issues you see with it.
> If you decide to accept and once I hear from Ingo I will also add
> additional information to the documentation so the users can use this
> functionality.
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/ultisnips/+bug/1171227/+subscriptions
>

Revision history for this message
SirVer (sirver) wrote :

I am working on that branch, but it is not production ready at this point in time.

I am not aware of pump, and I am not aware how snippet 'selection' is done in the future in the new branch, but it might change radically. So if you feel strongly, provide a test and I'll merge it in current trunk. Or make a patch against the new branch, but expect this to be quite a while off still.

Revision history for this message
Jorge (skeept) wrote :

I will try to provide a test...
Can you point me to where I should look to get it ready? Like other
relevant tests?

Thanks!

On Mon, Apr 22, 2013 at 12:01 PM, SirVer <email address hidden> wrote:

> I am working on that branch, but it is not production ready at this
> point in time.
>
> I am not aware of pump, and I am not aware how snippet 'selection' is
> done in the future in the new branch, but it might change radically. So
> if you feel strongly, provide a test and I'll merge it in current trunk.
> Or make a patch against the new branch, but expect this to be quite a
> while off still.
>
> --
> You received this bug notification because you are subscribed to the bug
> report.
> https://bugs.launchpad.net/bugs/1171227
>
> Title:
> Add function do return a vim dictionary with current snippets
>
> Status in UltiSnips - The Ultimate Snippet Solution for Vim:
> Incomplete
>
> Bug description:
> Hi Sirver,
>
> I wrote two small functions, one in vimL UltiSnips_SnippetDict which
> calls another function in python
> list_snippets_dict which is similar to list_snippets but just returns a
> vim dictionary with the snippets and description.
>
> The purpose of this is to be able to list the matching snippets via a
> pump instead of in the bottom of the window.
> For this the user would need to install two plugins from Ingo Karkat:
> SnippetComplete and SnippetCompleteSnipMate.
>
> I will email the author of those plugins to see if he can change one of
> them so it will support ultisnips
> (the only change will be to replace the line
>
> let l:snippets = GetSnipsInCurrentScope()
> by the line
> let l:snippets = UltiSnips_SnippetDict()
>
> so I don't antecipate a big problem there.
>
> I tried to record a short video showing the functionality:
> http://youtu.be/Cm7lzcS7SWM
>
> Please accept the patch or let me know issues you see with it.
> If you decide to accept and once I hear from Ingo I will also add
> additional information to the documentation so the users can use this
> functionality.
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/ultisnips/+bug/1171227/+subscriptions
>

Revision history for this message
SirVer (sirver) wrote :

test.py - it is essentially end-to-end tests. write a new one there, just dump the output you expect into the buffer and compare it with a golden state.

Revision history for this message
Jorge (skeept) wrote :

Thanks, I'll look into it later. But in this case there should be no change
in the buffer when this function is called. All it does is to return a vim
dictionary to be used by external tools.

On Mon, Apr 22, 2013 at 12:56 PM, SirVer <email address hidden> wrote:

> test.py - it is essentially end-to-end tests. write a new one there,
> just dump the output you expect into the buffer and compare it with a
> golden state.
>
> --
> You received this bug notification because you are subscribed to the bug
> report.
> https://bugs.launchpad.net/bugs/1171227
>
> Title:
> Add function do return a vim dictionary with current snippets
>
> Status in UltiSnips - The Ultimate Snippet Solution for Vim:
> Incomplete
>
> Bug description:
> Hi Sirver,
>
> I wrote two small functions, one in vimL UltiSnips_SnippetDict which
> calls another function in python
> list_snippets_dict which is similar to list_snippets but just returns a
> vim dictionary with the snippets and description.
>
> The purpose of this is to be able to list the matching snippets via a
> pump instead of in the bottom of the window.
> For this the user would need to install two plugins from Ingo Karkat:
> SnippetComplete and SnippetCompleteSnipMate.
>
> I will email the author of those plugins to see if he can change one of
> them so it will support ultisnips
> (the only change will be to replace the line
>
> let l:snippets = GetSnipsInCurrentScope()
> by the line
> let l:snippets = UltiSnips_SnippetDict()
>
> so I don't antecipate a big problem there.
>
> I tried to record a short video showing the functionality:
> http://youtu.be/Cm7lzcS7SWM
>
> Please accept the patch or let me know issues you see with it.
> If you decide to accept and once I hear from Ingo I will also add
> additional information to the documentation so the users can use this
> functionality.
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/ultisnips/+bug/1171227/+subscriptions
>

Revision history for this message
SirVer (sirver) wrote :

I understand: but just dump it into the buffer and compare the output. It is a little awkward, but it makes sure that it's working as expected.

Revision history for this message
Jorge (skeept) wrote :

I finally had some time to work on this.
I have now added a test with the current patch.
The test is very simple, it just checks that the output is a dictionary and that is has the correct number of elements.

However some issues:
I haven't added any documentation yet. The reason for this is that I contacted the author of the plugins I mentioned but he hasn't replied yet.
I can add documentation of an example of using the provided function but it would be better if he could coordinate and provide a simple change in his plugin.

At this point for each pair (key, val) in the dictionary there is redundant information in the val because all the pairs are of the form
('test', '(test) "this is what test does"')
I would prefer them to be of the type:
('test', 'this is what test does')

Any simple way of accomplishing this?

Revision history for this message
SirVer (sirver) wrote :

you should throw in at least one utf-8 and/or unicode symbol in your test and check for it's correctness.

Revision history for this message
Jorge (skeept) wrote :

Sorry, I didn't have much time to work on this but I would like to put it
in a way that you would consider inclusion.

For that I will include some unicode symbol as you suggest, plus I need to
figure out a way of getting the description of the snippet without getting
the snippet definition itself.
Can you tell me what is the correct way of running a specific test?
When I was trying to run the tests it was taking a while because it was
running all the tests, I was just interested in running the test I wrote.

After I am done with this I will write documentation and will submit again
for your consideration.

On Thu, May 9, 2013 at 4:34 AM, SirVer <email address hidden> wrote:

> you should throw in at least one utf-8 and/or unicode symbol in your
> test and check for it's correctness.
>
> --
> You received this bug notification because you are subscribed to the bug
> report.
> https://bugs.launchpad.net/bugs/1171227
>
> Title:
> Add function do return a vim dictionary with current snippets
>
> Status in UltiSnips - The Ultimate Snippet Solution for Vim:
> Incomplete
>
> Bug description:
> Hi Sirver,
>
> I wrote two small functions, one in vimL UltiSnips_SnippetDict which
> calls another function in python
> list_snippets_dict which is similar to list_snippets but just returns a
> vim dictionary with the snippets and description.
>
> The purpose of this is to be able to list the matching snippets via a
> pump instead of in the bottom of the window.
> For this the user would need to install two plugins from Ingo Karkat:
> SnippetComplete and SnippetCompleteSnipMate.
>
> I will email the author of those plugins to see if he can change one of
> them so it will support ultisnips
> (the only change will be to replace the line
>
> let l:snippets = GetSnipsInCurrentScope()
> by the line
> let l:snippets = UltiSnips_SnippetDict()
>
> so I don't antecipate a big problem there.
>
> I tried to record a short video showing the functionality:
> http://youtu.be/Cm7lzcS7SWM
>
> Please accept the patch or let me know issues you see with it.
> If you decide to accept and once I hear from Ingo I will also add
> additional information to the documentation so the users can use this
> functionality.
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/ultisnips/+bug/1171227/+subscriptions
>

Revision history for this message
SirVer (sirver) wrote :

you can do ./test.py TestName to only run all tests that contain 'TestName', e.g.
$ ./test.py Simple

is something that I often do to make sure that I didn't break something stupid.

I am really glad that you take this contribution so seriously - I am currently working on the v3 of Ultisnips - and without tests your new feature would break surely again.

Revision history for this message
Jorge (skeept) wrote :

I was trying to test with adding a unicode char and this doesn't work.

The snippet is defined by
snippet testâ "abc123"
xxxô
endsnippet

and the error I get is

An error occured. This is either a bug in UltiSnips or a bug in a
snippet definition. If you think this is a bug, please report it to
https://bugs.launchpad.net/ultisnips/+filebug.

Following is the full stack trace:
Traceback (most recent call last):
  File "/home/stu/hinacio/.vim/bundle/UltiSnips/plugin/UltiSnips/__init__.py", line 23, in wrapper
    return f(self, *args, **kwds)
  File "/home/stu/hinacio/.vim/bundle/UltiSnips/plugin/UltiSnips/__init__.py", line 558, in list_snippets_dict
    _vim.command("let g:current_ulti_dict['{key}'] = '{val}'".format(key=snip.trigger, val=description))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe2' in position 4: ordinal not in range(128)

so the problematic line is
_vim.command("let g:current_ulti_dict['{key}'] = '{val}'".format(key=snip.trigger, val=description))

Any idea how I can solve this? It works fine with regular chars.

Also to make the test more robust I would like to define a snippet and get the description of the snippet in the test.
So when I define a snippet as above the dict would be {'testâ', "abc123"} but I don't know how to define a snippet with description on the test file.
Currently I am defining the snippet as
snippets = ('test', 'abc123') but in this case 'abc123' is what the snippet will be expanded to.

thanks!

Revision history for this message
SirVer (sirver) wrote :

> Any idea how I can solve this? It works fine with regular chars.

International characters are very much regular chars :). We had plenty of bug reports about them before in UltiSnips, so it is a very good thing that you added tests for them now.

The problem here is that you are trying to format a bytes string with a unicode object. Something along these lines should work:

    _vim.command(as_unicode("let g:current_ulti_dict['{key}'] = '{val}'").format(key=snip.trigger, val=description))

This should help with this problem.

Also think about what happens here if val or the trigger contain a " or a '. A test case for this might be indicated as well.

> So when I define a snippet as above the dict would be {'testâ', "abc123"} but I don't know how to define a snippet with description on the test file.
The code that spilts this tuple is in test.py, line 281. you can define a snippet as
("trigger", "content", "description", "options"), but you can leave out descriptions and/or options.

Hope that helps.

Revision history for this message
Jorge (skeept) wrote :
Download full text (3.3 KiB)

Thanks for the suggestions but I am still stuck.

I have in the code
description = as_unicode(description)
key = as_unicode(snip.trigger)
_vim.command(as_unicode("let g:current_ulti_dict['{key}'] =
'{val}'".format(key=key, val=description)))

but when trying on the snippets I mentioned before I get the error:
_vim.command(as_unicode("let g:current_ulti_dict['{key}'] =
'{val}'".format(key=key, val=description)))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe2' in
position 4: ordinal not in range(128)

I may try to ask this on the vim list since someone there might have an
idea on how to solve this (if it is possible to solve this at all).

Jorge

On Tue, May 28, 2013 at 12:49 AM, SirVer <email address hidden> wrote:

> > Any idea how I can solve this? It works fine with regular chars.
>
> International characters are very much regular chars :). We had plenty
> of bug reports about them before in UltiSnips, so it is a very good
> thing that you added tests for them now.
>
> The problem here is that you are trying to format a bytes string with a
> unicode object. Something along these lines should work:
>
> _vim.command(as_unicode("let g:current_ulti_dict['{key}'] =
> '{val}'").format(key=snip.trigger, val=description))
>
> This should help with this problem.
>
> Also think about what happens here if val or the trigger contain a " or
> a '. A test case for this might be indicated as well.
>
> > So when I define a snippet as above the dict would be {'testâ',
> "abc123"} but I don't know how to define a snippet with description on the
> test file.
> The code that spilts this tuple is in test.py, line 281. you can define a
> snippet as
> ("trigger", "content", "description", "options"), but you can leave out
> descriptions and/or options.
>
> Hope that helps.
>
> --
> You received this bug notification because you are subscribed to the bug
> report.
> https://bugs.launchpad.net/bugs/1171227
>
> Title:
> Add function do return a vim dictionary with current snippets
>
> Status in UltiSnips - The Ultimate Snippet Solution for Vim:
> Incomplete
>
> Bug description:
> Hi Sirver,
>
> I wrote two small functions, one in vimL UltiSnips_SnippetDict which
> calls another function in python
> list_snippets_dict which is similar to list_snippets but just returns a
> vim dictionary with the snippets and description.
>
> The purpose of this is to be able to list the matching snippets via a
> pump instead of in the bottom of the window.
> For this the user would need to install two plugins from Ingo Karkat:
> SnippetComplete and SnippetCompleteSnipMate.
>
> I will email the author of those plugins to see if he can change one of
> them so it will support ultisnips
> (the only change will be to replace the line
>
> let l:snippets = GetSnipsInCurrentScope()
> by the line
> let l:snippets = UltiSnips_SnippetDict()
>
> so I don't antecipate a big problem there.
>
> I tried to record a short video showing the functionality:
> http://youtu.be/Cm7lzcS7SWM
>
> Please accept the patch or let me know issues you see with it.
> If you decide to accept and once I hear from Ingo I will also add
> additional informa...

Read more...

Revision history for this message
SirVer (sirver) wrote :

no need to post to the vim list - this is a python problem alone. And I can still spot the problem:

You are doing
as_unicode("blah".format(...))

you need to be doing
as_unicode("blah").format(...)

Revision history for this message
Jorge (skeept) wrote :

Thanks for spotting the mistake.

I send a patch that includes tests for the things you mentioned, international chars and ' or " in trigger.

Please read the patch when you have a chance, and let me know if there is something that need to be changed.

The function defined is called UltiSnips_SnippetDict. Does it makes sense to call it GetSnipsInCurrentScope, since that is the snipmate function I am trying to emulate?

Regards,

Jorge

Revision history for this message
SirVer (sirver) wrote :

Jorge, that seems like a very solid patch that you baked there. Some comments:

- add the new chapter to the contents of the help file.

the tests fail for me on mac os x (all three of them). How do you run them (i mean which OS and which workflow).

Revision history for this message
Jorge (skeept) wrote :

For some reason I sent the patch with a minor mistake. The tests should pass now.

Also do you have any comment regarding the name of the function. I used UltiSnips_SnippetDict but the function I am
trying to replicate from SnippetMate is GetSnipsInCurrentScope.
Do you want to just use the same name for the implemented function? Or maybe UltiSnips_SnipsCurrentScope?

You can just change the name in the patch or I can send another one later.

Revision history for this message
SirVer (sirver) wrote :

The tests pass now, great.

I renamed the method to UltiSnips_SnippetsInCurrentScope.

Thanks for you contribution!

Changed in ultisnips:
status: Incomplete → Fix Committed
Revision history for this message
Jorge (skeept) wrote :

You're welcome :)

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Bug attachments

Remote bug watches

Bug watches keep track of this bug in other bug trackers.