web oriented universal language

haXe Completion

Posted on 2007-03-28 by Nicolas Cannasse in General

Since haXe 1.12 release, Code Completion is supported in the haxe FlashDevelop Plugin. I wanted to take some time to explain how this is working, since this might be of some interest for people which want to integrate haXe in their own custom IDE.

Image

In most of the cases for other languages, an IDE is reproducing a little part of the compiler in order to provide completion. At least the parser that can fully recognize the language syntax is needed, and sometimes a bit of typing. This works pretty well for languages such as Java that have a simple type system and were all types are previously declared. But at the same time it means duplicating some part of the compiler code into the IDE, which is not a very good separation, and this need to be done for every different IDE...

Because of haXe Type Inference, it was impossible for the IDE to provide appropriate completion without reproducing almost exactly the algorithms that are integrated into the compiler. That would mean integrating even more parts of the compiler into the IDE, with problems when some bugs are fixed or things get improved.

That's the reason why haXe Completion had to be supported directly by the compiler itself.

Implementation

haXe was then modified to be able to stop at a given byte position in a file and provide at this time the informations needed to hint the user with completion tips.

Let's take a first simple example :

class Test {
    public static function main() {
        trace("Hello".|
    }
}

The horizontal bar indicates the cursor position after the dot has been pressed. At this time, this is the job of the IDE to call the haXe compiler with usual compilation parameters, plus an additional one --display Test.hx@73.

This parameter tells the file in which we want some completion and the byte position of the cursor in the file. In that example if you count characters with windows CRLF lines ending, you should get 73.

haXe will perform all the parsing and typing as it would do for normal compilation, except that it will not generate any code. If during its compilation it is reaching the byte 73 in file Test.hx it will output the informations about the type of the expression that is on the left of the dot.

In our case, that would be String. Except that in the cases of an anonymous object or a class instance, the compiler will instead list the different fields in an XML and print them to the standard error output. Here's an example of output :

<list>
<i n="length"><t>Int</t><d>...</d></i>
<i n="charAt"><t>index : Int -> String</t><d>...</d></i>
<i n="charCodeAt"><t>index : Int -> Int</t><d>...</d></i>
<i n="indexOf">
  <t>value : String -> ?startIndex : Int -> Int</t>
  <d>...</d>
</i>
...
</list>

In that case, all public methods and fields of String are listed. This information can directly be used by the IDE to provide code hints and completion.

This completion mecanism works with both dot and opening parenthesis, so you can get type informations about fields list and function call arguments types. It also works with packages, by scanning the .hx files available in the classpath.

Pro and Cons

Good points about haXe Completion :

  • it's integrated into the compiler, so can easily be used by any IDE
  • the compiler is very fast, the retrieving completion informations is done very quickly, even for big projects
  • it uses the same algorithms than the compiler, so information is always exact

Bad points about haXe Completion :

  • it fails when some syntax error occurs
  • the file you are editing need to be compiled as part of your project, or completion will not work

Comments

Nicholas,

That's an absolutely brilliant idea. Well done - I wish more compiler authors would think so far ahead!

Cheers,
Ian
Posted by IanT , 2007-03-29 00:17:38
Great, hopefully I will find time once to create an IntelliJ IDEA plug-in...
Posted by Michael Pliskin , 2007-03-29 09:01:08
By this way, haXe becomes slowly independant of any IDE !
No more waiting for haXe plugin for your favourite code editor... (you'll be able to quickly write it by yourself).
It's really a great idea!
Thanks !
Posted by filt3rek , 2007-03-29 14:44:07
Thats beautyfull of code very good idea i liked it very much.
Posted by trisha , 2007-08-31 16:02:40

Post a comment

Name:
Email:
Url:
Security: Please enter 8760 here.
remember me
Comment:
 
 
Haxe Powered Rss flux Valid XHTML 1.0 Valid CSS