Home Download Buy Blog Forum Support

Python completions and help

Python completions and help

Postby agibsonsw on Tue Mar 13, 2012 5:46 pm

I realise there is already a plugin for Python completions (and other features) but that hasn't stopped me developing my own simple version ;) It's at my Gist until I get round to a Repo.

It shouldn't/doesn't interfere with existing HTML and PHP completions. When not working with a Python file, it still shows the default/ file completions list.

At the moment I've only included standard Python functions. But, cleverly, if you're working with the ST API it adds these methods as well. However, I've only added a few ST methods (set_clipboard, set_timeout, error_message and status_message) so far. If this is a useful aspect then I can extend this idea, such that adding a library will also add the library's methods to the completions list :D

If you still want to include the default file completions then comment out this line at the end of the file:
Code: Select all
py_compl = (py_compl, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)

I've also been working on a help system ffor Python functions. Add a key-binding such as:

Code: Select all
{ "keys": ["shift+f1"], "command": "py_help" }

Click into a Python function (dir, eval, sum, etc.) and press the key-combination to display help text in the status bar. The text is paraphrased from the Python library.

Some of the help text is a little long (but helpful). It's probably possible to change the status message delay. Personally, I just keep pressing the key-combo until I've read it all :)

I shall modify the completions so that they are global (or static). But, in the meantime I would appreciate any comments, suggestions (encouragement ;) )
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Python completions and help

Postby agibsonsw on Tue Mar 13, 2012 6:08 pm

Actually, I've commented out

Code: Select all
py_compl = (py_compl, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)

for the moment anyway as this expression is wrong.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Python completions and help

Postby agibsonsw on Tue Mar 13, 2012 6:53 pm

I've revised the completions file to use global lists, so should be much more efficient. At Gist.

Just switch these comments around at the end of the file:

Code: Select all
return (completions, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)
# return completions

if you want to include the default file completions as well. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Python completions and help

Postby agibsonsw on Tue Mar 13, 2012 11:09 pm

Python completions: Gist

I've added the ST-API methods and the completions are more useful/focussed now:

If using Python and not starting with a dot '.' - it will list standard Python functions;
If importing 'sublime' and starting with a dot - it will list sublime .methods;
Otherwise - it will list the default file completions.

Andy.

Added: The next step would be to add all the string, list, etc., methods. But, of course, being a dynamic language, it will not be possible to target when these are shown (other than being preceded by a dot).
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Python completions and help

Postby agibsonsw on Tue Mar 13, 2012 11:56 pm

Added: The next step would be to add all the string, list, etc., methods. But, of course, being a dynamic language, it will not be possible to target when these are shown (other than being preceded by a dot).


Actually (continuing my own conversation ;) ) I can cheat, he, he. I can follow a dot with a single letter, such as '.s', and it can (dynamically) add all the string methods to the completions list. t = tuple, i = int, f = float, l = list, d = dict, c = complex.

But I'm undecided at the moment... Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Python completions and help

Postby agibsonsw on Wed Mar 14, 2012 9:30 pm

Still no feedback, but I don't care :D. Update is here/Gist.

I've added (most of) the Python methods (for list, string, dict, file, etc.). The ST-API completions are also more helpful, as their description indicates the object-type it applies to (View, Window, etc.).

Basically, if you're in a Python file and type a dot '.' (and invoke auto-complete) it will list Python members - islower(), pop(), etc.. Without a dot, it will list Python functions. But if you've imported 'sublime' or 'sublime_plugin', then following a dot it will also include ST-API methods in the completions list. If you're not in a Python file then the normal completions will still appear. [Note: square brackets around parameters indicate that they are optional.]

Further work:

I'm considering using 'on_pre' or 'on_post_save' to make it more efficient. If you're not using the ST-API then it's efficient already, as the existing global completions are just re-used each time; that is, they are not rebuilt every time the completions are invoked. However, if using the ST-API, then the completions list is copied/re-built each time. By using a save event, I could examine the current Python file and look for 'import sublime' (or any other standard library!). Then I could add the library methods to the completions list and store it as an instance variable (for that view). Looking forward, I like the idea that importing a library will then add these library methods to the completions list :D. [It might even be possible (longer term) to refactor the library to automatically add completions, whe'hay!]

I haven't updated my 'PyHelp.py' file to include Python methods, but I might do this ;). I suppose there could also be two help files - then use Shift-F1 for Python help, and Ctrl-F1 for library help??

PS: My only slight concern about the completions (as they stand) is that there may be a few Python functions will also double as methods (len()?) - but they won't currently appear following a dot. The Python reference material is a little unclear on this. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Python completions and help

Postby agibsonsw on Wed Mar 14, 2012 10:01 pm

Amazing the inspiration that comes when you jump in the bath :lol:. I don't need to use on_post_save. If I do this, and save a completions list per view, then I'll end up hogging memory!

All I need to do is have three (global) versions of the completions-list, and just use the version that applies for the current context!

I could make the completion-lists class attributes (rather than globals), or import them from a separate file. Is there any advantage in doing either of these please?

Andy.

Added: Yes, more efficient now in this Gist.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Python completions and help

Postby Fed03 on Wed Mar 14, 2012 10:38 pm

mhh i don't know nor use python but everytime someone wants to improve this fantastic text editor, it's awesome
Fed03
 
Posts: 79
Joined: Sun Oct 16, 2011 6:38 pm

Re: Python completions and help

Postby agibsonsw on Wed Mar 14, 2012 10:54 pm

I'm considering this (quite) interesting piece of code:

Code: Select all
if prefix.upper() == 'ST':
   edit = view.begin_edit()
   view.erase(edit, sublime.Region(locations[0], locations[0]-len(prefix)))
   view.end_edit(edit)
   completions = subl_methods
else:
   completions = sublime_methods_all


When you type 'something.st' and invoke auto-complete, it removes the letters 'st' and just displays (all) ST-API methods. This idea could possibly be extended to things like 'list', 'str' methods - or, perhaps more useful, to just list library methods. Not sure of the efficacy of this - given fuzzy matching anyway - but it could prove useful?!

That is, even though Python is a dynamic language, we could persuade the auto-completion to be (almost..) context sensitive :). Further, entering 'something.1' and invoking autocomplete to list members from the first library that you've attached(?).
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Python completions and help

Postby agibsonsw on Thu Mar 15, 2012 8:44 pm

When someone saves a Python file, I can check for 'import somelibrary', and use features such as __dict__ and __class__, vars(), etc., to to build a completions list based on members from the library. I think it will be possible to distinguish between methods and attributes, and so add brackets () if appropriate.

But, if anyone's done something similar before :?: will it be possible for me to discover the number and names of method parameters? If so, I could dynamically create snippets that include the correct parameters (fields) for each method ;)

If the libraries contain (doctype) descriptions it may even be possible for me to extract descriptions for the parameters :?: and (perhaps..) show this help text in the status bar, as the user completes the snippet.

So I welcome any pointers from anyone who might have attempted something similar. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Next

Return to Plugin Announcements

Who is online

Users browsing this forum: Yahoo [Bot] and 11 guests