dotfiles/mzte-nv/src/main.zig

99 lines
2.6 KiB
Zig
Raw Normal View History

2022-10-18 23:03:39 +02:00
const std = @import("std");
2023-04-23 18:07:38 +02:00
const nvim = @import("nvim");
const znvim = @import("znvim");
2022-10-18 23:03:39 +02:00
const ffi = @import("ffi.zig");
2022-10-20 15:04:28 +02:00
const ser = @import("ser.zig");
2022-10-18 23:03:39 +02:00
const c = ffi.c;
pub const version = "1.2.0";
2022-10-20 15:04:28 +02:00
2022-10-18 23:03:39 +02:00
const modules = struct {
2022-10-22 13:21:48 +02:00
const cmp = @import("modules/cmp.zig");
2022-11-19 01:53:49 +01:00
const compile = @import("modules/compile.zig");
2022-10-18 23:03:39 +02:00
const jdtls = @import("modules/jdtls.zig");
2023-02-21 21:42:55 +01:00
const tsn_actions = @import("modules/tsn_actions.zig");
2022-11-14 16:26:12 +01:00
const utils = @import("modules/utils.zig");
2022-10-18 23:03:39 +02:00
};
2023-01-17 15:07:16 +01:00
pub const std_options = struct {
pub fn logFn(
comptime level: std.log.Level,
comptime scope: @TypeOf(.EnumLiteral),
comptime format: []const u8,
args: anytype,
) void {
2023-04-23 18:07:38 +02:00
var msg_buf: [512]u8 = undefined;
const msg = std.fmt.bufPrintZ(&msg_buf, format, args) catch return;
2023-01-17 15:07:16 +01:00
2023-04-23 18:07:38 +02:00
var title_buf: [512]u8 = undefined;
2023-01-17 15:07:16 +01:00
const title = std.fmt.bufPrintZ(
2023-04-23 18:07:38 +02:00
&title_buf,
2023-01-17 15:07:16 +01:00
"MZTE-NV ({s})",
.{@tagName(scope)},
) catch return;
2023-04-23 18:07:38 +02:00
const lvl = switch (level) {
.debug => nvim.LOGLVL_DBG,
.info => nvim.LOGLVL_INF,
.warn => nvim.LOGLVL_WRN,
.err => nvim.LOGLVL_ERR,
};
var dict = znvim.Dictionary{.alloc = std.heap.c_allocator};
defer dict.deinit();
dict.push(@constCast("title"), znvim.nvimObject(@as([]u8, title))) catch return;
var e = znvim.Error{};
_ = nvim.nvim_notify(
znvim.nvimString(msg),
lvl,
dict.dict,
&e.err,
);
2022-11-19 01:53:49 +01:00
}
2023-01-17 15:07:16 +01:00
pub const log_level = .debug;
};
2022-11-19 01:53:49 +01:00
2022-10-18 23:03:39 +02:00
export fn luaopen_mzte_nv(l_: ?*c.lua_State) c_int {
const l = l_.?;
2022-10-20 15:04:28 +02:00
ser.luaPushAny(l, .{
.onInit = ffi.luaFunc(lOnInit),
2022-10-22 13:21:48 +02:00
.cmp = modules.cmp,
2022-11-19 01:53:49 +01:00
.compile = modules.compile,
2022-10-20 15:04:28 +02:00
.jdtls = modules.jdtls,
2023-02-21 21:42:55 +01:00
.tsn_actions = modules.tsn_actions,
2022-11-14 16:26:12 +01:00
.utils = modules.utils,
2022-10-20 15:04:28 +02:00
});
2022-10-18 23:03:39 +02:00
return 1;
}
2022-10-20 15:04:28 +02:00
fn lOnInit(l: *c.lua_State) !c_int {
2023-05-24 19:12:56 +02:00
try @import("options.zig").initOptions();
2022-10-20 15:04:28 +02:00
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);
2022-11-19 01:53:49 +01:00
std.log.info(
2022-10-20 15:04:28 +02:00
"MZTE-NV v{s} Initialized on NVIM v{}.{}.{}{s}",
.{ version, major, minor, patch, prerelease },
);
return 0;
}