update mztenv

This commit is contained in:
LordMZTE 2022-10-20 15:04:28 +02:00
parent b0d9845f23
commit 356fd16258
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
4 changed files with 141 additions and 17 deletions

View file

@ -5,3 +5,5 @@ local success = pcall(require, "mzte_nv");
if not success then
error "Failed to preload mzte_nv. Is it installed?"
end
require("mzte_nv").onInit()

View file

@ -1,15 +1,52 @@
const std = @import("std");
const ffi = @import("ffi.zig");
const ser = @import("ser.zig");
const c = ffi.c;
pub const version = "0.1.0";
const modules = struct {
const jdtls = @import("modules/jdtls.zig");
};
export fn luaopen_mzte_nv(l_: ?*c.lua_State) c_int {
const l = l_.?;
c.lua_newtable(l);
modules.jdtls.pushModtable(l);
c.lua_setfield(l, -2, "jdtls");
ser.luaPushAny(l, .{
.onInit = ffi.luaFunc(lOnInit),
.jdtls = modules.jdtls,
});
return 1;
}
fn lOnInit(l: *c.lua_State) !c_int {
c.lua_getglobal(l, "vim"); // 1
c.lua_getfield(l, 1, "version");
c.lua_call(l, 0, 1); // 2
c.lua_getfield(l, 2, "major");
const major = c.lua_tointeger(l, -1);
c.lua_getfield(l, 2, "minor");
const minor = c.lua_tointeger(l, -1);
c.lua_getfield(l, 2, "patch");
const patch = c.lua_tointeger(l, -1);
c.lua_getfield(l, 2, "prerelease");
const prerelease = if (c.lua_toboolean(l, -1) != 0) " (prerelease)" else "";
c.lua_settop(l, 1);
var buf: [128]u8 = undefined;
const s = try std.fmt.bufPrintZ(
&buf,
"MZTE-NV v{s} Initialized on NVIM v{}.{}.{}{s}",
.{ version, major, minor, patch, prerelease },
);
c.lua_getfield(l, 1, "notify");
c.lua_pushstring(l, s.ptr);
c.lua_call(l, 1, 0);
return 0;
}

View file

@ -1,18 +1,19 @@
/// Module for the JDTLS java language server, including utilities
/// for setting up nvim-jdtls
const std = @import("std");
const ser = @import("../ser.zig");
const ffi = @import("../ffi.zig");
const c = ffi.c;
pub fn pushModtable(l: *c.lua_State) void {
c.lua_newtable(l);
c.lua_pushcfunction(l, ffi.luaFunc(lFindRuntimes));
c.lua_setfield(l, -2, "findRuntimes");
pub fn luaPush(l: *c.lua_State) void {
ser.luaPushAny(l, .{
.findRuntimes = ffi.luaFunc(lFindRuntimes),
});
}
const Runtime = struct {
version: []const u8,
name: []const u8,
version: [:0]const u8,
name: [:0]const u8,
};
const runtime_map = [_]Runtime{
.{ .version = "18", .name = "JavaSE-18" },
@ -50,14 +51,10 @@ fn lFindRuntimes(l: *c.lua_State) !c_int {
// push a table with a name field (must be a name from runtime_map)
// and a path field (path to the runtime's home)
c.lua_newtable(l);
c.lua_pushstring(l, rt.name.ptr);
c.lua_setfield(l, -2, "name");
const path = try std.fmt.bufPrintZ(&buf, "/usr/lib/jvm/{s}/", .{jvm.name});
c.lua_pushstring(l, path.ptr);
c.lua_setfield(l, -2, "path");
ser.luaPushAny(l, .{
.name = rt.name,
.path = try std.fmt.bufPrintZ(&buf, "/usr/lib/jvm/{s}/", .{jvm.name}),
});
// append table to list
c.lua_rawseti(l, -2, idx);

88
mzte_nv/src/ser.zig Normal file
View file

@ -0,0 +1,88 @@
const std = @import("std");
const c = @import("ffi.zig").c;
pub fn luaPushAny(l: *c.lua_State, x: anytype) void {
const T = @TypeOf(x);
switch (@typeInfo(T)) {
.Void, .Null => c.lua_pushnil(l),
.Bool => c.lua_pushboolean(l, @intCast(c_int, @boolToInt(x))),
.Int, .ComptimeInt => c.lua_pushinteger(l, @intCast(c_int, x)),
.Float, .ComptimeFloat => c.lua_pushnumber(l, @floatCast(c.lua_Number, x)),
.Pointer => |P| {
switch (P.size) {
.One => {
if (T == c.lua_CFunction or
T == @typeInfo(c.lua_CFunction).Optional.child)
c.lua_pushcfunction(l, x)
else
luaPushAny(l, x.*);
},
.Slice => {
if (P.child == u8) {
if (P.sentinel == null)
@compileError("luaPushAny doesn't support " ++ @typeName(T));
c.lua_pushstring(l, x.ptr);
} else {
c.lua_createtable(l, x.len, 0);
for (x) |element, i| {
luaPushAny(l, element);
c.lua_rawseti(l, -2, i + 1);
}
}
},
.C => {
if (P.child != u8)
@compileError("luaPushAny doesn't support " ++ @typeName(T));
c.lua_pushstring(l, x);
},
.Many => {
if (P.child != u8)
@compileError("luaPushAny doesn't support " ++ @typeName(T));
c.lua_pushstring(l, x);
},
}
},
.Array => luaPushAny(l, &x),
.Struct => |S| {
if (comptime std.meta.trait.hasFn("luaPush")(T)) {
return x.luaPush(l);
}
if (S.is_tuple) {
c.lua_createtable(l, S.fields.len, 0);
inline for (S.fields) |Field, i| {
luaPushAny(l, @field(x, Field.name));
c.lua_rawseti(l, -2, i + 1);
}
} else {
c.lua_createtable(l, 0, S.fields.len);
inline for (S.fields) |Field| {
luaPushAny(l, @field(x, Field.name));
c.lua_setfield(l, -2, Field.name.ptr);
}
}
},
.Optional => {
if (x) |val| {
luaPushAny(l, val);
} else {
c.lua_pushnil(l);
}
},
.Enum, .EnumLiteral => c.lua_pushstring(l, @tagName(x)),
.Type => {
if (@hasDecl(x, "luaPush")) {
return x.luaPush(l);
}
@compileError("luaPushAny doesn't support " ++ @typeName(T));
},
else => @compileError("luaPushAny doesn't support " ++ @typeName(T)),
}
}