mzte-nv: begin haxe rewrite

This commit is contained in:
LordMZTE 2024-10-26 19:26:16 +02:00
parent 177dc43b10
commit c39139fa56
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
26 changed files with 428 additions and 157 deletions

View file

@ -1,8 +1,11 @@
(require :mzte_nv_preloader)
;; Preload mzte-nv
(set package.cpath (.. (. package :cpath) ";" (vim.loop.os_homedir)
:/.local/share/nvim/mzte-nv.so))
(require :settings)
(require :plugins)
(require :maps)
(require :lsp)
(require :pipe)
(require :eval)
(let [(success mztenv) (pcall require :mzte_nv)]
(when (not success)
(error "Failed to preload mzte-nv. Is it installed?"))
(mztenv.onInit))
;; Invoke the haxe config
(require :hx)

View file

@ -1,8 +0,0 @@
;; This module is responsible for loading the native mzte-nv lua module
(set package.cpath (.. (. package :cpath) ";" (vim.loop.os_homedir)
:/.local/share/nvim/mzte-nv.so))
(let [(success mztenv) (pcall require :mzte_nv)]
(when (not success)
(error "Failed to preload mzte-nv. Is it installed?"))
(mztenv.onInit))

View file

@ -1,10 +0,0 @@
(local autopairs (require :nvim-autopairs))
(local cmp-autopairs (require :nvim-autopairs.completion.cmp))
(local cmp (require :cmp))
(autopairs.setup {:check_ts true
:fast_wrap {}
:enable_check_bracket_line false})
(cmp.event:on :confirm_done
(cmp-autopairs.on_confirm_done {:map_char {:tex ""}}))

View file

@ -1,49 +0,0 @@
(local (mztenv catppuccin palettes)
(values (require :mzte_nv) (require :catppuccin)
(require :catppuccin.palettes)))
(local flavour :mocha)
(catppuccin.setup {: flavour
:term_colors true
:dim_inactive {:enabled true}
;; Enable all relevant integrations
:default_integrations false
:integrations {:cmp true
:dap true
:dap_ui true
:gitsigns true
:harpoon true
:lsp_saga true
:markdown true
:native_lsp {:enabled true
:virtual_text (collect [_ diag (ipairs [:errors
:hints
:warnings
:information])]
(values diag
[:italic]))
:underlines (collect [_ diag (ipairs [:errors
:hints
:warnings
:information])]
(values diag
[:underline]))
:inlay_hints {:background true}}
:neogit true
:nvimtree true
:rainbow_delimiters true
:semantic_tokens true
:telescope {:enabled true}
:treesitter true
:treesitter_context true}})
(vim.cmd.colorscheme :catppuccin)
;; Optimize this by saving the palette table
(let [get-palette palettes.get_palette]
(set mztenv.reg.catppuccin-palette (get-palette))
(set palettes.get_palette
(fn [flav]
(if (or (not flav) (= flav flavour)) mztenv.reg.catppuccin-palette
(get-palette flav)))))

View file

@ -1,5 +1,6 @@
(local mztenv (require :mzte_nv))
(local lspc (require :lspconfig))
(local util (require :lspconfig.util))
(local lsp-configs (require :lspconfig.configs))
(macro setup [conf args]
@ -63,5 +64,11 @@
{:settings {:rust-analyzer {:checkOnSave {:command :clippy}}}})
(setup :taplo)
(setup :tinymist {:single_file_support true :offset_encoding :utf-8})
(setup :tinymist
{:single_file_support true
:offset_encoding :utf-8
:root_dir (util.root_pattern :.typstroot)
:settings {:tinymist {:settings {:formatterMode :typstyle
:formatterPrintWidth 100}}}})
(setup :zls)

View file

@ -1,2 +0,0 @@
(local nu (require :nu))
(nu.setup {})

View file

