clemos / haxe-sublime-bundle

Sublime Text bundle for Haxe programming language
Apache License 2.0
235 stars 86 forks source link

Autocompletion doesn't work in macro-generated constructor #266

Open ciscoheat opened 7 years ago

ciscoheat commented 7 years ago

As the title says, there seems to be a problem with autocompletion for parameters in a macro-generated constructor, especially for anonymous types. Minimal example:

Main.hx

class Main 
{   
    static function main() {
        // Autocompletion works fine with a normal constructor:
        new AnotherPerson(...

        // But autocompletion doesn't work in a 
        // macro-generated constructor with an anonymous type
        new Person(...

        var p = new Person({birthYear: 1978, name: "Andreas"});     
        // But it works in a macro-generated method:
        p.ageInYear(...
    }   
}

@:build(AddConstructor.run())
class Person
{
    public var birthYear : Int;
    public var name : String;
}

class AnotherPerson
{
    public var birthYear : Int;
    public var name : String;

    public function new(data : { birthYear: Int, name: String }) {
        this.birthYear = data.birthYear;
        this.name = data.name;
    }
}

AddConstructor.hx

import haxe.macro.Context;
import haxe.macro.Expr;

class AddConstructor
{
    static public function run() {
        var fields = Context.getBuildFields();

        var constructor = {
            name: 'new',
            doc: null,
            meta: [],
            access: [APublic],
            kind: FFun({
                args: [{
                    meta: null,
                    name: 'data',
                    opt: false,
                    type: macro : {birthYear: Int, name: String},
                    value: null
                }],
                expr: macro {
                    this.birthYear = data.birthYear;
                    this.name = data.name;
                },
                params: null,
                ret: null
            }),
            pos: Context.currentPos()
        };

        var ageInYear = {
            name: 'ageInYear',
            doc: null,
            meta: [],
            access: [APublic],
            kind: FFun({
                args: [{
                    meta: null,
                    name: 'year',
                    opt: false,
                    type: macro : Int,
                    value: null
                }],
                expr: macro return year - birthYear,
                params: null,
                ret: macro : Int
            }),
            pos: Context.currentPos()
        };

        fields.push(constructor);
        fields.push(ageInYear);

        return fields;
    }
}

I've tested on Sublime 3 with the latest Haxe package, also on HaxeDevelop 5.2.0.3. For anonymous types, it doesn't work in any of them. A simpler type like macro : Int works in Sublime.

Cross-posted issue: https://github.com/HaxeFoundation/haxe/issues/5968