From 9cac801359cde21cc77ce520b2a8873cecbf41e9 Mon Sep 17 00:00:00 2001 From: Haelnorr Date: Sun, 2 Mar 2025 11:31:32 +1100 Subject: [PATCH] Added go snippet for modelling json structs --- lua/haelnorr/snippets/go.lua | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lua/haelnorr/snippets/go.lua b/lua/haelnorr/snippets/go.lua index 56a63df..f35cd35 100644 --- a/lua/haelnorr/snippets/go.lua +++ b/lua/haelnorr/snippets/go.lua @@ -3,6 +3,7 @@ local ls = require("luasnip") local s = ls.snippet local c = ls.choice_node local d = ls.dynamic_node +local f = ls.function_node local i = ls.insert_node local t = ls.text_node local sn = ls.snippet_node @@ -124,6 +125,10 @@ local go_return_values = function(args) ) end +local camel_to_snake = function(str) + return str:gsub("([a-z])([A-Z])", "%1_%2"):lower() +end + ls.add_snippets("go", { s( "efi", @@ -164,4 +169,19 @@ func Handle() http.Handler { } ) ), + s( + "jf", + fmta( + [[ + `json:""` + ]], + { + field = i(1, "Field"), + type = i(2, "type"), + key = f(function(args) + return camel_to_snake(args[1][1]) + end, { 1 }), + } + ) + ), })