@ -1,64 +0,0 @@
(local mztenv (require :mzte_nv))
(tset mztenv.reg :plugin_load_callbacks [])
(let [path mztenv.reg.nvim_plugins]
(when path
(vim.opt.runtimepath:prepend (.. path "/*"))
(vim.opt.runtimepath:append (.. path :/*/after))))
;; Plugins to load before nvim finishes startup
(local startup-plugins [])
;; Plugins to load in the background
(local deferred-plugins [:catppuccin
:lspconf
:lsp-saga
:cmp
:luasnip
:nullls
:lspprogress
:line
:treesitter
:nvimtree
:devicons
:neogit
:telescope
:autopairs
:tterm
:ts-context
:ufo
:dap
:harpoon
:recorder
:tsn-actions
:dressing
:gitsigns
:nu
:nix-update])
(local errors {})
(fn load-plugin [plugin]
(let [(success ret) (pcall require (.. :pluginconf/p- plugin))]
(when (not success)
(tset errors plugin ret))))
(each [_ p (ipairs startup-plugins)]
(load-plugin p))
(fn load-one-deferred [idx]
(let [plugin (. deferred-plugins idx)]
(if plugin
(do
(load-plugin plugin)
(vim.schedule #(load-one-deferred (+ idx 1))))
(do
(when (next errors)
(vim.notify (accumulate [text "Errors loading plugin configs:\n" plugin err (pairs errors)]
(.. text " - " plugin ": " err "\n"))
vim.log.levels.error))
(each [_ cb (ipairs mztenv.reg.plugin_load_callbacks)]
(pcall cb))))))
(vim.schedule #(load-one-deferred 1))

View file

@ -1,16 +0,0 @@
(local mztenv (require :mzte_nv))
(local cmd vim.cmd)
;; Update $PATH with nvim tools path
(let [lsppath mztenv.reg.nvim_tools]
(when lsppath
(set vim.env.PATH (.. lsppath "/bin:" vim.env.PATH))))
;; CPBuf command
(vim.api.nvim_create_user_command :CPBuf mztenv.cpbuf.copyBuf {:nargs 0})
;; Compile commands
(let [compile-path mztenv.compile.compilePath
make-cmd vim.api.nvim_create_user_command]
(make-cmd :CompileConfig
#(compile-path (.. (vim.fn.getenv :HOME) :/.config/nvim)) {:nargs 0}))

1
mzte-nv/haxe/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
out.lua

6
mzte-nv/haxe/IPlugin.hx Normal file
View file

@ -0,0 +1,6 @@
package;
interface IPlugin {
var name:String;
function init():Void;
}

15
mzte-nv/haxe/LuaPlugin.hx Normal file
View file

@ -0,0 +1,15 @@
package;
import lua.Lua;
class LuaPlugin implements IPlugin {
public var name:String;
public function new(id:String) {
this.name = id;
}
public function init() {
Lua.require("pluginconf.p-" + this.name);
}
}

14
mzte-nv/haxe/Main.hx Normal file
View file

@ -0,0 +1,14 @@
package;
import lua.Lua;
import init.Settings;
import init.Plugins;
function main() {
Settings.init();
new Plugins().init();
Lua.require("maps");
Lua.require("lsp");
Lua.require("pipe");
Lua.require("eval");
}

10
mzte-nv/haxe/build.hxml Normal file
View file

@ -0,0 +1,10 @@
# Only for haxe-languageserver
-cp .
--main Main
-D lua-vanilla
-D lua-jit
-D dce=full
--lua out.lua

View file

@ -0,0 +1,5 @@
package ext.mzte_nv;
extern class CPBuf {
function copyBuf():Void;
}

View file

@ -0,0 +1,6 @@
package ext.mzte_nv;
extern class Compile {
@:luaDotMethod
function compilePath(path:String):Void;
}

View file

@ -0,0 +1,14 @@
package ext.mzte_nv;
import lua.Table.AnyTable;
@:luaRequire("mzte_nv")
extern class MZTENv {
public static var reg:AnyTable;
public static var cpbuf:CPBuf;
public static var compile:Compile;
@:luaDotMethod
public static function onInit():Void;
}

View file

@ -0,0 +1,28 @@
package ext.vim;
import lua.Table.AnyTable;
typedef CreateUserCommandOptions = {
// incomplete
nargs:Int,
};
private extern class ApiExt {
@:luaDotMethod
function nvim_create_user_command(name:String, command:Dynamic, opts:CreateUserCommandOptions):Void;
}
/**
Removes Haxe metadata from tables for picky lua functions.
See: https://github.com/HaxeFoundation/haxe/issues/11805
**/
private inline function removeMeta(x:Dynamic) {
cast(x, AnyTable).__fields__ = null;
}
abstract Api(ApiExt) {
public inline function createUserCommand(name:String, command:Dynamic, opts:CreateUserCommandOptions) {
removeMeta(opts);
this.nvim_create_user_command(name, command, opts);
}
}

View file

@ -0,0 +1,9 @@
package ext.vim;
/**
This class is a more complete version of the luv event loop.
**/
extern class Loop extends lua.lib.luv.Loop {
@:luaDotMethod
extern function os_homedir():String;
}

View file

@ -0,0 +1,7 @@
package ext.vim;
extern class Opt implements Dynamic {
function prepend(x:Dynamic):Void;
function append(x:Dynamic):Void;
function remove(x:Dynamic):Void;
}

View file

@ -0,0 +1,26 @@
package ext.vim;
import lua.Table;
enum abstract LogLevel(Int) {
var Trace;
var Debug;
var Info;
var Warn;
var Error;
var Off;
}
@:native("vim")
extern class Vim {
public static var loop:Loop;
public static var api:Api;
public static var env:Table<String, String>;
public static var opt:Table<String, Opt>;
public static function print(x:Dynamic):Void;
public static function schedule(f:() -> Void):Void;
public static function notify(msg:String, ?level:LogLevel, ?opts:AnyTable):Void;
public static function cmd(cmd:String):Void;
}

View file

@ -0,0 +1,144 @@
package init;
#if !macro
import ext.mzte_nv.MZTENv;
import ext.vim.Vim;
import lua.Table.AnyTable;
using lua.PairTools;
#end
import haxe.Exception;
import haxe.macro.Context;
import haxe.macro.Expr;
using Lambda;
#if !macro
class Plugins {
var startupPlugins:Array<IPlugin>;
var deferredPlugins:Array<IPlugin>;
var errors:Array<{plug:IPlugin, err:Exception}> = [];
public function new() {
regPlugins(["Nu", "Autopairs", "Catppuccin"]);
this.deferredPlugins = this.deferredPlugins.concat([
"cmp",
"dap",
"devicons",
"dressing",
"gitsigns",
"harpoon",
"line",
"lsp-saga",
"lspconf",
"lspprogress",
"luasnip",
"neogit",
"nix-update",
"nullls",
"nvimtree",
"recorder",
"telescope",
"treesitter",
"ts-context",
"tsn-actions",
"tterm",
"ufo",
].map(n -> (new LuaPlugin(n) : IPlugin)));
}
public function init() {
final pluginpath:String = MZTENv.reg.nvim_plugins;
if (pluginpath != null) {
Vim.opt.runtimepath.prepend(pluginpath + "/*");
Vim.opt.runtimepath.append(pluginpath + ":/*/after");
}
if (MZTENv.reg.plugin_load_callbacks == null) {
MZTENv.reg.plugin_load_callbacks = {};
}
for (p in this.startupPlugins) {
this.loadPlugin(p);
}
Vim.schedule(() -> this.loadOneDeferred(0));
}
private function loadPlugin(p:IPlugin) {
try {
p.init();
} catch (e) {
errors.push({plug: p, err: e});
}
}
private function loadOneDeferred(idx:Int) {
this.loadPlugin(this.deferredPlugins[idx]);
if (++idx < this.deferredPlugins.length) {
Vim.schedule(() -> loadOneDeferred(idx));
} else {
if (this.errors.length != 0) {
Vim.notify(
this.errors.fold((e, m) -> '$m - ${e.plug.name}: ${e.err}\n', "Errors while loading plugins:\n"),
LogLevel.Error
);
}
cast(MZTENv.reg.plugin_load_callbacks, AnyTable).ipairsEach((_, cb) -> try {
cb();
});
}
}
}
#end
private macro function regPlugins(names:Array<String>):Expr {
var startupPlugins = [];
var deferredPlugins = [];
for (name in names) {
final cl = Context.getType('plugins.P${name}');
switch (cl) {
case TInst(inst, _):
final params = inst.get().meta.extract("plugin")[0].params;
switch (params) {
case [{expr: EConst(CInt(prio, _))}, {expr: EConst(CIdent(startup))}]:
if (startup == "true") {
startupPlugins.push({prio: Std.parseInt(prio), type: inst.get()});
} else {
deferredPlugins.push({prio: Std.parseInt(prio), type: inst.get()});
}
default:
Context.error("Invalid params", inst.get().meta.extract("plugin")[0].pos);
}
default:
}
}
startupPlugins.sort((a, b) -> a.prio - b.prio);
deferredPlugins.sort((a, b) -> a.prio - b.prio);
final makeConstructorCall = (p) -> {
final tpath = {
params: null,
sub: null,
name: p.type.name,
pack: p.type.pack
};
return macro new $tpath();
};
final startupPluginExpr = [for (p in startupPlugins) makeConstructorCall(p)];
final deferredPluginExpr = [for (p in deferredPlugins) makeConstructorCall(p)];
return macro {
this.startupPlugins = $a{startupPluginExpr};
this.deferredPlugins = $a{deferredPluginExpr};
};
}

View file

@ -0,0 +1,23 @@
package init;
import lua.Os;
import ext.mzte_nv.MZTENv;
import ext.vim.Vim;
class Settings {
public static function init() {
// Update $PATH with nvim tools path
final toolspath:Null<String> = MZTENv.reg.nvim_tools;
if (toolspath != null) {
Vim.env.PATH = '${toolspath}/bin:${Vim.env.PATH}';
}
// CPBuf command
Vim.api.createUserCommand("CPBuf", MZTENv.cpbuf.copyBuf, {nargs: 0});
// Compile Command
Vim.api.createUserCommand("CompileConfig", () -> {
MZTENv.compile.compilePath(Os.getenv("HOME") + "/.config/nvim");
}, {nargs: 0});
}
}

View file

@ -0,0 +1,23 @@
package plugins;
import lua.Lua;
@plugin(50, false)
class PAutopairs implements IPlugin {
public var name:String = "Autopairs";
public function new() {}
public function init() {
Lua.require("nvim-autopairs").setup({
check_ts: true,
fast_wrap: {},
enable_check_bracket_line: false,
});
Lua.require("cmp").event.on(
"confirm_done",
Lua.require("nvim-autopairs.completion.cmp")[untyped "on_confirm_done"]({map_char: {tex: ""}})
);
}
}

View file

@ -0,0 +1,53 @@
package plugins;
import lua.Table;
import ext.vim.Vim;
import lua.Lua;
@plugin(20, false)
class PCatppuccin implements IPlugin {
public var name:String = "Catppuccin";
public function new() {}
public function init() {
final catppuccin = Lua.require("catppuccin");
catppuccin[untyped "setup"]({
flavour: "mocha",
term_colors: true,
dim_inactive: {enabled: true},
// Enable only relevant integrations
default_integrations: false,
integrations: {
cmp: true,
dap: true,
dap_ui: true,
gitsigns: true,
harpoon: true,
lsp_saga: true,
markdown: true,
native_lsp: {
enabled: true,
virtual_text: Table.fromMap(
[
for (t in ["errors", "hints", "warnings", "information"])
t => Table.fromArray(["italic"])
]
),
underlines: Table.fromMap([for (t in ["errors", "hints", "warnings", "information"]) t => Table.fromArray(["italic"])]),
inlay_hints: {background: true},
},
neogit: true,
nvimtree: true,
rainbow_delimiters: true,
semantic_tokens: true,
telescope: {enabled: true},
treesitter: true,
treesitter_context: true,
},
});
Vim.cmd("colorscheme catppuccin");
}
}

View file

@ -0,0 +1,19 @@
package plugins;
import lua.Lua;
@plugin(50, false)
class PNu implements IPlugin {
public var name:String = "Nushell";
public function new() {}
public function init() {
final nu:Nu = Lua.require("nu");
nu.setup({});
}
}
private extern class Nu {
@:luaDotMethod
function setup(opts:{}):Void;
}

View file

@ -8,5 +8,12 @@
(install-zig "mzte-nv" "ReleaseSafe")
(rm nvim-config-dir)
(copy "mzte-nv/conf" nvim-config-dir)
(cmd "haxe"
"-cp" "mzte-nv/haxe"
"--main" "Main"
"-D" "lua-vanilla"
"-D" "dce=full"
"-D" "lua-jit"
"--lua" (build-path nvim-config-dir "lua" "hx.lua"))
(cmd "mzte-nv-compile" (path->string nvim-config-dir))
null)