Calendar

July 2016
Mo Tu We Th Fr Sa Su
<< >>
123
45678910
11121314151617
18192021222324
25262728293031

Langs

Haxe Completion

Posted on Mar 28 2007

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.

haxefd_codetips.png

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 vertical 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 mechanism 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
4 comments
  • Nathan
    Feb 21, 2009 at 14:24

    Thanks for the information! I want to try the "first simple example", but I can not see the horizontal bar or dot in the example.

  • Mar 01, 2009 at 11:51

    Fixed ;)

  • Jul 23, 2010 at 17:12

    is it possible to get completion for different platform specific APIs in a single file at the same time? example: js.Lib completion AND flash.Lib completion in the one file.

    I noticed in FlashDevelop that you have to change the publishing settings in order to get one or the other (i.e., they don't both complete for a single publishing setting). Is this a limitation of the IDE or of Haxe completion?

  • Jul 23, 2010 at 21:17

    Haxe uses separate compilations for every platforms (although they can be chained with --next) so you cannot have completion for multiplatform so far since it depends on the "current compilation" platform.

Name : Email : Website : Message :