Skip to main content

VS Code Snippets

Being able to quickly create services, controllers, or other Knit-related items is very useful when using Knit as a framework. To keep Knit lightweight, there are no required extensions or plugins. Instead, below are some VS Code snippets that can be used to speed up development.

Snippets

Using Snippets

Snippets are a Visual Studio Code feature. Check out the Snippets documentation for more info. Adding Snippets for Lua is very easy.

  1. Within Visual Studio, navigate from the toolbar: File -> Preferences -> User Snippets
  2. Type in and select lua.json
  3. Within the {} braces, include any or all of the snippets below
  4. Save the file
  5. Within your actual source files, start typing a prefix (e.g. "knit") and select the autocompleted snippet to paste it in
  6. 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)

Knit Snippets

Below are useful VS Code snippets for Knit. The snippets assume that the Knit module has been placed within ReplicatedStorage.

Knit

Include a require statement for Knit.

Snippet
"Knit": {
"prefix": ["knit"],
"body": ["local Knit = require(ReplicatedStorage.Packages.Knit)"],
"description": "Require the Knit module"
}
Code Result
local Knit = require(ReplicatedStorage.Packages.Knit)

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")

Knit Service

Reference Knit, create a service, and return the service.

Snippet
"Knit Service": {
"prefix": ["knitservice"],
"body": [
"local Knit = require(ReplicatedStorage.Packages.Knit)",
"",
"local ${0:$TM_FILENAME_BASE} = Knit.CreateService {",
"\tName = \"${0:$TM_FILENAME_BASE}\",",
"\tClient = {},",
"}",
"",
"",
"function ${0:$TM_FILENAME_BASE}:KnitStart()",
"\t",
"end",
"",
"",
"function ${0:$TM_FILENAME_BASE}:KnitInit()",
"\t",
"end",
"",
"",
"return ${0:$TM_FILENAME_BASE}",
""
],
"description": "Knit Service template"
}
Code Result
local Knit = require(ReplicatedStorage.Packages.Knit)

local MyService = Knit.CreateService {
Name = "MyService",
Client = {},
}

function MyService:KnitStart()
end

function MyService:KnitInit()
end

return MyService

Knit Controller

Reference Knit, create a controller, and return the controller.

Snippet
"Knit Controller": {
"prefix": ["knitcontroller"],
"body": [
"local Knit = require(ReplicatedStorage.Packages.Knit)",
"",
"local ${0:$TM_FILENAME_BASE} = Knit.CreateController { Name = \"${0:$TM_FILENAME_BASE}\" }",
"",
"",
"function ${0:$TM_FILENAME_BASE}:KnitStart()",
"\t",
"end",
"",
"",
"function ${0:$TM_FILENAME_BASE}:KnitInit()",
"\t",
"end",
"",
"",
"return ${0:$TM_FILENAME_BASE}",
""
],
"description": "Knit Controller template"
}
Code Result
local Knit = require(ReplicatedStorage.Packages.Knit)

local MyController = Knit.CreateController {
Name = "MyController",
}

function MyController:KnitStart()
end

function MyController:KnitInit()
end

return MyController

Knit Require

Require a module within Knit.

Snippet
"Knit Require": {
"prefix": ["knitrequire"],
"body": ["local ${1:Name} = require(Knit.${2:Util}.${1:Name})"],
"description": "Knit Require template"
}
Code Result
local Signal = require(Knit.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"
},

"Knit": {
"prefix": ["knit"],
"body": ["local Knit = require(ReplicatedStorage.Packages.Knit)"],
"description": "Require the Knit module"
},

"Knit Service": {
"prefix": ["knitservice"],
"body": [
"local Knit = require(ReplicatedStorage.Packages.Knit)",
"",
"local ${0:$TM_FILENAME_BASE} = Knit.CreateService {",
"\tName = \"${0:$TM_FILENAME_BASE}\",",
"\tClient = {},",
"}",
"",
"",
"function ${0:$TM_FILENAME_BASE}:KnitStart()",
"\t",
"end",
"",
"",
"function ${0:$TM_FILENAME_BASE}:KnitInit()",
"\t",
"end",
"",
"",
"return ${0:$TM_FILENAME_BASE}",
""
],
"description": "Knit Service template"
},

"Knit Controller": {
"prefix": ["knitcontroller"],
"body": [
"local Knit = require(ReplicatedStorage.Packages.Knit)",
"",
"local ${0:$TM_FILENAME_BASE} = Knit.CreateController { Name = \"${0:$TM_FILENAME_BASE}\" }",
"",
"",
"function ${0:$TM_FILENAME_BASE}:KnitStart()",
"\t",
"end",
"",
"",
"function ${0:$TM_FILENAME_BASE}:KnitInit()",
"\t",
"end",
"",
"",
"return ${0:$TM_FILENAME_BASE}",
""
],
"description": "Knit Controller template"
},

"Knit Require": {
"prefix": ["knitrequire"],
"body": ["local ${1:Name} = require(Knit.${2:Util}.${1:Name})"],
"description": "Knit Require template"
}

}