Service · inherits Instance
ScriptEditorService
This service is used for interacting with ScriptDocument instances.
About ScriptEditorService
This service is used for interacting with ScriptDocument instances. It
provides methods to open, find, and list script documents that represent
scripts currently open in the Studio
Script Editor. It also enables plugins to
register custom callbacks for autocomplete and script analysis, and fires
events when script documents are opened, closed, or changed.
This service is only available in Studio plugins and the command bar (Plugin security level).
Methods 9
DeregisterAutocompleteCallback
(name: string) → ()PluginRemoves a previously registered callback with the namename.Removes a previously registered autocomplete callback with the name
name. The callback must have been registered withScriptEditorService.RegisterAutocompleteCallback. Throws an error if no callback with the given name is currently registered.DeregisterScriptAnalysisCallback
(name: string) → ()PluginRemoves a previously registered callback with the namename.Removes a previously registered Script Analysis callback with the name
name. The callback must have been registered withScriptEditorService.RegisterScriptAnalysisCallback. Throws an error if no callback with the given name is currently registered.After the callback is removed, Script Analysis automatically reruns on all open scripts to update diagnostics.
FindScriptDocument
(script: LuaSourceContainer) → ScriptDocumentPluginReturns the openScriptDocumentcorresponding to the givenLuaSourceContainer, ornilif the given script is not open.Returns the open
ScriptDocumentcorresponding to the givenLuaSourceContainer, ornilif the given script is not open.A
ScriptDocumentonly exists while the script has an open tab in the Script Editor. If the script's editor tab is closed, this method returnsnileven if the script instance still exists in the DataModel.GetEditorSource
(script: LuaSourceContainer) → stringPluginReturns the edit-time source for the given script.Returns the edit-time source for the given script.
If the script is open in the Script Editor, this method returns the text currently being displayed in the editor. If the script is not open in the editor, the method returns the text that the editor would display if it's opened. The edit-time source is not always be consistent with the
Script.Sourceproperty.GetScriptDocuments
() → List<ScriptDocument>PluginReturns an array of the currently open script documents, including the command bar.Returns an array of the currently open script documents, including the command bar. Each entry is a
ScriptDocumentrepresenting one open editor tab. The command bar's document can be identified withScriptDocument.IsCommandBarif you need to exclude it.OpenScriptDocumentAsync
(script: LuaSourceContainer, options: Dictionary = nil) → TupleYieldsPluginRequests that a Script Editor open the specified script. Returns (true, nil) if the request succeeds. Returns (false, string) if the request fails, with a string that describes the problem.Requests that a Script Editor open the specified script. Returns (true, nil) if the request succeeds. Returns (false, string) if the request fails, with a string that describes the problem.
If the script is already open, this function succeeds and switches tabs to the associated editor.
RegisterAutocompleteCallback
(name: string, priority: int, callbackFunction: Function) → ()PluginRegisters an autocomplete callbackcallbackFunctionnamednamewith prioritypriority.Registers an autocomplete callback
callbackFunctionnamednamewith prioritypriority.When the Script Editor invokes autocomplete, all registered autocomplete callbacks call in order of ascending priority with the autocomplete request and response. Multiple callbacks may share a priority, but then their calling order is unpredictable. Each callback is intended to return a response table with the same format as the response input table. Callbacks shouldn't yield. The first callback invoked receives the internal autocomplete's response as its response table, and subsequent callbacks receive the previous callback's output as their response table. Callbacks may either modify the passed table or return a new table of the same format.
The
callbackFunctionmust have the following type:(Request: table, Response: table) -> tableThe Request table has the following format:
type Request = { position: { line: number, character: number }, textDocument: { document: ScriptDocument?, script: LuaSourceContainer? } }positionis the one-indexed cursor position where you are autocompleting.textDocument.documentis the openScriptDocumentyou are completing in, if it exists.textDocument.scriptis theLuaSourceContaineryou are completing in, if it exists.
If both
textDocument.documentandtextDocument.scriptare present, then they correspond to each other:req.textDocument.document:GetScript() == req.textDocument.scriptThe Response table has the following format:
type Response = { items: { { label: string, -- The label kind: Enum.CompletionItemKind?, tags: {Enum.CompletionItemTag}?, detail: string?, documentation: { value: string, }?, overloads: number?, learnMoreLink: string?, codeSample: string?, preselect: boolean?, textEdit: { newText: string, insert: { start: { line: number, character: number }, ["end"]: { line: number, character: number } }, replace: { start: { line: number, character: number }, ["end"]: { line: number, character: number } }, }? } } }Response.itemsis an array of the completion items. The order of this array is insignificant, and it resorts in the editor as the user types.Response.items[n].labelis the label of the item which display in the autocomplete menu.Response.items[n].kindspecifies what type of autocomplete item this is. Primarily this controls the icon given to the item in the editor. Not all kinds have a unique icon. If not specified, the editor uses the "Text" icon. Unsupported kinds default to displaying the "Property" icon.Response.items[n].tagsspecifies an array of tags describing this completion item. See theCompletionItemTagfor details on their function.Response.items[n].detailsspecifies a string describing details about the completion item. For default items, this is a string representation of their type. Note that, in order for the documentation widget to display,documentationmust be present, butdocumentation.valuemay be empty.Response.items[n].documentationspecifies the main body of the documentation in itsvaluefield.documentationis present, even if value is empty, so the documentation window displays if either details or overloads are specified.Response.items[n].overloadsspecifies the number of overloads of a function autocompletion.Response.items[n].learnMoreLinklinks to a relevant page on the creator docs. This URL must be ahttpsrequest to create.roblox.com; no other URLs display in the editor.Response.items[n].codeSamplespecifies a sample use of the completion item.documentationmust be non-empty to display this field.Response.items[n].preselectIf true, the editor sorts this completion item ahead of all others and selects it for the user by default. No effect if false or missing.Response.items[n].textEditIf present, accepting the completion applies this text edit - inserting or replacing the span between the positions start and end with newText.
If a callback returns a malformed result or encounters an error, the editor discards the modified Response table and uses the built-in autocomplete result list.
RegisterScriptAnalysisCallback
(name: string, priority: int, callbackFunction: Function) → ()PluginRegisters a Script Analysis callbackcallbackFunctionnamednamewithpriority.Registers a Script Analysis callback
callbackFunctionnamednamewithpriority. When Script Analysis in Studio runs, all registered callbacks call in order of ascending priority. Each callback is intended to return a response table matching the format specified below. Callbacks should not yield.The request table has the following format, where
scriptis theLuaSourceContainerthat is going to be analyzed.type Request = { script: LuaSourceContainer? }The response table has the following format, where
diagnosticsis an array of diagnostic tables. Each diagnostic table has the entries listed below.type Response = { diagnostics: { { range: { start: { line: number, character: number, }, ["end"]: { line: number, character: number, } }, code: string?, message: string, severity: Enum.Severity?, codeDescription: { href: string }? } } }rangerepresents a text range that should be highlighted by the linter, providing what line/character to start highlighting and what line/character to stop highlighting.codeis a label for the message.messageis a warning message to be displayed for the line. This will also appear on a tooltip when the user hovers their cursor over the line in the Script Editor.severityis aSeverityvalue for the diagnostics. This determines how the diagnostic is categorized in the Script Analysis tool in Studio, as well as how text is highlighted in the Script Editor.codeDescriptionlinks to a relevant page on the creator docs. This URL must be anhttpsrequest tocreate.roblox.com; no other URLs display in the editor.
UpdateSourceAsync
(script: LuaSourceContainer, callback: Function) → ()YieldsPluginGenerates new content from the old script and updates the script editor if it's open, or theScriptinstance if the script editor is closed.Returns the edit-time
Script.Sourcefor the given script.This function calls the passed callback using the old contents of the script to calculate the new contents of the script.
If the script is open in the Script Editor, then it issues a request to the editor to update its source. The editor may reject this update if the
Script.Sourceproperty was out of date with the user's version of the script when this function was called, in which case the callback will be re-invoked and the attempt will be repeated.The callback may not yield. If the callback returns
nil, the operation is cancelled. This function yields until the operation is cancelled or succeeds.If the script is not open in the editor, the new content updates to the script source, which is the text the editor would display if it is opened.
Events 3
TextDocumentDidChange
ScriptEditorService.TextDocumentDidChange:Connect(function(document, changesArray))PluginFires just after aScriptDocumentchanges.Fires just after a
ScriptDocumentchanges. ThetextChangedis an array of change structures of the format:{ range : { start : { line : number, character : number }, end : { line : number, character : number } }, text: string }TextDocumentDidClose
ScriptEditorService.TextDocumentDidClose:Connect(function(oldDocument))PluginFires just before aScriptDocumentobject is destroyed, which happens right after the script editor closes.Fires just before a
ScriptDocumentobject is destroyed, which happens right after the script editor closes. After this event fires, theScriptDocumententers a "Closed" state, and trying to call its methods throws an error.ScriptDocumentobjects aren't reusable, even if the script editor reopens the same script.TextDocumentDidOpen
ScriptEditorService.TextDocumentDidOpen:Connect(function(newDocument))PluginFires just after aScriptDocumentobject is created and parented to the service, which happens right after the script editor opens.Fires just after a
ScriptDocumentobject is created and parented to the service, which happens right after the script editor opens. TheScriptDocumentpassed to the handler is fully initialized and ready to read or modify. This event also fires for the command bar document.
ScriptEditorService also has every member of Instance. Deprecated and Roblox-only members are hidden.
Reference text from the Roblox Creator Documentation, licensed CC BY 4.0, reorganized by rbxtoolshub.com · Studio 0.743.0.7431133.