Author Topic: Python Code Completion  (Read 71801 times)

Offline dmoore

  • Developer
  • Lives here!
  • *****
  • Posts: 1576
Python Code Completion
« on: October 06, 2012, 06:13:10 pm »
I've started working on a Code Completion plugin for Python. Sources are here:

Code
svn checkout svn://svn.berlios.de/cbilplugin/trunk/PythonCodeCompletion

Build Instructions for Linux and Windows

The project files (respectively PythonCodeCompletion-unix.cbp and PythonCodeCompletion.cbp) will create a packaged cbPlugin, that you can install from Plugins->Manage Plugins

1. Get the sources, using the command above.
2. Make sure you have codeblocks-dev package installed and jedi (and python, of course)
3. In Code::Blocks, open the project file and build target All
4. You should see PythonCodeCompletion.cbplugin in the root of the project folder, install it from Plugins -> Manage Plugins

Features
The current version supports the features of jedi for code assist, providing completion popups and calltips.

Todo
* Better integration with C::B (depends on Alpha's progress with CodeCompletion overhaul)
* Symbol browser
* Configuration options

Screenshots


How it's done

This plugin uses jedi, a python library, as the engine for the code completion running in a separate process and communicates with it via XMLRPC. Python provides a lot of nice introspection features making it easy to parse python source and extract symbols (even from the builtin libs) that jedi takes advantage of. Jedi improves on the limitations of rope, the library that was being used in an earlier version of this plugin.
« Last Edit: October 29, 2013, 06:11:31 pm by dmoore »

Offline dmoore

  • Developer
  • Lives here!
  • *****
  • Posts: 1576
Re: Python Code Completion
« Reply #1 on: October 11, 2012, 09:17:24 pm »
New:
1. Windows project file (which means you can now build on linux, the linux project file is now named PythonCodeCompletion-unix.cbp)
2. Call tips with call sig and docstrings for standard library functions
3. Now can do the XMLRPC communication with the python completion process over pipe as an alternative to a socket. (Pipe is currently hardcoded as default.)
4. Parsing files outside of the standard lib
5. Understanding symbol from context (i.e. should know module sys only after it is imported, or function f when it is defined)

Coming:
4. Parsing files outside of the standard lib
5. Understanding symbol from context (i.e. should know module sys only after it is imported, or function f when it is defined)

6. Symbol/module browsing
7. Use python specific icons for graphic representation of symbol types (module, class, function, attribute etc.)
8. Configuration options
9. User-defined rules for determining return types that can't be determined using introspection (e.g. return type of numpy.array -> ndarray)
« Last Edit: October 14, 2012, 04:33:29 pm by dmoore »

Offline dmoore

  • Developer
  • Lives here!
  • *****
  • Posts: 1576
Re: Python Code Completion
« Reply #2 on: October 13, 2012, 05:13:51 am »
Update: Now using Rope for code completion. I've updated the original post with new build instructions. WARNING: Rope litters your project folders with hidden .ropeproject folders.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Python Code Completion
« Reply #3 on: October 13, 2012, 01:51:35 pm »
Pretty nice!
I see we have C/C++ code completion plugin(in trunk), Fortran code completion plugin, python code completion plugin. Maybe, we need to a common interface for all those plugins. ;)
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline dmoore

  • Developer
  • Lives here!
  • *****
  • Posts: 1576
Re: Python Code Completion
« Reply #4 on: October 13, 2012, 03:54:13 pm »
I see we have C/C++ code completion plugin(in trunk), Fortran code completion plugin, python code completion plugin. Maybe, we need to a common interface for all those plugins. ;)

Indeed. :)

For calltips and the code completion lists, not too much needs to change, I think. Code completion for python doesn't appear to get in the way of the C/C++ one and, mostly, vice versa. One thing that does get in the way occasionally are the keyword completions. Does anyone know how I can disable those?

Related to sharing GUI, it would be nice if:

* the shortcut keys (e.g. ctrl + space, ctrl+shift+space) worked for all CC plugins.

