Sublime Forum

Completions[] + auto_complete

#1

I’m new to python so this might be a basic/stupid question but,

when using run_command auto_complete is it possible to display the completion similar to sublime-completions files.
ie: completions.append(“MyCompletion\tSpecial Completions”,“MyCompletion”) # which isn’t working of course (different type error)

0 Likes

#2

Are you referring to triggering the auto_completion within a textCommand? Or do you mean [pre=#2A2A2A]def on_query_completions(self, view, prefix, locations):[/pre]

What do you mean by “similar to sublime-completions files”? Do you mean how you can have helper in grey on the popup?

Either way, on_query_completions is expecting a list of tuples. Append only accepts one parameter and will not automatically create a tuple for you. So you could do:

sometuple = ("MyCompletion\thelper text","MyCompletion") completions.append(sometuple)

0 Likes

#3

Yeah, self.view.run_command(‘auto_complete’)…

Exactly that. I want to add the helper text like in sublime-completions files but to the auto_complete using run_command in a textCommand plugin.

completions.append() isn’t taking tuples I get:

TypeError: No registered converter was able to produce a C++ rvalue of type class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > from this Python object of type tuple

Appending plain strings works as expected, but I’d love to add helper text as well.
Edit: Apologies for the PM, I hope that was cool. :wink:

0 Likes

#4

Unless I’m missing something, you can’t add autocompletions via a textCommand. You need to add them with on_query_completions. You can send completions from a textCommand to on_query_completions though.

0 Likes

#5

Really? I learned to use it by looking at your DisplayFunctions plugin (for Java) on GitHub which has the following in a textCommand:

        self.view.run_command('auto_complete', {
                'disable_auto_insert': True,
                'api_completions_only': True,
                'next_competion_if_showing': True
                })
0 Likes

#6

That triggers the autocompletion popup to appear. It’s doing the same job as control+space. However, it doesn’t add anything to the completions. In that plugin, I have a global variable “completions.” The textCommand adds completions to the variable. Then, because it’s global, the method on_query_completions has access to it. On_query_completions then returns a list of tuples for the autocompletion.

0 Likes

#7

That makes sense. Does that mean we aren’t able add helper text to the global completions variable?
Is it perhaps possible to edit the list of tuples in on_query_completions method so we can add helper text?

0 Likes

#8

github.com/BoundInCode/SMART-Sn … ts.py#L223

0 Likes

#9

Brilliant work @COD312. Thanks again for sharing it.

0 Likes

#10

Did you get it working? Let me know if you need help.

0 Likes

#11

Okay. I stepped out for lunch, and now I need to refactor a bit.
But I’ll ping back definitely in a bit. Thanks.

0 Likes

#12

It works fine. In your Display Functions plugin, help text can also be added in the on_query_completions (I just missed it): github.com/BoundInCode/Display- … ns.py#L172

It’s amazing how you figured all this out. I searched Google for run_command(‘auto_complete’) and there is only sparse information and most of it is from you. For example, how did you know about ‘next_competion_if_showing’? I couldn’t find documentation for it anywhere.

0 Likes

#13

sublimetext.com/dev

search for Build 2148.

When I was first figuring stuff out, I used github.com/Kronuz/SublimeCodeIn … el.py#L290 as a guide. Helped get me off my feet, since at the time, CodeIntel was one of the only plugins to use on_query_completions (zencoding is a whole different story).

0 Likes

#14

This is a small nuisance, but if the auto_complete popup is present, triggering my own api completions wont work unless the popup is closed by pressing escape. Is there a way to make sure the completions popup is closed in textCommand?

0 Likes

#15

It’s funny because I came across that same exact problem. I tried a few things such as closing the autocomplete at the beginning of the plugin and setting a short timeout. However, nothing worked as smooth as just setting auto_complete_triggers to ‘.’ for java (in Display-Java’s case). I’ll play around with in though and see what I can do.

0 Likes

#16

I tried, as you probably did (but not working either):

[pre=#2A2A2A]self.view.run_command(‘hide_auto_complete’)[/pre]

I also thought maybe I could change the keybinding to work when auto_complete_visible is true but it seems like Sublime is hard coded to kill auto_complete when any key not used by auto_complete is sent.
There just might not be away around it. I’ll keep my hopes up.

0 Likes

#17

Awesome. I got it working. All you need to do is set a timeout to run_command(‘auto_complete’)

This is the code I used: [pre=#2A2A2A] def activate_ac(self):
self.view.run_command(‘auto_complete’, {
‘disable_auto_insert’: True,
‘api_completions_only’: True,
‘next_competion_if_showing’: False
})[/pre][pre=#2A2A2A]view.run_command(‘hide_auto_complete’)[/pre][pre=#2A2A2A]sublime.set_timeout(self.activate_ac,100)[/pre]

0 Likes

#18

[quote=“C0D312”]Awesome. I got it working. All you need to do is set a timeout to run_command(‘auto_complete’)

This is the code I used: [pre=#2A2A2A] def activate_ac(self):
self.view.run_command(‘auto_complete’, {
‘disable_auto_insert’: True,
‘api_completions_only’: True,
‘next_competion_if_showing’: False
})[/pre][pre=#2A2A2A]view.run_command(‘hide_auto_complete’)[/pre][pre=#2A2A2A]sublime.set_timeout(self.activate_ac,100)[/pre][/quote]

My lazy butt didn’t try a timeout since you mentioned you tried that as well.

Glad you posted back. I’ll give you a heartfelt shout out for the help on the plugin readme man - thanks.
I’ll try it in a bit and see how well it works.

0 Likes