From 73d251f9cf40ff585b90d6e778de5cfffb670966 Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Tue, 3 Jan 2023 22:57:55 +0100 Subject: [PATCH] feat: mzte-nv now compiles packer-compiled.lua --- mzte-nv/conf/lua/mzte_nv_preloader.lua | 4 +-- mzte-nv/conf/lua/plugins.lua | 11 +++++-- mzte-nv/conf/lua/settings.lua | 4 +++ mzte-nv/src/compiler.zig | 44 ++++++++++++++------------ mzte-nv/src/main.zig | 2 +- 5 files changed, 39 insertions(+), 26 deletions(-) diff --git a/mzte-nv/conf/lua/mzte_nv_preloader.lua b/mzte-nv/conf/lua/mzte_nv_preloader.lua index 78b36b7..5b07458 100644 --- a/mzte-nv/conf/lua/mzte_nv_preloader.lua +++ b/mzte-nv/conf/lua/mzte_nv_preloader.lua @@ -1,9 +1,9 @@ -- This module is responsible for loading the native mzte-nv lua module package.cpath = package.cpath .. ";" .. vim.loop.os_homedir() .. "/.local/share/nvim/mzte-nv.so" -local success = pcall(require, "mzte_nv"); +local success, mztenv = pcall(require, "mzte_nv") if not success then error "Failed to preload mzte-nv. Is it installed?" end -require("mzte_nv").onInit() +mztenv.onInit() diff --git a/mzte-nv/conf/lua/plugins.lua b/mzte-nv/conf/lua/plugins.lua index f1ab94a..f5cc613 100644 --- a/mzte-nv/conf/lua/plugins.lua +++ b/mzte-nv/conf/lua/plugins.lua @@ -186,6 +186,11 @@ 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 }) +-- actually compile packer-generated config after packer's "compile" step +vim.api.nvim_create_autocmd("User", { + pattern = "PackerCompileDone", + once = true, + callback = function() + require("mzte_nv").compile.compilePath(require("packer").config.compile_path) + end, +}) diff --git a/mzte-nv/conf/lua/settings.lua b/mzte-nv/conf/lua/settings.lua index 2c0a288..4116240 100644 --- a/mzte-nv/conf/lua/settings.lua +++ b/mzte-nv/conf/lua/settings.lua @@ -40,3 +40,7 @@ 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 }) + +vim.api.nvim_create_user_command("CompilePlugins", function() + require("mzte_nv").compile.compilePath(require("packer").config.package_root) +end, { nargs = 0 }) diff --git a/mzte-nv/src/compiler.zig b/mzte-nv/src/compiler.zig index b813a3f..e899fc5 100644 --- a/mzte-nv/src/compiler.zig +++ b/mzte-nv/src/compiler.zig @@ -46,32 +46,36 @@ pub fn doCompile(path: []const u8, alloc: std.mem.Allocator) !void { // prepare state c.lua_getfield(l, c.LUA_GLOBALSINDEX, "string"); - 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 }); + // a list of lua files to compile + var files = std.ArrayList([]const u8).init(alloc); + defer files.deinit(); - switch (entry.kind) { - .File => { - if (std.mem.endsWith(u8, entry.path, ".lua")) { - try files.append(entry_path); - } - }, - else => {}, + if ((try std.fs.cwd().statFile(path)).kind == .Directory) { + var dir = try std.fs.cwd().openIterableDir(path, .{}); + defer dir.close(); + + var walker = try dir.walk(alloc); + defer walker.deinit(); + + 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 => {}, + } } + } else { + try files.append(path); } for (files.items) |luafile| { @@ -80,7 +84,7 @@ pub fn doCompile(path: []const u8, alloc: std.mem.Allocator) !void { c.lua_getfield(l, -1, "dump"); if (c.luaL_loadfile(l, luafile_z) != 0) { - std.log.warn( + log.warn( "error compiling lua object {s}: {s}", .{ luafile, c.lua_tolstring(l, -1, null) }, ); diff --git a/mzte-nv/src/main.zig b/mzte-nv/src/main.zig index e2d1fd4..c58a773 100644 --- a/mzte-nv/src/main.zig +++ b/mzte-nv/src/main.zig @@ -3,7 +3,7 @@ const ffi = @import("ffi.zig"); const ser = @import("ser.zig"); const c = ffi.c; -pub const version = "1.1.0"; +pub const version = "1.2.0"; const modules = struct { const cmp = @import("modules/cmp.zig");