Skip to content

Instantly share code, notes, and snippets.

@MaximumADHD
Created April 13, 2023 20:36
Show Gist options
  • Save MaximumADHD/6dd8173f0be78f9b0eec5885404fd563 to your computer and use it in GitHub Desktop.
Save MaximumADHD/6dd8173f0be78f9b0eec5885404fd563 to your computer and use it in GitHub Desktop.
Autocompletes _# with game.Selection:Get()[#] in Roblox Studio's command bar.
--!strict
local ScriptEditorService = game:GetService("ScriptEditorService")
local ALIAS = "game.Selection:Get()[%s]"
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
if scriptDoc and scriptDoc:IsCommandBar() then
local line = scriptDoc:GetLine()
if line:sub(-5) == "for _" then
-- Typing a placeholder, ignore
return response
end
local pos = request.position
local startAt, endAt, index = line:find("_(%d+)$")
if startAt and endAt and index then
table.insert(response.items, {
label = '_' .. index,
kind = Enum.CompletionItemKind.Snippet,
detail = "(Snippet)",
documentation = {
value = `Alias for {ALIAS:format(index)}`
},
textEdit = {
newText = ALIAS:format(index),
replace = {
start = {
line = pos.line,
character = startAt,
},
["end"] = {
line = pos.line,
character = endAt + 1,
}
}
}
})
elseif line:sub(-1) == "_" then
table.insert(response.items, {
label = "_#",
kind = Enum.CompletionItemKind.Folder,
detail = "(Hint)",
documentation = {
value = `Type a number after an underscore to auto-complete {ALIAS:format('#')}`
},
textEdit = {
newText = "",
replace = {
start = pos,
["end"] = pos
}
}
})
end
end
return response
end
pcall(function ()
-- If testing, deregister the existing callback.
ScriptEditorService:DeregisterAutocompleteCallback(ALIAS)
end)
ScriptEditorService:RegisterAutocompleteCallback(ALIAS, 1000, snippetCallback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment