Skip to content

Instantly share code, notes, and snippets.

@aifrak
Last active February 15, 2025 06:44
Show Gist options
  • Select an option

  • Save aifrak/56d75e001d20bf0d309fcb241083cd38 to your computer and use it in GitHub Desktop.

Select an option

Save aifrak/56d75e001d20bf0d309fcb241083cd38 to your computer and use it in GitHub Desktop.
VSCode Elixir snippets
{
"Loops through recursion": {
"prefix": "rec",
"body": [
"def ${1:recursion}(${2:list}) do",
" ${1:recursion}(${2:list}, ${3:[]})",
"end",
"",
"def ${1:recursion}([${4:head} | ${5:tail}], ${6:acc}) do",
" ${0:CODE}",
" ${1:recursion}(${5:tail}, [${4:head} | ${6:acc}])",
"end",
"",
"def ${1:recursion}(${3:[]}, ${6:acc}), do: Enum.reverse(${6:acc})"
],
"description": "Loops through recursion"
},
"Loops through recursion with a function": {
"prefix": "recf",
"body": [
"def ${1:recursion}(${2:list}, ${3:fun}) do",
" ${1:recursion}(${2:list}, ${4:[]}, ${3:fun})",
"end",
"",
"def ${1:recursion}([${5:head} | ${6:tail}], ${7:acc}, ${3:fun}) do",
" ${0:CODE}",
" ${1:recursion}(${6:tail}, [${5:head} | ${7:acc}])",
"end",
"",
"def ${1:recursion}(${4:[]}, ${7:acc}, _${3:fun}), do: Enum.reverse(${7:acc})"
],
"description": "Loops through recursion with a function"
},
"Anonymous function.": {
"prefix": "funanon",
"body": [
"${1:parent_function}(fn -> ${2:function} end)"
],
"description": "Anonymous function."
},
"Module, function, argumants (MFA)": {
"prefix": "funmfa",
"body": [
"${1:parent_function}(${2:module}, :${3:function_name}, [${4:arguments}])"
],
"description": "Module, function, argumants (MFA)"
},
"dfp": {
"prefix": "dfp",
"body": "defp $1, do: $0",
"description": "defp (one line)"
},
"noreply": {
"prefix": "noreply",
"body": "{:noreply, $1}",
"description": "{:noreply, ... }"
},
// BEGIN: GenServer
"GenServer init (Client + Callback)": {
"prefix": "gs_init",
"body": [
"def start_link(${1:args}) do",
" GenServer.start_link(__MODULE__, ${1:args})",
"end",
"",
"@impl GenServer",
"def init(${1:args}) do",
" ${0:CODE}",
" {:ok, ${1:args}}",
"end\""
],
"description": "GenServer init (Client + Callback)"
},
"GenServer handle_call (Client + Callback)": {
"prefix": "gs_hcall",
"body": [
"def ${1:client_function}(${2:args}) do",
" GenServer.call(__MODULE__, ${3:request})",
"end",
"",
"@impl GenServer",
"def handle_call(${3:request}, ${4:from}, ${5:state}) do",
" ${0:CODE}",
" {:reply, ${6:reply}, ${5:state}}",
"end"
],
"description": "GenServer handle_call (Client + Callback)"
},
"GenServer handle_cast (Client + Callback)": {
"prefix": "gs_hcast",
"body": [
"def ${1:client_function}(${2:args}) do",
" GenServer.cast(__MODULE__, ${3:request})",
"end",
"",
"@impl GenServer",
"def handle_cast(${3:request}, ${4:state}) do",
" ${0:CODE}",
" {:noreply, ${4:state}}",
"end"
],
"description": "GenServer handle_cast (Client + Callback)"
},
"GenServer handle_info (Callback)": {
"prefix": "gs_hinfo",
"body": [
"@impl GenServer",
"def handle_info(${1:message}, ${2:state}) do",
" ${0:CODE}",
" {:noreply, ${2:state}}",
"end"
],
"description": "GenServer handle_cast (Client + Callback)"
},
// END: GenServer
// BEGIN: Eex
// "eex_render_block": {
// "prefix": ["e=", "eex_rende"],
// "body": ["<%= $1 %>"],
// "description": "<%= %> render block"
// },
// "eex_exec_block": {
// "prefix": ["ee", "e-", "eex_exec"],
// "body": ["<% $1 %>"],
// "description": "<% %> exec block"
// },
// "eex_comment_block": {
// "prefix": ["eex_comment", "e#"],
// "body": ["<%# $1 %>"],
// "description": "<%# %> comment block"
// },
// "eex_end_tag": {
// "prefix": "eex_end",
// "body": ["<% end %>$1"],
// "description": "<% end %> end tag"
// },
// "efor": {
// "prefix": "eex_for",
// "body": ["<%= for ${1:item} <- @$1s do %>", " $2", "<% end %>"],
// "description": "eex for"
// },
// "eex_if": {
// "prefix": "eex_if",
// "body": ["<%= if $1 do %>", " $2", "<% end %>"],
// "description": "eex if"
// },
// "eex_if_else": {
// "prefix": "eex_ife",
// "body": ["<%= if $1 do %>", " $2", "<% else %>", " $3", "<% end %>"],
// "description": "eex if else"
// },
// "eex_else": {
// "prefix": "eex_else",
// "body": ["<% else %>"],
// "description": "eex else"
// },
// "eex_cond": {
// "prefix": "eex_cond",
// "body": [
// "<%= cond do %>",
// " <% $1 -> %>",
// " $2",
// " <% true -> %>",
// " $3",
// "<% end %>"
// ],
// "description": "eex cond"
// },
// "eex_unless": {
// "prefix": "eex_unless",
// "body": ["<%= unless $1 do %>", " $2", "<% end %>"],
// "description": "eex unless"
// },
// "eex_form_for": {
// "prefix": "eex_ff",
// "body": [
// "<%= form_for @${1:changeset}, ${2:url}, ${3:[]}, fn f -> %>",
// " $4",
// "<% end %>"
// ],
// "description": "eex form_for"
// },
// "eex_error_tag": {
// "prefix": "eex_err",
// "body": ["<%= error_tag ${1:f}, :${2:field} %>"],
// "description": "eex form error tag"
// },
// "eex_text_input": {
// "prefix": "eex_ti",
// "body": ["<%= text_input ${1:f}, :${2:field} %>"],
// "description": "eex form text input"
// },
// "eex_label": {
// "prefix": "eex_la",
// "body": ["<%= label ${1:f}, :${2:field}, \"${3:Text}\" %>"],
// "description": "eex form label"
// },
// "eex_submit": {
// "prefix": "eex_esubm",
// "body": ["<%= submit ${1:Text} %>"],
// "description": "eex form submit"
// },
// "eex_submit_c": {
// "prefix": "eex_submc",
// "body": ["<%= submit ${1:Text}, class: \"${3:btn btn-primary}\" %>"],
// "description": "eex form submit with class"
// },
// "eex_password_input": {
// "prefix": "eex_pass",
// "body": ["<%= password_input ${1:f}, :${2:field} %>"],
// "description": "eex form password input"
// },
// "eex_link": {
// "prefix": "elt",
// "body": ["<%= link \"${1:text}\", to: ${2:url} %>"],
// "description": "eex link"
// },
// "eex_link_block": {
// "prefix": "eex_ltb",
// "body": ["<%= link to: ${1:url} do %>", "\t${2:text}", "<% end %>"],
// "description": "eex link block"
// },
// "eex_render": {
// "prefix": "eex_render",
// "body": ["<%= render \"${1:partial}.html\", ${2:local_var: @local} %>"],
// "description": "eex render"
// },
// END: Eex
// BEGIN: LiveView
"LiveView module": {
"prefix": "lv_mod",
"body": [
"defmodule ${WORKSPACE_NAME/(.*)/${1:/pascalcase}/g}Web.${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/g} do",
" use ${WORKSPACE_NAME/(.*)/${1:/pascalcase}/g}Web, :live_view",
" $0",
"end"
],
"description": "LiveView module"
},
"LiveComponent module": {
"prefix": "lv_comp",
"body": [
"defmodule ${WORKSPACE_NAME/(.*)/${1:/pascalcase}/g}Web.${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/g} do",
" use ${WORKSPACE_NAME/(.*)/${1:/pascalcase}/g}Web, :live_component",
" $0",
"end"
],
"description": "LiveComponent module"
},
"LiveView mount/3": {
"prefix": "lv_mount",
"body": [
"def mount(_params, _session, socket) do",
" socket = assign(socket, ${1:key}: ${2:value})",
" {:ok, socket}",
"end"
],
"description": "LiveView mount/3"
},
"LiveView render/1": {
"prefix": "lv_render",
"body": [
"def render(assigns) do",
" ~H\"\"\"",
" ${0}",
" \"\"\"",
"end"
],
"description": "LiveView render/1"
},
"LiveView handle_event/3": {
"prefix": "lv_hevent",
"body": [
"def handle_event(${1:event}, _, socket) do",
" {:noreply, socket}",
"end"
],
"description": "LiveView handle_event/3"
},
"LiveView handle_event/3 with assign": {
"prefix": "lv_hevent_assign",
"body": [
"def handle_event(${1:event}, _, socket) do",
" socket = assign(socket, ${2:key}: ${3:value})",
" {:noreply, socket}",
"end"
],
"description": "LiveView handle_event/3 with assign"
},
"LiveView handle_info/2": {
"prefix": "lv_hinfo",
"body": [
"def handle_info(${1:message}, socket) do",
" {:noreply, socket}",
"end"
],
"description": "LiveView handle_info/2"
},
"LiveView handle_info/2 with assign": {
"prefix": "lv_hinfo_assign",
"body": [
"def handle_info(${1:message}, socket) do",
" socket = assign(socket, ${2:key}: ${3:value})",
" {:noreply, socket}",
"end"
],
"description": "LiveView handle_info/2 with assign"
},
"LiveView handle_params/3": {
"prefix": "lv_hparams",
"body": [
"def handle_params(${1:params}, _uri, socket) do",
" {:noreply, socket}",
"end"
],
"description": "LiveView handle_params/3"
},
"LiveView assign/2": {
"prefix": "lv_assign",
"body": [
"socket = assign(socket, ${1:key}: ${2:value})"
],
"description": "LiveView assign/2"
},
"LiveView update/3": {
"prefix": "lv_update",
"body": [
"socket = update(socket, :${1:key}, &(${2:captured_function}))"
],
"description": "LiveView update/3"
},
"LiveView attribute": {
"prefix": "lv_attr",
"body": [
"attr :${1:name}, ${2|:any,:string,:atom,:boolean,:integer,:float,:list,:map|}, required: ${3|true,false|}, default: ${4:nil}, values: ${5:[\"Paul\", \"John\"]}, examples: ${6:[\"Paul\", \"John\"]}, doc: ${7:\"This is the name field.\"}"
],
// https://hexdocs.pm/phoenix_live_view/Phoenix.Component.html#module-attributes
"description": "LiveView attr"
},
"LiveView global attribute": {
"prefix": "lv_attr_global",
"body": [
"attr :${1:rest}, :${2:global}, default: ${3:%{class: \"bg-blue-200\"}}, include: ${4:~w(form)}, examples: ${5:%{class: \"bg-blue-200\"}}, doc: ${6:\"Additional HTML attributes.\"}"
],
"description": "LiveView global attribute"
// see: https://hexdocs.pm/phoenix_live_view/Phoenix.Component.html#module-global-attributes
// see: https://hexdocs.pm/phoenix_live_view/Phoenix.Component.html#module-included-globals
// see: https://hexdocs.pm/phoenix_live_view/Phoenix.Component.html#module-custom-global-attribute-prefixes
},
"LiveView slot": {
"prefix": "lv_slot",
"body": [
"slot :${1:inner_block}, required: ${2:true}, doc: ${3:\"The content rendered inside the component.\"}"
],
"description": "LiveView slot"
// see: https://hexdocs.pm/phoenix_live_view/Phoenix.Component.html#module-slots
},
"LiveView slot attributes": {
"prefix": "lv_slot_attr",
"body": [
"slot :${1:column}, required: ${3|true,false|}, doc: ${4:\"Columns with column labels\"} do",
" attr :${5:label}, ${2|:any,:string,:atom,:boolean,:integer,:float,:list,:map|}, required: ${7|true,false|}, doc: ${8:\"Column label\"}",
"end"
],
"description": "LiveView slot attributes"
// see: https://hexdocs.pm/phoenix_live_view/Phoenix.Component.html#module-slot-attributes
}
// END: LiveView
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment