Haxe Code Cookbook
Haxe programming cookbookMacrosCode completion from URL

Code completion from URL

This example will load an URL, scrape all id's from the HTML page and use them for auto-completion.

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

class MyMacro {
  public static function build(url:String) {
    var h = haxe.Http.requestUrl(url);

    //trace(h);

    var r = ~/id=["']([A-Za-z0-9]+)["']/;
    var ids = [];
    while (r.match(h)) {
      var id = r.matched(1);
      ids.remove(id);
      ids.push(id);
      h = r.matchedRight();
    }

    var fields = Context.getBuildFields();
    var gtype = TAnonymous([for (id in ids) { 
      name : id, 
      pos : Context.currentPos(), 
      kind : FVar(macro:String) 
    }]);
    
    var gids:Field = {
      name : "gids",
      pos : Context.currentPos(),
      kind : FVar(gtype),
      access : [AStatic],
    };
    fields.push(gids);
    return fields;
  }
}

Usage

@:build(MyMacro.build("http://www.msn.com/en-us/"))
class Main {
  static function main() {
    Main.gids; // auto-complete here
  }
}

Demo

Code completion from URL

Explanation in video

Start at 21:00

More info: https://haxe.org/blog/nicolas-about-haxe-3


Contributors:
Last modified:
Created:
Category:  Macros