VS Code Snippets
Being able to quickly create services, controllers, or other Crystal-related items is very useful when using Crystal as a framework. To keep Crystal lightweight, there are no required extensions or plugins. Instead, below are some VS Code snippets that can be used to speed up development.
Using Snippets
Snippets are a Visual Studio Code feature. Check out the Snippets documentation for more info. Adding Snippets for Lua is very easy.
- Within Visual Studio, navigate from the toolbar:
File -> Preferences -> User Snippets
- Type in and select
lua.json
- Within the
{}
braces, include any or all of the snippets below - Save the file
- Within your actual source files, start typing a prefix (e.g. "Crystal") and select the autocompleted snippet to paste it in
- Depending on the snippet, parts of the pasted code will be selected and can be typed over (e.g. setting the name of a service)
Crystal Snippets
Below are useful VS Code snippets for Crystal. The snippets assume that the Crystal module has been placed within ReplicatedStorage.
Crystal
Include a require
statement for Crystal.
Snippet
"Crystal": {
"prefix": ["Crystal"],
"body": ["local Crystal = require(ReplicatedStorage.Packages.Crystal)"],
"description": "Require the Crystal module"
}
Code Result
local Crystal = require(ReplicatedStorage.Packages.Crystal)
Service
Reference a Roblox service.
Snippet
"Service": {
"prefix": ["service"],
"body": ["local ${0:Name}Service = game:GetService(\"${0:Name}Service\")"],
"description": "Roblox Service"
}
Code Result
local HttpService = game:GetService("HttpService")
Crystal Service
Reference Crystal, create a service, and return the service.
Snippet
"Crystal Service": {
"prefix": ["Crystalservice"],
"body": [
"local Crystal = require(ReplicatedStorage.Packages.Crystal)",
"",
"local ${0:$TM_FILENAME_BASE} = Crystal.CreateService {",
"\tName = \"${0:$TM_FILENAME_BASE}\",",
"\tClient = {},",
"}",
"",
"",
"function ${0:$TM_FILENAME_BASE}:CrystalStart()",
"\t",
"end",
"",
"",
"function ${0:$TM_FILENAME_BASE}:CrystalInit()",
"\t",
"end",
"",
"",
"return ${0:$TM_FILENAME_BASE}",
""
],
"description": "Crystal Service template"
}
Code Result
local Crystal = require(ReplicatedStorage.Packages.Crystal)
local MyService = Crystal.CreateService {
Name = "MyService",
Client = {},
}
function MyService:CrystalStart()
end
function MyService:CrystalInit()
end
return MyService
Crystal Controller
Reference Crystal, create a controller, and return the controller.
Snippet
"Crystal Controller": {
"prefix": ["Crystalcontroller"],
"body": [
"local Crystal = require(ReplicatedStorage.Packages.Crystal)",
"",
"local ${0:$TM_FILENAME_BASE} = Crystal.CreateController { Name = \"${0:$TM_FILENAME_BASE}\" }",
"",
"",
"function ${0:$TM_FILENAME_BASE}:CrystalStart()",
"\t",
"end",
"",
"",
"function ${0:$TM_FILENAME_BASE}:CrystalInit()",
"\t",
"end",
"",
"",
"return ${0:$TM_FILENAME_BASE}",
""
],
"description": "Crystal Controller template"
}
Code Result
local Crystal = require(ReplicatedStorage.Packages.Crystal)
local MyController = Crystal.CreateController {
Name = "MyController",
}
function MyController:CrystalStart()
end
function MyController:CrystalInit()
end
return MyController
Crystal Require
Require a module within Crystal.
Snippet
"Crystal Require": {
"prefix": ["Crystalrequire"],
"body": ["local ${1:Name} = require(Crystal.${2:Util}.${1:Name})"],
"description": "Crystal Require template"
}
Code Result
local Signal = require(Crystal.Util.Signal)
Lua Class
A standard Lua class.
Snippet
"Class": {
"prefix": ["class"],
"body": [
"local ${0:$TM_FILENAME_BASE} = {}",
"${0:$TM_FILENAME_BASE}.__index = ${0:$TM_FILENAME_BASE}",
"",
"",
"function ${0:$TM_FILENAME_BASE}.new()",
"\tlocal self = setmetatable({}, ${0:$TM_FILENAME_BASE})",
"\treturn self",
"end",
"",
"",
"function ${0:$TM_FILENAME_BASE}:Destroy()",
"\t",
"end",
"",
"",
"return ${0:$TM_FILENAME_BASE}",
""
],
"description": "Lua Class"
}
Code Result
local MyClass = {}
MyClass.__index = MyClass
function MyClass.new()
local self = setmetatable({}, MyClass)
return self
end
function MyClass:Destroy()
end
return MyClass
All
All the above snippets together.
All Snippets
{
"Service": {
"prefix": ["service"],
"body": ["local ${0:Name}Service = game:GetService(\"${0:Name}Service\")"],
"description": "Roblox Service"
},
"Class": {
"prefix": ["class"],
"body": [
"local ${0:$TM_FILENAME_BASE} = {}",
"${0:$TM_FILENAME_BASE}.__index = ${0:$TM_FILENAME_BASE}",
"",
"",
"function ${0:$TM_FILENAME_BASE}.new()",
"\tlocal self = setmetatable({}, ${0:$TM_FILENAME_BASE})",
"\treturn self",
"end",
"",
"",
"function ${0:$TM_FILENAME_BASE}:Destroy()",
"\t",
"end",
"",
"",
"return ${0:$TM_FILENAME_BASE}",
""
],
"description": "Lua Class"
},
"Crystal": {
"prefix": ["Crystal"],
"body": ["local Crystal = require(ReplicatedStorage.Packages.Crystal)"],
"description": "Require the Crystal module"
},
"Crystal Service": {
"prefix": ["Crystalservice"],
"body": [
"local Crystal = require(ReplicatedStorage.Packages.Crystal)",
"",
"local ${0:$TM_FILENAME_BASE} = Crystal.CreateService {",
"\tName = \"${0:$TM_FILENAME_BASE}\",",
"\tClient = {},",
"}",
"",
"",
"function ${0:$TM_FILENAME_BASE}:CrystalStart()",
"\t",
"end",
"",
"",
"function ${0:$TM_FILENAME_BASE}:CrystalInit()",
"\t",
"end",
"",
"",
"return ${0:$TM_FILENAME_BASE}",
""
],
"description": "Crystal Service template"
},
"Crystal Controller": {
"prefix": ["Crystalcontroller"],
"body": [
"local Crystal = require(ReplicatedStorage.Packages.Crystal)",
"",
"local ${0:$TM_FILENAME_BASE} = Crystal.CreateController { Name = \"${0:$TM_FILENAME_BASE}\" }",
"",
"",
"function ${0:$TM_FILENAME_BASE}:CrystalStart()",
"\t",
"end",
"",
"",
"function ${0:$TM_FILENAME_BASE}:CrystalInit()",
"\t",
"end",
"",
"",
"return ${0:$TM_FILENAME_BASE}",
""
],
"description": "Crystal Controller template"
},
"Crystal Require": {
"prefix": ["Crystalrequire"],
"body": ["local ${1:Name} = require(Crystal.${2:Util}.${1:Name})"],
"description": "Crystal Require template"
}
}