diff --git a/justfile b/justfile index fb5e7fb..823d6e7 100644 --- a/justfile +++ b/justfile @@ -40,3 +40,8 @@ install-lsps-paru: install-mzte-nv: cd mzte-nv && zig build -Drelease-fast -p ~/.local + +setup-nvim-config: install-mzte-nv + rm -rf ~/.config/nvim + cp -r mzte-nv/conf ~/.config/nvim + mzte-nv-compile ~/.config/nvim diff --git a/mzte-nv/build.zig b/mzte-nv/build.zig index daa11bf..302208e 100644 --- a/mzte-nv/build.zig +++ b/mzte-nv/build.zig @@ -17,6 +17,16 @@ pub fn build(b: *std.build.Builder) !void { lib.unwind_tables = true; b.getInstallStep().dependOn(&(try InstallStep.init(b, lib)).step); + + // this is the install step for the lua config compiler binary + const compiler = b.addExecutable("mzte-nv-compile", "src/compiler.zig"); + compiler.setBuildMode(mode); + + compiler.linkLibC(); + + compiler.strip = mode != .Debug; + + compiler.install(); } const InstallStep = struct { diff --git a/.config/nvim/.gitignore b/mzte-nv/conf/.gitignore similarity index 100% rename from .config/nvim/.gitignore rename to mzte-nv/conf/.gitignore diff --git a/.config/nvim/ftplugin/java.lua b/mzte-nv/conf/ftplugin/java.lua similarity index 100% rename from .config/nvim/ftplugin/java.lua rename to mzte-nv/conf/ftplugin/java.lua diff --git a/.config/nvim/ginit.vim b/mzte-nv/conf/ginit.vim similarity index 100% rename from .config/nvim/ginit.vim rename to mzte-nv/conf/ginit.vim diff --git a/.config/nvim/init.lua b/mzte-nv/conf/init.lua similarity index 100% rename from .config/nvim/init.lua rename to mzte-nv/conf/init.lua diff --git a/.config/nvim/lua/maps.lua b/mzte-nv/conf/lua/maps.lua similarity index 100% rename from .config/nvim/lua/maps.lua rename to mzte-nv/conf/lua/maps.lua diff --git a/.config/nvim/lua/mzte_nv_preloader.lua b/mzte-nv/conf/lua/mzte_nv_preloader.lua similarity index 100% rename from .config/nvim/lua/mzte_nv_preloader.lua rename to mzte-nv/conf/lua/mzte_nv_preloader.lua diff --git a/.config/nvim/lua/neovide.lua b/mzte-nv/conf/lua/neovide.lua similarity index 100% rename from .config/nvim/lua/neovide.lua rename to mzte-nv/conf/lua/neovide.lua diff --git a/.config/nvim/lua/pipe.lua b/mzte-nv/conf/lua/pipe.lua similarity index 100% rename from .config/nvim/lua/pipe.lua rename to mzte-nv/conf/lua/pipe.lua diff --git a/.config/nvim/lua/pluginconf/autopairs.lua b/mzte-nv/conf/lua/pluginconf/autopairs.lua similarity index 100% rename from .config/nvim/lua/pluginconf/autopairs.lua rename to mzte-nv/conf/lua/pluginconf/autopairs.lua diff --git a/.config/nvim/lua/pluginconf/line.lua b/mzte-nv/conf/lua/pluginconf/line.lua similarity index 100% rename from .config/nvim/lua/pluginconf/line.lua rename to mzte-nv/conf/lua/pluginconf/line.lua diff --git a/.config/nvim/lua/pluginconf/lspconf.lua b/mzte-nv/conf/lua/pluginconf/lspconf.lua similarity index 100% rename from .config/nvim/lua/pluginconf/lspconf.lua rename to mzte-nv/conf/lua/pluginconf/lspconf.lua diff --git a/.config/nvim/lua/pluginconf/nullls.lua b/mzte-nv/conf/lua/pluginconf/nullls.lua similarity index 100% rename from .config/nvim/lua/pluginconf/nullls.lua rename to mzte-nv/conf/lua/pluginconf/nullls.lua diff --git a/.config/nvim/lua/pluginconf/nvim_aerial.lua b/mzte-nv/conf/lua/pluginconf/nvim_aerial.lua similarity index 100% rename from .config/nvim/lua/pluginconf/nvim_aerial.lua rename to mzte-nv/conf/lua/pluginconf/nvim_aerial.lua diff --git a/.config/nvim/lua/pluginconf/nvim_cmp.lua b/mzte-nv/conf/lua/pluginconf/nvim_cmp.lua similarity index 100% rename from .config/nvim/lua/pluginconf/nvim_cmp.lua rename to mzte-nv/conf/lua/pluginconf/nvim_cmp.lua diff --git a/.config/nvim/lua/pluginconf/nvim_dap.lua b/mzte-nv/conf/lua/pluginconf/nvim_dap.lua similarity index 100% rename from .config/nvim/lua/pluginconf/nvim_dap.lua rename to mzte-nv/conf/lua/pluginconf/nvim_dap.lua diff --git a/.config/nvim/lua/pluginconf/nvim_luasnip.lua b/mzte-nv/conf/lua/pluginconf/nvim_luasnip.lua similarity index 100% rename from .config/nvim/lua/pluginconf/nvim_luasnip.lua rename to mzte-nv/conf/lua/pluginconf/nvim_luasnip.lua diff --git a/.config/nvim/lua/pluginconf/nvim_neogit.lua b/mzte-nv/conf/lua/pluginconf/nvim_neogit.lua similarity index 100% rename from .config/nvim/lua/pluginconf/nvim_neogit.lua rename to mzte-nv/conf/lua/pluginconf/nvim_neogit.lua diff --git a/.config/nvim/lua/pluginconf/nvim_ufo.lua b/mzte-nv/conf/lua/pluginconf/nvim_ufo.lua similarity index 100% rename from .config/nvim/lua/pluginconf/nvim_ufo.lua rename to mzte-nv/conf/lua/pluginconf/nvim_ufo.lua diff --git a/.config/nvim/lua/pluginconf/nvimtree.lua b/mzte-nv/conf/lua/pluginconf/nvimtree.lua similarity index 100% rename from .config/nvim/lua/pluginconf/nvimtree.lua rename to mzte-nv/conf/lua/pluginconf/nvimtree.lua diff --git a/.config/nvim/lua/pluginconf/rust_tools.lua b/mzte-nv/conf/lua/pluginconf/rust_tools.lua similarity index 100% rename from .config/nvim/lua/pluginconf/rust_tools.lua rename to mzte-nv/conf/lua/pluginconf/rust_tools.lua diff --git a/.config/nvim/lua/pluginconf/telescope.lua b/mzte-nv/conf/lua/pluginconf/telescope.lua similarity index 100% rename from .config/nvim/lua/pluginconf/telescope.lua rename to mzte-nv/conf/lua/pluginconf/telescope.lua diff --git a/.config/nvim/lua/pluginconf/treesitter.lua b/mzte-nv/conf/lua/pluginconf/treesitter.lua similarity index 100% rename from .config/nvim/lua/pluginconf/treesitter.lua rename to mzte-nv/conf/lua/pluginconf/treesitter.lua diff --git a/.config/nvim/lua/pluginconf/ts-context.lua b/mzte-nv/conf/lua/pluginconf/ts-context.lua similarity index 100% rename from .config/nvim/lua/pluginconf/ts-context.lua rename to mzte-nv/conf/lua/pluginconf/ts-context.lua diff --git a/.config/nvim/lua/pluginconf/tterm.lua b/mzte-nv/conf/lua/pluginconf/tterm.lua similarity index 100% rename from .config/nvim/lua/pluginconf/tterm.lua rename to mzte-nv/conf/lua/pluginconf/tterm.lua diff --git a/.config/nvim/lua/pluginconf/zigtools.lua b/mzte-nv/conf/lua/pluginconf/zigtools.lua similarity index 100% rename from .config/nvim/lua/pluginconf/zigtools.lua rename to mzte-nv/conf/lua/pluginconf/zigtools.lua diff --git a/.config/nvim/lua/plugins.lua b/mzte-nv/conf/lua/plugins.lua similarity index 94% rename from .config/nvim/lua/plugins.lua rename to mzte-nv/conf/lua/plugins.lua index 941cf69..210f7bb 100644 --- a/.config/nvim/lua/plugins.lua +++ b/mzte-nv/conf/lua/plugins.lua @@ -55,7 +55,7 @@ local function cmp_plugins(use) } end -return require("packer").startup(function(use) +require("packer").startup(function(use) use "wbthomason/packer.nvim" use { @@ -180,3 +180,7 @@ return require("packer").startup(function(use) cmp_plugins(use) end) + +vim.api.nvim_create_user_command("CompilePlugins", function() + require("mzte_nv").compile.compilePath(require("packer").config.package_root) +end, { nargs = 0 }) diff --git a/.config/nvim/lua/settings.lua b/mzte-nv/conf/lua/settings.lua similarity index 80% rename from .config/nvim/lua/settings.lua rename to mzte-nv/conf/lua/settings.lua index 24c7e52..2c0a288 100644 --- a/.config/nvim/lua/settings.lua +++ b/mzte-nv/conf/lua/settings.lua @@ -36,3 +36,7 @@ cmd "colorscheme dracula" cmd "autocmd StdinReadPre * let s:std_in=1" cmd "filetype plugin on" + +vim.api.nvim_create_user_command("CompileConfig", function() + require("mzte_nv").compile.compilePath(vim.fn.getenv("HOME") .. "/.config/nvim") +end, { nargs = 0 }) diff --git a/.config/nvim/queries/java/highlights.scm b/mzte-nv/conf/queries/java/highlights.scm similarity index 100% rename from .config/nvim/queries/java/highlights.scm rename to mzte-nv/conf/queries/java/highlights.scm diff --git a/.config/nvim/queries/lua/highlights.scm b/mzte-nv/conf/queries/lua/highlights.scm similarity index 100% rename from .config/nvim/queries/lua/highlights.scm rename to mzte-nv/conf/queries/lua/highlights.scm diff --git a/.config/nvim/queries/rust/highlights.scm b/mzte-nv/conf/queries/rust/highlights.scm similarity index 100% rename from .config/nvim/queries/rust/highlights.scm rename to mzte-nv/conf/queries/rust/highlights.scm diff --git a/.config/nvim/queries/zig/highlights.scm b/mzte-nv/conf/queries/zig/highlights.scm similarity index 100% rename from .config/nvim/queries/zig/highlights.scm rename to mzte-nv/conf/queries/zig/highlights.scm diff --git a/mzte-nv/src/compiler.zig b/mzte-nv/src/compiler.zig new file mode 100644 index 0000000..f4a1566 --- /dev/null +++ b/mzte-nv/src/compiler.zig @@ -0,0 +1,105 @@ +const std = @import("std"); + +const log = std.log.scoped(.compiler); + +pub const log_level = .debug; + +pub fn main() !void { + if (std.os.argv.len != 2) { + log.err( + \\Usage: {s} [dir] + \\ + \\`input` is a path to a normal lua neovim configuration + \\(or any other path containing lua files.) + , + .{std.os.argv[0]}, + ); + + return error.InvalidArgs; + } + + const input_arg = std.mem.span(std.os.argv[1]); + + try doCompile(input_arg); +} + +pub fn doCompile(path: []const u8) !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + + const alloc = gpa.allocator(); + + var dir = try std.fs.cwd().openIterableDir(path, .{}); + defer dir.close(); + + var walker = try dir.walk(alloc); + defer walker.deinit(); + + // a list of lua files to compile + var files = std.ArrayList([]const u8).init(alloc); + defer files.deinit(); + + // an arena allocator to hold data to be used during the build + var build_arena = std.heap.ArenaAllocator.init(alloc); + defer build_arena.deinit(); + const build_alloc = build_arena.allocator(); + + while (try walker.next()) |entry| { + const entry_path = try std.fs.path.join(build_alloc, &.{ path, entry.path }); + + switch (entry.kind) { + .File => { + if (std.mem.endsWith(u8, entry.path, ".lua")) { + try files.append(entry_path); + } + }, + else => {}, + } + } + + // a buffer containing the content of the currently compiling lua file + var content_buf = std.ArrayList(u8).init(alloc); + defer content_buf.deinit(); + + for (files.items) |luafile| { + content_buf.clearRetainingCapacity(); + + var lfile = try std.fs.cwd().openFile(luafile, .{}); + defer lfile.close(); + var lfifo = std.fifo.LinearFifo(u8, .{ .Static = 1024 * 64 }).init(); + try lfifo.pump(lfile.reader(), content_buf.writer()); + + const argv = try build_alloc.allocSentinel( + ?[*:0]const u8, + 5, + null, + ); + + // TODO: maybe try doing this through to luajit C api instead of a process? + // not sure if that's possible + argv[0] = "luajit"; + argv[1] = "-O9"; + argv[2] = "-b"; + argv[3] = "-"; + argv[4] = try std.cstr.addNullByte(build_alloc, luafile); + + // Doing it the C way because zig's ChildProcess ain't got this + const pipe = try std.os.pipe2(0); + const pid = try std.os.fork(); + if (pid == 0) { + std.os.close(pipe[1]); + try std.os.dup2(pipe[0], 0); + return std.os.execvpeZ(argv[0].?, argv, std.c.environ); + } + + std.os.close(pipe[0]); + + try (std.fs.File{ .handle = pipe[1] }).writeAll(content_buf.items); + + std.os.close(pipe[1]); + if (std.os.waitpid(pid, 0).status != 0) { + log.warn("luajit crashed compiling {s}, skipping", .{luafile}); + } + } + log.info("compiled {} lua objects @ {s}", .{ files.items.len, path }); +} diff --git a/mzte-nv/src/main.zig b/mzte-nv/src/main.zig index c25b3f5..e18f91e 100644 --- a/mzte-nv/src/main.zig +++ b/mzte-nv/src/main.zig @@ -3,19 +3,71 @@ const ffi = @import("ffi.zig"); const ser = @import("ser.zig"); const c = ffi.c; -pub const version = "0.3.0"; +pub const version = "1.0.0"; const modules = struct { const cmp = @import("modules/cmp.zig"); + const compile = @import("modules/compile.zig"); const jdtls = @import("modules/jdtls.zig"); const utils = @import("modules/utils.zig"); }; +var lua_state: ?*c.lua_State = null; + +pub fn log( + comptime level: std.log.Level, + comptime scope: @TypeOf(.EnumLiteral), + comptime format: []const u8, + args: anytype, +) void { + // if there's no lua state, we can't invoke nvim notifications. + const l = lua_state orelse return; + + const stacktop = c.lua_gettop(l); + defer c.lua_settop(l, stacktop); + + var fmtbuf: [2048]u8 = undefined; + + c.lua_getglobal(l, "vim"); + c.lua_getfield(l, -1, "log"); + c.lua_getfield(l, -1, "levels"); + switch (level) { + .err => c.lua_getfield(l, -1, "ERROR"), + .warn => c.lua_getfield(l, -1, "WARN"), + .info => c.lua_getfield(l, -1, "INFO"), + .debug => c.lua_getfield(l, -1, "DEBUG"), + } + + const vim_lvl = c.lua_tointeger(l, -1); + c.lua_pop(l, 3); + + c.lua_getfield(l, -1, "notify"); + + const msg = std.fmt.bufPrintZ(&fmtbuf, format, args) catch return; + c.lua_pushstring(l, msg.ptr); + + c.lua_pushinteger(l, vim_lvl); + + const title = std.fmt.bufPrintZ( + &fmtbuf, + "MZTE-NV ({s})", + .{@tagName(scope)}, + ) catch return; + ser.luaPushAny(l, .{ + .title = title, + }); + c.lua_call(l, 3, 0); +} + +pub const log_level = .debug; + export fn luaopen_mzte_nv(l_: ?*c.lua_State) c_int { + lua_state = l_; const l = l_.?; ser.luaPushAny(l, .{ .onInit = ffi.luaFunc(lOnInit), .cmp = modules.cmp, + .compile = modules.compile, .jdtls = modules.jdtls, .utils = modules.utils, }); @@ -41,16 +93,9 @@ fn lOnInit(l: *c.lua_State) !c_int { c.lua_settop(l, 1); - var buf: [128]u8 = undefined; - const s = try std.fmt.bufPrintZ( - &buf, + std.log.info( "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; } diff --git a/mzte-nv/src/modules/compile.zig b/mzte-nv/src/modules/compile.zig new file mode 100644 index 0000000..7c32cca --- /dev/null +++ b/mzte-nv/src/modules/compile.zig @@ -0,0 +1,19 @@ +/// Module for compiling lua files using luajit +/// and mzte-nv-compiler. +const std = @import("std"); +const ser = @import("../ser.zig"); +const ffi = @import("../ffi.zig"); +const c = ffi.c; +const compiler = @import("../compiler.zig"); + +pub fn luaPush(l: *c.lua_State) void { + ser.luaPushAny(l, .{ + .compilePath = ffi.luaFunc(lCompilePath), + }); +} + +fn lCompilePath(l: *c.lua_State) !c_int { + const path = c.luaL_checklstring(l, 1, null); + try compiler.doCompile(std.mem.span(path)); + return 0; +}