ScriptDocument

Veraltete anzeigen

*Dieser Inhalt wurde mit KI (Beta) übersetzt und kann Fehler enthalten. Um diese Seite auf Englisch zu sehen, klicke hier.

Nicht erstellbar
Nicht repliziert

Eine ScriptDocument ist ein Proxy des Dokuments eines Studio-Skript-Editors.Es unterscheidet sich vom LuaSourceContainer , das im Editor geöffnet wird, da es den ephemeren Zustand eines offenen Dokuments darstellt, und seine Darstellung in einem Format ist, das besser geeignet ist, um Code zu lesen und zu bearbeiten, als ihn auszuführen.Insbesondere reflektiert ScriptDocument alle Änderungen, die am offenen Skript im Entwurfsmodus vorgenommen wurden, die die Eigenschaft der Quelle nicht hat.

Der Skript-Editor selbst existiert und ändert sich auf einem anderen Thread als jede DataModel, so repliziert der ScriptDocument den geöffneten Skript-Editor, aber er ist nicht der geöffnete Editor.Aufgrund der Replikation gibt es manchmal eine leichte Verzögerung zwischen dem Ändern des Textes im Editor und der Aktualisierung des ScriptDocument.Die Verzögerung tritt in der Regel auf, weil das DataModel beschäftigt ist und es fast immer extrem klein ist, aber es existiert immer noch.

Die Existenz eines ScriptDocument zeigt an, dass ein Dokument im Skript-Editor geöffnet ist.Alle ScriptDocument Instanzen haben ScriptEditorService als ihren übergeordnetes Teil.Jede Instanz hält sich an die folgenden Codierungsconventionen:

  • Alle Text in ScriptDocument ist UTF-8 codiert.
  • Alle Zeilenindizes sind 1-indexiert.
  • Alle Zeichenindizes sind 1-indexiert und zählen UTF-8-Bytes, nicht Grapheme, da die gleiche Warnung von TextBox.CursorPosition gilt: Viele Unicode-Zeichen nehmen mehr als einen Byte ein.
  • Alle Bereiche enthalten ihre Startposition und sind exklusiv für ihre Endposition, sodass start == end eine leere Reichweite impliziert.

Alle APIs für ScriptDocument sind auf Plugin Sicherheitsniveau.

Zusammenfassung

Methoden

  • GetLine(lineIndex : number?):string
    Plugin-Sicherheit

    Gibt den Text der angegebenen Zeile zurück. Wenn kein Argument angegeben wird, gibt er die Zeile der aktuellen Cursorposition zurück.

  • Plugin-Sicherheit

    Gibt die Anzahl der Zeilen im Dokument zurück.

  • Plugin-Sicherheit

    Gibt die zugrunde liegende LuaSourceContainer Instanz zurück, wenn es eine gibt, sonst nil .

  • Plugin-Sicherheit

    Holen Sie sich den Text aus, der im Editor ausgewählt wurde, oder eine leere Zeichenkette, wenn keine Auswahl vorliegt.

  • Plugin-Sicherheit

    Gibt die letzte bekannte Auswahl des Skripteditors im Format zurück: CursorLine, CursorChar, AnchorLine, AnchorChar . Wenn der Skripteditor keine Auswahl hat, CursorLine == AnchorLine und CursorChar == AnchorChar .

  • Plugin-Sicherheit

    Holt die größere Position des Cursors und des Ankers. Wenn der Editor keine Auswahl hat, sind sie dasselbe Wert.

  • Plugin-Sicherheit

    Holt die kleinere Position des Cursors und des Ankers. Wenn der Editor keine Auswahl hat, sind sie dasselbe Wert.

  • GetText(startLine : number?,startCharacter : number?,endLine : number?,endCharacter : number?):string
    Plugin-Sicherheit

    Gibt Text aus dem geöffneten Editor zurück.

  • Plugin-Sicherheit

    Gibt die derzeit angezeigten Zeilennummern im Editor-Änderungsprozess zurück.

  • Plugin-Sicherheit

    Gibt zurück, ob der Editor irgendeinen Text ausgewählt hat oder nicht.

  • Plugin-Sicherheit

    Gibt wahr zurück, wenn die ScriptDocument die Befehlsleiste darstellt.

  • Angehalten
    Plugin-Sicherheit

    Anfragen, die der Editor mit diesem Dokument verknüpft, schließen. Gibt den aktuellen Thread frei, bis der Editor auf die Anfrage antwortet.

  • EditTextAsync(newText : string,startLine : number,startCharacter : number,endLine : number,endCharacter : number):Tuple
    Angehalten
    Plugin-Sicherheit

    Ersetzt den Text in der angegebenen Reichweite von ( startLine, startColumn ) bis ( endLine, endColumn ) durch neuen Text.

  • ForceSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
    Angehalten
    Plugin-Sicherheit

    Bittet den Editor, seine Cursorauswahl auf die Argumentwerte zu setzen.

  • Angehalten
    Plugin-Sicherheit
  • RequestSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
    Angehalten
    Plugin-Sicherheit

    Bittet den Editor, seine Cursorauswahl auf die Argumentwerte zu setzen.

