Class
ScriptDocument
NotCreatableNotReplicated
Represents the ephemeral state of an open document in the Studio Script Editor, providing APIs for reading and editing code.
A ScriptDocument instance is a proxy of the document of a Studio
Script Editor. It's different from the LuaSourceContainer open in the
editor in that it represents the ephemeral state of an open document, and its
representation is in a format that's more suited for reading and editing code
than executing it. In particular, ScriptDocument reflects any changes
that have been made to the open script in Drafts Mode, which the source
property doesn't.
The Script Editor itself exists and changes on a different thread than any
DataModel, so the ScriptDocument replicates the open Script
Editor, but it isn't the open editor. Because of the replication, there's
sometimes a slight delay between changing the text in the editor and updating
the ScriptDocument. The delay usually occurs because the
DataModel is busy, and it's almost always extremely small, but it
still exists.
The existence of a ScriptDocument indicates that a document is open in
the Script Editor. All ScriptDocument instances have
ScriptEditorService as its parent. Each instance adheres to the
following encoding conventions:
- All text in
ScriptDocumentis UTF-8 encoded. - All line indices are 1-indexed.
- All character indices are 1-indexed and count UTF-8 bytes, not graphemes, so
the same warning from
TextBox.CursorPositionapplies: many Unicode characters take more than one byte. - All ranges are inclusive of their start position and exclusive of their end position, so start == end implies an empty range.
All APIs for ScriptDocument are at Plugin level security.
Methods 17#
| CloseAsync | Requests that the editor associated with this document close. Yields the current thread until the editor responds to the request.PluginSecurity securityYields |
| EditTextAsync | Replaces the text in the specified range from (startLine, startColumn)
to (endLine, endColumn) with newText.PluginSecurity securityYields |
| ForceSetSelectionAsync | Asks the editor to set its cursor selection to the argument values.PluginSecurity securityYields |
| GetLine | Returns the text of the specified line. When no argument is provided, returns the line of the current cursor position.PluginSecurity security |
| GetLineCount | Returns the number of lines in the document.PluginSecurity security |
| GetScript | Returns the underlying LuaSourceContainer instance, if one exists,
otherwise nil.PluginSecurity security |
| GetSelectedText | Gets the text selected in the editor, or an empty string if there is no selection.PluginSecurity security |
| GetSelection | Returns the last known selection of the Script Editor in the format:
CursorLine, CursorChar, AnchorLine, AnchorChar. If the Script Editor has
no selection, CursorLine == AnchorLine and CursorChar == AnchorChar.PluginSecurity security |
| GetSelectionEnd | Gets the larger of the cursor position and anchor. If the editor has no selection, they are the same value.PluginSecurity security |
| GetSelectionStart | Gets the smaller of the cursor position and anchor. If the editor has no selection, they are the same value.PluginSecurity security |
| GetText | Returns text from the open editor.PluginSecurity security |
| GetViewport | Returns the currently displayed line numbers in the editor change.PluginSecurity security |
| HasSelectedText | Returns whether or not the editor has any text selected.PluginSecurity security |
| IsCommandBar | Returns true if the ScriptDocument represents the Command bar.PluginSecurity security |
| MultiEditTextAsync | Applies a batch of text edits to the document as a single atomic operation. Yields the current thread until the editor responds.PluginSecurity securityYields |
| RequestSetSelectionAsync | Asks the editor to set its cursor selection to the argument values.PluginSecurity securityYields |
| ReviewableTextEditsAsync | Applies a batch of line-based text edits that appear in the editor as reviewable inline diffs, allowing users to accept or reject each change.PluginSecurity securityYields |
CloseAsync(): Tuple#
YieldsPluginSecurity security
Requests that the editor associated with this document close. Yields the current thread until the editor responds to the request. If the function succeeds, it returns (true, nil). If the function fails, it returns (false, string) as a description of the problem.
This function can't close the command bar.
Returns
Tuple— A tuple where the first element istrueif the editor closed successfully, orfalsefollowed by a string describing the failure reason.
EditTextAsync(newText: string, startLine: int, startCharacter: int, endLine: int, endCharacter: int): Tuple#
YieldsPluginSecurity security
Replaces the text in the specified range from (startLine, startColumn)
to (endLine, endColumn) with newText. If the range is empty, then
the function inserts the text at (startLine, startColumn). If the text
cursor is within the specified range, the cursor moves to the end position
of the edit. Otherwise, the text cursor doesn't move. This function yields
the current thread until it receives a reply from the editor about the
edit.
If the function succeeds, it returns (true, nil).
The function throws an error if:
- The range is invalid.
- The range would slice one Unicode character, for example replace only some of the bytes of the unicode character.
- The
newTextitself contains invalid UTF-8.
If the function fails, it returns (false, string). The string is a
description of the problem. The most common failure type is a version
mismatch. This occurs when you try to call EditTextAsync during the time
when the ScriptDocument is out of sync with the contents of the
editor. If this happens, you can retry the edit.
| Name | Type | Default | Description |
|---|---|---|---|
newText | string | The replacement string to insert into the specified range. | |
startLine | int | The 1-indexed line number where the replacement range begins. | |
startCharacter | int | The 1-indexed UTF-8 byte offset within startLine where the
replacement range begins. | |
endLine | int | The 1-indexed line number where the replacement range ends (exclusive). | |
endCharacter | int | The 1-indexed UTF-8 byte offset within endLine where the replacement
range ends (exclusive). |
Returns
Tuple— A tuple where the first element istrueif the edit was applied successfully, orfalsefollowed by a string describing the failure reason.
ForceSetSelectionAsync(cursorLine: int, cursorCharacter: int, anchorLine: int? = nil, anchorCharacter: int? = nil): Tuple#
YieldsPluginSecurity security
Asks the editor to set its cursor selection to the argument values. Both
anchor arguments must be passed, or neither. If neither is passed, then
they each default to being the same as the corresponding cursor argument.
The editor might decline to update its cursor if the text content of the
document has changed. Unlike
ScriptDocument:RequestSetSelectionAsync(), the editor will not
decline to move its cursor if the cursor has moved since the request was
made. Returns (true, nil) if the cursor was updated, and (false, string)
with an explanation string if it was not. Yields the current thread until
the editor replies.
| Name | Type | Default | Description |
|---|---|---|---|
cursorLine | int | The 1-indexed line number for the cursor position. | |
cursorCharacter | int | The 1-indexed UTF-8 byte offset within the line for the cursor position. | |
anchorLine | int? | nil | The 1-indexed line number for the anchor position. Defaults to
cursorLine if not provided. |
anchorCharacter | int? | nil | The 1-indexed UTF-8 byte offset within the line for the anchor
position. Defaults to cursorCharacter if not provided. |
Returns
Tuple— A tuple where the first element istrueif the cursor was updated, orfalsefollowed by a string explaining why the update was declined.
GetLine(lineIndex: int? = nil): string#
PluginSecurity security
Returns the text of the specified line. When no argument is provided, returns the line of the current cursor position.
| Name | Type | Default | Description |
|---|---|---|---|
lineIndex | int? | nil | The 1-indexed line number to retrieve. Defaults to the current cursor line if not provided. |
Returns
string— The text content of the specified line as a string.
GetLineCount(): int#
PluginSecurity security
Returns the number of lines in the active document.
Returns
int— The total number of lines in the document.
GetScript(): LuaSourceContainer#
PluginSecurity security
Returns the underlying LuaSourceContainer instance, if one exists,
otherwise nil.
Returns
LuaSourceContainer— TheLuaSourceContainerthat the document is editing, ornilif the document does not represent a script instance in the place (e.g. the Command Bar is not a real script instance).
GetSelectedText(): string#
PluginSecurity security
Gets the text selected in the editor, or an empty string if there is no selection.
Returns
string— The currently selected text as a string, or an empty string if nothing is selected.
GetSelection(): Tuple#
PluginSecurity security
Returns the last known selection of the Script Editor in the format:
CursorLine, CursorChar, AnchorLine, AnchorChar. If the Script Editor has
no selection, CursorLine == AnchorLine and CursorChar == AnchorChar.
Returns
Tuple— CursorLine, CursorChar, AnchorLine, AnchorChar.
GetSelectionEnd(): Tuple#
PluginSecurity security
Gets the larger of the cursor position and anchor. If the editor has no selection, they are the same value.
Returns
Tuple— A tuple of (line, character) representing the larger of the cursor and anchor positions, both 1-indexed.
GetSelectionStart(): Tuple#
PluginSecurity security
Gets the smaller of the cursor position and anchor. If the editor has no selection, they are the same value.
Returns
Tuple— A tuple of (line, character) representing the smaller of the cursor and anchor positions, both 1-indexed.
GetText(startLine: int? = nil, startCharacter: int? = nil, endLine: int? = nil, endCharacter: int? = nil): string#
PluginSecurity security
Returns text from the open editor. Must be called with 0, 2 or 4 arguments:
- If called with 0 arguments, gets the entire contents of the open editor.
- If called with 2 arguments, gets the text of the document starting at
(
startLine,startColumn). - If called with 4 arguments, gets the text of the document starting at
(
startLine,startColumn) and ending at (endLine,endColumn).
| Name | Type | Default | Description |
|---|---|---|---|
startLine | int? | nil | The 1-indexed line number where the text range begins. Optional; omit all arguments to get the entire document. |
startCharacter | int? | nil | The 1-indexed UTF-8 byte offset within startLine where the text
range begins. |
endLine | int? | nil | The 1-indexed line number where the text range ends (exclusive).
Optional; omit to read from startLine/startCharacter to the end of
the document. |
endCharacter | int? | nil | The 1-indexed UTF-8 byte offset within endLine where the text range
ends (exclusive). |
Returns
string— The text content within the specified range as a string.
GetViewport(): Tuple#
PluginSecurity security
Returns the currently displayed line numbers in the editor change. The editor displays the lines between startLine and endLine, inclusive. The first and last line might only display partially. For example, only the topmost pixel of the last line might be on screen. Furthermore, code folding might hide lines between startLine and endLine.
Returns
Tuple— A tuple of (startLine, endLine) representing the 1-indexed range of lines currently visible in the editor.
HasSelectedText(): boolean#
PluginSecurity security
Returns whether or not the editor has any text selected.
Returns
boolean—trueif the editor has a non-empty text selection,falseotherwise.
IsCommandBar(): boolean#
PluginSecurity security
Returns true if the ScriptDocument represents the Command bar. The
command bar has special rules and limitations in this API:
- Studio creates the Command bar before running plugins, so it doesn't always fire the opened event, although it does close and reopen as Studio transitions between DataModels.
- You can't edit the Command bar with
EditTextAsyncfor security reasons.
Returns
boolean—trueif this document represents the Command bar,falseotherwise.
MultiEditTextAsync(edits: Array): Tuple#
YieldsPluginSecurity security
Applies a batch of text edits to the document as a single atomic
operation, then yields the current thread until it receives a reply from
the editor. Each entry in the edits array is a table describing one
replacement, with the same meaning as the arguments to
ScriptDocument:EditTextAsync(), and the following fields:
NewText: The replacement text.StartLine: The line of the start of the range to replace.StartCharacter: The character of the start of the range to replace.EndLine: The line of the end of the range to replace.EndCharacter: The character of the end of the range to replace.
The edits are applied together, so either all of them succeed or none of them are applied. Before applying any edit, the function validates every edit and throws an error if:
- Any range is invalid.
- Any range would slice one Unicode character, for example replace only some of the bytes of the unicode character.
- Any
NewTextitself contains invalid UTF-8. - The ranges of two edits overlap.
If the function succeeds, it returns (true, nil). If the function
fails, it returns (false, string), where the string describes the
problem. As with ScriptDocument:EditTextAsync(), the most common
failure is a version mismatch, which occurs when the
ScriptDocument is momentarily out of sync with the contents of the
editor; if this happens, you can retry the edits.
| Name | Type | Default | Description |
|---|---|---|---|
edits | Array | An array of tables, each describing a text replacement with fields
NewText, StartLine, StartCharacter, EndLine, and
EndCharacter. |
Returns
Tuple— A tuple where the first element istrueif all edits were applied successfully, orfalsefollowed by a string describing the failure reason.
RequestSetSelectionAsync(cursorLine: int, cursorCharacter: int, anchorLine: int? = nil, anchorCharacter: int? = nil): Tuple#
YieldsPluginSecurity security
Asks the editor to set its cursor selection to the argument values. Both anchor arguments must be passed, or neither. If neither is passed, then they each default to being the same as the corresponding cursor argument. The editor might decline to update its cursor if the text content of the document has changed, or the cursor has moved since the request was made. Returns (true, nil) if the cursor was updated, and (false, string) with an explanation string if it was not. Yields the current thread until the editor replies.
| Name | Type | Default | Description |
|---|---|---|---|
cursorLine | int | The 1-indexed line number for the cursor position. | |
cursorCharacter | int | The 1-indexed UTF-8 byte offset within the line for the cursor position. | |
anchorLine | int? | nil | The 1-indexed line number for the anchor position. Defaults to
cursorLine if not provided. |
anchorCharacter | int? | nil | The 1-indexed UTF-8 byte offset within the line for the anchor
position. Defaults to cursorCharacter if not provided. |
Returns
Tuple— A tuple where the first element istrueif the cursor was updated, orfalsefollowed by a string explaining why the update was declined.
ReviewableTextEditsAsync(changes: Array): Tuple#
YieldsPluginSecurity security
Applies a batch of line-based text edits to the document that are
presented to the user as reviewable inline diffs in the Script Editor
gutter. Each entry in the changes array is a table with the following
fields:
Text: The replacement text to insert. Must be a non-empty string unlessEndLineis provided.StartLine: The 1-indexed line number where the edit begins. IfEndLineis omitted, the text is inserted before this line. IfEndLineis provided, lines fromStartLinethroughEndLineare replaced.EndLine: Optional. The 1-indexed last line of the range to replace (inclusive). Must be greater than or equal toStartLine.
The edits must not overlap or border each other; there must be at least
one unchanged line between any two edits. Edits are applied atomically.
The function throws an error if any entry has invalid fields. If the
function fails due to a version mismatch or other editor-side issue, it
returns (false, string).
This method cannot be used on the Command bar.
| Name | Type | Default | Description |
|---|---|---|---|
changes | Array | An array of tables, each with a Text field (the replacement string),
a StartLine field (1-indexed line to insert or begin replacing at),
and an optional EndLine field (1-indexed last line to replace,
inclusive). |
Returns
Tuple— A tuple where the first element istrueif the edits were applied successfully, orfalsefollowed by a string describing the failure reason.
Events 2#
| SelectionChanged | Fires when the ScriptDocument changes, including immediately after a text change.PluginSecurity security |
| ViewportChanged | Fires when the displayed line numbers in the editor change.PluginSecurity security |
SelectionChanged(positionLine: int64, positionCharacter: int64, anchorLine: int64, anchorCharacter: int64)#
PluginSecurity security
Fires when the ScriptDocument changes, including immediately after a text change.
| Name | Type | Default | Description |
|---|---|---|---|
positionLine | int64 | The 1-indexed line number of the cursor position after the change. | |
positionCharacter | int64 | The 1-indexed UTF-8 byte offset of the cursor position after the change. | |
anchorLine | int64 | The 1-indexed line number of the selection anchor after the change. | |
anchorCharacter | int64 | The 1-indexed UTF-8 byte offset of the selection anchor after the change. |
ViewportChanged(startLine: int64, endLine: int64)#
PluginSecurity security
Fires when the displayed line numbers in the editor change. See
ScriptDocument.GetViewport for details.
| Name | Type | Default | Description |
|---|---|---|---|
startLine | int64 | The 1-indexed first line currently visible in the editor viewport. | |
endLine | int64 | The 1-indexed last line currently visible in the editor viewport. |
Inherited members#
Inherited from Instance 58
Properties (10)
Archivable, archivable, Capabilities, IsInSandbox, Name, Parent, PredictionMode, RobloxLocked, Sandboxed, UniqueId
Methods (39)
AddTag, children, ClearAllChildren, Clone, clone, Destroy, destroy, FindFirstAncestor, FindFirstAncestorOfClass, FindFirstAncestorWhichIsA, FindFirstChild, findFirstChild, FindFirstChildOfClass, FindFirstChildWhichIsA, FindFirstDescendant, GetActor, GetAttribute, GetAttributeChangedSignal, GetAttributes, GetChildren, getChildren, GetDebugId, GetDescendants, GetFullName, GetStyled, GetStyledPropertyChangedSignal, GetTags, HasTag, IsAncestorOf, IsDescendantOf, isDescendantOf, IsPropertyModified, QueryDescendants, Remove, remove, RemoveTag, ResetPropertyToDefault, SetAttribute, WaitForChild