Compare commits

...

2 commits

Author SHA1 Message Date
LordMZTE afda305c27
feat: add script for neovim 2024-04-14 19:31:47 +02:00
LordMZTE 67266797d9
chore: port to latest Zig 2024-04-14 19:31:34 +02:00
4 changed files with 40 additions and 20 deletions

View file

@ -1,18 +1,18 @@
const std = @import("std"); const std = @import("std");
pub fn build(b: *std.build.Builder) void { pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const mode = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.name = "portingtools", .name = "portingtools",
.root_source_file = .{ .path = "src/main.zig" }, .root_source_file = .{ .path = "src/main.zig" },
.target = target, .target = target,
.optimize = mode, .optimize = optimize,
}); });
exe.install(); b.installArtifact(exe);
const run_cmd = exe.run(); const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep()); run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| { if (b.args) |args| {
run_cmd.addArgs(args); run_cmd.addArgs(args);

View file

@ -5,7 +5,7 @@ str: []const u8,
const Self = @This(); const Self = @This();
pub fn read(reader: anytype, alloc: std.mem.Allocator) !Self { pub fn read(reader: anytype, alloc: std.mem.Allocator) !Self {
const strlen = try reader.readIntBig(u32); const strlen = try reader.readInt(u32, .big);
const str = try alloc.alloc(u8, strlen); const str = try alloc.alloc(u8, strlen);
errdefer alloc.free(str); errdefer alloc.free(str);
@ -16,6 +16,6 @@ pub fn read(reader: anytype, alloc: std.mem.Allocator) !Self {
} }
pub fn write(self: *const Self, writer: anytype) !void { pub fn write(self: *const Self, writer: anytype) !void {
try writer.writeIntBig(u32, @intCast(u32, self.str.len)); try writer.writeInt(u32, @intCast(self.str.len), .big);
try writer.writeAll(self.str); try writer.writeAll(self.str);
} }

View file

@ -3,8 +3,8 @@ const csv_reader = @import("csv_reader.zig");
const StringPacket = @import("StringPacket.zig"); const StringPacket = @import("StringPacket.zig");
pub const std_options = struct { pub const std_options = std.Options{
pub const log_level = .debug; .log_level = .debug,
}; };
const Mapping = struct { const Mapping = struct {
@ -14,7 +14,7 @@ const Mapping = struct {
fn getAddr(alloc: std.mem.Allocator) !std.net.Address { fn getAddr(alloc: std.mem.Allocator) !std.net.Address {
const sockpath = try std.fs.path.join(alloc, &.{ const sockpath = try std.fs.path.join(alloc, &.{
std.os.getenv("XDG_RUNTIME_DIR") orelse return error.MissingRuntimeDir, std.posix.getenv("XDG_RUNTIME_DIR") orelse return error.MissingRuntimeDir,
"portingtools.sock", "portingtools.sock",
}); });
defer alloc.free(sockpath); defer alloc.free(sockpath);
@ -51,14 +51,14 @@ fn map() !void {
const addr = try getAddr(alloc); const addr = try getAddr(alloc);
const sockfd = try std.os.socket( const sockfd = try std.posix.socket(
std.os.AF.UNIX, std.posix.AF.UNIX,
std.os.SOCK.STREAM | std.os.SOCK.CLOEXEC, std.posix.SOCK.STREAM | std.posix.SOCK.CLOEXEC,
0, 0,
); );
defer std.os.closeSocket(sockfd); defer std.posix.close(sockfd);
try std.os.connect(sockfd, &addr.any, addr.getOsSockLen()); try std.posix.connect(sockfd, &addr.any, addr.getOsSockLen());
const stream = std.net.Stream{ .handle = sockfd }; const stream = std.net.Stream{ .handle = sockfd };
@ -110,7 +110,7 @@ fn runServer() !void {
std.log.warn("couldn't open renames file: {}, skipping", .{err}); std.log.warn("couldn't open renames file: {}, skipping", .{err});
} }
var mappings_dir = try std.fs.cwd().openIterableDir("mappings", .{}); var mappings_dir = try std.fs.cwd().openDir("mappings", .{ .iterate = true });
defer mappings_dir.close(); defer mappings_dir.close();
var mappings_iter = mappings_dir.iterate(); var mappings_iter = mappings_dir.iterate();
@ -146,11 +146,9 @@ fn runServer() !void {
std.log.info("loaded {} mappings", .{mappings.count()}); std.log.info("loaded {} mappings", .{mappings.count()});
var server = std.net.StreamServer.init(.{});
defer server.deinit();
const addr = try getAddr(alloc); const addr = try getAddr(alloc);
try server.listen(addr); var server = try addr.listen(.{});
defer server.deinit();
std.log.info("listening on {}", .{addr}); std.log.info("listening on {}", .{addr});
while (true) { while (true) {

22
vim.lua Normal file
View file

@ -0,0 +1,22 @@
-- A script for vim to map the word under the cursor with portingtools by pressing m, and all
-- mapped expressions in the file with M
local function map(name)
return vim.fn.systemlist("portingtools map", name)[1]
end
vim.keymap.set("n", "m", function()
local word = vim.fn.expand "<cword>"
local mapped = map(word)
local replaced = vim.fn.getline("."):gsub(word, mapped);
vim.fn.setline(".", replaced)
end)
vim.keymap.set("n", "M", function()
local nlines = 99
for i = 1, nlines do
print(i .. "/" .. nlines)
local line = vim.fn.getline(i)
local mapped = line:gsub("field_%d+_[%a_]+", map):gsub("func_%d+_[%a_]+", map);
vim.fn.setline(i, mapped)
end
end)