Events

Eigenschaften

Methoden

GetLine

Plugin-Sicherheit

Parameter

lineIndex: number
Standardwert: "nil"

Rückgaben

Code-Beispiele

ScriptDocument.SelectionChanged and ScriptDocument:GetLine()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function(positionLine, positionCharacter, anchorLine, anchorCharacter)
print(`Selected: Line {positionLine}, Char {positionCharacter}`)
print(`Anchor: Line {anchorLine}, Char {anchorCharacter}`)
local lineText = scriptDocument:GetLine(positionLine)
print(`Selected line text: {lineText}`)
end)
else
print("No scripts open")
end

GetLineCount

Plugin-Sicherheit

Rückgaben

Code-Beispiele

ScriptDocument:GetLineCount()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local lineCount = scriptDocument:GetLineCount()
print(`The script has {lineCount} lines!`)
else
print("No scripts open")
end
Plugin-Sicherheit

Rückgaben

Code-Beispiele

ScriptDocument:GetScript()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local openScript = scriptDocument:GetScript()
print(`Currently open script: {openScript:GetFullName()}`)
else
print("No scripts open")
end

GetSelectedText

Plugin-Sicherheit

Rückgaben

Code-Beispiele

ScriptDocument:HasSelectedText() and :GetSelectedText()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function()
if scriptDocument:HasSelectedText() then
local selectedText = scriptDocument:GetSelectedText()
print(`Currently selected text: {selectedText}`)
else
print("No text currently selected")
end
end)
else
print("No scripts open")
end

GetSelection

Plugin-Sicherheit

Rückgaben

GetSelectionEnd

Plugin-Sicherheit

Rückgaben

Code-Beispiele

ScriptDocument:GetSelectionStart() and :GetSelectionEnd()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local startLine, startCharacter = scriptDocument:GetSelectionStart()
local endLine, endCharacter = scriptDocument:GetSelectionEnd()
print(`Selection start: Line {startLine}, Char {startCharacter}`)
print(`Selection end: Line {endLine}, Char {endCharacter}`)
else
print("No scripts open")
end

GetSelectionStart

Plugin-Sicherheit

Rückgaben

Code-Beispiele

ScriptDocument:GetSelectionStart() and :GetSelectionEnd()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local startLine, startCharacter = scriptDocument:GetSelectionStart()
local endLine, endCharacter = scriptDocument:GetSelectionEnd()
print(`Selection start: Line {startLine}, Char {startCharacter}`)
print(`Selection end: Line {endLine}, Char {endCharacter}`)
else
print("No scripts open")
end

GetText

Plugin-Sicherheit

Parameter

startLine: number
Standardwert: "nil"
startCharacter: number
Standardwert: "nil"
endLine: number
Standardwert: "nil"
endCharacter: number
Standardwert: "nil"

Rückgaben

Code-Beispiele

ScriptDocument:GetText()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local text = scriptDocument:GetText()
print(`Script contents: {text}`)
else
print("No scripts open")
end

GetViewport

Plugin-Sicherheit

Rückgaben

Code-Beispiele

ScriptDocument:GetViewport

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local firstLine, lastLine = scriptDocument:GetViewport()
print(`Currently viewing lines {firstLine} to {lastLine}`)
else
print("No scripts open")
end

HasSelectedText

Plugin-Sicherheit

Rückgaben

Code-Beispiele

ScriptDocument:HasSelectedText() and :GetSelectedText()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function()
if scriptDocument:HasSelectedText() then
local selectedText = scriptDocument:GetSelectedText()
print(`Currently selected text: {selectedText}`)
else
print("No text currently selected")
end
end)
else
print("No scripts open")
end

IsCommandBar

Plugin-Sicherheit

Rückgaben

Code-Beispiele

ScriptDocument:IsCommandBar()

--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if document:IsCommandBar() then
print("Command bar document:", document)
end
end

CloseAsync

