Skip to content

Commit

Permalink
release: the macro
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed Mar 24, 2023
1 parent 9bd1dc8 commit 7303010
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
4 changes: 2 additions & 2 deletions haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"externs"
],
"description": "Create neovim plugins using Haxe",
"version": "0.2.2",
"version": "0.2.3",
"classPath": "src/",
"releasenote": "better user arguments support",
"releasenote": "macro for easier plugin integration",
"contributors": [
"danielo515"
],
Expand Down
18 changes: 9 additions & 9 deletions src/vim/plugin/Plugin.hx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package vim.plugin;

import lua.Lua;

inline function load< T >(pluginName:String):Option< T > {
final requireResult = Lua.pcall(Lua.require, pluginName);
if (requireResult.status) {
return Some(requireResult.value);
} else {
return None;
}
@:autoBuild(vim.plugin.PluginMacro.pluginInterface())
interface VimPlugin {
/*
This is an empty interface that is used to attach the @:autoBuild
to classes that implement it. The @:autoBuild macro will generate
the required require code to load the plugin safely.
The implementing class must have a field named libName, which will be used
in the generated require function.
*/
}
38 changes: 38 additions & 0 deletions src/vim/plugin/PluginMacro.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package vim.plugin;

using haxe.macro.TypeTools;

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

class PluginMacro {
macro static public function pluginInterface():Array< Field > {
final fields = Context.getBuildFields();
final localType = Context.getLocalType().toComplexType();
final returnType = macro :$localType;
final newFields = [for (field in fields) {
switch field {
case {name: "libName", kind: FVar(_, {expr: EConst(CString(val, _))})}:
final built = macro class X {
inline static public function require():Null< $returnType > {
final module = lua.Lua.pcall(lua.Lua.require, $v{val});
final value = if (module.status) {
module.value;
} else {
null;
};
return value;
}
};
built.fields[0];

case _:
continue;
}
}];
if (newFields.length == 0) {
Context.error("No libName field found in plugin interface", Context.currentPos());
}
return fields.concat(newFields);
}
}

0 comments on commit 7303010

Please sign in to comment.