Skip to content

Commit

Permalink
html: add support for self-closing tags under <svg>
Browse files Browse the repository at this point in the history
  • Loading branch information
kristoff-it committed Jul 11, 2024
1 parent 8585d86 commit 118b5e1
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 54 deletions.
11 changes: 11 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,15 @@ pub fn build(b: *std.Build) !void {
.root_source_file = b.path("src/cli.zig"),
.target = target,
.optimize = optimize,
.single_threaded = true,
});

const verbose_logging = b.option(bool, "log", "Enable verbose logging also in release modes") orelse false;
const scopes = b.option([]const []const u8, "scope", "Enable this scope (all scopes are enabled when none is specified through this option), can be used multiple times") orelse &[0][]const u8{};
const options = b.addOptions();
options.addOption(bool, "verbose_logging", verbose_logging);
options.addOption([]const []const u8, "enabled_scopes", scopes);

const folders = b.dependency("known-folders", .{});
const lsp = b.dependency("zig-lsp-kit", .{});

Expand All @@ -45,6 +52,7 @@ pub fn build(b: *std.Build) !void {
folders.module("known-folders"),
);
super_cli.root_module.addImport("lsp", lsp.module("lsp"));
super_cli.root_module.addOptions("build_options", options);

const run_exe = b.addRunArtifact(super_cli);
if (b.args) |args| run_exe.addArgs(args);
Expand All @@ -66,6 +74,7 @@ pub fn build(b: *std.Build) !void {
folders.module("known-folders"),
);
super_cli_check.root_module.addImport("lsp", lsp.module("lsp"));
super_cli_check.root_module.addOptions("build_options", options);

const check = b.step("check", "Check if Super compiles");
check.dependOn(&super_cli_check.step);
Expand Down Expand Up @@ -105,6 +114,7 @@ pub fn build(b: *std.Build) !void {
folders.module("known-folders"),
);
super_exe_release.root_module.addImport("lsp", lsp.module("lsp"));
super_exe_release.root_module.addOptions("build_options", options);

const target_output = b.addInstallArtifact(super_exe_release, .{
.dest_dir = .{
Expand Down Expand Up @@ -133,6 +143,7 @@ pub fn build(b: *std.Build) !void {

super_wasm_lsp.root_module.addImport("super", super);
super_wasm_lsp.root_module.addImport("lsp", lsp.module("lsp"));
super_wasm_lsp.root_module.addOptions("build_options", options);

const wasm = b.step("wasm", "Generate WASM Build of the LSP for VSCode");
const target_output = b.addInstallArtifact(super_wasm_lsp, .{
Expand Down
17 changes: 8 additions & 9 deletions src/Ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ pub fn init(
const html_node_idx = cursor.idx;
const depth = cursor.depth;

switch (html_node.tag) {
switch (html_node.kind) {
.element,
.element_void,
.element_self_closing,
Expand Down Expand Up @@ -699,6 +699,9 @@ const Parser = struct {
const block_context = block_mode and depth == 1;
if (block_context) tmp_result.kind = .block;

// used to detect when a block is missing an id
var seen_id_attr = false;

var start_tag = elem.startTag(p.src, .superhtml);
const tag_name = start_tag.name_span;

Expand Down Expand Up @@ -858,9 +861,6 @@ const Parser = struct {
.root, .extend, .super_block, .super => unreachable,
}

var attrs_seen = std.StringHashMap(Span).init(gpa);
defer attrs_seen.deinit();

var last_attr_end = tag_name.end;
var scripted_attrs_span: Node.ScriptedAttrsSpan = .{
.start = @intCast(p.scripted_attrs.items.len),
Expand Down Expand Up @@ -963,6 +963,7 @@ const Parser = struct {
}

tmp_result.id_template_parentid = attr;
seen_id_attr = true;

continue;
}
Expand Down Expand Up @@ -1489,8 +1490,7 @@ const Parser = struct {
else => {},
}

// TODO: see if the error reporting order makes sense
if (tmp_result.kind.role() == .block and !attrs_seen.contains("id")) {
if (tmp_result.kind.role() == .block and !seen_id_attr) {
try p.errors.append(gpa, .{
.kind = .block_missing_id,
.main_location = tag_name,
Expand Down Expand Up @@ -1520,9 +1520,8 @@ const Parser = struct {
// having inserted the node in the tree. anything that
// can be tested sooner should go in self.buildNode().
//
// NOTE: We can only validate constraints *upwards* with regards
// to the SuperTree.

// NOTE: We can only validate constraints *upwards*, as the
// rest of the tree has not yet been built.
switch (node.kind.role()) {
.root => unreachable,
.element => {},
Expand Down
5 changes: 5 additions & 0 deletions src/cli.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const std = @import("std");
const builtin = @import("builtin");
const build_options = @import("build_options");
const super = @import("super");
const logging = @import("cli/logging.zig");
const fmt_exe = @import("cli/fmt.zig");
Expand All @@ -13,6 +14,10 @@ pub const known_folders_config = .{
};

pub const std_options: std.Options = .{
.log_level = if (build_options.verbose_logging)
.debug
else
std.log.default_level,
.logFn = logging.logFn,
};

Expand Down
17 changes: 16 additions & 1 deletion src/cli/logging.zig
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
const std = @import("std");
const builtin = @import("builtin");
const build_options = @import("build_options");
const folders = @import("known-folders");

pub var log_file: ?std.fs.File = switch (builtin.target.os.tag) {
.linux, .macos => std.io.getStdErr(),
else => null,
};

// const enabled_scopes = blk: {
// const len = build_options.enabled_scopes.len;
// const scopes: [len]@Type(.EnumLiteral) = undefined;
// for (build_options.enabled_scopes, &scopes) |s, *e| {
// e.* = @Type()
// }
// };

pub fn logFn(
comptime level: std.log.Level,
comptime scope: @Type(.EnumLiteral),
comptime format: []const u8,
args: anytype,
) void {
// if (scope != .ws and scope != .network) return;
if (build_options.enabled_scopes.len > 0) {
inline for (build_options.enabled_scopes) |es| {
if (comptime std.mem.eql(u8, es, @tagName(scope))) {
break;
}
} else return;
}

const l = log_file orelse return;
const scope_prefix = "(" ++ @tagName(scope) ++ "): ";
Expand Down
4 changes: 4 additions & 0 deletions src/cli/lsp.zig
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ pub const Handler = struct {

const doc = self.files.getPtr(request.textDocument.uri) orelse return null;

if (doc.html.errors.len != 0) {
return null;
}

log.debug("format!!", .{});

var buf = std.ArrayList(u8).init(self.gpa);
Expand Down
Loading

0 comments on commit 118b5e1

Please sign in to comment.