* For symbol browsing (which I haven't even begun to implement for python yet), share the symbol tab in the manager pane across plugins, though not sure how feasible that is. How would one separate the symbols for different languages?

*  the goto declaration/implementation stuff had a shared GUI (or at least didn't show inappropriately).

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Python Code Completion
« Reply #5 on: October 14, 2012, 07:44:34 am »
* the shortcut keys (e.g. ctrl + space, ctrl+shift+space) worked for all CC plugins.
Isn't this the case when all CC plugins call event.Ski()  in their handler accordingly?

* For symbol browsing (which I haven't even begun to implement for python yet), share the symbol tab in the manager pane across plugins, though not sure how feasible that is. How would one separate the symbols for different languages?
I would prefer having a separate UI for the languages, like done with Fortran. Because for mixed language projects (i.e. Fortran is often mixed with C/C++) this is the only way to go.

*  the goto declaration/implementation stuff had a shared GUI (or at least didn't show inappropriately).
I don't get it: Did it have a shared UI (when/where is it it gone?!) or do you wish it had one?!
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline dmoore

  • Developer
  • Lives here!
  • *****
  • Posts: 1576
Re: Python Code Completion
« Reply #6 on: October 14, 2012, 04:31:45 pm »
* the shortcut keys (e.g. ctrl + space, ctrl+shift+space) worked for all CC plugins.
Isn't this the case when all CC plugins call event.Ski()  in their handler accordingly?

No because the menu items are provided by the Code Completion plugin and not the SDK

see plugins/codecompletion/codecompletion.cpp(635)
Code
            m_EditMenu->Append(idMenuCodeComplete, _("Complete code\tCtrl-Space"));

        m_EditMenu->Append(idMenuShowCallTip, _("Show call tip\tCtrl-Shift-Space"));

Quote
I would prefer having a separate UI for the languages, like done with Fortran. Because for mixed language projects (i.e. Fortran is often mixed with C/C++) this is the only way to go.

Well I think it would be useful to at least have them share the symbols tab.

Quote
*  the goto declaration/implementation stuff had a shared GUI (or at least didn't show inappropriately).
I don't get it: Did it have a shared UI (when/where is it it gone?!) or do you wish it had one?!

You missed the "it would be nice if..." that prefaced those bullets. So no it doesn't have a shared UI and never did because the CC plugin currently provides those menu options. It would be better if the SDK did and called each plugins' handler. Also, can we switch off the goto declaration/implementation options in CC if the highlight language is not in some predefined set (e.g. C/C++/D/Fortran?).

see plugins/codecompletion/codecompletion.cpp(644)
Code
    pos = menuBar->FindMenu(_("Sea&rch"));
    if (pos != wxNOT_FOUND)
    {
        m_SearchMenu = menuBar->GetMenu(pos);
        m_SearchMenu->Append(idMenuGotoFunction,       _("Goto function...\tCtrl-Alt-G"));
        m_SearchMenu->Append(idMenuGotoPrevFunction,   _("Goto previous function\tCtrl-PgUp"));
        m_SearchMenu->Append(idMenuGotoNextFunction,   _("Goto next function\tCtrl-PgDn"));
        m_SearchMenu->Append(idMenuGotoDeclaration,    _("Goto declaration\tCtrl-Shift-."));
        m_SearchMenu->Append(idMenuGotoImplementation, _("Goto implementation\tCtrl-."));
        m_SearchMenu->Append(idMenuFindReferences,     _("Find references\tAlt-."));
        m_SearchMenu->Append(idMenuOpenIncludeFile,    _("Open include file\tCtrl-Alt-."));
    }

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Python Code Completion
« Reply #7 on: October 14, 2012, 04:39:28 pm »
No because the menu items are provided by the Code Completion plugin and not the SDK
Understood and makes sense.

Well I think it would be useful to at least have them share the symbols tab.
But how would you differ symbols with the same name coming from different CC's? And also, syncing the symbols tab with the user's selection and re-freshing it becomes an issues, especially if it is thread based (as it is now). I think having a symbol tab for each language is not too much and makes it way easier to handle. Maybe a CC plugin can "request" such a tab from the SDK.

So no it doesn't have a shared UI and never did because the CC plugin currently provides those menu options. [...]
Well - so altogether this sounds like a similar concept as for the debugger, isn't it? Sharing the dialogs via the SDK and having the plugins to specialise it. It would be unique (I've never heard of an IDE having such) but sounds good.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Python Code Completion
« Reply #8 on: October 15, 2012, 05:31:23 pm »
But how would you differ symbols with the same name coming from different CC's? And also, syncing the symbols tab with the user's selection and re-freshing it becomes an issues, especially if it is thread based (as it is now). I think having a symbol tab for each language is not too much and makes it way easier to handle. Maybe a CC plugin can "request" such a tab from the SDK.
You'll have a combobox with the current language showed by the symbol browser. Changing the value in the combo could change the UI - ask the plugin to build the correct UI.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline dmoore

  • Developer
  • Lives here!
  • *****
  • Posts: 1576
Re: Python Code Completion
« Reply #9 on: October 16, 2012, 04:29:58 am »
You'll have a combobox with the current language showed by the symbol browser. Changing the value in the combo could change the UI - ask the plugin to build the correct UI.

Yes, I think this would work well. Perhaps the combo could be switched automatically to whatever type of source the user has open in the active editor.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Python Code Completion
« Reply #10 on: October 16, 2012, 07:10:46 am »
You'll have a combobox with the current language showed by the symbol browser. Changing the value in the combo could change the UI - ask the plugin to build the correct UI.
I am still not really convinced. If you have large projects with a lot of CC members, building the UI every time you switch really takes time and can be avoided if you have just two symbol browser that both would need no update when switching between them. You can see this now already with the C++ tree, if you enable "workspace level".

Have you ever worked with the Fortran project? There, you have another symbol browser which works just fine besides the C/C++ one. This usage is really convenient. And in fact if you have multiple language projects (which is very common at least for C/C++ and Fortran) you would switch pretty often. For Fortran, I am pretty happy with how it is.

...just my 2 cents...
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Python Code Completion
« Reply #11 on: October 16, 2012, 09:20:35 am »
...If you have large projects with a lot of CC members, building the UI every time you switch really takes time...
When switching the language the only operations that will happen will be oldPanel->Hide() and newPanel->Show(). The plugins will keep all panels up to date and full with the correct data.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Python Code Completion
« Reply #12 on: October 16, 2012, 10:23:16 am »
When switching the language the only operations that will happen will be oldPanel->Hide() and newPanel->Show().
True, but it wouldn't allow you to have i.e. both symbol trees under each other, which may be desired. If that's possible, too I am fine with it. Remember, that in mixed-code project you switch rather often from one to another language. So seeing i.e. all relevant methods of your project together is helpful for navigation.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Python Code Completion
« Reply #13 on: October 16, 2012, 10:33:18 am »
We can provide a way to have any number of symbol browser tabs, so the user can have some control.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline dmoore

  • Developer
  • Lives here!
  • *****
  • Posts: 1576
Re: Python Code Completion
« Reply #14 on: October 18, 2012, 02:52:12 pm »
It would be easy enough to develop prototypes with different approaches to handling the UI (all in one tab, across multiple tabs etc)

What's bothering me more at the moment is that CC still tries to offer suggestions for python if the python completion returns nothing. This is quite annoying as is the find declaration/implementation stuff. Would anyone mind if I did some lexer checks in the  CC plugin to not show tips/options for python files? What happens with Fortran files?