refactor: migrate to flow-syntax
This commit is contained in:
parent
87446df0ca
commit
a98e9f9f2c
6 changed files with 7 additions and 615 deletions
79
build.zig
79
build.zig
|
@ -4,75 +4,9 @@ pub fn build(b: *std.Build) void {
|
||||||
const target = b.standardTargetOptions(.{});
|
const target = b.standardTargetOptions(.{});
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
||||||
const tree_sitter_dep = b.dependency("tree-sitter", .{ .target = target, .optimize = optimize });
|
|
||||||
const clap_dep = b.dependency("clap", .{ .target = target, .optimize = optimize });
|
const clap_dep = b.dependency("clap", .{ .target = target, .optimize = optimize });
|
||||||
const themes_dep = b.dependency("themes", .{});
|
const themes_dep = b.dependency("themes", .{});
|
||||||
|
const syntax_dep = b.dependency("syntax", .{ .target = target, .optimize = optimize });
|
||||||
const syntax_mod = b.createModule(.{
|
|
||||||
.root_source_file = .{ .path = "src/syntax.zig" },
|
|
||||||
.imports = &.{
|
|
||||||
.{ .name = "treez", .module = tree_sitter_dep.module("treez") },
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-agda/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-bash/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-c-sharp/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-c/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-cpp/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-css/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-diff/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-dockerfile/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-git-rebase/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-gitcommit/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-go/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-fish/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-haskell/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-html/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-java/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-javascript/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-jsdoc/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-json/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-lua/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-make/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-markdown/tree-sitter-markdown/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-markdown/tree-sitter-markdown-inline/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-nasm/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-ninja/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-nix/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-ocaml/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-openscad/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-org/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-php/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-python/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-purescript/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-regex/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-ruby/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-rust/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-ssh-config/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-scala/queries/scala/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-scheme/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-toml/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-typescript/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-xml/dtd/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-xml/xml/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-zig/queries/highlights.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-ziggy/tree-sitter-ziggy/queries/highlights.scm"),
|
|
||||||
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-cpp/queries/injections.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-gitcommit/queries/injections.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-html/queries/injections.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-javascript/queries/injections.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-lua/queries/injections.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-markdown/tree-sitter-markdown-inline/queries/injections.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-markdown/tree-sitter-markdown/queries/injections.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-nasm/queries/injections.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-nix/queries/injections.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-openscad/queries/injections.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-php/queries/injections.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-purescript/queries/injections.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-purescript/vim_queries/injections.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-rust/queries/injections.scm"),
|
|
||||||
file_module(b, tree_sitter_dep, "tree-sitter-zig/queries/injections.scm"),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const exe = b.addExecutable(.{
|
const exe = b.addExecutable(.{
|
||||||
.name = "zat",
|
.name = "zat",
|
||||||
|
@ -80,7 +14,7 @@ pub fn build(b: *std.Build) void {
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
exe.root_module.addImport("syntax", syntax_mod);
|
exe.root_module.addImport("syntax", syntax_dep.module("syntax"));
|
||||||
exe.root_module.addImport("theme", themes_dep.module("theme"));
|
exe.root_module.addImport("theme", themes_dep.module("theme"));
|
||||||
exe.root_module.addImport("themes", themes_dep.module("themes"));
|
exe.root_module.addImport("themes", themes_dep.module("themes"));
|
||||||
exe.root_module.addImport("clap", clap_dep.module("clap"));
|
exe.root_module.addImport("clap", clap_dep.module("clap"));
|
||||||
|
@ -92,12 +26,3 @@ pub fn build(b: *std.Build) void {
|
||||||
const run_step = b.step("run", "Run the app");
|
const run_step = b.step("run", "Run the app");
|
||||||
run_step.dependOn(&run_cmd.step);
|
run_step.dependOn(&run_cmd.step);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn file_module(b: *std.Build, dep: *std.Build.Dependency, comptime sub_path: []const u8) std.Build.Module.Import {
|
|
||||||
return .{
|
|
||||||
.name = sub_path,
|
|
||||||
.module = b.createModule(.{
|
|
||||||
.root_source_file = dep.path(sub_path),
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,10 +2,6 @@
|
||||||
.name = "zat",
|
.name = "zat",
|
||||||
.version = "0.0.1",
|
.version = "0.0.1",
|
||||||
.dependencies = .{
|
.dependencies = .{
|
||||||
.@"tree-sitter" = .{
|
|
||||||
.url = "https://github.com/neurocyte/tree-sitter/releases/download/master-fb56a3e014f77e8189ae8b60408c555f8fe8be88/source.tar.gz",
|
|
||||||
.hash = "12200ead593d0f6aa6352dba239cc83a1d1457b060738560dbb890fd5cc5957d710f",
|
|
||||||
},
|
|
||||||
.clap = .{
|
.clap = .{
|
||||||
.url = "https://github.com/Hejsil/zig-clap/archive/9c23bcb5aebe0c2542b4de4472f60959974e2222.tar.gz",
|
.url = "https://github.com/Hejsil/zig-clap/archive/9c23bcb5aebe0c2542b4de4472f60959974e2222.tar.gz",
|
||||||
.hash = "12209e829da9d7d0bc089e4e0cbc07bb882f6192cd583277277da34df53cd05b8f2a",
|
.hash = "12209e829da9d7d0bc089e4e0cbc07bb882f6192cd583277277da34df53cd05b8f2a",
|
||||||
|
@ -14,6 +10,10 @@
|
||||||
.url = "https://github.com/neurocyte/flow-themes/releases/download/master-62f09365e311aa72a3b6703b7041c5d1352b15f6/flow-themes.tar.gz",
|
.url = "https://github.com/neurocyte/flow-themes/releases/download/master-62f09365e311aa72a3b6703b7041c5d1352b15f6/flow-themes.tar.gz",
|
||||||
.hash = "12205b1842c2de65ffbfe08742fbd15cf3800174e7d943a7fa5dcfb5d3b09b8c386b",
|
.hash = "12205b1842c2de65ffbfe08742fbd15cf3800174e7d943a7fa5dcfb5d3b09b8c386b",
|
||||||
},
|
},
|
||||||
|
.syntax = .{
|
||||||
|
.url = "https://github.com/neurocyte/flow-syntax/archive/e58df221cc9ea8086fdb26df348f04429361d1f0.tar.gz",
|
||||||
|
.hash = "1220f1772359f6c1fc04df79b3f9aa5899efc87b3b8d0e65f44b40df66260cf79183",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
.paths = .{
|
.paths = .{
|
||||||
"build.zig",
|
"build.zig",
|
||||||
|
|
|
@ -1,127 +0,0 @@
|
||||||
const std = @import("std");
|
|
||||||
const treez = @import("treez");
|
|
||||||
pub const FileType = @This();
|
|
||||||
|
|
||||||
color: u24,
|
|
||||||
icon: []const u8,
|
|
||||||
name: []const u8,
|
|
||||||
lang_fn: LangFn,
|
|
||||||
extensions: []const []const u8,
|
|
||||||
highlights: [:0]const u8,
|
|
||||||
injections: ?[:0]const u8,
|
|
||||||
first_line_matches: ?FirstLineMatch = null,
|
|
||||||
comment: []const u8,
|
|
||||||
|
|
||||||
pub fn get_by_name(name: []const u8) ?*const FileType {
|
|
||||||
for (file_types) |*file_type|
|
|
||||||
if (std.mem.eql(u8, file_type.name, name))
|
|
||||||
return file_type;
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn guess(file_path: ?[]const u8, content: []const u8) ?*const FileType {
|
|
||||||
if (guess_first_line(content)) |ft| return ft;
|
|
||||||
for (file_types) |*file_type|
|
|
||||||
if (file_path) |fp| if (match_file_type(file_type, fp))
|
|
||||||
return file_type;
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn guess_first_line(content: []const u8) ?*const FileType {
|
|
||||||
const first_line = if (std.mem.indexOf(u8, content, "\n")) |pos| content[0..pos] else content;
|
|
||||||
for (file_types) |*file_type|
|
|
||||||
if (file_type.first_line_matches) |match|
|
|
||||||
if (match_first_line(match, first_line))
|
|
||||||
return file_type;
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn match_first_line(match: FirstLineMatch, first_line: []const u8) bool {
|
|
||||||
if (match.prefix) |prefix|
|
|
||||||
if (prefix.len > first_line.len or !std.mem.eql(u8, first_line[0..prefix.len], prefix))
|
|
||||||
return false;
|
|
||||||
if (match.content) |content|
|
|
||||||
if (std.mem.indexOf(u8, first_line, content)) |_| {} else return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn match_file_type(file_type: *const FileType, file_path: []const u8) bool {
|
|
||||||
const basename = std.fs.path.basename(file_path);
|
|
||||||
const extension = std.fs.path.extension(file_path);
|
|
||||||
return for (file_type.extensions) |ext| {
|
|
||||||
if (ext.len == basename.len and std.mem.eql(u8, ext, basename))
|
|
||||||
return true;
|
|
||||||
if (extension.len > 0 and ext.len == extension.len - 1 and std.mem.eql(u8, ext, extension[1..]))
|
|
||||||
return true;
|
|
||||||
} else false;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn Parser(comptime lang: []const u8) LangFn {
|
|
||||||
return get_parser(lang);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_parser(comptime lang: []const u8) LangFn {
|
|
||||||
const language_name = ft_func_name(lang);
|
|
||||||
return @extern(?LangFn, .{ .name = "tree_sitter_" ++ language_name }) orelse @compileError(std.fmt.comptimePrint("Cannot find extern tree_sitter_{s}", .{language_name}));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ft_func_name(comptime lang: []const u8) []const u8 {
|
|
||||||
var func_name: [lang.len]u8 = undefined;
|
|
||||||
for (lang, 0..) |c, i|
|
|
||||||
func_name[i] = if (c == '-') '_' else c;
|
|
||||||
return &func_name;
|
|
||||||
}
|
|
||||||
|
|
||||||
const LangFn = *const fn () callconv(.C) ?*const treez.Language;
|
|
||||||
|
|
||||||
const FirstLineMatch = struct {
|
|
||||||
prefix: ?[]const u8 = null,
|
|
||||||
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,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const file_types = load_file_types(@import("file_types.zig"));
|
|
||||||
|
|
||||||
fn load_file_types(comptime Namespace: type) []FileType {
|
|
||||||
comptime switch (@typeInfo(Namespace)) {
|
|
||||||
.Struct => |info| {
|
|
||||||
var count = 0;
|
|
||||||
for (info.decls) |_| {
|
|
||||||
// @compileLog(decl.name, @TypeOf(@field(Namespace, decl.name)));
|
|
||||||
count += 1;
|
|
||||||
}
|
|
||||||
var cmds: [count]FileType = undefined;
|
|
||||||
var i = 0;
|
|
||||||
for (info.decls) |decl| {
|
|
||||||
cmds[i] = DeclLang(decl.name, @field(Namespace, decl.name));
|
|
||||||
i += 1;
|
|
||||||
}
|
|
||||||
return &cmds;
|
|
||||||
},
|
|
||||||
else => @compileError("expected tuple or struct type"),
|
|
||||||
};
|
|
||||||
}
|
|
|
@ -1,294 +0,0 @@
|
||||||
pub const agda = .{
|
|
||||||
.extensions = &[_][]const u8{"agda"},
|
|
||||||
.comment = "--",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const bash = .{
|
|
||||||
.color = 0x3e474a,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{ "sh", "bash" },
|
|
||||||
.comment = "#",
|
|
||||||
.first_line_matches = .{ .prefix = "#!", .content = "sh" },
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const c = .{
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{ "c", "h" },
|
|
||||||
.comment = "//",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const @"c-sharp" = .{
|
|
||||||
.color = 0x68217a,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"cs"},
|
|
||||||
.comment = "//",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const conf = .{
|
|
||||||
.color = 0x000000,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{ "conf", "config", ".gitconfig" },
|
|
||||||
.highlights = fish.highlights,
|
|
||||||
.comment = "#",
|
|
||||||
.parser = fish.parser,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const cpp = .{
|
|
||||||
.color = 0x9c033a,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{ "cc", "cpp", "cxx", "hpp", "hxx", "h" },
|
|
||||||
.comment = "//",
|
|
||||||
.injections = @embedFile("tree-sitter-cpp/queries/injections.scm"),
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const css = .{
|
|
||||||
.color = 0x3d8fc6,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"css"},
|
|
||||||
.comment = "//",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const diff = .{
|
|
||||||
.extensions = &[_][]const u8{ "diff", "patch" },
|
|
||||||
.comment = "#",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const dockerfile = .{
|
|
||||||
.color = 0x019bc6,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{ "Dockerfile", "dockerfile", "docker", "Containerfile", "container" },
|
|
||||||
.comment = "#",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const dtd = .{
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"dtd"},
|
|
||||||
.comment = "<!--",
|
|
||||||
.highlights = @embedFile("tree-sitter-xml/dtd/queries/highlights.scm"),
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const fish = .{
|
|
||||||
.extensions = &[_][]const u8{"fish"},
|
|
||||||
.comment = "#",
|
|
||||||
.parser = @import("file_type.zig").Parser("fish"),
|
|
||||||
.highlights = @embedFile("tree-sitter-fish/queries/highlights.scm"),
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const @"git-rebase" = .{
|
|
||||||
.color = 0xf34f29,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"git-rebase-todo"},
|
|
||||||
.comment = "#",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const gitcommit = .{
|
|
||||||
.color = 0xf34f29,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"COMMIT_EDITMSG"},
|
|
||||||
.comment = "#",
|
|
||||||
.injections = @embedFile("tree-sitter-gitcommit/queries/injections.scm"),
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const go = .{
|
|
||||||
.color = 0x00acd7,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"go"},
|
|
||||||
.comment = "//",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const haskell = .{
|
|
||||||
.color = 0x5E5185,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"hs"},
|
|
||||||
.comment = "--",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const html = .{
|
|
||||||
.color = 0xe54d26,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"html"},
|
|
||||||
.comment = "<!--",
|
|
||||||
.injections = @embedFile("tree-sitter-html/queries/injections.scm"),
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const java = .{
|
|
||||||
.color = 0xEA2D2E,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"java"},
|
|
||||||
.comment = "//",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const javascript = .{
|
|
||||||
.color = 0xf0db4f,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"js"},
|
|
||||||
.comment = "//",
|
|
||||||
.injections = @embedFile("tree-sitter-javascript/queries/injections.scm"),
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const json = .{
|
|
||||||
.extensions = &[_][]const u8{"json"},
|
|
||||||
.comment = "//",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const lua = .{
|
|
||||||
.color = 0x000080,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"lua"},
|
|
||||||
.comment = "--",
|
|
||||||
.injections = @embedFile("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" },
|
|
||||||
.comment = "#",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const markdown = .{
|
|
||||||
.color = 0x000000,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"md"},
|
|
||||||
.comment = "<!--",
|
|
||||||
.highlights = @embedFile("tree-sitter-markdown/tree-sitter-markdown/queries/highlights.scm"),
|
|
||||||
.injections = @embedFile("tree-sitter-markdown/tree-sitter-markdown/queries/injections.scm"),
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const @"markdown-inline" = .{
|
|
||||||
.color = 0x000000,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{},
|
|
||||||
.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"),
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const nasm = .{
|
|
||||||
.extensions = &[_][]const u8{ "asm", "nasm" },
|
|
||||||
.comment = "#",
|
|
||||||
.injections = @embedFile("tree-sitter-nasm/queries/injections.scm"),
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const ninja = .{
|
|
||||||
.extensions = &[_][]const u8{"ninja"},
|
|
||||||
.comment = "#",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const nix = .{
|
|
||||||
.color = 0x5277C3,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"nix"},
|
|
||||||
.comment = "#",
|
|
||||||
.injections = @embedFile("tree-sitter-nix/queries/injections.scm"),
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const ocaml = .{
|
|
||||||
.color = 0xF18803,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{ "ml", "mli" },
|
|
||||||
.comment = "(*",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const openscad = .{
|
|
||||||
.color = 0x000000,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"scad"},
|
|
||||||
.comment = "//",
|
|
||||||
.injections = @embedFile("tree-sitter-openscad/queries/injections.scm"),
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const org = .{
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"org"},
|
|
||||||
.comment = "#",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const php = .{
|
|
||||||
.color = 0x6181b6,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"php"},
|
|
||||||
.comment = "//",
|
|
||||||
.injections = @embedFile("tree-sitter-php/queries/injections.scm"),
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const purescript = .{
|
|
||||||
.color = 0x14161a,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"purs"},
|
|
||||||
.comment = "--",
|
|
||||||
.injections = @embedFile("tree-sitter-purescript/queries/injections.scm"),
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const python = .{
|
|
||||||
.color = 0xffd845,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"py"},
|
|
||||||
.comment = "#",
|
|
||||||
.first_line_matches = .{ .prefix = "#!", .content = "/bin/bash" },
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const regex = .{
|
|
||||||
.extensions = &[_][]const u8{},
|
|
||||||
.comment = "#",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const ruby = .{
|
|
||||||
.color = 0xd91404,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"rb"},
|
|
||||||
.comment = "#",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const rust = .{
|
|
||||||
.color = 0x000000,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"rs"},
|
|
||||||
.comment = "//",
|
|
||||||
.injections = @embedFile("tree-sitter-rust/queries/injections.scm"),
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const scheme = .{
|
|
||||||
.extensions = &[_][]const u8{ "scm", "ss", "el" },
|
|
||||||
.comment = ";",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const @"ssh-config" = .{
|
|
||||||
.extensions = &[_][]const u8{".ssh/config"},
|
|
||||||
.comment = "#",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const toml = .{
|
|
||||||
.extensions = &[_][]const u8{ "toml" },
|
|
||||||
.comment = "#",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const typescript = .{
|
|
||||||
.color = 0x007acc,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{ "ts", "tsx" },
|
|
||||||
.comment = "//",
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const xml = .{
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{"xml"},
|
|
||||||
.comment = "<!--",
|
|
||||||
.highlights = @embedFile("tree-sitter-xml/xml/queries/highlights.scm"),
|
|
||||||
.first_line_matches = .{ .prefix = "<?xml " },
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const zig = .{
|
|
||||||
.color = 0xf7a41d,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{ "zig", "zon" },
|
|
||||||
.comment = "//",
|
|
||||||
.injections = @embedFile("tree-sitter-zig/queries/injections.scm"),
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const ziggy = .{
|
|
||||||
.color = 0xf7a41d,
|
|
||||||
.icon = "",
|
|
||||||
.extensions = &[_][]const u8{ "ziggy" },
|
|
||||||
.comment = "//",
|
|
||||||
.highlights = @embedFile("tree-sitter-ziggy/tree-sitter-ziggy/queries/highlights.scm"),
|
|
||||||
};
|
|
|
@ -109,7 +109,7 @@ fn render_file(a: std.mem.Allocator, writer: anytype, content: []const u8, file_
|
||||||
content: []const u8,
|
content: []const u8,
|
||||||
theme: *const Theme,
|
theme: *const Theme,
|
||||||
last_pos: usize = 0,
|
last_pos: usize = 0,
|
||||||
fn cb(ctx: *@This(), range: syntax.Range, scope: []const u8, id: u32, idx: usize) error{Stop}!void {
|
fn cb(ctx: *@This(), range: syntax.Range, scope: []const u8, id: u32, idx: usize, _: *const syntax.Node) error{Stop}!void {
|
||||||
if (idx > 0) return;
|
if (idx > 0) return;
|
||||||
|
|
||||||
if (ctx.last_pos < range.start_byte) {
|
if (ctx.last_pos < range.start_byte) {
|
||||||
|
|
112
src/syntax.zig
112
src/syntax.zig
|
@ -1,112 +0,0 @@
|
||||||
const std = @import("std");
|
|
||||||
const treez = @import("treez");
|
|
||||||
|
|
||||||
const Self = @This();
|
|
||||||
|
|
||||||
pub const Edit = treez.InputEdit;
|
|
||||||
pub const FileType = @import("file_type.zig");
|
|
||||||
pub const Range = treez.Range;
|
|
||||||
pub const Point = treez.Point;
|
|
||||||
const Language = treez.Language;
|
|
||||||
const Parser = treez.Parser;
|
|
||||||
const Query = treez.Query;
|
|
||||||
const Tree = treez.Tree;
|
|
||||||
|
|
||||||
a: std.mem.Allocator,
|
|
||||||
lang: *const Language,
|
|
||||||
file_type: *const FileType,
|
|
||||||
parser: *Parser,
|
|
||||||
query: *Query,
|
|
||||||
injections: *Query,
|
|
||||||
tree: ?*Tree = null,
|
|
||||||
|
|
||||||
pub fn create(file_type: *const FileType, a: std.mem.Allocator, content: []const u8) !*Self {
|
|
||||||
const self = try a.create(Self);
|
|
||||||
self.* = .{
|
|
||||||
.a = a,
|
|
||||||
.lang = file_type.lang_fn() orelse std.debug.panic("tree-sitter parser function failed for language: {d}", .{file_type.name}),
|
|
||||||
.file_type = file_type,
|
|
||||||
.parser = try Parser.create(),
|
|
||||||
.query = try Query.create(self.lang, file_type.highlights),
|
|
||||||
.injections = try Query.create(self.lang, file_type.highlights),
|
|
||||||
};
|
|
||||||
errdefer self.destroy();
|
|
||||||
try self.parser.setLanguage(self.lang);
|
|
||||||
try self.parse(content);
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn create_file_type(a: std.mem.Allocator, content: []const u8, lang_name: []const u8) !*Self {
|
|
||||||
const file_type = FileType.get_by_name(lang_name) orelse return error.NotFound;
|
|
||||||
return create(file_type, a, content);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn create_guess_file_type(a: std.mem.Allocator, content: []const u8, file_path: ?[]const u8) !*Self {
|
|
||||||
const file_type = FileType.guess(file_path, content) orelse return error.NotFound;
|
|
||||||
return create(file_type, a, content);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn destroy(self: *Self) void {
|
|
||||||
if (self.tree) |tree| tree.destroy();
|
|
||||||
self.query.destroy();
|
|
||||||
self.parser.destroy();
|
|
||||||
self.a.destroy(self);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse(self: *Self, content: []const u8) !void {
|
|
||||||
if (self.tree) |tree| tree.destroy();
|
|
||||||
self.tree = try self.parser.parseString(null, content);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn refresh_full(self: *Self, content: []const u8) !void {
|
|
||||||
return self.parse(content);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn edit(self: *Self, ed: Edit) void {
|
|
||||||
if (self.tree) |tree| tree.edit(&ed);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn refresh(self: *Self, content: []const u8) !void {
|
|
||||||
const old_tree = self.tree;
|
|
||||||
defer if (old_tree) |tree| tree.destroy();
|
|
||||||
self.tree = try self.parser.parseString(old_tree, content);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn CallBack(comptime T: type) type {
|
|
||||||
return fn (ctx: T, sel: Range, scope: []const u8, id: u32, capture_idx: usize) error{Stop}!void;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn render(self: *const Self, ctx: anytype, comptime cb: CallBack(@TypeOf(ctx)), range: ?Range) !void {
|
|
||||||
const cursor = try Query.Cursor.create();
|
|
||||||
defer cursor.destroy();
|
|
||||||
const tree = if (self.tree) |p| p else return;
|
|
||||||
cursor.execute(self.query, tree.getRootNode());
|
|
||||||
if (range) |r| cursor.setPointRange(r.start_point, r.end_point);
|
|
||||||
while (cursor.nextMatch()) |match| {
|
|
||||||
var idx: usize = 0;
|
|
||||||
for (match.captures()) |capture| {
|
|
||||||
try cb(ctx, capture.node.getRange(), self.query.getCaptureNameForId(capture.id), capture.id, idx);
|
|
||||||
idx += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn highlights_at_point(self: *const Self, ctx: anytype, comptime cb: CallBack(@TypeOf(ctx)), point: Point) void {
|
|
||||||
const cursor = Query.Cursor.create() catch return;
|
|
||||||
defer cursor.destroy();
|
|
||||||
const tree = if (self.tree) |p| p else return;
|
|
||||||
cursor.execute(self.query, tree.getRootNode());
|
|
||||||
cursor.setPointRange(.{ .row = point.row, .column = 0 }, .{ .row = point.row + 1, .column = 0 });
|
|
||||||
while (cursor.nextMatch()) |match| {
|
|
||||||
for (match.captures()) |capture| {
|
|
||||||
const range = capture.node.getRange();
|
|
||||||
const start = range.start_point;
|
|
||||||
const end = range.end_point;
|
|
||||||
const scope = self.query.getCaptureNameForId(capture.id);
|
|
||||||
if (start.row == point.row and start.row <= point.column and point.column < end.column)
|
|
||||||
cb(ctx, range, scope, capture.id, 0) catch return;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue