refactor: simplify file type declartions and add formatters

This commit is contained in:
CJ van den Berg 2024-03-11 17:32:41 +01:00
parent e58df221cc
commit 2de8ec65a2
2 changed files with 97 additions and 89 deletions

View file

@ -11,6 +11,7 @@ highlights: [:0]const u8,
injections: ?[:0]const u8, injections: ?[:0]const u8,
first_line_matches: ?FirstLineMatch = null, first_line_matches: ?FirstLineMatch = null,
comment: []const u8, comment: []const u8,
formatter: ?[]const []const u8,
pub fn get_by_name(name: []const u8) ?*const FileType { pub fn get_by_name(name: []const u8) ?*const FileType {
for (file_types) |*file_type| for (file_types) |*file_type|
@ -79,33 +80,24 @@ const FirstLineMatch = struct {
content: ?[]const u8 = null, content: ?[]const u8 = null,
}; };
const FileTypeOptions = struct { fn FormatterCmd(comptime args: anytype) []const []const u8 {
extensions: []const []const u8 = &[_][]const u8{}, const cmd: []const []const u8 = &[_][]const u8{};
comment: []const u8, inline for (args) |arg| {
icon: ?[]const u8 = null, cmd = cmd ++ arg;
color: ?u24 = null, }
highlights: ?[:0]const u8 = null, return cmd;
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,
};
} }
pub const file_types = load_file_types(@import("file_types.zig")); 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 { fn load_file_types(comptime Namespace: type) []FileType {
comptime switch (@typeInfo(Namespace)) { comptime switch (@typeInfo(Namespace)) {
.Struct => |info| { .Struct => |info| {
@ -114,13 +106,26 @@ fn load_file_types(comptime Namespace: type) []FileType {
// @compileLog(decl.name, @TypeOf(@field(Namespace, decl.name))); // @compileLog(decl.name, @TypeOf(@field(Namespace, decl.name)));
count += 1; count += 1;
} }
var cmds: [count]FileType = undefined; var types: [count]FileType = undefined;
var i = 0; var i = 0;
for (info.decls) |decl| { 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; i += 1;
} }
return &cmds; return &types;
}, },
else => @compileError("expected tuple or struct type"), else => @compileError("expected tuple or struct type"),
}; };

View file

