Are you familiar with Neovim, the popular text editor? If so, you may have heard of LuaSnip, a snippet engine written entirely in Lua.

I recently wanted to add my own snippet to create Sidekiq workers. During my search, I came across a YouTube playlist called LuaSnip Zero to Hero. s1n7ax does a great job explaining how to write snippets in Lua. I recommend anyone interested in writing their own Lua snippets to watch it.

Whether it’s XML or JSON-based, most snippet engines come with their own syntax definitions which are often encumbered with constraints. I have dealt with them in the past. After playing around with Lua snippets, I was amazed how flexible they are. You have to see it to believe it. So check it out.

Here’s the snippet I wrote for creating an ActiveRecord Job:

.config/nvim/snippets/rails.lua
local luasnip = require("luasnip")
local fmt = require("luasnip.extras.fmt").fmt

local s = luasnip.snippet
local t = luasnip.text_node
local c = luasnip.choice_node
local i = luasnip.insert_node

luasnip.add_snippets("rails", {
  s(
    "job",
    fmt(
      [[
      class {}Job < ActiveJob::Base
        queue_as :{}
        sidekiq_options retry: {}, backtrace: {}

        def perform({})
          {}
        end
      end
      ]],
      {
        i(1, "Example"),
        c(2, { t("default"), t("low"), t("high"), t("critical") }),
        i(3, "25"),
        i(4, "false"),
        i(5, "*args"),
        i(6),
      }
    )
  ),
})

I had to add this line to load my custom snippets in my lsp-zero config:

lua/config/plugins/lsp-zero.lua
require("luasnip.loaders.from_lua").load({ paths = vim.fn.stdpath("config") .. "/snippets/" })
require("luasnip.loaders.from_vscode").lazy_load() -- friendly-snippets

Go wild!