# ScriptDocument

Roblox Engine API class from Roblox Studio 0.744.0.7441077 (version-7aaa6838d36c4448).

- Inherits from, nearest first: [Instance](https://rapi.sleaze.dev/classes/Instance.md), [Object](https://rapi.sleaze.dev/classes/Object.md)
- Tags: Not creatable, Not replicated
- Memory category: Instances
- HTML page: https://rapi.sleaze.dev/classes/ScriptDocument/
- Roblox documentation: https://create.roblox.com/docs/reference/engine/classes/ScriptDocument

Represents the ephemeral state of an open document in the Studio Script Editor, providing APIs for reading and editing code.

## Methods

### AddReadOnlyReasonsAsync

```luau
ScriptDocument:AddReadOnlyReasonsAsync(reasons: { any })
```

- Tags: Yields, Plugin security

### CloseAsync

```luau
ScriptDocument:CloseAsync(): ...any
```

- Tags: Yields, Plugin security

Requests that the editor associated with this document close. Yields the current thread until the editor responds to the request.

Returns: A tuple where the first element is `true` if the editor closed successfully, or `false` followed by a string describing the failure reason.

### EditTextAsync

```luau
ScriptDocument:EditTextAsync(newText: string, startLine: number, startCharacter: number, endLine: number, endCharacter: number): ...any
```

- Tags: Yields, Plugin security

Replaces the text in the specified range from (`startLine`, `startColumn`) to (`endLine`, `endColumn`) with newText.

Parameters:

- `newText`: The replacement string to insert into the specified range.
- `startLine`: The 1-indexed line number where the replacement range begins.
- `startCharacter`: The 1-indexed UTF-8 byte offset within `startLine` where the replacement range begins.
- `endLine`: The 1-indexed line number where the replacement range ends (exclusive).
- `endCharacter`: The 1-indexed UTF-8 byte offset within `endLine` where the replacement range ends (exclusive).

Returns: A tuple where the first element is `true` if the edit was applied successfully, or `false` followed by a string describing the failure reason.

### ForceSetSelectionAsync

```luau
ScriptDocument:ForceSetSelectionAsync(cursorLine: number, cursorCharacter: number, anchorLine: number? = nil, anchorCharacter: number? = nil): ...any
```

- Tags: Yields, Plugin security

Asks the editor to set its cursor selection to the argument values.

Parameters:

- `cursorLine`: The 1-indexed line number for the cursor position.
- `cursorCharacter`: The 1-indexed UTF-8 byte offset within the line for the cursor position.
- `anchorLine`: The 1-indexed line number for the anchor position. Defaults to `cursorLine` if not provided.
- `anchorCharacter`: The 1-indexed UTF-8 byte offset within the line for the anchor position. Defaults to `cursorCharacter` if not provided.

Returns: A tuple where the first element is `true` if the cursor was updated, or `false` followed by a string explaining why the update was declined.

### GetInternalUri

```luau
ScriptDocument:GetInternalUri(): string
```

- Tags: Roblox script security

### GetLine

```luau
ScriptDocument:GetLine(lineIndex: number? = nil): string
```

- Tags: Plugin security

Returns the text of the specified line. When no argument is provided, returns the line of the current cursor position.

Parameters:

- `lineIndex`: The 1-indexed line number to retrieve. Defaults to the current cursor line if not provided.

Returns: The text content of the specified line as a string.

### GetLineCount

```luau
ScriptDocument:GetLineCount(): number
```

- Tags: Plugin security

Returns the number of lines in the document.

Returns: The total number of lines in the document.

### GetReadOnlyReasons

```luau
ScriptDocument:GetReadOnlyReasons(): { any }
```

- Tags: Plugin security

### GetScript

```luau
ScriptDocument:GetScript(): LuaSourceContainer
```

- Tags: Plugin security

Returns the underlying [`LuaSourceContainer`](https://rapi.sleaze.dev/classes/LuaSourceContainer.md) instance, if one exists, otherwise `nil`.

Returns: The [`LuaSourceContainer`](https://rapi.sleaze.dev/classes/LuaSourceContainer.md) that the document is editing, or `nil` if the document does not represent a script instance in the place (e.g. the Command Bar is not a real script instance).

### GetSelectedText

```luau
ScriptDocument:GetSelectedText(): string
```

- Tags: Plugin security

Gets the text selected in the editor, or an empty string if there is no selection.

Returns: The currently selected text as a string, or an empty string if nothing is selected.

### GetSelection

```luau
ScriptDocument:GetSelection(): ...any
```

- Tags: Plugin 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: CursorLine, CursorChar, AnchorLine, AnchorChar.

### GetSelectionEnd

```luau
ScriptDocument:GetSelectionEnd(): ...any
```

- Tags: Plugin security

Gets the larger of the cursor position and anchor. If the editor has no selection, they are the same value.

Returns: A tuple of (line, character) representing the larger of the cursor and anchor positions, both 1-indexed.

### GetSelectionStart

```luau
ScriptDocument:GetSelectionStart(): ...any
```

- Tags: Plugin security

Gets the smaller of the cursor position and anchor. If the editor has no selection, they are the same value.

Returns: A tuple of (line, character) representing the smaller of the cursor and anchor positions, both 1-indexed.

### GetText

```luau
ScriptDocument:GetText(startLine: number? = nil, startCharacter: number? = nil, endLine: number? = nil, endCharacter: number? = nil): string
```

- Tags: Plugin security

Returns text from the open editor.

Parameters:

- `startLine`: The 1-indexed line number where the text range begins. Optional; omit all arguments to get the entire document.
- `startCharacter`: The 1-indexed UTF-8 byte offset within `startLine` where the text range begins.
- `endLine`: 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`: The 1-indexed UTF-8 byte offset within `endLine` where the text range ends (exclusive).

Returns: The text content within the specified range as a string.

### GetViewport

```luau
ScriptDocument:GetViewport(): ...any
```

- Tags: Plugin security

Returns the currently displayed line numbers in the editor change.

Returns: A tuple of (startLine, endLine) representing the 1-indexed range of lines currently visible in the editor.

### HasSelectedText

```luau
ScriptDocument:HasSelectedText(): boolean
```

- Tags: Plugin security

Returns whether or not the editor has any text selected.

Returns: `true` if the editor has a non-empty text selection, `false` otherwise.

### IsCommandBar

```luau
ScriptDocument:IsCommandBar(): boolean
```

- Tags: Plugin security

Returns true if the [`ScriptDocument`](https://rapi.sleaze.dev/classes/ScriptDocument.md) represents the Command bar.

Returns: `true` if this document represents the Command bar, `false` otherwise.

### IsReadOnly

```luau
ScriptDocument:IsReadOnly(): boolean
```

- Tags: Plugin security

### MultiEditTextAsync

```luau
ScriptDocument:MultiEditTextAsync(edits: { any }): ...any
```

- Tags: Yields, Plugin security

Applies a batch of text edits to the document as a single atomic operation. Yields the current thread until the editor responds.

Parameters:

- `edits`: An array of tables, each describing a text replacement with fields `NewText`, `StartLine`, `StartCharacter`, `EndLine`, and `EndCharacter`.

Returns: A tuple where the first element is `true` if all edits were applied successfully, or `false` followed by a string describing the failure reason.

### RemoveReadOnlyReasonsAsync

```luau
ScriptDocument:RemoveReadOnlyReasonsAsync(reasons: { any })
```

- Tags: Yields, Plugin security

### RequestSetSelectionAsync

```luau
ScriptDocument:RequestSetSelectionAsync(cursorLine: number, cursorCharacter: number, anchorLine: number? = nil, anchorCharacter: number? = nil): ...any
```

- Tags: Yields, Plugin security

Asks the editor to set its cursor selection to the argument values.

Parameters:

- `cursorLine`: The 1-indexed line number for the cursor position.
- `cursorCharacter`: The 1-indexed UTF-8 byte offset within the line for the cursor position.
- `anchorLine`: The 1-indexed line number for the anchor position. Defaults to `cursorLine` if not provided.
- `anchorCharacter`: The 1-indexed UTF-8 byte offset within the line for the anchor position. Defaults to `cursorCharacter` if not provided.

Returns: A tuple where the first element is `true` if the cursor was updated, or `false` followed by a string explaining why the update was declined.

### ReviewableTextEditsAsync

```luau
ScriptDocument:ReviewableTextEditsAsync(changes: { any }): ...any
```

- Tags: Yields, Plugin security

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.

Parameters:

- `changes`: 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: A tuple where the first element is `true` if the edits were applied successfully, or `false` followed by a string describing the failure reason.

## Events

### SelectionChanged

```luau
ScriptDocument.SelectionChanged: RBXScriptSignal<(positionLine: number, positionCharacter: number, anchorLine: number, anchorCharacter: number)>
```

- Tags: Plugin security

Fires when the ScriptDocument changes, including immediately after a text change.

### ViewportChanged

```luau
ScriptDocument.ViewportChanged: RBXScriptSignal<(startLine: number, endLine: number)>
```

- Tags: Plugin security

Fires when the displayed line numbers in the editor change.

## Inherited members

- From [Instance](https://rapi.sleaze.dev/classes/Instance.md): `Archivable`, `archivable`, `Attributes`, `AttributesReplicate`, `AttributesSerialize`, `HistoryId`, `Name`, `Parent`, `PredictionMode`, `PropertyStatusStudio`, `RobloxLocked`, `SerializedOverrides`, `SourceAssetId`, `Tags`, `UniqueId`, `numExpectedDirectChildren`, `DataCost`, `Capabilities`, `DefinesCapabilities`, `IsInSandbox`, `Sandboxed`, `AddTag`, `ClearAllChildren`, `Clone`, `Destroy`, `FindFirstAncestor`, `FindFirstAncestorOfClass`, `FindFirstAncestorWhichIsA`, `FindFirstChild`, `FindFirstChildOfClass`, `FindFirstChildWhichIsA`, `FindFirstDescendant`, `GetActor`, `GetAttribute`, `GetAttributeChangedSignal`, `GetAttributes`, `GetChildren`, `GetDebugId`, `GetDescendants`, `GetFullName`, `GetStyled`, `GetStyledPropertyChangedSignal`, `GetTags`, `HasTag`, `IsAncestorOf`, `IsDescendantOf`, `IsPropertyModified`, `QueryDescendants`, `RemoveTag`, `ResetPropertyToDefault`, `SetAttribute`, `WaitForChild`, `Remove`, `children`, `clone`, `destroy`, `findFirstChild`, `getChildren`, `isDescendantOf`, `remove`, `AncestryChanged`, `AttributeChanged`, `ChildAdded`, `ChildRemoved`, `DescendantAdded`, `DescendantRemoving`, `Destroying`, `StyledPropertiesChanged`, `childAdded`
- From [Object](https://rapi.sleaze.dev/classes/Object.md): `ClassName`, `className`, `GetPropertyChangedSignal`, `IsA`, `isA`, `Changed`