@ -1,33 +1,34 @@
pub const agda = .{ pub const agda = .{
.extensions = &[_][]const u8{"agda"}, .extensions = .{"agda"},
.comment = "--", .comment = "--",
}; };
pub const bash = .{ pub const bash = .{
.color = 0x3e474a, .color = 0x3e474a,
.icon = "󱆃", .icon = "󱆃",
.extensions = &[_][]const u8{ "sh", "bash" }, .extensions = .{ "sh", "bash", ".profile" },
.comment = "#", .comment = "#",
.first_line_matches = .{ .prefix = "#!", .content = "sh" }, .first_line_matches = .{ .prefix = "#!", .content = "sh" },
}; };
pub const c = .{ pub const c = .{
.icon = "󰙱", .icon = "󰙱",
.extensions = &[_][]const u8{ "c", "h" }, .extensions = .{ "c", "h" },
.comment = "//", .comment = "//",
.formatter = .{"clang-format"},
}; };
pub const @"c-sharp" = .{ pub const @"c-sharp" = .{
.color = 0x68217a, .color = 0x68217a,
.icon = "󰌛", .icon = "󰌛",
.extensions = &[_][]const u8{"cs"}, .extensions = .{"cs"},
.comment = "//", .comment = "//",
}; };
pub const conf = .{ pub const conf = .{
.color = 0x000000, .color = 0x000000,
.icon = "", .icon = "",
.extensions = &[_][]const u8{ "conf", "config", ".gitconfig" }, .extensions = .{ "conf", "config", ".gitconfig" },
.highlights = fish.highlights, .highlights = fish.highlights,
.comment = "#", .comment = "#",
.parser = fish.parser, .parser = fish.parser,
@ -36,259 +37,261 @@ pub const conf = .{
pub const cpp = .{ pub const cpp = .{
.color = 0x9c033a, .color = 0x9c033a,
.icon = "", .icon = "",
.extensions = &[_][]const u8{ "cc", "cpp", "cxx", "hpp", "hxx", "h", "ipp", "ixx" }, .extensions = .{ "cc", "cpp", "cxx", "hpp", "hxx", "h", "ipp", "ixx" },
.comment = "//", .comment = "//",
.injections = @embedFile("tree-sitter-cpp/queries/injections.scm"), .injections = "tree-sitter-cpp/queries/injections.scm",
.formatter = .{"clang-format"},
}; };
pub const css = .{ pub const css = .{
.color = 0x3d8fc6, .color = 0x3d8fc6,
.icon = "󰌜", .icon = "󰌜",
.extensions = &[_][]const u8{"css"}, .extensions = .{"css"},
.comment = "//", .comment = "//",
}; };
pub const diff = .{ pub const diff = .{
.extensions = &[_][]const u8{ "diff", "patch" }, .extensions = .{ "diff", "patch" },
.comment = "#", .comment = "#",
}; };
pub const dockerfile = .{ pub const dockerfile = .{
.color = 0x019bc6, .color = 0x019bc6,
.icon = "", .icon = "",
.extensions = &[_][]const u8{ "Dockerfile", "dockerfile", "docker", "Containerfile", "container" }, .extensions = .{ "Dockerfile", "dockerfile", "docker", "Containerfile", "container" },
.comment = "#", .comment = "#",
}; };
pub const dtd = .{ pub const dtd = .{
.icon = "󰗀", .icon = "󰗀",
.extensions = &[_][]const u8{"dtd"}, .extensions = .{"dtd"},
.comment = "<!--", .comment = "<!--",
.highlights = @embedFile("tree-sitter-xml/dtd/queries/highlights.scm"), .highlights = "tree-sitter-xml/dtd/queries/highlights.scm",
}; };
pub const fish = .{ pub const fish = .{
.extensions = &[_][]const u8{"fish"}, .extensions = .{"fish"},
.comment = "#", .comment = "#",
.parser = @import("file_type.zig").Parser("fish"), .parser = @import("file_type.zig").Parser("fish"),
.highlights = @embedFile("tree-sitter-fish/queries/highlights.scm"), .highlights = "tree-sitter-fish/queries/highlights.scm",
}; };
pub const @"git-rebase" = .{ pub const @"git-rebase" = .{
.color = 0xf34f29, .color = 0xf34f29,
.icon = "", .icon = "",
.extensions = &[_][]const u8{"git-rebase-todo"}, .extensions = .{"git-rebase-todo"},
.comment = "#", .comment = "#",
}; };
pub const gitcommit = .{ pub const gitcommit = .{
.color = 0xf34f29, .color = 0xf34f29,
.icon = "", .icon = "",
.extensions = &[_][]const u8{"COMMIT_EDITMSG"}, .extensions = .{"COMMIT_EDITMSG"},
.comment = "#", .comment = "#",
.injections = @embedFile("tree-sitter-gitcommit/queries/injections.scm"), .injections = "tree-sitter-gitcommit/queries/injections.scm",
}; };
pub const go = .{ pub const go = .{
.color = 0x00acd7, .color = 0x00acd7,
.icon = "󰟓", .icon = "󰟓",
.extensions = &[_][]const u8{"go"}, .extensions = .{"go"},
.comment = "//", .comment = "//",
}; };
pub const haskell = .{ pub const haskell = .{
.color = 0x5E5185, .color = 0x5E5185,
.icon = "󰲒", .icon = "󰲒",
.extensions = &[_][]const u8{"hs"}, .extensions = .{"hs"},
.comment = "--", .comment = "--",
}; };
pub const html = .{ pub const html = .{
.color = 0xe54d26, .color = 0xe54d26,
.icon = "󰌝", .icon = "󰌝",
.extensions = &[_][]const u8{"html"}, .extensions = .{"html"},
.comment = "<!--", .comment = "<!--",
.injections = @embedFile("tree-sitter-html/queries/injections.scm"), .injections = "tree-sitter-html/queries/injections.scm",
}; };
pub const java = .{ pub const java = .{
.color = 0xEA2D2E, .color = 0xEA2D2E,
.icon = "", .icon = "",
.extensions = &[_][]const u8{"java"}, .extensions = .{"java"},
.comment = "//", .comment = "//",
}; };
pub const javascript = .{ pub const javascript = .{
.color = 0xf0db4f, .color = 0xf0db4f,
.icon = "󰌞", .icon = "󰌞",
.extensions = &[_][]const u8{"js"}, .extensions = .{"js"},
.comment = "//", .comment = "//",
.injections = @embedFile("tree-sitter-javascript/queries/injections.scm"), .injections = "tree-sitter-javascript/queries/injections.scm",
}; };
pub const json = .{ pub const json = .{
.extensions = &[_][]const u8{"json"}, .extensions = .{"json"},
.comment = "//", .comment = "//",
}; };
pub const lua = .{ pub const lua = .{
.color = 0x000080, .color = 0x02027d,
.icon = "󰢱", .icon = "󰢱",
.extensions = &[_][]const u8{"lua"}, .extensions = .{"lua"},
.comment = "--", .comment = "--",
.injections = @embedFile("tree-sitter-lua/queries/injections.scm"), .injections = "tree-sitter-lua/queries/injections.scm",
.first_line_matches = .{ .prefix = "--", .content = "lua" }, .first_line_matches = .{ .prefix = "--", .content = "lua" },
}; };
pub const make = .{ pub const make = .{
.extensions = &[_][]const u8{ "makefile", "Makefile", "MAKEFILE", "GNUmakefile", "mk", "mak", "dsp" }, .extensions = .{ "makefile", "Makefile", "MAKEFILE", "GNUmakefile", "mk", "mak", "dsp" },
.comment = "#", .comment = "#",
}; };
pub const markdown = .{ pub const markdown = .{
.color = 0x000000, .color = 0x000000,
.icon = "󰍔", .icon = "󰍔",
.extensions = &[_][]const u8{"md"}, .extensions = .{"md"},
.comment = "<!--", .comment = "<!--",
.highlights = @embedFile("tree-sitter-markdown/tree-sitter-markdown/queries/highlights.scm"), .highlights = "tree-sitter-markdown/tree-sitter-markdown/queries/highlights.scm",
.injections = @embedFile("tree-sitter-markdown/tree-sitter-markdown/queries/injections.scm"), .injections = "tree-sitter-markdown/tree-sitter-markdown/queries/injections.scm",
}; };
pub const @"markdown-inline" = .{ pub const @"markdown-inline" = .{
.color = 0x000000, .color = 0x000000,
.icon = "󰍔", .icon = "󰍔",
.extensions = &[_][]const u8{}, .extensions = .{},
.comment = "<!--", .comment = "<!--",
.highlights = @embedFile("tree-sitter-markdown/tree-sitter-markdown-inline/queries/highlights.scm"), .highlights = "tree-sitter-markdown/tree-sitter-markdown-inline/queries/highlights.scm",
.injections = @embedFile("tree-sitter-markdown/tree-sitter-markdown-inline/queries/injections.scm"), .injections = "tree-sitter-markdown/tree-sitter-markdown-inline/queries/injections.scm",
}; };
pub const nasm = .{ pub const nasm = .{
.extensions = &[_][]const u8{ "asm", "nasm" }, .extensions = .{ "asm", "nasm" },
.comment = "#", .comment = "#",
.injections = @embedFile("tree-sitter-nasm/queries/injections.scm"), .injections = "tree-sitter-nasm/queries/injections.scm",
}; };
pub const ninja = .{ pub const ninja = .{
.extensions = &[_][]const u8{"ninja"}, .extensions = .{"ninja"},
.comment = "#", .comment = "#",
}; };
pub const nix = .{ pub const nix = .{
.color = 0x5277C3, .color = 0x5277C3,
.icon = "󱄅", .icon = "󱄅",
.extensions = &[_][]const u8{"nix"}, .extensions = .{"nix"},
.comment = "#", .comment = "#",
.injections = @embedFile("tree-sitter-nix/queries/injections.scm"), .injections = "tree-sitter-nix/queries/injections.scm",
}; };
pub const ocaml = .{ pub const ocaml = .{
.color = 0xF18803, .color = 0xF18803,
.icon = "", .icon = "",
.extensions = &[_][]const u8{ "ml", "mli" }, .extensions = .{ "ml", "mli" },
.comment = "(*", .comment = "(*",
}; };
pub const openscad = .{ pub const openscad = .{
.color = 0x000000, .color = 0x000000,
.icon = "󰻫", .icon = "󰻫",
.extensions = &[_][]const u8{"scad"}, .extensions = .{"scad"},
.comment = "//", .comment = "//",
.injections = @embedFile("tree-sitter-openscad/queries/injections.scm"), .injections = "tree-sitter-openscad/queries/injections.scm",
}; };
pub const org = .{ pub const org = .{
.icon = "", .icon = "",
.extensions = &[_][]const u8{"org"}, .extensions = .{"org"},
.comment = "#", .comment = "#",
}; };
pub const php = .{ pub const php = .{
.color = 0x6181b6, .color = 0x6181b6,
.icon = "󰌟", .icon = "󰌟",
.extensions = &[_][]const u8{"php"}, .extensions = .{"php"},
.comment = "//", .comment = "//",
.injections = @embedFile("tree-sitter-php/queries/injections.scm"), .injections = "tree-sitter-php/queries/injections.scm",
}; };
pub const purescript = .{ pub const purescript = .{
.color = 0x14161a, .color = 0x14161a,
.icon = "", .icon = "",
.extensions = &[_][]const u8{"purs"}, .extensions = .{"purs"},
.comment = "--", .comment = "--",
.injections = @embedFile("tree-sitter-purescript/queries/injections.scm"), .injections = "tree-sitter-purescript/queries/injections.scm",
}; };
pub const python = .{ pub const python = .{
.color = 0xffd845, .color = 0xffd845,
.icon = "󰌠", .icon = "󰌠",
.extensions = &[_][]const u8{"py"}, .extensions = .{"py"},
.comment = "#", .comment = "#",
.first_line_matches = .{ .prefix = "#!", .content = "/bin/bash" }, .first_line_matches = .{ .prefix = "#!", .content = "/bin/bash" },
}; };
pub const regex = .{ pub const regex = .{
.extensions = &[_][]const u8{}, .extensions = .{},
.comment = "#", .comment = "#",
}; };
pub const ruby = .{ pub const ruby = .{
.color = 0xd91404, .color = 0xd91404,
.icon = "󰴭", .icon = "󰴭",
.extensions = &[_][]const u8{"rb"}, .extensions = .{"rb"},
.comment = "#", .comment = "#",
}; };
pub const rust = .{ pub const rust = .{
.color = 0x000000, .color = 0x000000,
.icon = "󱘗", .icon = "󱘗",
.extensions = &[_][]const u8{"rs"}, .extensions = .{"rs"},
.comment = "//", .comment = "//",
.injections = @embedFile("tree-sitter-rust/queries/injections.scm"), .injections = "tree-sitter-rust/queries/injections.scm",
}; };
pub const scheme = .{ pub const scheme = .{
.extensions = &[_][]const u8{ "scm", "ss", "el" }, .extensions = .{ "scm", "ss", "el" },
.comment = ";", .comment = ";",
}; };
pub const @"ssh-config" = .{ pub const @"ssh-config" = .{
.extensions = &[_][]const u8{".ssh/config"}, .extensions = .{".ssh/config"},
.comment = "#", .comment = "#",
}; };
pub const toml = .{ pub const toml = .{
.extensions = &[_][]const u8{ "toml" }, .extensions = .{"toml"},
.comment = "#", .comment = "#",
}; };
pub const typescript = .{ pub const typescript = .{
.color = 0x007acc, .color = 0x007acc,
.icon = "󰛦", .icon = "󰛦",
.extensions = &[_][]const u8{ "ts", "tsx" }, .extensions = .{ "ts", "tsx" },
.comment = "//", .comment = "//",
}; };
pub const xml = .{ pub const xml = .{
.icon = "󰗀", .icon = "󰗀",
.extensions = &[_][]const u8{"xml"}, .extensions = .{"xml"},
.comment = "<!--", .comment = "<!--",
.highlights = @embedFile("tree-sitter-xml/xml/queries/highlights.scm"), .highlights = "tree-sitter-xml/xml/queries/highlights.scm",
.first_line_matches = .{ .prefix = "<?xml " }, .first_line_matches = .{ .prefix = "<?xml " },
}; };
pub const zig = .{ pub const zig = .{
.color = 0xf7a41d, .color = 0xf7a41d,
.icon = "", .icon = "",
.extensions = &[_][]const u8{ "zig", "zon" }, .extensions = .{ "zig", "zon" },
.comment = "//", .comment = "//",
.injections = @embedFile("tree-sitter-zig/queries/injections.scm"), .formatter = .{ "zig", "fmt", "--stdin" },
.injections = "tree-sitter-zig/queries/injections.scm",
}; };
pub const ziggy = .{ pub const ziggy = .{
.color = 0xf7a41d, .color = 0xf7a41d,
.icon = "", .icon = "",
.extensions = &[_][]const u8{ "ziggy" }, .extensions = .{"ziggy"},
.comment = "//", .comment = "//",
.highlights = @embedFile("tree-sitter-ziggy/tree-sitter-ziggy/queries/highlights.scm"), .highlights = "tree-sitter-ziggy/tree-sitter-ziggy/queries/highlights.scm",
}; };