GSoC/GCI Archive
Google Code-in 2013 KDE

kdevelop: python support: implement generating correction files

completed by: Atanas

mentors: Sven Brauch

kdev-python provides python language support for KDevelop.

One of the most prominent problems of Python language support is errors in what it thinks library API looks like. This means for example that the IDE will not provide useful code completion suggestions for that library. One interesting such library is matplotlib, a plotting library for python: http://matplotlib.org/examples/index.html

The library is written in python. kdev-python supports it by reading the library code, and trying to figure out things from it, e.g. what methods return what kinds of objects, and what functions and variables exist. Unfortunately, there are some cases where this can never work -- Python is, unlike C++, not a statically typed language, and trying to find information about objects in the code without actually running the code is always guesswork to a certain extent. This is the reason that for Matplotlib in particular, kdev-python's support is very bad (just try pasting some of the examples from [1] into kdevelop and look at all the things which are not being highlighted).

On the bright side however, when you look at the Matplotlib code, you can do a quite small amount of changes to it which allow kdev-python to be very useful, simply by adding assert(isinstance(foo, Bar)) in a few places to tell kdev-python that the variable foo is of type Bar. Just about ten of those statements in central places are enough to fix a lot of missing links and allow the IDE to do lots of useful things.

Obviously, we can't upstream these changes into Matplotlib (or whatever library) itself. What we want to do is giving the users a possibility to tell kdev-python what type a certain variable is -- without touching the code itself. We also want to save this information into a simple file format, to allow editing and also becaus ewe want to include some of it in kdev-python's distribution itself. A prototype for this is already implemented in the correction-files branch of kdev-python: look at the correction_files folder for examples how we can give hints for parsing matplotlib with this.

Writing those hint files by hand is an annoying and error-prone task. It would be so much cooler if we could have an assistant which generates them, right? Thus, the ultimate goal is to have a keyboard shortcut you can press when your cursor is over a variable declaration, then a window opens and you can specify a type there which will be saved for that variable.

Now for the actual task: The part of reading the files is already done in the correction-files branch. I also created a UI widget to enter the type (see codegen/ folder). What is missing, and what should be created as part of this task, is the code which generates and modifies the actual files (similar to those in correction_files/matplotlib/). I created some possible API for how this could work (correctionfilegenerator.h), but you're not bound to use it exactly like this. Probably the best option for generating the code is just looking for the relevant line numbers in the file (by either using the AST or the semantic information on the file -- ask me for help on this, I'll explain), and then insert some string there (e.g. inserting "class Foo: \n  pass" at line 4 of the file). A few cases need to be considered, especially updating existing hints.

You can go quite far with this; for completing this task I would expect that you:

 * have a method which pops up the dialog to add a hint (adding a shortcut for it is recommended, because otherwise you can't really test it)

and then, as a reaction to the user confirming the dialog

 * can create new files when there's no pre-existing hint file

 * can add new hints for previously-existing or new classes and methods

 * Dealing with already existing hints which are overwritten is optional, you don't need to do it.

 

This task is without doubt difficult, but if it's not a challenge, it would be boring, right? ;)

________

[1] http://matplotlib.org/examples/index.html