revolutionize neovim config

This commit is contained in:
LordMZTE 2022-11-19 01:53:49 +01:00
parent 2434c43f4d
commit a17571af2b
Signed by: LordMZTE
GPG Key ID: B64802DC33A64FF6
36 changed files with 202 additions and 10 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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 })

View File

@ -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 })

105
mzte-nv/src/compiler.zig Normal file
View File

@ -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 });
}

View File

@ -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;
}

View File

@ -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;
}