diff --git a/src/file_type.zig b/src/file_type.zig index bba91e4..c590ddc 100644 --- a/src/file_type.zig +++ b/src/file_type.zig @@ -11,6 +11,7 @@ highlights: [:0]const u8, injections: ?[:0]const u8, first_line_matches: ?FirstLineMatch = null, comment: []const u8, +formatter: ?[]const []const u8, pub fn get_by_name(name: []const u8) ?*const FileType { for (file_types) |*file_type| @@ -79,33 +80,24 @@ const FirstLineMatch = struct { content: ?[]const u8 = null, }; -const FileTypeOptions = struct { - extensions: []const []const u8 = &[_][]const u8{}, - comment: []const u8, - icon: ?[]const u8 = null, - color: ?u24 = null, - highlights: ?[:0]const u8 = null, - injections: ?[:0]const u8 = null, - first_line_matches: ?FirstLineMatch = null, - parser: ?LangFn = null, -}; - -fn DeclLang(comptime lang: []const u8, comptime args: FileTypeOptions) FileType { - return .{ - .color = args.color orelse 0xffffff, - .icon = args.icon orelse "󱀫", - .name = lang, - .lang_fn = if (args.parser) |p| p else get_parser(lang), - .extensions = args.extensions, - .comment = args.comment, - .highlights = if (args.highlights) |h| h else @embedFile("tree-sitter-" ++ lang ++ "/queries/highlights.scm"), - .injections = args.injections, - .first_line_matches = args.first_line_matches, - }; +fn FormatterCmd(comptime args: anytype) []const []const u8 { + const cmd: []const []const u8 = &[_][]const u8{}; + inline for (args) |arg| { + cmd = cmd ++ arg; + } + return cmd; } pub const file_types = load_file_types(@import("file_types.zig")); +fn vec(comptime args: anytype) []const []const u8 { + var cmd: []const []const u8 = &[_][]const u8{}; + inline for (args) |arg| { + cmd = cmd ++ [_][]const u8{arg}; + } + return cmd; +} + fn load_file_types(comptime Namespace: type) []FileType { comptime switch (@typeInfo(Namespace)) { .Struct => |info| { @@ -114,13 +106,26 @@ fn load_file_types(comptime Namespace: type) []FileType { // @compileLog(decl.name, @TypeOf(@field(Namespace, decl.name))); count += 1; } - var cmds: [count]FileType = undefined; + var types: [count]FileType = undefined; var i = 0; for (info.decls) |decl| { - cmds[i] = DeclLang(decl.name, @field(Namespace, decl.name)); + const lang = decl.name; + const args = @field(Namespace, lang); + types[i] = .{ + .color = if (@hasField(@TypeOf(args), "color")) args.color else 0xffffff, + .icon = if (@hasField(@TypeOf(args), "icon")) args.icon else "󱀫", + .name = lang, + .lang_fn = if (@hasField(@TypeOf(args), "parser")) args.parser else get_parser(lang), + .extensions = vec(args.extensions), + .comment = args.comment, + .highlights = if (@hasField(@TypeOf(args), "highlights")) @embedFile(args.highlights) else @embedFile("tree-sitter-" ++ lang ++ "/queries/highlights.scm"), + .injections = if (@hasField(@TypeOf(args), "injections")) @embedFile(args.injections) else null, + .first_line_matches = if (@hasField(@TypeOf(args), "first_line_matches")) args.first_line_matches else null, + .formatter = if (@hasField(@TypeOf(args), "formatter")) vec(args.formatter) else null, + }; i += 1; } - return &cmds; + return &types; }, else => @compileError("expected tuple or struct type"), }; diff --git a/src/file_types.zig b/src/file_types.zig index b718b8a..2b85783 100644 --- a/src/file_types.zig +++ b/src/file_types.zig @@ -1,33 +1,34 @@ pub const agda = .{ - .extensions = &[_][]const u8{"agda"}, + .extensions = .{"agda"}, .comment = "--", }; pub const bash = .{ .color = 0x3e474a, .icon = "󱆃", - .extensions = &[_][]const u8{ "sh", "bash" }, + .extensions = .{ "sh", "bash", ".profile" }, .comment = "#", .first_line_matches = .{ .prefix = "#!", .content = "sh" }, }; pub const c = .{ .icon = "󰙱", - .extensions = &[_][]const u8{ "c", "h" }, + .extensions = .{ "c", "h" }, .comment = "//", + .formatter = .{"clang-format"}, }; pub const @"c-sharp" = .{ .color = 0x68217a, .icon = "󰌛", - .extensions = &[_][]const u8{"cs"}, + .extensions = .{"cs"}, .comment = "//", }; pub const conf = .{ .color = 0x000000, .icon = "", - .extensions = &[_][]const u8{ "conf", "config", ".gitconfig" }, + .extensions = .{ "conf", "config", ".gitconfig" }, .highlights = fish.highlights, .comment = "#", .parser = fish.parser, @@ -36,259 +37,261 @@ pub const conf = .{ pub const cpp = .{ .color = 0x9c033a, .icon = "", - .extensions = &[_][]const u8{ "cc", "cpp", "cxx", "hpp", "hxx", "h", "ipp", "ixx" }, + .extensions = .{ "cc", "cpp", "cxx", "hpp", "hxx", "h", "ipp", "ixx" }, .comment = "//", - .injections = @embedFile("tree-sitter-cpp/queries/injections.scm"), + .injections = "tree-sitter-cpp/queries/injections.scm", + .formatter = .{"clang-format"}, }; pub const css = .{ .color = 0x3d8fc6, .icon = "󰌜", - .extensions = &[_][]const u8{"css"}, + .extensions = .{"css"}, .comment = "//", }; pub const diff = .{ - .extensions = &[_][]const u8{ "diff", "patch" }, + .extensions = .{ "diff", "patch" }, .comment = "#", }; pub const dockerfile = .{ .color = 0x019bc6, .icon = "", - .extensions = &[_][]const u8{ "Dockerfile", "dockerfile", "docker", "Containerfile", "container" }, + .extensions = .{ "Dockerfile", "dockerfile", "docker", "Containerfile", "container" }, .comment = "#", }; pub const dtd = .{ .icon = "󰗀", - .extensions = &[_][]const u8{"dtd"}, + .extensions = .{"dtd"}, .comment = "