Angehalten
Plugin-Sicherheit

Rückgaben

Code-Beispiele

ScriptDocument:CloseAsync

--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
local scriptDocument
-- Find the first open script document
for _, document in documents do
-- The Command Bar can't be closed, so don't select it
if not document:IsCommandBar() then
scriptDocument = document
break
end
end
if scriptDocument then
local success, err = scriptDocument:CloseAsync()
if success then
print(`Closed {scriptDocument.Name}`)
else
warn(`Failed to close {scriptDocument.Name} because: {err}`)
end
else
print("No open scripts")
end

EditTextAsync

Angehalten
Plugin-Sicherheit

Parameter

newText: string
Standardwert: ""
startLine: number
Standardwert: ""
startCharacter: number
Standardwert: ""
endLine: number
Standardwert: ""
endCharacter: number
Standardwert: ""

Rückgaben

ForceSetSelectionAsync

Angehalten
Plugin-Sicherheit

Parameter

cursorLine: number
Standardwert: ""
cursorCharacter: number
Standardwert: ""
anchorLine: number
Standardwert: "nil"
anchorCharacter: number
Standardwert: "nil"

Rückgaben

Code-Beispiele

ScriptDocument:ForceSetSelectionAsync()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
-- Get the text on the cursor's current line
local cursorLine = scriptDocument:GetSelection()
local lineText = scriptDocument:GetLine(cursorLine)
-- Force select the entire line of text
local success, err = scriptDocument:ForceSetSelectionAsync(cursorLine, 1, cursorLine, #lineText + 1)
if success then
print("Set selection!")
else
print(`Failed to set selection because: {err}`)
end
else
print("No scripts open")
end

MultiEditTextAsync

Angehalten
Plugin-Sicherheit

Parameter

edits: Array
Standardwert: ""

Rückgaben

RequestSetSelectionAsync

Angehalten
Plugin-Sicherheit

Parameter

cursorLine: number
Standardwert: ""
cursorCharacter: number
Standardwert: ""
anchorLine: number
Standardwert: "nil"
anchorCharacter: number
Standardwert: "nil"

Rückgaben

Code-Beispiele

ScriptDocument:RequestSetSelectionAsync()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
-- Get the text on the cursor's current line
local cursorLine = scriptDocument:GetSelection()
local lineText = scriptDocument:GetLine(cursorLine)
-- Force select the entire line of text
local success, err = scriptDocument:RequestSetSelectionAsync(cursorLine, 1, cursorLine, #lineText + 1)
if success then
print("Set selection!")
else
print(`Failed to set selection because: {err}`)
end
else
print("No scripts open")
end

Events

SelectionChanged

Plugin-Sicherheit

Parameter

positionLine: number
positionCharacter: number
anchorLine: number
anchorCharacter: number

Code-Beispiele

ScriptDocument.SelectionChanged and ScriptDocument:GetLine()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function(positionLine, positionCharacter, anchorLine, anchorCharacter)
print(`Selected: Line {positionLine}, Char {positionCharacter}`)
print(`Anchor: Line {anchorLine}, Char {anchorCharacter}`)
local lineText = scriptDocument:GetLine(positionLine)
print(`Selected line text: {lineText}`)
end)
else
print("No scripts open")
end

ViewportChanged

Plugin-Sicherheit

Parameter

startLine: number
endLine: number

Code-Beispiele

Connecting to ScriptDocument.ViewportChanged

--!nocheck
--[[
To run:
1. Ensure Output view is open
2. Run the below code in the Command Bar
3. Scroll up and down in the opened Script window
Print statements from the ViewportChanged event will appear in the Output
]]
local Workspace = game:GetService("Workspace")
local ScriptEditorService = game:GetService("ScriptEditorService")
-- Create text that spans many lines
local dummyText = string.rep("-- Dummy Text\n", 60)
-- Create a script containing the dummy text and open it
local otherScript = Instance.new("Script")
otherScript.Source = dummyText
otherScript.Parent = Workspace
local success, err = ScriptEditorService:OpenScriptDocumentAsync(otherScript)
if not success then
warn(`Failed to open script because: {err}`)
return
end
-- Get a reference to the opened script
local scriptDocument = ScriptEditorService:FindScriptDocument(otherScript)
local function onViewportChanged(startLine: number, endLine: number)
print(`Script Viewport Changed - startLine: {startLine}, endLine: {endLine}`)
end
-- Connect the ViewportChanged event to the function above that prints the start and end line of the updated viewport
scriptDocument.ViewportChanged:Connect(onViewportChanged)