Skip to content

Instantly share code, notes, and snippets.

@MaximumADHD
Created October 31, 2025 00:37
Show Gist options
  • Select an option

  • Save MaximumADHD/c9c98a247ce3b11da491fa440dd14cb1 to your computer and use it in GitHub Desktop.

Select an option

Save MaximumADHD/c9c98a247ce3b11da491fa440dd14cb1 to your computer and use it in GitHub Desktop.
--!strict
local ScriptEditorService = game:GetService("ScriptEditorService")
local Selection = game:GetService("Selection")
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 not scriptDoc then
return response
end
if not scriptDoc:IsCommandBar() then
if scriptDoc:GetScript() ~= nil then
return response
end
end
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
local num = tonumber(index)
local selected = Selection:Get()
local target = num and selected[num]
local pathTo: string?
if target and target:IsDescendantOf(game) then
local parent = target.Parent
local child = parent and parent:FindFirstChild(target.Name)
if child == target then
local at = target
local path = ""
while at and at ~= game do
if at.Name:match("^[A-z_][A-z0-9_]+$") then
path = `.{at.Name}{path}`
else
path = `["{at.Name}"]{path}`
end
at = at.Parent
end
if path:sub(1, 10) == ".Workspace" then
path = "w" .. path:sub(3)
else
path = "game" .. path
end
pathTo = path
end
end
local alias = ALIAS:format(index)
if pathTo then
table.insert(response.items, {
label = `_{index} (Direct Path)`,
kind = Enum.CompletionItemKind.Snippet,
detail = "Direct Path",
documentation = {
value = `<code>{alias}</code> as:<br><code>{pathTo}</code>`,
},
textEdit = {
newText = pathTo,
replace = {
start = {
line = pos.line,
character = startAt,
},
["end"] = {
line = pos.line,
character = endAt + 1,
},
},
},
})
end
table.insert(response.items, {
label = `_{index} (Selection Alias)`,
kind = Enum.CompletionItemKind.Snippet,
detail = "Selection Alias",
documentation = {
value = `Exactly: <code>{alias}</code>`,
},
textEdit = {
newText = alias,
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
return response
end
pcall(function()
-- If testing, deregister the existing callback.
ScriptEditorService:DeregisterAutocompleteCallback(ALIAS)
end)
ScriptEditorService:RegisterAutocompleteCallback(ALIAS, 1000, snippetCallback)
@Lolzsoepik
Copy link

dude, fuck yes.

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