Skip to content

Instantly share code, notes, and snippets.

@MaximumADHD
Created April 13, 2023 17:28
Show Gist options
  • Save MaximumADHD/ab5ff790252f7cf1f6256a8f9efc31be to your computer and use it in GitHub Desktop.
Save MaximumADHD/ab5ff790252f7cf1f6256a8f9efc31be to your computer and use it in GitHub Desktop.
Template for creating a snippet plugin with one argument in Roblox Studio.
local ScriptEditorService = game:GetService("ScriptEditorService")
local SNIPPET_NAME = "new_class"
local SNIPPET_DESC = "Create a new prototype class with an export type defined!"
local SNIPPET = table.concat({
"local ${1:%s} = {}",
"${1}.__index = ${1}",
"",
"export type Class = typeof(setmetatable({} :: {",
" ",
"}, ${1}))",
"",
"function ${1}.new(): Class",
" return setmetatable({",
" ",
" }, ${1})",
"end",
"",
"return ${1}"
}, "\n")
type AutocompleteItem = {
label: string,
kind: Enum.CompletionItemKind?,
tags: {Enum.CompletionItemTag}?,
detail: string?,
documentation: {
value: string,
}?,
overloads: number?,
learnMoreLink: string?,
codeSample: string?,
preselect: boolean?,
textEdit: {
newText: string,
replace: {
start: { line: number, character: number },
["end"]: { line: number, character: number }
},
}?
}
type AutocompleteResponse = {
items: { AutocompleteItem }
}
type AutocompleteRequest = {
position: {
line: number,
character: number
},
textDocument: {
document: ScriptDocument?,
script: LuaSourceContainer?
}
}
local function snippetCallback(request: AutocompleteRequest, response: AutocompleteResponse)
local textDoc = request.textDocument
local scriptDoc = textDoc.document
local luaSource = textDoc.script
local pos = request.position
local id = "Module"
if luaSource then
id = luaSource.Name
end
if scriptDoc then
local char = pos.character - 1
local line = scriptDoc:GetLine(pos.line)
if line:sub(1, char) ~= SNIPPET_NAME:sub(1, char) then
-- Line doesn't start with the snippet, peace out.
return response
end
end
table.insert(response.items, {
label = SNIPPET_NAME,
kind = Enum.CompletionItemKind.Snippet,
documentation = {
value = SNIPPET_DESC
},
textEdit = {
newText = SNIPPET:format(id),
replace = {
start = {
line = pos.line,
character = 1
},
["end"] = pos
}
}
})
return response
end
pcall(function ()
-- If testing, deregister the existing callback.
ScriptEditorService:DeregisterAutocompleteCallback(SNIPPET_NAME)
end)
ScriptEditorService:RegisterAutocompleteCallback(SNIPPET_NAME, 1000, snippetCallback)
@TominoCZ
Copy link

Cool stuff!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment