aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/hello-zig/.editorconfig6
-rw-r--r--examples/hello-zig/.gitignore2
-rw-r--r--examples/hello-zig/.nova/Configuration.json8
-rw-r--r--examples/hello-zig/.nova/Tasks/Zig Debug.json12
-rw-r--r--examples/hello-zig/.nova/Tasks/Zig Package (macOS Terminal).json7
-rw-r--r--examples/hello-zig/.nova/Tasks/Zig Package.json8
-rw-r--r--examples/hello-zig/build.zig47
-rw-r--r--examples/hello-zig/build.zig.zon12
-rw-r--r--examples/hello-zig/src/main.zig32
-rw-r--r--examples/hello-zig/src/root.zig83
-rw-r--r--examples/sample-config.zon19
-rw-r--r--examples/showcase.zig80
12 files changed, 316 insertions, 0 deletions
diff --git a/examples/hello-zig/.editorconfig b/examples/hello-zig/.editorconfig
new file mode 100644
index 0000000..d5f8c65
--- /dev/null
+++ b/examples/hello-zig/.editorconfig
@@ -0,0 +1,6 @@
1root = true
2
3[*]
4indent_style = space
5indent_size = 4
6insert_final_newline = true
diff --git a/examples/hello-zig/.gitignore b/examples/hello-zig/.gitignore
new file mode 100644
index 0000000..3389c86
--- /dev/null
+++ b/examples/hello-zig/.gitignore
@@ -0,0 +1,2 @@
1.zig-cache/
2zig-out/
diff --git a/examples/hello-zig/.nova/Configuration.json b/examples/hello-zig/.nova/Configuration.json
new file mode 100644
index 0000000..47e341a
--- /dev/null
+++ b/examples/hello-zig/.nova/Configuration.json
@@ -0,0 +1,8 @@
1{
2 "at.dcz.nova-zig.toolchain.lldb-dap-path" : "\/Library\/Developer\/CommandLineTools\/usr\/bin\/lldb-dap",
3 "at.dcz.nova-zig.toolchain.zig-path" : "\/opt\/homebrew\/bin\/zig",
4 "at.dcz.nova-zig.zls.build-on-save" : true,
5 "at.dcz.nova-zig.zls.enabled" : true,
6 "zls.enable_build_on_save" : true,
7 "zls.zig_exe_path" : "\/opt\/homebrew\/bin\/zig"
8}
diff --git a/examples/hello-zig/.nova/Tasks/Zig Debug.json b/examples/hello-zig/.nova/Tasks/Zig Debug.json
new file mode 100644
index 0000000..53b7f13
--- /dev/null
+++ b/examples/hello-zig/.nova/Tasks/Zig Debug.json
@@ -0,0 +1,12 @@
1{
2 "extension" : {
3 "identifier" : "at.dcz.nova-zig",
4 "name" : "Zig"
5 },
6 "extensionTemplate" : "zigDebug",
7 "extensionValues" : {
8 "console" : "internalConsole",
9 "programPath" : "zig-out\/bin\/hello-zig"
10 },
11 "openLogOnRun" : "start"
12}
diff --git a/examples/hello-zig/.nova/Tasks/Zig Package (macOS Terminal).json b/examples/hello-zig/.nova/Tasks/Zig Package (macOS Terminal).json
new file mode 100644
index 0000000..d1fa04d
--- /dev/null
+++ b/examples/hello-zig/.nova/Tasks/Zig Package (macOS Terminal).json
@@ -0,0 +1,7 @@
1{
2 "extension" : {
3 "identifier" : "at.dcz.nova-zig",
4 "name" : "Zig"
5 },
6 "extensionTemplate" : "zigBuildRunTerminal"
7}
diff --git a/examples/hello-zig/.nova/Tasks/Zig Package.json b/examples/hello-zig/.nova/Tasks/Zig Package.json
new file mode 100644
index 0000000..10830e6
--- /dev/null
+++ b/examples/hello-zig/.nova/Tasks/Zig Package.json
@@ -0,0 +1,8 @@
1{
2 "buildBeforeRunning" : true,
3 "extension" : {
4 "identifier" : "at.dcz.nova-zig",
5 "name" : "Zig"
6 },
7 "extensionTemplate" : "zigBuildRun"
8}
diff --git a/examples/hello-zig/build.zig b/examples/hello-zig/build.zig
new file mode 100644
index 0000000..5c56fb0
--- /dev/null
+++ b/examples/hello-zig/build.zig
@@ -0,0 +1,47 @@
1const std = @import("std");
2
3pub fn build(b: *std.Build) void {
4 const target = b.standardTargetOptions(.{});
5 const optimize = b.standardOptimizeOption(.{});
6
7 const lib_mod = b.addModule("hello_zig", .{
8 .root_source_file = b.path("src/root.zig"),
9 .target = target,
10 });
11
12 const exe = b.addExecutable(.{
13 .name = "hello-zig",
14 .root_module = b.createModule(.{
15 .root_source_file = b.path("src/main.zig"),
16 .target = target,
17 .optimize = optimize,
18 .imports = &.{
19 .{ .name = "hello_zig", .module = lib_mod },
20 },
21 }),
22 });
23
24 b.installArtifact(exe);
25
26 const run_step = b.step("run", "Run the sample app");
27 const run_cmd = b.addRunArtifact(exe);
28 run_step.dependOn(&run_cmd.step);
29
30 if (b.args) |args| {
31 run_cmd.addArgs(args);
32 }
33
34 const lib_tests = b.addTest(.{
35 .root_module = lib_mod,
36 });
37 const exe_tests = b.addTest(.{
38 .root_module = exe.root_module,
39 });
40
41 const run_lib_tests = b.addRunArtifact(lib_tests);
42 const run_exe_tests = b.addRunArtifact(exe_tests);
43
44 const test_step = b.step("test", "Run the sample tests");
45 test_step.dependOn(&run_lib_tests.step);
46 test_step.dependOn(&run_exe_tests.step);
47}
diff --git a/examples/hello-zig/build.zig.zon b/examples/hello-zig/build.zig.zon
new file mode 100644
index 0000000..09f7da2
--- /dev/null
+++ b/examples/hello-zig/build.zig.zon
@@ -0,0 +1,12 @@
1.{
2 .name = .hello_zig,
3 .version = "0.1.0",
4 .fingerprint = 0x1c1a468675f426fe, // Changing this has security and trust implications.
5 .minimum_zig_version = "0.15.2",
6 .dependencies = .{},
7 .paths = .{
8 "build.zig",
9 "build.zig.zon",
10 "src",
11 },
12}
diff --git a/examples/hello-zig/src/main.zig b/examples/hello-zig/src/main.zig
new file mode 100644
index 0000000..74eb590
--- /dev/null
+++ b/examples/hello-zig/src/main.zig
@@ -0,0 +1,32 @@
1const std = @import("std");
2const hello_zig = @import("hello_zig");
3
4pub fn main() !void {
5 var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
6 defer arena.deinit();
7
8 const allocator = arena.allocator();
9 const args = try std.process.argsAlloc(allocator);
10 const name = if (args.len > 1) args[1] else "Nova";
11
12 var greeter = hello_zig.Greeter{
13 .name = name,
14 .punctuation = '!',
15 };
16
17 const stdout = std.fs.File.stdout().deprecatedWriter();
18 try greeter.write(stdout, .verbose);
19
20 const sample_values = [_]i32{ -3, -2, 0, 1, 2, 3, 4 };
21 var squares = try hello_zig.collectEvenSquares(allocator, &sample_values);
22 defer squares.deinit(allocator);
23
24 std.debug.print("even squares: {any}\n", .{squares.items});
25}
26
27test "main module can compute a summary" {
28 const greeting = try hello_zig.describeNumber(std.testing.allocator, 42);
29 defer std.testing.allocator.free(greeting);
30
31 try std.testing.expect(std.mem.startsWith(u8, greeting, "positive"));
32}
diff --git a/examples/hello-zig/src/root.zig b/examples/hello-zig/src/root.zig
new file mode 100644
index 0000000..f6600ac
--- /dev/null
+++ b/examples/hello-zig/src/root.zig
@@ -0,0 +1,83 @@
1const std = @import("std");
2
3pub const OutputStyle = enum {
4 compact,
5 verbose,
6};
7
8pub const NumberKind = union(enum) {
9 negative: i64,
10 zero,
11 positive: u64,
12};
13
14pub const Greeter = struct {
15 name: []const u8,
16 punctuation: u8 = '!',
17
18 pub fn write(self: Greeter, writer: anytype, style: OutputStyle) !void {
19 switch (style) {
20 .compact => try writer.print("hello, {s}{c}\n", .{ self.name, self.punctuation }),
21 .verbose => {
22 try writer.print(
23 \\hello, {s}{c}
24 \\this sample is here to exercise Zig syntax support in Nova.
25 \\
26 , .{ self.name, self.punctuation });
27 },
28 }
29 }
30};
31
32pub fn classifyNumber(value: i64) NumberKind {
33 if (value == 0) return .zero;
34 if (value < 0) return .{ .negative = value };
35 return .{ .positive = @intCast(value) };
36}
37
38pub fn describeNumber(allocator: std.mem.Allocator, value: i64) ![]u8 {
39 return switch (classifyNumber(value)) {
40 .zero => std.fmt.allocPrint(allocator, "zero", .{}),
41 .negative => |negative| std.fmt.allocPrint(allocator, "negative({d})", .{negative}),
42 .positive => |positive| std.fmt.allocPrint(allocator, "positive({d})", .{positive}),
43 };
44}
45
46pub fn collectEvenSquares(
47 allocator: std.mem.Allocator,
48 values: []const i32,
49) !std.ArrayList(i32) {
50 var result: std.ArrayList(i32) = .empty;
51 errdefer result.deinit(allocator);
52
53 for (values) |value| {
54 if (@mod(value, 2) != 0) continue;
55 try result.append(allocator, value * value);
56 }
57
58 return result;
59}
60
61test "classifyNumber covers the main branches" {
62 try std.testing.expectEqual(NumberKind{ .negative = -5 }, classifyNumber(-5));
63 try std.testing.expectEqual(NumberKind.zero, classifyNumber(0));
64 try std.testing.expectEqual(NumberKind{ .positive = 9 }, classifyNumber(9));
65}
66
67test "collectEvenSquares keeps only even numbers" {
68 const values = [_]i32{ -4, -3, 0, 1, 2, 7 };
69 var result = try collectEvenSquares(std.testing.allocator, &values);
70 defer result.deinit(std.testing.allocator);
71
72 try std.testing.expectEqualSlices(i32, &.{ 16, 0, 4 }, result.items);
73}
74
75test "Greeter.write renders verbose output" {
76 var list: std.ArrayList(u8) = .empty;
77 defer list.deinit(std.testing.allocator);
78
79 const writer = list.writer(std.testing.allocator);
80 try (Greeter{ .name = "Zig" }).write(writer, .verbose);
81
82 try std.testing.expect(std.mem.indexOf(u8, list.items, "exercise Zig syntax support") != null);
83}
diff --git a/examples/sample-config.zon b/examples/sample-config.zon
new file mode 100644
index 0000000..b346a84
--- /dev/null
+++ b/examples/sample-config.zon
@@ -0,0 +1,19 @@
1.{
2 .name = "nova-zig-example",
3 .enabled = true,
4 .targets = .{
5 "native",
6 "wasm32-wasi",
7 },
8 .retry = .{
9 .max_attempts = 3,
10 .backoff_seconds = 2.5,
11 },
12 .theme = .solarized,
13 .features = .{
14 .language_server = true,
15 .tree_sitter = true,
16 .run_tasks = true,
17 .debug_adapter = false,
18 },
19}
diff --git a/examples/showcase.zig b/examples/showcase.zig
new file mode 100644
index 0000000..54b195f
--- /dev/null
+++ b/examples/showcase.zig
@@ -0,0 +1,80 @@
1const std = @import("std");
2
3pub const AppError = error{
4 MissingName,
5 EmptyInput,
6};
7
8pub const Theme = enum {
9 light,
10 dark,
11 solarized,
12};
13
14pub const Settings = struct {
15 name: []const u8,
16 retries: u8 = 3,
17 theme: Theme = .dark,
18 tags: []const []const u8 = &.{},
19
20 pub fn validate(self: Settings) AppError!void {
21 if (self.name.len == 0) return error.MissingName;
22 if (self.tags.len == 0) return error.EmptyInput;
23 }
24};
25
26pub fn summarize(settings: Settings) []const u8 {
27 return switch (settings.theme) {
28 .light => "bright",
29 .dark => "focused",
30 .solarized => "balanced",
31 };
32}
33
34pub fn findLongestTag(tags: []const []const u8) ?[]const u8 {
35 var longest: ?[]const u8 = null;
36
37 for (tags) |tag| {
38 if (longest == null or tag.len > longest.?.len) {
39 longest = tag;
40 }
41 }
42
43 return longest;
44}
45
46pub fn renderExample(writer: anytype) !void {
47 const settings = Settings{
48 .name = "Nova Zig",
49 .tags = &.{ "tree-sitter", "zls", "tasks" },
50 };
51
52 try settings.validate();
53 try writer.print("theme summary: {s}\n", .{summarize(settings)});
54
55 if (findLongestTag(settings.tags)) |tag| {
56 try writer.print("longest tag: {s}\n", .{tag});
57 }
58
59 const block_value = label: {
60 var total: usize = 0;
61 for (settings.tags) |tag| total += tag.len;
62 break :label total;
63 };
64 try writer.print("combined tag length: {}\n", .{block_value});
65
66 const multiline =
67 \\sample text block
68 \\with enough lines to make folding obvious
69 \\inside Nova's editor
70 ;
71 _ = multiline;
72}
73
74test "showcase remains valid" {
75 var output: std.ArrayList(u8) = .empty;
76 defer output.deinit(std.testing.allocator);
77
78 try renderExample(output.writer(std.testing.allocator));
79 try std.testing.expect(std.mem.indexOf(u8, output.items, "theme summary") != null);
80}