Skip to content

Instantly share code, notes, and snippets.

@totaltrash
Last active July 18, 2022 04:28
Show Gist options
  • Save totaltrash/3e40674af74937d87ebfaf66672470f2 to your computer and use it in GitHub Desktop.
Save totaltrash/3e40674af74937d87ebfaf66672470f2 to your computer and use it in GitHub Desktop.
Elixir snippets
// See https://github.com/florinpatrascu/vscode-elixir-snippets/blob/master/snippets/snippets.json for more
{
"lv_mount": {
"prefix": "mount_lv_plain",
"body": [
"@impl true",
"def mount(_params, _session, socket) do",
" socket = assign(socket, ${1:key}: ${2:value})",
" {:ok, socket}",
"end"
],
"description": "LiveView mount function"
},
"lv_mount_page": {
"prefix": "mount_lv_page",
"body": [
"@impl true",
"def mount(_params, _session, socket) do",
" socket =",
" socket",
" |> assign(page_title: \"$1\")$0",
"",
" {:ok, socket}",
"end"
],
"description": "Page LiveView mount function"
},
"lv_render": {
"prefix": "render",
"body": [
"@impl true",
"def render(assigns) do",
" ~H\"\"\"",
" $0",
" \"\"\"",
"end"
],
"description": "LiveView render function"
},
"lv_functional_component": {
"prefix": "functional_component",
"body": [
"def $1(assigns) do",
" ~H\"\"\"",
" $0",
" \"\"\"",
"end"
],
"description": "LiveView functional component"
},
"lv_handle_event": {
"prefix": "handle_event",
"body": [
"@impl true",
"def handle_event(${1:event}, %{\"item\" => _item} = _params, socket) do",
" socket =",
" socket",
" |> assign($1: \"3\")$0",
"",
" {:noreply, socket}",
"end"
],
"description": "LiveView handle_event function"
},
"lv_handle_params": {
"prefix": "handle_params",
"body": [
"@impl true",
"def handle_params(_params, _url, socket) do",
" {:noreply, socket}",
"end"
],
"description": "LiveView handle_params function"
},
"lv_handle_params_apply_action": {
"prefix": "handle_params_apply_action",
"body": [
"@impl true",
"def handle_params(params, _url, socket) do",
" {:noreply, apply_action(socket, params, socket.assigns.live_action)}",
"end"
],
"description": "LiveView handle_params function"
},
"lv_apply_action_with_params": {
"prefix": "apply_action_with_params",
"body": [
"defp apply_action(socket, %{\"id\" => id}, :${1:action}) do",
" socket",
" |> assign(${2:key}: id)",
"end"
],
"description": "Apply action function with params"
},
"lv_apply_action_no_params": {
"prefix": "apply_action_no_params",
"body": [
"defp apply_action(socket, _params, :${1:action}) do",
" socket",
" |> assign(${2:key}: ${3:value})",
"end"
],
"description": "Apply action function no params"
},
"variables_test": {
"prefix": "variables_test",
"body": [
"# Standard variables",
"# ----------------------------------------------------",
"# \\$TM_FILENAME: $TM_FILENAME",
"# \\$TM_FILENAME_BASE: $TM_FILENAME_BASE",
"# \\$TM_DIRECTORY: $TM_DIRECTORY",
"# \\$TM_FILEPATH: $TM_FILEPATH",
"# \\$RELATIVE_FILEPATH: $RELATIVE_FILEPATH",
"#",
"# Transformed variables",
"# ----------------------------------------------------",
"# Full module name: ${TM_DIRECTORY/(.*lib\\/)|(\\/)|([a-z_]+)/${3:/pascalcase}${2:+.}/g}.${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/}",
"# Full module name, dropping the Live element from the path:",
"# ${TM_DIRECTORY/(.*lib\\/)|(\\/)|(live\\/)|([a-z_]+)/${4:/pascalcase}${2:+.}/g}.${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/}",
"# Getting the base directory (MyApp, or MyAppWeb):",
"# ${TM_DIRECTORY/(.*lib\\/)|([a-z_]+)|(.*)/${2:/pascalcase}/g}",
"# Assigning transformations as default to tabstop \\$1:",
"# ${1:${TM_DIRECTORY/(.*lib\\/)|([a-z_]+)|(.*)/${2:/pascalcase}/g}}",
"# And printing \\$1: $1",
],
"description": "Testing built in snippet variables and calculating module path"
},
"transformer_test_pascal_case": {
"prefix": "transformer_test_pascal_case",
"body": [
"# Enter a string here in PascalCase",
"# Then tab through to set all variables",
"# ",
"# $1",
"# -------------------------------------------------------",
"# PascalCase: $1",
"# snake_case: ${2:${1/([A-Z][a-z]+$)|([A-Z][a-z]+)/${1:/downcase}${2:/downcase}${2:+_}/g}}",
"# lower case: ${3:${1/([A-Z][a-z]+$)|([A-Z][a-z]+)/${1:/downcase}${2:/downcase}${2:+ }/g}}",
"# Capitalize All: ${4:${1/([A-Z][a-z]+$)|([A-Z][a-z]+)/${1:/capitalize}${2:/capitalize}${2:+ }/g}}",
"# Capitalize first: ${5:${1/(^[A-Z][a-z]+)|([A-Z][a-z]+)/${1:/capitalize}${2:+ }${2:/downcase}/g}}",
"# ",
"# For a SomeResource resource, we need: ",
"# SomeResource: $1",
"# some_resource: $2",
"# some resource: $3",
"# Some Resource: $4",
"# Some resource: $5"
],
"description": "Testing snippet transformers to transform PascalCase"
},
"transformer_test_sentence_case": {
"prefix": "transformer_test_sentence_case",
"body": [
"# Enter a string here...",
"# - in lower case",
"# - with spaces to delineate words",
"# Then tab through to set all variables",
"# ",
"# ${1}",
"# -------------------------------------------------------",
"# raw sentence: ${1}",
"# PascalCase: ${2:${1/(.*)/${1:/pascalcase}/}}",
"# lower case: ${3:${1/(.*)/${1:/downcase}/}}",
"# snake_case: ${4:${1/[\" \"]/_/g}}",
"# Capitalize first: ${5:${1/(.*)/${1:/capitalize}/}}",
"# Capitalize All: ${6:${1/\\b(\\w)/${1:/upcase}/g}}",
"# ",
"# For a SomeResource resource, we need: ",
"# SomeResource: $2",
"# some resource: $3",
"# some_resource: $4",
"# Some resource: $5",
"# Some Resource: $6"
],
"description": "Testing snippet transformers to transform sentence case"
},
"lv_live_view_skeleton": {
"prefix": "live_view_skeleton",
"body": [
"defmodule ${1:${TM_DIRECTORY/(.*lib\\/)|(\\/)|(live\\/)|([a-z_]+)/${4:/pascalcase}${2:+.}/g}.${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/}} do",
" use ${2:${TM_DIRECTORY/(.*lib\\/)|([a-z_]+)|(.*)/${2:/pascalcase}/g}}, :live_view",
"",
" import IFix.Components",
"",
" $0",
"",
"end",
],
"description": "LiveView module skeleton"
},
"resource": {
"prefix": "resource",
"body": [
"defmodule $1 do",
" use Ash.Resource, data_layer: AshPostgres.DataLayer",
"",
" postgres do",
" table \"$2\"",
" repo ${TM_DIRECTORY/(.*lib\\/)|([a-z_]+)|(.*)/${2:/pascalcase}/g}.Repo",
" end",
"",
" actions do",
" defaults [:create, :read, :update, :destroy]",
" end",
"",
" attributes do",
" uuid_primary_key :id",
"",
" attribute :name, :string do",
" allow_nil? false",
" end",
"",
" attribute :enabled, :boolean do",
" default true",
" end",
"",
" create_timestamp :created_date",
" update_timestamp :updated_date",
" end",
"end",
],
"description": "LiveView module skeleton"
},
"lv_live_view_router": {
"prefix": "live_view_router",
"body": [
"live \"/$1\", $2Live, :index",
"live \"/$1/new\", $2Live, :new",
"live \"/$1/:id\", $2Live, :show",
"live \"/$1/:id/edit\", $2Live, :edit",
"live \"/$1/:id/delete\", $2Live, :delete",
],
"description": "LiveView router routes"
},
"lv_live_view_crud_test": {
"prefix": "live_view_crud_feature_test",
"body": [
"# Enter everything in PascalCase:",
"# - the resource name for the LiveView: $1",
"# ",
"# Then tab through to set all variables:",
"# -------------------------------------------------------",
"# Resource naming (ensure casing is correct):",
"# PascalCase: $1",
"# snake_case: ${2:${1/([A-Z][a-z]+$)|([A-Z][a-z]+)/${1:/downcase}${2:/downcase}${2:+_}/g}}",
"# lower case: ${3:${1/([A-Z][a-z]+$)|([A-Z][a-z]+)/${1:/downcase}${2:/downcase}${2:+ }/g}}",
"# Capitalize All: ${4:${1/([A-Z][a-z]+$)|([A-Z][a-z]+)/${1:/capitalize}${2:/capitalize}${2:+ }/g}}",
"# Capitalize first: ${5:${1/(^[A-Z][a-z]+)|([A-Z][a-z]+)/${1:/capitalize}${2:+ }${2:/downcase}/g}}",
"# -------------------------------------------------------",
"$0defmodule Test.Feature.${TM_DIRECTORY/(.*features\\/)|(\\/)|([a-z_]+)/${3:/pascalcase}${2:+.}/g}.${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/} do",
" use Test.FeatureCase",
"",
" @moduletag user: %{roles: []}",
" @moduletag :wip",
"",
" @item %{",
" name: \"Some $4\",",
" enabled: true",
" }",
"",
" feature \"resource lifecycle\", %{session: session} do",
" update_item = %{name: \"New $4\"}",
" updated = Map.merge(@item, update_item)",
"",
" session",
" # CREATE",
" |> click(admin_menu_button())",
" |> click(menu_link())",
" |> click(add_button())",
" |> fill_form(@item)",
" |> click(save_button())",
" |> assert_created(@item)",
"",
" # READ",
" |> visit(index_route())",
" |> assert_listed(@item)",
" |> click(show_link(@item))",
" |> assert_showing(@item)",
"",
" # UPDATE",
" |> click(edit_button())",
" |> fill_form(update_item)",
" |> click(save_button())",
" |> assert_updated(updated)",
"",
" # DELETE",
" |> click(delete_button())",
" |> click(confirm_delete_button())",
" |> assert_deleted(updated)",
" end",
"",
" describe \"validations\" do",
" feature \"fields are required\", %{session: session} do",
" session",
" |> visit(add_route())",
" |> click(save_button())",
" |> assert_form_error(\"Required field\")",
" end",
" end",
"",
" defp index_route(), do: Routes.$2_path(@endpoint, :index)",
" defp add_route(), do: Routes.$2_path(@endpoint, :new)",
" # defp show_route(item), do: Routes.$2_path(@endpoint, :show, item.id)",
" # defp edit_route(item), do: Routes.$2_path(@endpoint, :edit, item.id)",
" defp admin_menu_button, do: Query.css(\"#navbar-admin-dropdown-button\")",
" defp menu_link, do: Query.link(\"$4s\")",
" defp show_link(item), do: Query.link(item.name)",
" defp add_button(), do: Query.link(\"Add New\")",
" defp save_button(), do: Query.button(\"Save\")",
" defp edit_button(), do: Query.link(\"Edit\")",
" defp delete_button(), do: Query.link(\"Delete\")",
" defp confirm_delete_button(), do: Query.button(\"Confirm, Delete\")",
"",
" defp fill_form(session, attributes) do",
" for {attribute, value} <- attributes do",
" case attribute do",
" :enabled ->",
" fill_checkbox(session, field_for_attribute(attribute), value)",
"",
" _ ->",
" fill_field(session, field_for_attribute(attribute), value)",
" end",
" end",
"",
" session",
" end",
"",
" def field_for_attribute(:name), do: \"Name\"",
" def field_for_attribute(:enabled), do: \"Enabled\"",
"",
" defp assert_created(session, item) do",
" assert_flash_message(session, \"$5 successfully created\")",
" assert_showing(session, item)",
" session",
" end",
"",
" defp assert_updated(session, item) do",
" assert_flash_message(session, \"$5 successfully updated\")",
" assert_showing(session, item)",
" session",
" end",
"",
" defp assert_deleted(session, item) do",
" assert_flash_message(session, \"$5 successfully deleted\")",
" refute_listed(session, item)",
" session",
" end",
"",
" defp assert_listed(session, item) do",
" assert_has(session, Query.css(\"td\", text: item.name))",
" session",
" end",
"",
" defp refute_listed(session, item) do",
" assert_has(session, Query.css(\"td\", text: item.name, count: 0))",
" session",
" end",
"",
" defp assert_showing(session, item) do",
" assert_has(session, Query.css(\"dd\", text: item.name))",
" session",
" end",
"end",
],
"description": "LiveView feature test"
},
"lv_live_view_crud": {
"prefix": "live_view_crud",
"body": [
"# Enter everything in PascalCase:",
"# - the root application module name: $1",
"# - the api name: $2",
"# - the resource name for the LiveView: $3",
"# ",
"# Then tab through to set all variables:",
"# -------------------------------------------------------",
"# Resource naming (ensure casing is correct):",
"# PascalCase: $3",
"# snake_case: ${4:${3/([A-Z][a-z]+$)|([A-Z][a-z]+)/${1:/downcase}${2:/downcase}${2:+_}/g}}",
"# lower case: ${5:${3/([A-Z][a-z]+$)|([A-Z][a-z]+)/${1:/downcase}${2:/downcase}${2:+ }/g}}",
"# Capitalize All: ${6:${3/([A-Z][a-z]+$)|([A-Z][a-z]+)/${1:/capitalize}${2:/capitalize}${2:+ }/g}}",
"# Capitalize first: ${7:${3/(^[A-Z][a-z]+)|([A-Z][a-z]+)/${1:/capitalize}${2:+ }${2:/downcase}/g}}",
"# -------------------------------------------------------",
"$0defmodule $1Web.$3Live do",
" use $1Web, :live_view",
"",
" import IFix.Components",
"",
" @api $1.$2",
" @resource $1.$2.$3",
"",
" @impl true",
" def render(assigns) do",
" ~H\"\"\"",
" <%= if @display == :index do %>",
" <.page_layout title=\"$6s\">",
" <:toolbar>",
" <.toolbar_button label=\"Add New\" type=\"link\" icon=\"plus\" href={router_path(@socket, :new)} />",
" </:toolbar>",
" <.data_table items={@items} class=\"mb-8\">",
" <:col let={item} label=\"Name\">",
" <.link href={router_path(@socket, :show, item.id)}>",
" <%= item.name %>",
" </.link>",
" </:col>",
" <:col let={item} label=\"Enabled\">",
" <%= item.enabled %>",
" </:col>",
" </.data_table>",
"",
" <%= if @live_action == :new do %>",
" <.modal",
" heading=\"Add $6\"",
" let={f}",
" form={@form}",
" form_submit=\"form_submit\"",
" icon=\"pencil\"",
" >",
" <.form_fields form={f} />",
" <:buttons>",
" <.modal_button label=\"Save\" color=\"primary\" type=\"submit\" />",
" <.modal_button",
" label=\"Cancel\"",
" color=\"default\"",
" type=\"link\"",
" href={router_path(@socket, :index)}",
" />",
" </:buttons>",
" </.modal>",
" <% end %>",
" </.page_layout>",
" <% end %>",
"",
" <%= if @display == :show_details do %>",
" <.page_layout title=\"$6\" sub_title={@selected_item.name}>",
" <:toolbar>",
" <.toolbar_button",
" label=\"Edit\"",
" icon=\"pencil\"",
" type=\"link\"",
" color=\"primary\"",
" href={router_path(@socket, :edit, @selected_item.id)}",
" />",
" <.toolbar_button_group label=\"More\" icon=\"cog\">",
" <:item",
" label=\"Delete\"",
" icon=\"trash\"",
" type=\"link\"",
" color=\"danger\"",
" href={router_path(@socket, :delete, @selected_item.id)}",
" />",
" </.toolbar_button_group>",
" <.toolbar_button",
" label=\"Back\"",
" icon=\"chevron-left\"",
" type=\"link\"",
" href={router_path(@socket, :index)}",
" />",
" </:toolbar>",
" <.item_detail_nav current=\"details\" item={@selected_item} />",
" <.item_detail item={@selected_item} />",
"",
" <%= if @live_action == :edit do %>",
" <.modal",
" heading=\"Edit $6\"",
" let={f}",
" form={@form}",
" form_submit=\"form_submit\"",
" icon=\"pencil\"",
" >",
" <.form_fields form={f} />",
" <:buttons>",
" <.modal_button label=\"Save\" color=\"primary\" type=\"submit\" />",
" <.modal_button",
" label=\"Cancel\"",
" color=\"default\"",
" type=\"link\"",
" href={router_path(@socket, :show, @selected_item.id)}",
" />",
" </:buttons>",
" </.modal>",
" <% end %>",
"",
" <%= if @live_action == :delete do %>",
" <.modal heading=\"Delete $6\" icon=\"trash\" icon_color=\"bg-red-100 text-red-600\">",
" <.item_detail item={@selected_item} />",
" <:buttons>",
" <.modal_button label=\"Confirm, Delete\" color=\"danger\" phx-click=\"confirm_delete\" />",
" <.modal_button",
" label=\"Cancel\"",
" type=\"link\"",
" color=\"default\"",
" href={router_path(@socket, :show, @selected_item.id)}",
" />",
" </:buttons>",
" </.modal>",
" <% end %>",
" </.page_layout>",
" <% end %>",
" \"\"\"",
" end",
"",
" defp form_fields(assigns) do",
" ~H\"\"\"",
" <.field_row>",
" <%= label(@form, :name) %>",
" <%= text_input(@form, :name, \"phx-hook\": \"Autofocus\") %>",
" <%= error_tag(@form, :name) %>",
" </.field_row>",
" <.field_row>",
" <%= label(@form, :enabled) %>",
" <%= checkbox(@form, :enabled) %>",
" <%= error_tag(@form, :enabled) %>",
" </.field_row>",
" \"\"\"",
" end",
"",
" defp item_detail(assigns) do",
" ~H\"\"\"",
" <.detail>",
" <:item title=\"Name\" value={@item.name} /><:item title=\"Enabled\" value={@item.enabled} />",
" </.detail>",
" \"\"\"",
" end",
"",
" defp item_detail_nav(assigns) do",
" ~H\"\"\"",
" <.nav current={@current} class=\"my-4\">",
" <:nav_item id=\"details\" label=\"Details\" href={router_path($1Web.Endpoint, :show, @item.id)} />",
" </.nav>",
" \"\"\"",
" end",
"",
" @impl true",
" def handle_event(\"form_submit\", %{\"item\" => params}, socket) do",
" params = normalize_params(params, socket.assigns.form.action)",
"",
" case AshPhoenix.Form.submit(socket.assigns.form, params: params) do",
" {:ok, item} ->",
" {:noreply,",
" socket",
" |> put_flash(:info, submit_message(socket.assigns.form.action))",
" |> push_redirect(to: router_path(socket, :show, item.id))}",
"",
" {:error, form} ->",
" {:noreply, assign(socket, form: form)}",
" end",
" end",
"",
" def handle_event(\"confirm_delete\", _params, %{assigns: %{selected_item: item}} = socket) do",
" item",
" |> Ash.Changeset.for_destroy(:destroy)",
" |> @api.destroy()",
" |> case do",
" :ok ->",
" {:noreply,",
" socket",
" |> put_flash(:info, \"$7 successfully deleted\")",
" |> push_redirect(to: router_path(socket, :index))}",
"",
" _ ->",
" {:noreply,",
" socket",
" |> put_flash(:error, \"Error: unable to delete $5\")",
" |> push_redirect(to: router_path(socket, :show, item.id))}",
" end",
" end",
"",
" defp submit_message(:create), do: \"$7 successfully created\"",
" defp submit_message(:update), do: \"$7 successfully updated\"",
"",
" @impl true",
" def mount(_params, _session, socket) do",
" socket =",
" socket",
" |> assign(page_title: \"$6s\")",
" |> assign_items()",
"",
" {:ok, socket}",
" end",
"",
" defp assign_items(socket) do",
" socket",
" |> assign_new(:items, fn ->",
" @resource",
" |> Ash.Query.for_read(:read)",
" |> @api.read!()",
" end)",
" end",
"",
" @impl true",
" def handle_params(params, _url, socket) do",
" {:noreply, apply_action(socket, params, socket.assigns.live_action)}",
" end",
"",
" defp apply_action(socket, _, :index) do",
" socket",
" |> assign(display: :index)",
" |> assign(selected_item: nil)",
" |> assign(form: nil)",
" end",
"",
" defp apply_action(socket, _, :new) do",
" socket",
" |> assign(display: :index)",
" |> assign(selected_item: nil)",
" |> assign_form_for_create()",
" end",
"",
" defp apply_action(socket, %{\"id\" => id}, :show) do",
" socket",
" |> assign(display: :show_details)",
" |> assign_selected_item(id)",
" |> assign(form: nil)",
" end",
"",
" defp apply_action(socket, %{\"id\" => id}, :delete) do",
" socket",
" |> assign(display: :show_details)",
" |> assign_selected_item(id)",
" |> assign(form: nil)",
" end",
"",
" defp apply_action(socket, %{\"id\" => id}, :edit) do",
" socket",
" |> assign(display: :show_details)",
" |> assign_selected_item(id)",
" |> assign_form_for_update(:update)",
" end",
"",
" defp assign_form_for_create(socket) do",
" form =",
" AshPhoenix.Form.for_create(@resource, :create,",
" as: \"item\",",
" api: @api",
" )",
"",
" assign(socket, form: form)",
" end",
"",
" defp assign_form_for_update(socket, action) do",
" form =",
" AshPhoenix.Form.for_update(socket.assigns.selected_item, action,",
" as: \"item\",",
" api: @api",
" )",
"",
" assign(socket, form: form)",
" end",
"",
" defp assign_selected_item(%{assigns: %{selected_item: %{id: id}}} = socket, id) do",
" socket",
" end",
"",
" defp assign_selected_item(socket, id) do",
" item = @api.get!(@resource, id)",
"",
" assign(socket, selected_item: item)",
" end",
"",
" defp router_path(socket, action), do: Routes.$4_path(socket, action)",
" defp router_path(socket, action, param), do: Routes.$4_path(socket, action, param)",
"",
" # defp normalize_params(params, action) when action in [:create, :update] do",
" # ensure_params_has_collection(params, \"roles\")",
" # end",
"",
" defp normalize_params(params, _), do: params",
"end",
""
],
"description": "Big old resource crud live view"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment