Skip to content

Commit 92ec5be

Browse files
committed
improve code formatting when generated file contains syntax errors
1 parent fcd6e90 commit 92ec5be

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/codegen/codegen.zig

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const std = @import("std");
22
const MetaModel = @import("MetaModel.zig");
33

4-
pub fn main() !void {
4+
pub fn main() !u8 {
55
var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
66
defer _ = debug_allocator.deinit();
77

@@ -32,12 +32,14 @@ pub fn main() !void {
3232
if (zig_tree.errors.len != 0) {
3333
std.log.warn("generated file contains syntax errors! (cannot format file)", .{});
3434
try out_file.writeAll(source);
35+
return 1;
3536
} else {
3637
var buf: [1024]u8 = undefined;
3738
var out = out_file.writer(&buf);
3839
const w = &out.interface;
3940
try zig_tree.render(gpa, w, .{});
4041
try w.flush();
42+
return 0;
4143
}
4244
}
4345

@@ -294,7 +296,7 @@ const Renderer = struct {
294296
if (payload.documentation) |docs| try r.w.print("{f}", .{fmtDocs(docs, .doc)});
295297
if (payload.original_name) |original_name| {
296298
if (!std.mem.eql(u8, name, original_name)) {
297-
if (payload.documentation != null) try r.w.writeAll("\n///\n");
299+
if (payload.documentation != null) try r.w.writeAll("///\n");
298300
try r.w.print("/// LSP Specification name: `{s}`\n", .{original_name});
299301
}
300302
}
@@ -305,7 +307,7 @@ const Renderer = struct {
305307
.structure => |*structure| {
306308
if (structure.documentation) |docs| try r.w.print("{f}", .{fmtDocs(docs, .doc)});
307309
if (!std.mem.eql(u8, name, structure.name)) {
308-
if (structure.documentation != null) try r.w.writeAll("\n///\n");
310+
if (structure.documentation != null) try r.w.writeAll("///\n");
309311
try r.w.print("/// LSP Specification name: `{s}`\n", .{structure.name});
310312
}
311313

@@ -325,7 +327,7 @@ const Renderer = struct {
325327
};
326328
if (enumeration.documentation) |docs| try r.w.print("{f}", .{fmtDocs(docs, .doc)});
327329
if (!std.mem.eql(u8, name, enumeration.name)) {
328-
if (enumeration.documentation != null) try r.w.writeAll("\n///\n");
330+
if (enumeration.documentation != null) try r.w.writeAll("///\n");
329331
try r.w.print("/// LSP Specification name: `{s}`\n", .{enumeration.name});
330332
}
331333
try r.w.print("pub const {f} = {s} {{\n", .{ std.zig.fmtId(name), container_kind });
@@ -582,10 +584,10 @@ const Renderer = struct {
582584
} else {
583585
try r.renderType(property.type, .empty);
584586
}
587+
try r.w.writeAll(" = null");
585588
} else {
586589
try r.renderType(property.type, .empty);
587590
}
588-
if (isNullable(property.type)) try r.w.writeAll(" = null");
589591
try r.w.writeByte(',');
590592
}
591593

@@ -1035,7 +1037,7 @@ fn renderMetaModel(gpa: std.mem.Allocator, meta_model: *MetaModel) error{ OutOfM
10351037
for (original_symbol_names.keys()) |name| {
10361038
switch (symbol_map.get(name).?) {
10371039
.remove => continue,
1038-
.rename, .replace_with => |new_name| try renderer.w.print("pub const {s} = types.{s};\n", .{ name, new_name }),
1040+
.rename, .replace_with => |new_name| try renderer.w.print(" pub const {s} = types.{s};\n", .{ name, new_name }),
10391041
}
10401042
}
10411043
try renderer.w.writeAll("};\n\n");
@@ -1052,7 +1054,7 @@ fn renderMetaModel(gpa: std.mem.Allocator, meta_model: *MetaModel) error{ OutOfM
10521054
for (meta_model.requests) |request| {
10531055
try renderer.renderRequest(request);
10541056
}
1055-
try renderer.w.writeAll("\n});");
1057+
try renderer.w.writeAll("});\n");
10561058
}
10571059

10581060
return try aw.toOwnedSliceSentinel(0);

0 commit comments

Comments
 (0)