From b1c60fe4d92150bb1f7378ab6d69a79ce5b40714 Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Wed, 30 Oct 2024 09:37:17 +0100 Subject: [PATCH] mzte-nv: port tsn-actions config to haxe --- mzte-nv/conf/lua/pluginconf/p-tsn-actions.fnl | 55 -------- mzte-nv/haxe/DynTable.hx | 15 --- mzte-nv/haxe/StaticTools.hx | 17 +++ mzte-nv/haxe/ext/mzte_nv/CPBuf.hx | 1 + mzte-nv/haxe/ext/mzte_nv/MZTENv.hx | 2 + mzte-nv/haxe/ext/mzte_nv/TSNActions.hx | 15 +++ mzte-nv/haxe/ext/mzte_nv/Utils.hx | 5 + mzte-nv/haxe/ext/vim/Api.hx | 12 +- mzte-nv/haxe/ext/vim/Keymap.hx | 18 +++ mzte-nv/haxe/ext/vim/Vim.hx | 1 + mzte-nv/haxe/init/Plugins.hx | 69 ++-------- mzte-nv/haxe/plugins/PAutopairs.hx | 1 - mzte-nv/haxe/plugins/PCatppuccin.hx | 1 - mzte-nv/haxe/plugins/PNu.hx | 1 - mzte-nv/haxe/plugins/PTSNActions.hx | 122 ++++++++++++++++++ 15 files changed, 192 insertions(+), 143 deletions(-) delete mode 100644 mzte-nv/conf/lua/pluginconf/p-tsn-actions.fnl delete mode 100644 mzte-nv/haxe/DynTable.hx create mode 100644 mzte-nv/haxe/StaticTools.hx create mode 100644 mzte-nv/haxe/ext/mzte_nv/TSNActions.hx create mode 100644 mzte-nv/haxe/ext/mzte_nv/Utils.hx create mode 100644 mzte-nv/haxe/ext/vim/Keymap.hx create mode 100644 mzte-nv/haxe/plugins/PTSNActions.hx diff --git a/mzte-nv/conf/lua/pluginconf/p-tsn-actions.fnl b/mzte-nv/conf/lua/pluginconf/p-tsn-actions.fnl deleted file mode 100644 index 9dcb508..0000000 --- a/mzte-nv/conf/lua/pluginconf/p-tsn-actions.fnl +++ /dev/null @@ -1,55 +0,0 @@ -(local (mztenv tsna tsna-actions tsna-helpers nullls) - (values (require :mzte_nv) (require :ts-node-action) - (require :ts-node-action.actions) - (require :ts-node-action.helpers) (require :null-ls))) - -(local zig-padding {")" " %s" - "]" " %s" - "}" " %s" - "{" "%s " - "," "%s " - := " %s " - :+ " %s " - :- " %s " - :* " %s " - :/ " %s " - :** " %s " - :++ " %s "}) - -(macro md-marker-fn [level] - `[{1 (fn [_#] ,(string.rep "#" level)) :name ,(.. "Convert to H" level)}]) - -(local int-toggle-action {1 #(mztenv.tsn_actions.intToggle (tsna-helpers.node_text $1)) - :name "Toggle Dec/Hex"}) - -(local int-to-hex-action {1 #(mztenv.tsn_actions.intToHex (tsna-helpers.node_text $1)) - :name "Convert to Hex"}) - -(local int-to-dec-action {1 #(mztenv.tsn_actions.intToDec (tsna-helpers.node_text $1)) - :name "Convert to Decimal"}) - -(tsna.setup {:zig {:FnCallArguments (tsna-actions.toggle_multiline zig-padding) - :InitList (tsna-actions.toggle_multiline zig-padding) - :VarDecl [{1 #(mztenv.tsn_actions.zigToggleMutability (tsna-helpers.node_text $1)) - :name "Toggle Mutability"}] - :INTEGER [int-toggle-action]} - :markdown {:atx_h1_marker (md-marker-fn 2) - :atx_h2_marker (md-marker-fn 3) - :atx_h3_marker (md-marker-fn 4) - :atx_h4_marker (md-marker-fn 5) - :atx_h5_marker (md-marker-fn 6) - :atx_h6_marker (md-marker-fn 1)} - :java {:hex_integer_literal [int-to-dec-action] - :decimal_integer_literal [int-to-hex-action]} - :c {:number_literal [int-toggle-action]} - :cpp {:number_literal [int-toggle-action]} - :lua {:number [int-toggle-action]} - :fennel {:number [int-toggle-action]}}) - -(nullls.register {:name :TSNA - :method [(. nullls :methods :CODE_ACTION)] - :filetypes [:_all] - :generator {:fn (. tsna :available_actions)}}) - -(vim.keymap.set :n :U (. tsna :node_action) - (. (require :mzte_nv) :utils :map_opt)) diff --git a/mzte-nv/haxe/DynTable.hx b/mzte-nv/haxe/DynTable.hx deleted file mode 100644 index aa5b78f..0000000 --- a/mzte-nv/haxe/DynTable.hx +++ /dev/null @@ -1,15 +0,0 @@ -package; - -import lua.Table; - -abstract DynTable(AnyTable) from AnyTable to AnyTable { - public inline function new() { - this = Table.create(); - } - - @:arrayAccess - public inline function get(k:Dynamic):Dynamic return this[untyped k]; - - @:arrayAccess - public inline function set(k:Dynamic, v:Dynamic) this[untyped k] = v; -} diff --git a/mzte-nv/haxe/StaticTools.hx b/mzte-nv/haxe/StaticTools.hx new file mode 100644 index 0000000..ccf0ce8 --- /dev/null +++ b/mzte-nv/haxe/StaticTools.hx @@ -0,0 +1,17 @@ +package; + +import lua.Table; + +class StaticTools { + public static inline function toTable(map:Map):Table { + return (untyped map : Dynamic).h; + } + + /** + Removes Haxe metadata from tables for picky lua functions. + See: https://github.com/HaxeFoundation/haxe/issues/11805 + **/ + public static inline function removeMeta(x:Dynamic) { + cast(x, AnyTable).__fields__ = null; + } +} diff --git a/mzte-nv/haxe/ext/mzte_nv/CPBuf.hx b/mzte-nv/haxe/ext/mzte_nv/CPBuf.hx index 70804e5..5177dce 100644 --- a/mzte-nv/haxe/ext/mzte_nv/CPBuf.hx +++ b/mzte-nv/haxe/ext/mzte_nv/CPBuf.hx @@ -1,5 +1,6 @@ package ext.mzte_nv; extern class CPBuf { + @:luaDotMethod function copyBuf():Void; } diff --git a/mzte-nv/haxe/ext/mzte_nv/MZTENv.hx b/mzte-nv/haxe/ext/mzte_nv/MZTENv.hx index 38e1840..3cedd0d 100644 --- a/mzte-nv/haxe/ext/mzte_nv/MZTENv.hx +++ b/mzte-nv/haxe/ext/mzte_nv/MZTENv.hx @@ -8,6 +8,8 @@ extern class MZTENv { public static var cpbuf:CPBuf; public static var compile:Compile; + public static var tsn_actions:TSNActions; + public static var utils:Utils; @:luaDotMethod public static function onInit():Void; diff --git a/mzte-nv/haxe/ext/mzte_nv/TSNActions.hx b/mzte-nv/haxe/ext/mzte_nv/TSNActions.hx new file mode 100644 index 0000000..60ebe14 --- /dev/null +++ b/mzte-nv/haxe/ext/mzte_nv/TSNActions.hx @@ -0,0 +1,15 @@ +package ext.mzte_nv; + +extern class TSNActions { + @:luaDotMethod + function zigToggleMutability(s:String):String; + + @:luaDotMethod + function intToggle(s:String):String; + + @:luaDotMethod + function intToHex(s:String):String; + + @:luaDotMethod + function intToDec(s:String):String; +} diff --git a/mzte-nv/haxe/ext/mzte_nv/Utils.hx b/mzte-nv/haxe/ext/mzte_nv/Utils.hx new file mode 100644 index 0000000..3017879 --- /dev/null +++ b/mzte-nv/haxe/ext/mzte_nv/Utils.hx @@ -0,0 +1,5 @@ +package ext.mzte_nv; + +extern class Utils { + var map_opt:Dynamic; +} diff --git a/mzte-nv/haxe/ext/vim/Api.hx b/mzte-nv/haxe/ext/vim/Api.hx index 2e55474..f6cb12b 100644 --- a/mzte-nv/haxe/ext/vim/Api.hx +++ b/mzte-nv/haxe/ext/vim/Api.hx @@ -1,6 +1,6 @@ package ext.vim; -import lua.Table.AnyTable; +using StaticTools; typedef CreateUserCommandOptions = { // incomplete @@ -12,17 +12,9 @@ private extern class ApiExt { 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); + opts.removeMeta(); this.nvim_create_user_command(name, command, opts); } } diff --git a/mzte-nv/haxe/ext/vim/Keymap.hx b/mzte-nv/haxe/ext/vim/Keymap.hx new file mode 100644 index 0000000..eacb007 --- /dev/null +++ b/mzte-nv/haxe/ext/vim/Keymap.hx @@ -0,0 +1,18 @@ +package ext.vim; + +import haxe.Constraints.Function; +import haxe.extern.EitherType; + +using StaticTools; + +private extern class KeymapExt { + @:luaDotMethod + function set(mode:String, bind:String, handler:EitherType, options:Dynamic):Void; +} + +abstract Keymap(KeymapExt) { + public inline function set(mode:String, bind:String, handler:EitherType, options:Dynamic):Void { + options.removeMeta(); + this.set(mode, bind, handler, options); + } +} diff --git a/mzte-nv/haxe/ext/vim/Vim.hx b/mzte-nv/haxe/ext/vim/Vim.hx index 1cd4dad..ca7594d 100644 --- a/mzte-nv/haxe/ext/vim/Vim.hx +++ b/mzte-nv/haxe/ext/vim/Vim.hx @@ -15,6 +15,7 @@ enum abstract LogLevel(Int) { extern class Vim { public static var loop:Loop; public static var api:Api; + public static var keymap:Keymap; public static var env:Table; public static var opt:Table; diff --git a/mzte-nv/haxe/init/Plugins.hx b/mzte-nv/haxe/init/Plugins.hx index ec38882..1f5b165 100644 --- a/mzte-nv/haxe/init/Plugins.hx +++ b/mzte-nv/haxe/init/Plugins.hx @@ -1,20 +1,14 @@ package init; -#if !macro +import plugins.*; +import haxe.Exception; 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; var deferredPlugins:Array; @@ -22,9 +16,13 @@ class Plugins { var errors:Array<{plug:IPlugin, err:Exception}> = []; public function new() { - regPlugins(["Nu", "Autopairs", "Catppuccin"]); - - this.deferredPlugins = this.deferredPlugins.concat([ + this.startupPlugins = []; + this.deferredPlugins = [ + new PAutopairs(), + new PCatppuccin(), + new PNu(), + new PTSNActions(), + ].concat([ "cmp", "dap", "devicons", @@ -44,7 +42,6 @@ class Plugins { "telescope", "treesitter", "ts-context", - "tsn-actions", "tterm", "ufo", ].map(n -> (new LuaPlugin(n) : IPlugin))); @@ -94,51 +91,3 @@ class Plugins { } } } -#end - -private macro function regPlugins(names:Array):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}; - }; -} diff --git a/mzte-nv/haxe/plugins/PAutopairs.hx b/mzte-nv/haxe/plugins/PAutopairs.hx index 43dd53d..ff768e4 100644 --- a/mzte-nv/haxe/plugins/PAutopairs.hx +++ b/mzte-nv/haxe/plugins/PAutopairs.hx @@ -2,7 +2,6 @@ package plugins; import lua.Lua; -@plugin(50, false) class PAutopairs implements IPlugin { public var name:String = "Autopairs"; diff --git a/mzte-nv/haxe/plugins/PCatppuccin.hx b/mzte-nv/haxe/plugins/PCatppuccin.hx index be1b9fc..9633825 100644 --- a/mzte-nv/haxe/plugins/PCatppuccin.hx +++ b/mzte-nv/haxe/plugins/PCatppuccin.hx @@ -4,7 +4,6 @@ import lua.Table; import ext.vim.Vim; import lua.Lua; -@plugin(20, false) class PCatppuccin implements IPlugin { public var name:String = "Catppuccin"; diff --git a/mzte-nv/haxe/plugins/PNu.hx b/mzte-nv/haxe/plugins/PNu.hx index 77bde5d..652a782 100644 --- a/mzte-nv/haxe/plugins/PNu.hx +++ b/mzte-nv/haxe/plugins/PNu.hx @@ -2,7 +2,6 @@ package plugins; import lua.Lua; -@plugin(50, false) class PNu implements IPlugin { public var name:String = "Nushell"; public function new() {} diff --git a/mzte-nv/haxe/plugins/PTSNActions.hx b/mzte-nv/haxe/plugins/PTSNActions.hx new file mode 100644 index 0000000..3a66c63 --- /dev/null +++ b/mzte-nv/haxe/plugins/PTSNActions.hx @@ -0,0 +1,122 @@ +package plugins; + +import ext.vim.Vim; +import ext.mzte_nv.MZTENv; +import lua.Lua; +import lua.Table; + +using StaticTools; +using StringTools; +using lua.NativeStringTools; + +class PTSNActions implements IPlugin { + public var name:String = "Tree-Sitter Node Actions"; + public var zig_padding:Table = [ + ")" => " %s", + "]" => " %s", + "}" => " %s", + "{" => "%s ", + "," => "%s ", + "=" => " %s ", + "+" => " %s ", + "-" => " %s ", + "*" => " %s ", + "/" => " %s ", + "**" => " %s ", + "++" => " %s ", + ].toTable(); + + private var tsna_helpers:Dynamic; + + public function new() {} + + public function init() { + final tsna = Lua.require("ts-node-action"); + final tsna_actions = Lua.require("ts-node-action.actions"); + this.tsna_helpers = Lua.require("ts-node-action.helpers"); + final nullls = Lua.require("null-ls"); + + final zig_multiline_action = tsna_actions[untyped "toggle_multiline"](this.zig_padding); + final toggle_int_action = Table.create([ + Table.create( + [(node) -> MZTENv.tsn_actions.intToggle(this.tsna_helpers[untyped "node_text"](node))], + {name: "Toggle Dec/Hex"} + ) + ]); + + tsna[untyped "setup"]({ + zig: { + call_expression: zig_multiline_action, + struct_declaration: zig_multiline_action, + initializer_list: zig_multiline_action, + variable_declaration: Table.create([ + Table.create( + [(node) -> MZTENv.tsn_actions.zigToggleMutability(this.tsna_helpers[untyped "node_text"](node))], + { + name: "Toggle Mutability", + } + ) + ]), + integer: toggle_int_action, + }, + markdown: [for (i in 1...7) 'atx_h${i}_marker' => { + final next = i == 6 ? 1 : i + 1; + Table.create([_ -> NativeStringTools.rep("#", next)], { + name: 'Convert to H$next' + }); + }].toTable(), + java: { + hex_integer_literal: Table.create( + [ + Table.create( + [(node) -> MZTENv.tsn_actions.intToDec(this.tsna_helpers[untyped "node_text"](node))], + { + name: "Convert to Decimal", + } + ) + ] + ), + decimal_integer_literal: Table.create([Table.create([(node) -> + MZTENv.tsn_actions.intToHex(this.tsna_helpers[untyped "node_text"](node))], { + name: "Convert to Hexadecimal", + })]), + }, + c: { + number_literal: toggle_int_action, + }, + cpp: { + number_literal: toggle_int_action, + }, + lua: { + number: toggle_int_action, + }, + fennel: { + number: toggle_int_action, + }, + typst: { + math: Table.create([Table.create([toggleTypstMath], {name: "Expand/Contract math"})]) + }, + }); + + nullls[untyped "register"]({ + name: "TSNA", + method: Table.create([(cast nullls : Dynamic).methods.CODE_ACTION]), + filetypes: Table.create(["_all"]), + generator: {fn: tsna[untyped "available_actions"]}, + }); + + Vim.keymap.set("n", "U", tsna[untyped "node_action"], MZTENv.utils.map_opt); + } + + private function toggleTypstMath(node:Dynamic):String { + final text:String = this.tsna_helpers[untyped "node_text"](node); + + final sub1 = text.gsub("^%$%s+(.*)%s+%$$", "%$%1%$"); + if (sub1 == text) { + // This assignment is necessary because haxe doesn't realize that gsub is a multireturn. + // TODO: open issue about this + final sub2 = text.gsub("^%$", "%$ ").gsub("%$$", " %$"); + return sub2; + } else return sub1; + } +}