From 56d55b5371c79cc44fb904a06ed2522f2788187f Mon Sep 17 00:00:00 2001 From: Jost Alemann Date: Thu, 20 Mar 2025 12:19:44 +0100 Subject: [PATCH 1/7] zig: add ifelse --- snippets/zig.json | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/snippets/zig.json b/snippets/zig.json index 19a72c4b..575380f3 100644 --- a/snippets/zig.json +++ b/snippets/zig.json @@ -21,5 +21,16 @@ "b.installArtifact(exe);" ], "description": "Building an exe" - } + }, + "if/else": { + "prefix": "ifelse", + "body": [ + "if (${1:condition}) {", + " ${2:pass}", + "} else {", + " ${3:pass}", + "}" + ], + "description": "if / else statement" + } } From b9f3f5a97dc5a2e7aae68c3f4423726c0b589d10 Mon Sep 17 00:00:00 2001 From: Jost Alemann Date: Thu, 20 Mar 2025 20:40:54 +0100 Subject: [PATCH 2/7] zig: add basic snippets add snippets for: if statement error handling while loop while loop with continue expression for loop for loop with index function switch enum enum with type specification struct struct with defaults --- snippets/zig.json | 94 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/snippets/zig.json b/snippets/zig.json index 575380f3..a05564af 100644 --- a/snippets/zig.json +++ b/snippets/zig.json @@ -32,5 +32,99 @@ "}" ], "description": "if / else statement" + }, + "if error handling": { + "prefix": "iferr", + "body": [ + "if (${1:condition}) |${2}| {", + " ${3:pass}", + "} else |${4:err}| switch (${4:err}) {", + " ${5:err_value} => ${6:action}", + "}" + ], + "description": "if statement error handling" + }, + "while": { + "prefix": "while", + "body": [ + "while (${1:condition}) {", + " ${2:pass}", + "}" + ], + "description": "while loop" + }, + "while w/ continue expr": { + "prefix": "whilec", + "body": [ + "while (${1:condition}) : (${2:continue_exp}) {", + " ${3:pass}", + "}" + ], + "description": "while loop with continue expression" + }, + "for": { + "prefix": "for", + "body": [ + "for (${1:iterable}) |${2:item}| {", + " ${3:pass}", + "}" + ], + "description": "for loop" + }, + "for w/ index": { + "prefix": "fori", + "body": [ + "for (${1:iterable}, ${2:ind_range}) |${3}, ${4}| {", + " ${5:pass}", + "}" + ], + "description": "for loop with index" + }, + "function": { + "prefix": "fn", + "body": [ + "fn ${1:fn_name}(${2:args}) ${3:return_type}", + " return ${4:pass}", + "}" + ], + "description": "function" + }, + "switch": { + "prefix": "switch", + "body": [ + "switch (${1:expr}) {", + " ${2:value} => {${3:action}},", + " else => {${4:action}},", + "}" + ], + "description": "switch" + }, + "enum": { + "prefix": "enum", + "body": [ + "const ${1:Name} = enum { ${2:named_values} };" + ], + "description": "enum" + }, + "enum w/ type": { + "prefix": "tenum", + "body": [ + "const ${1:Name}(${2:type}) = enum { ${3:named_values} };" + ], + "description": "enum with type specification" + }, + "struct": { + "prefix": "struct", + "body": [ + "const ${1:Name} = struct { ${2:named_field}: ${3:field_type} };" + ], + "description": "struct" + }, + "struct w/ defaults": { + "prefix": "structd", + "body": [ + "const ${1:Name} = struct { ${2:named_field}: ${3:field_type} = ${4:default_value} };" + ], + "description": "struct with defaults" } } From 317687ef9b7e37bc4b03905572dee21011af3c10 Mon Sep 17 00:00:00 2001 From: Jost Alemann Date: Thu, 20 Mar 2025 20:49:07 +0100 Subject: [PATCH 3/7] specify placeholders --- snippets/zig.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/zig.json b/snippets/zig.json index a05564af..dbb48c31 100644 --- a/snippets/zig.json +++ b/snippets/zig.json @@ -36,7 +36,7 @@ "if error handling": { "prefix": "iferr", "body": [ - "if (${1:condition}) |${2}| {", + "if (${1:condition}) |${2:value}| {", " ${3:pass}", "} else |${4:err}| switch (${4:err}) {", " ${5:err_value} => ${6:action}", @@ -74,7 +74,7 @@ "for w/ index": { "prefix": "fori", "body": [ - "for (${1:iterable}, ${2:ind_range}) |${3}, ${4}| {", + "for (${1:iterable}, ${2:ind_range}) |${3:value}, ${4:index}| {", " ${5:pass}", "}" ], From 22e6e2397ff6b3f0453b7819462a0081c1f78f78 Mon Sep 17 00:00:00 2001 From: Jost Alemann Date: Thu, 20 Mar 2025 20:58:36 +0100 Subject: [PATCH 4/7] zig: fix function --- snippets/zig.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/zig.json b/snippets/zig.json index dbb48c31..6ff12dd5 100644 --- a/snippets/zig.json +++ b/snippets/zig.json @@ -81,10 +81,10 @@ "description": "for loop with index" }, "function": { - "prefix": "fn", + "prefix": "func", "body": [ - "fn ${1:fn_name}(${2:args}) ${3:return_type}", - " return ${4:pass}", + "fn ${1:fn_name}(${2:args}) ${3:return_type} {", + " return ${4:value}", "}" ], "description": "function" From 16d34e2598b8e06c83fcd52945687f3f5be1f06c Mon Sep 17 00:00:00 2001 From: Jost Alemann Date: Fri, 21 Mar 2025 00:25:32 +0100 Subject: [PATCH 5/7] zig: add error, union, tagged union --- snippets/zig.json | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/snippets/zig.json b/snippets/zig.json index 6ff12dd5..fe5bec3a 100644 --- a/snippets/zig.json +++ b/snippets/zig.json @@ -89,6 +89,13 @@ ], "description": "function" }, + "error": { + "prefix": "error", + "body": [ + "const ${1:Name} = error { ${2:named_errors} };" + ], + "description": "error set" + }, "switch": { "prefix": "switch", "body": [ @@ -126,5 +133,19 @@ "const ${1:Name} = struct { ${2:named_field}: ${3:field_type} = ${4:default_value} };" ], "description": "struct with defaults" + }, + "union": { + "prefix": "union", + "body": [ + "const ${1:Name} = union { ${2:named_field}: ${3:field_type} };" + ], + "description": "union" + }, + "tagged union": { + "prefix": "tagunion", + "body": [ + "const ${1:Name} = union(${2:TagEnum}) { ${3:named_field}: ${4:field_type} };" + ], + "description": "tagged union" } } From 0db904e12dea538d559055ac9b003a9006e69729 Mon Sep 17 00:00:00 2001 From: Jost Alemann Date: Sun, 30 Mar 2025 14:55:48 +0200 Subject: [PATCH 6/7] zig: add allocators add: - page allocator - fixed buffer allocator - debug allocator --- snippets/zig.json | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/snippets/zig.json b/snippets/zig.json index fe5bec3a..cb90c749 100644 --- a/snippets/zig.json +++ b/snippets/zig.json @@ -147,5 +147,36 @@ "const ${1:Name} = union(${2:TagEnum}) { ${3:named_field}: ${4:field_type} };" ], "description": "tagged union" + }, + "page_allocator": { + "prefix": "page_allocator", + "body": [ + "const ${1:allocator} = std.heap.page_allocator;", + "const ${2:memory} = try ${1:allocator}.alloc(u8, ${3:size});", + "defer ${1:allocator}.free(${2:memory});" + ], + "description": "page allocator" + }, + "FixedBufferAllocator": { + "prefix": "FixedBufferAllocator", + "body": [ + "var ${1:buffer}: [${2:size}]u8 = undefined;", + "var ${3:fba} = std.heap.FixedBufferAllocator.init(&${1:buffer});", + "const ${4:allocator} = ${3:fba}.allocator();", + "const ${5:memory} = try ${4:allocator}.alloc(u8, ${6:size});", + "defer ${4:allocator}.free(${5:memory});" + ], + "description": "fixed buffer allocator" + }, + "DebugAllocator": { + "prefix": "DebugAllocator", + "body": [ + "var ${1:gpa} = std.heap.DebugAllocator(.{}).init;", + "const ${2:allocator} = ${1:gpa}.allocator();", + "defer _ = ${1:gpa}.deinit();", + "const ${3:memory} = try ${2:allocator}.alloc(u8, ${4:size});", + "defer ${2:allocator}.free(${3:memory});" + ], + "description": "debug allocator" } } From 7e4f580229293a2c3cd2915a59767d16f808fa86 Mon Sep 17 00:00:00 2001 From: Jost Alemann Date: Fri, 16 May 2025 12:08:45 +0200 Subject: [PATCH 7/7] zig: merge struct and struct w/ defaults --- snippets/zig.json | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/snippets/zig.json b/snippets/zig.json index cb90c749..91240e4e 100644 --- a/snippets/zig.json +++ b/snippets/zig.json @@ -123,16 +123,9 @@ "struct": { "prefix": "struct", "body": [ - "const ${1:Name} = struct { ${2:named_field}: ${3:field_type} };" + "const ${1:Name} = struct { ${2:named_field}: ${3:field_type} ${0: = default_or_end} };" ], - "description": "struct" - }, - "struct w/ defaults": { - "prefix": "structd", - "body": [ - "const ${1:Name} = struct { ${2:named_field}: ${3:field_type} = ${4:default_value} };" - ], - "description": "struct with defaults" + "description": "struct with optional default value" }, "union": { "prefix": "union",