GSoC 2014: Improving the QML/Javascript Language Support of KDevelop

Published on mar 22 avril 2014 in Nepomuk, (Comments)

Hi!

I'm pleased to announce that I have been accepted as a student for this year's GSoC. After having worked on Nepomuk (now Baloo), I will have the chance to work on KDevelop, the wonderful IDE based on KDE technologies.

How KDevelop Supports Languages

The code editor component of KDevelop is based on Kate, which provides the basic syntax highlighting and editing capabilities (history, cut and paste, formatting, code folding, etc). KDevelop adds some nice features on top of that, for instance code completion and refactoring.

These features require that KDevelop understands the language used by the user. KDevelop has to know how a variable is declared, when it is used, what is a class in the language used, how functions look like, which symbols are visible in a particular context, etc. This is done using language support plugins that build something called a definition-use chain.

The job of these plugins is to parse a document and to build the definition-use chain for them. For instance, when a variable assignation is encountered (a = 2), the plugin informs KDevelop that the variable "a" has been used. When a variable declaration is encountered, the plugin tells KDevelop where this declaration occurs. This way, KDevelop can highlight the variables in different colors, and if the cursor is on a variable, KDevelop can show on which line the variable has been declared.

The plugin is also responsible for finding the types of the variables. For instance, "a" has been declared on a particular line and is of type "int". This information is very useful to the user.

Javascript and KDevelop

My project for the Google Summer of Code consists of improving the Javascript support of KDevelop. KDevelop already has a minimal JS support (variable and function declarations are supported), and my job is to add support for arrays, prototypes and "objects", anonymous functions, and the QML language. As the future of KDE will be built on top of Qt5 and QML, having a good Javascript support in KDevelop is becoming important.

Auto-completion of function types

The most challenging part of Javascript is that it is a dynamic language. The source code does not contain any type information, and infering the types of the variables is a great service to the users. It is also necessary in order to support auto-completion of array keys (KDevelop has to know that window.location is of type string in order to offer the methods of the String class as possible completions).

Type inference is an intersting topic and I worked on it for about two weeks (the first one was mainly thinking and discovering how KDevelop language plugins work, the second one was actual coding, and the third one was dedicated to submitting review requests for all that). The above screenshot shows that KDevelop is able to infer the return type of the function (the equality operator always returns a type), the type of the parameters (when the function is called, I give it integers, so these parameters are likely to be integers), and the type of the variable.

Other type inference mechanisms are also implemented. If I declare a variable and give it a value, the variable will take the type of the value. var a = "foo"; declares a variable of type string, and writing a = 3; after this line will change the type of "a" to "unsure (string, int)", because KDevelop cannot say if the variable is a string or an int (I advise the user to avoid writing code like that :-) ).

Plan for the Summer

This project is very interesting and there are still many things to do. Functions and variables now work reasonably well, but there are many built-in features of the Javascript language that need to be supported. Arrays and hash tables, for instance, need to be supported for the code-completion to work correctly, so are built-in types (String, Integer, etc). Functions that serve as constructors also need to be detected, so that lines like var o = new Object(); declare o as being an object, with methods and members.

Furthermore, special QML constructs need to be supported. QML has import statements, a special way of declaring objects (by attributing them an "id"), properties having their type declared, etc. This is all very interesting and I hope to be able to support all that by the end of the summer!

« Porting the Nepomuk Query Parser to Baloo   The coding period starts tomorrow »