dotfiles/mzte-nv/src/main.zig

121 lines
3.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");
2024-04-05 22:56:00 +02:00
const opts = @import("opts");
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");
2023-05-28 01:16:15 +02:00
const cpbuf = @import("modules/cpbuf.zig");
const fennel = @import("modules/fennel.zig");
2022-10-18 23:03:39 +02:00
const jdtls = @import("modules/jdtls.zig");
2024-01-18 18:23:20 +01:00
const lsp = @import("modules/lsp.zig");
2023-11-16 19:52:51 +01:00
const telescope = @import("modules/telescope.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
};
pub const std_options = std.Options{
.logFn = struct {
pub fn logFn(
comptime level: std.log.Level,
comptime scope: @TypeOf(.EnumLiteral),
comptime format: []const u8,
args: anytype,
) void {
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
var title_buf: [512]u8 = undefined;
const title = std.fmt.bufPrintZ(
&title_buf,
"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,
};
2023-04-23 18:07:38 +02:00
var dict = znvim.Dictionary{ .alloc = std.heap.c_allocator };
defer dict.deinit();
2023-04-23 18:07:38 +02:00
dict.push(@constCast("title"), znvim.nvimObject(@as([]u8, title))) catch return;
// noice notficitaions recognize this and show in the mini view instead of notifs
dict.push(@constCast("mzte_nv_mini"), comptime znvim.nvimObject(true)) catch return;
2023-04-23 18:07:38 +02:00
var e = znvim.Error{};
_ = nvim.nvim_notify(
znvim.nvimString(msg),
lvl,
dict.dict,
&e.err,
);
}
}.logFn,
2022-11-19 01:53:49 +01:00
.log_level = .debug,
2023-01-17 15:07:16 +01:00
};
2022-11-19 01:53:49 +01:00
const reg_key = "mzte-nv-reg";
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, .{
.reg = struct {
pub fn luaPush(lua: *c.lua_State) void {
c.lua_getfield(lua, c.LUA_REGISTRYINDEX, reg_key);
// registry uninitialized
if (c.lua_isnil(lua, -1)) {
c.lua_pop(lua, 1);
c.lua_newtable(lua);
c.lua_pushvalue(lua, -1);
c.lua_setfield(lua, c.LUA_REGISTRYINDEX, reg_key);
}
}
},
2022-10-20 15:04:28 +02:00
.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,
2023-05-28 01:16:15 +02:00
.cpbuf = modules.cpbuf,
.fennel = modules.fennel,
2022-10-20 15:04:28 +02:00
.jdtls = modules.jdtls,
2024-01-18 18:23:20 +01:00
.lsp = modules.lsp,
2023-11-16 19:52:51 +01:00
.telescope = modules.telescope,
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();
2024-04-05 22:56:00 +02:00
c.lua_getfield(l, c.LUA_REGISTRYINDEX, reg_key);
defer c.lua_pop(l, 1);
inline for (.{ "tree_sitter_parsers", "nvim_tools" }) |fname| {
if (@field(opts, fname)) |x| {
ffi.luaPushString(l, x);
c.lua_setfield(l, -2, fname);
}
}
2022-11-19 01:53:49 +01:00
std.log.info(
"MZTE-NV v{s} Initialized on {s}",
2023-05-27 22:57:31 +02:00
.{ version, nvim.longVersion },
2022-10-20 15:04:28 +02:00
);
return 0;
}