refactor: simplify file type declartions and add formatters
This commit is contained in:
parent
e58df221cc
commit
2de8ec65a2
2 changed files with 97 additions and 89 deletions
|
@ -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"),
|
||||
};
|
||||
|
|
|
@ -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 = "<!--",
|
||||
.highlights = @embedFile("tree-sitter-xml/dtd/queries/highlights.scm"),
|
||||
.highlights = "tree-sitter-xml/dtd/queries/highlights.scm",
|
||||
};
|
||||
|
||||
pub const fish = .{
|
||||
.extensions = &[_][]const u8{"fish"},
|
||||
.extensions = .{"fish"},
|
||||
.comment = "#",
|
||||
.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" = .{
|
||||
.color = 0xf34f29,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{"git-rebase-todo"},
|
||||
.extensions = .{"git-rebase-todo"},
|
||||
.comment = "#",
|
||||
};
|
||||
|
||||
pub const gitcommit = .{
|
||||
.color = 0xf34f29,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{"COMMIT_EDITMSG"},
|
||||
.extensions = .{"COMMIT_EDITMSG"},
|
||||
.comment = "#",
|
||||
.injections = @embedFile("tree-sitter-gitcommit/queries/injections.scm"),
|
||||
.injections = "tree-sitter-gitcommit/queries/injections.scm",
|
||||
};
|
||||
|
||||
pub const go = .{
|
||||
.color = 0x00acd7,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{"go"},
|
||||
.extensions = .{"go"},
|
||||
.comment = "//",
|
||||
};
|
||||
|
||||
pub const haskell = .{
|
||||
.color = 0x5E5185,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{"hs"},
|
||||
.extensions = .{"hs"},
|
||||
.comment = "--",
|
||||
};
|
||||
|
||||
pub const html = .{
|
||||
.color = 0xe54d26,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{"html"},
|
||||
.extensions = .{"html"},
|
||||
.comment = "<!--",
|
||||
.injections = @embedFile("tree-sitter-html/queries/injections.scm"),
|
||||
.injections = "tree-sitter-html/queries/injections.scm",
|
||||
};
|
||||
|
||||
pub const java = .{
|
||||
.color = 0xEA2D2E,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{"java"},
|
||||
.extensions = .{"java"},
|
||||
.comment = "//",
|
||||
};
|
||||
|
||||
pub const javascript = .{
|
||||
.color = 0xf0db4f,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{"js"},
|
||||
.extensions = .{"js"},
|
||||
.comment = "//",
|
||||
.injections = @embedFile("tree-sitter-javascript/queries/injections.scm"),
|
||||
.injections = "tree-sitter-javascript/queries/injections.scm",
|
||||
};
|
||||
|
||||
pub const json = .{
|
||||
.extensions = &[_][]const u8{"json"},
|
||||
.extensions = .{"json"},
|
||||
.comment = "//",
|
||||
};
|
||||
|
||||
pub const lua = .{
|
||||
.color = 0x000080,
|
||||
.color = 0x02027d,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{"lua"},
|
||||
.extensions = .{"lua"},
|
||||
.comment = "--",
|
||||
.injections = @embedFile("tree-sitter-lua/queries/injections.scm"),
|
||||
.injections = "tree-sitter-lua/queries/injections.scm",
|
||||
.first_line_matches = .{ .prefix = "--", .content = "lua" },
|
||||
};
|
||||
|
||||
pub const make = .{
|
||||
.extensions = &[_][]const u8{ "makefile", "Makefile", "MAKEFILE", "GNUmakefile", "mk", "mak", "dsp" },
|
||||
.extensions = .{ "makefile", "Makefile", "MAKEFILE", "GNUmakefile", "mk", "mak", "dsp" },
|
||||
.comment = "#",
|
||||
};
|
||||
|
||||
pub const markdown = .{
|
||||
.color = 0x000000,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{"md"},
|
||||
.extensions = .{"md"},
|
||||
.comment = "<!--",
|
||||
.highlights = @embedFile("tree-sitter-markdown/tree-sitter-markdown/queries/highlights.scm"),
|
||||
.injections = @embedFile("tree-sitter-markdown/tree-sitter-markdown/queries/injections.scm"),
|
||||
.highlights = "tree-sitter-markdown/tree-sitter-markdown/queries/highlights.scm",
|
||||
.injections = "tree-sitter-markdown/tree-sitter-markdown/queries/injections.scm",
|
||||
};
|
||||
|
||||
pub const @"markdown-inline" = .{
|
||||
.color = 0x000000,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{},
|
||||
.extensions = .{},
|
||||
.comment = "<!--",
|
||||
.highlights = @embedFile("tree-sitter-markdown/tree-sitter-markdown-inline/queries/highlights.scm"),
|
||||
.injections = @embedFile("tree-sitter-markdown/tree-sitter-markdown-inline/queries/injections.scm"),
|
||||
.highlights = "tree-sitter-markdown/tree-sitter-markdown-inline/queries/highlights.scm",
|
||||
.injections = "tree-sitter-markdown/tree-sitter-markdown-inline/queries/injections.scm",
|
||||
};
|
||||
|
||||
pub const nasm = .{
|
||||
.extensions = &[_][]const u8{ "asm", "nasm" },
|
||||
.extensions = .{ "asm", "nasm" },
|
||||
.comment = "#",
|
||||
.injections = @embedFile("tree-sitter-nasm/queries/injections.scm"),
|
||||
.injections = "tree-sitter-nasm/queries/injections.scm",
|
||||
};
|
||||
|
||||
pub const ninja = .{
|
||||
.extensions = &[_][]const u8{"ninja"},
|
||||
.extensions = .{"ninja"},
|
||||
.comment = "#",
|
||||
};
|
||||
|
||||
pub const nix = .{
|
||||
.color = 0x5277C3,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{"nix"},
|
||||
.extensions = .{"nix"},
|
||||
.comment = "#",
|
||||
.injections = @embedFile("tree-sitter-nix/queries/injections.scm"),
|
||||
.injections = "tree-sitter-nix/queries/injections.scm",
|
||||
};
|
||||
|
||||
pub const ocaml = .{
|
||||
.color = 0xF18803,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{ "ml", "mli" },
|
||||
.extensions = .{ "ml", "mli" },
|
||||
.comment = "(*",
|
||||
};
|
||||
|
||||
pub const openscad = .{
|
||||
.color = 0x000000,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{"scad"},
|
||||
.extensions = .{"scad"},
|
||||
.comment = "//",
|
||||
.injections = @embedFile("tree-sitter-openscad/queries/injections.scm"),
|
||||
.injections = "tree-sitter-openscad/queries/injections.scm",
|
||||
};
|
||||
|
||||
pub const org = .{
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{"org"},
|
||||
.extensions = .{"org"},
|
||||
.comment = "#",
|
||||
};
|
||||
|
||||
pub const php = .{
|
||||
.color = 0x6181b6,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{"php"},
|
||||
.extensions = .{"php"},
|
||||
.comment = "//",
|
||||
.injections = @embedFile("tree-sitter-php/queries/injections.scm"),
|
||||
.injections = "tree-sitter-php/queries/injections.scm",
|
||||
};
|
||||
|
||||
pub const purescript = .{
|
||||
.color = 0x14161a,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{"purs"},
|
||||
.extensions = .{"purs"},
|
||||
.comment = "--",
|
||||
.injections = @embedFile("tree-sitter-purescript/queries/injections.scm"),
|
||||
.injections = "tree-sitter-purescript/queries/injections.scm",
|
||||
};
|
||||
|
||||
pub const python = .{
|
||||
.color = 0xffd845,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{"py"},
|
||||
.extensions = .{"py"},
|
||||
.comment = "#",
|
||||
.first_line_matches = .{ .prefix = "#!", .content = "/bin/bash" },
|
||||
};
|
||||
|
||||
pub const regex = .{
|
||||
.extensions = &[_][]const u8{},
|
||||
.extensions = .{},
|
||||
.comment = "#",
|
||||
};
|
||||
|
||||
pub const ruby = .{
|
||||
.color = 0xd91404,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{"rb"},
|
||||
.extensions = .{"rb"},
|
||||
.comment = "#",
|
||||
};
|
||||
|
||||
pub const rust = .{
|
||||
.color = 0x000000,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{"rs"},
|
||||
.extensions = .{"rs"},
|
||||
.comment = "//",
|
||||
.injections = @embedFile("tree-sitter-rust/queries/injections.scm"),
|
||||
.injections = "tree-sitter-rust/queries/injections.scm",
|
||||
};
|
||||
|
||||
pub const scheme = .{
|
||||
.extensions = &[_][]const u8{ "scm", "ss", "el" },
|
||||
.extensions = .{ "scm", "ss", "el" },
|
||||
.comment = ";",
|
||||
};
|
||||
|
||||
pub const @"ssh-config" = .{
|
||||
.extensions = &[_][]const u8{".ssh/config"},
|
||||
.extensions = .{".ssh/config"},
|
||||
.comment = "#",
|
||||
};
|
||||
|
||||
pub const toml = .{
|
||||
.extensions = &[_][]const u8{ "toml" },
|
||||
.extensions = .{"toml"},
|
||||
.comment = "#",
|
||||
};
|
||||
|
||||
pub const typescript = .{
|
||||
.color = 0x007acc,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{ "ts", "tsx" },
|
||||
.extensions = .{ "ts", "tsx" },
|
||||
.comment = "//",
|
||||
};
|
||||
|
||||
pub const xml = .{
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{"xml"},
|
||||
.extensions = .{"xml"},
|
||||
.comment = "<!--",
|
||||
.highlights = @embedFile("tree-sitter-xml/xml/queries/highlights.scm"),
|
||||
.highlights = "tree-sitter-xml/xml/queries/highlights.scm",
|
||||
.first_line_matches = .{ .prefix = "<?xml " },
|
||||
};
|
||||
|
||||
pub const zig = .{
|
||||
.color = 0xf7a41d,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{ "zig", "zon" },
|
||||
.extensions = .{ "zig", "zon" },
|
||||
.comment = "//",
|
||||
.injections = @embedFile("tree-sitter-zig/queries/injections.scm"),
|
||||
.formatter = .{ "zig", "fmt", "--stdin" },
|
||||
.injections = "tree-sitter-zig/queries/injections.scm",
|
||||
};
|
||||
|
||||
pub const ziggy = .{
|
||||
.color = 0xf7a41d,
|
||||
.icon = "",
|
||||
.extensions = &[_][]const u8{ "ziggy" },
|
||||
.extensions = .{"ziggy"},
|
||||
.comment = "//",
|
||||
.highlights = @embedFile("tree-sitter-ziggy/tree-sitter-ziggy/queries/highlights.scm"),
|
||||
.highlights = "tree-sitter-ziggy/tree-sitter-ziggy/queries/highlights.scm",
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue