add TSNA to toggle dec/hex ints

This commit is contained in:
LordMZTE 2023-05-19 17:38:42 +02:00
parent e1a734d9b3
commit d1f3b46fbc
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
3 changed files with 92 additions and 3 deletions

View file

@ -19,16 +19,32 @@
(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"}]}
: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)}})
: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)]

View file

@ -1,5 +1,7 @@
(vim.cmd "packadd packer.nvim")
(local packer (require :packer))
(macro pconf [plugin]
`#(require ,(.. :pluginconf.p- plugin)))
@ -72,7 +74,12 @@
(use {1 :Olical/conjure :setup (pconf :conjure)})
(cmp-plugins use))
((. (require :packer) :startup) init)
(packer.startup init)
;; PackerCompile automagically
(when (= 0 (length (vim.api.nvim_get_runtime_file :plugin/packer_compiled.lua
false)))
(packer.compile))
;; actually compile packer-generated config after packer's "compile" step
(fn compile-packer-generated []

View file

@ -7,6 +7,9 @@ const c = ffi.c;
pub fn luaPush(l: *c.lua_State) void {
ser.luaPushAny(l, .{
.zigToggleMutability = ffi.luaFunc(lZigToggleMutability),
.intToHex = ffi.luaFunc(lIntToHex),
.intToDec = ffi.luaFunc(lIntToDec),
.intToggle = ffi.luaFunc(lIntToggle),
});
}
@ -34,3 +37,66 @@ fn lZigToggleMutability(l: *c.lua_State) !c_int {
return 1;
}
/// Given a hex number literal, determine the index where the actual number starts.
/// Returns 0 if this isn't a hex number.
fn hexStartIdx(s: []const u8) u2 {
if (s.len < 3)
return 0;
const offset: u2 = if (s[0] == '-') 1 else 0;
if (s.len - offset < 3 or !std.mem.eql(u8, "0x", s[offset..][0..2]))
return 0;
return 2 + offset;
}
fn lIntToHex(l: *c.lua_State) !c_int {
const inp = ffi.luaCheckstring(l, 1);
const parsed = try std.fmt.parseInt(i64, inp, 10);
var buf: [128]u8 = undefined;
ffi.luaPushString(
l,
if (parsed < 0)
try std.fmt.bufPrint(&buf, "-0x{x}", .{-parsed})
else
try std.fmt.bufPrint(&buf, "0x{x}", .{parsed}),
);
return 1;
}
fn lIntToDec(l: *c.lua_State) !c_int {
const inp = ffi.luaCheckstring(l, 1);
const start_idx = hexStartIdx(inp);
if (start_idx == 0)
return error.InvalidHex;
const parsed = try std.fmt.parseInt(
i64,
inp[start_idx..],
16,
) * @as(i2, if (start_idx == 3) -1 else 1);
var buf: [128]u8 = undefined;
ffi.luaPushString(l, try std.fmt.bufPrint(&buf, "{d}", .{parsed}));
return 1;
}
fn lIntToggle(l: *c.lua_State) !c_int {
const inp = ffi.luaCheckstring(l, 1);
for (inp) |char| {
// return input for floats
if (char == '.' or char == 'f' or char == 'f') {
c.lua_pushvalue(l, 1);
return 1;
}
}
if (hexStartIdx(inp) != 0) {
return try lIntToDec(l);
} else {
return try lIntToHex(l);
}
}