-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9bd1dc8
commit 7303010
Showing
3 changed files
with
49 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |