ScriptDocument

Visualizza obsoleti

*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.

Non costruibile
Non Replicato

Un'istanza A ScriptDocument è un proxy del documento di uno Studio Editore script.Si differenzia dal LuaSourceContainer aperto nell'editor in quanto rappresenta lo stato ephemerale di un documento aperto, e la sua rappresentazione è in un formato più adatto alla lettura e all'editing del codice piuttosto che alla sua esecuzione.In particolare, ScriptDocument riflette qualsiasi modifica che è stata apportata allo script aperto in Modalità Bozze, che la proprietà di origine non ha.

L'Editor di script stesso esiste e cambia su un thread diverso da qualsiasi DataModel, quindi il ScriptDocument replica l'Editore scriptaperto, ma non è l'editor aperto.A causa della replicazione, a volte c'è un leggero ritardo tra il cambio del testo nell'editor e l'aggiornamento del ScriptDocument.Il ritardo si verifica di solito perché il DataModel è occupato, e è quasi sempre estremamente piccolo, ma esiste ancora.

L'esistenza di un ScriptDocument indica che un documento è aperto nell'Editor di script.Tutte le ScriptDocument hanno come parent>.Ogni istanza aderisce alle seguenti convenzioni di codifica:

  • Tutto il testo in ScriptDocument è codificato in UTF-8.
  • Tutti gli indici di linea sono 1-indexati.
  • Tutti gli indici dei personaggi sono 1-indexati e contano byte UTF-8, non grafemi, quindi si applica lo stesso avviso da TextBox.CursorPosition : molti personaggi Unicode occupano più di un byte.
  • Tutti gli intervalli sono inclusi nella loro posizione di inizio e esclusivi della loro posizione finale, quindi start == end implica un intervallo vuoto.

Tutte le API per ScriptDocument sono a livello di sicurezza del plugin .

Sommario

Metodi

  • GetLine(lineIndex : number?):string
    Sicurezza Plugin

    Restituisce il testo della linea specificata. Quando non viene fornito alcun argomento, restituisce la linea della posizione del cursore attuale.

  • Sicurezza Plugin

    Restituisce il numero di righe nel documento.

  • Sicurezza Plugin

    Restituisce l'esempiosottostante LuaSourceContainer , se esiste, altrimenti nil .

  • Sicurezza Plugin

    Ottiene il testo selezionato nell'editor, o una stringa vuota se non c'è una selezione.

  • Sicurezza Plugin

    Restituisce l'ultima selezione conosciuta dello Editor di Script nel formato: CursorLine, CursorChar, AnchorLine, AnchorChar . Se lo Editor di Script non ha una selezione, CursorLine == AnchorLine e CursorChar == AnchorChar .

  • Sicurezza Plugin

    Ottiene la posizione del cursore e l'ancoraggio più grande. Se l'editor non ha una selezione, sono lo stesso valore.

  • Sicurezza Plugin

    Ottiene la posizione del cursore e l'ancoraggio più piccola. Se l'editor non ha una selezione, sono lo stesso valore.

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

    Restituisce il testo dall'editor aperto.

  • Sicurezza Plugin

    Restituisce i numeri di linea attualmente visualizzati nella modifica dell'editor.

  • Sicurezza Plugin

    Restituisce se l'editor ha o meno del testo selezionato.

  • Sicurezza Plugin

    Restituisce vero se il ScriptDocument rappresenta la barra dei comandi.

  • Resa
    Sicurezza Plugin

    Richieste che l'editor associato a questo documento Chiudere. Produce il thread attuale fino a quando l'editor non risponde alla Richiesta.

  • EditTextAsync(newText : string,startLine : number,startCharacter : number,endLine : number,endCharacter : number):Tuple
    Resa
    Sicurezza Plugin

    Sostituisce il testo nella gamma specificata da ( startLine , startColumn ) a ( endLine , endColumn ) con il nuovo testo.

  • ForceSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
    Resa
    Sicurezza Plugin

    Chiede all'editor di impostare la selezione del cursore ai valori degli argomenti.

  • Resa
    Sicurezza Plugin
  • RequestSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
    Resa
    Sicurezza Plugin

    Chiede all'editor di impostare la selezione del cursore ai valori degli argomenti.

Eventi

Proprietà

Metodi

GetLine

Sicurezza Plugin

Restituisce il testo della linea specificata. Quando non viene fornito alcun argomento, restituisce la linea della posizione del cursore attuale.

Parametri

lineIndex: number
Valore predefinito: "nil"

Restituzioni

Campioni di codice

ScriptDocument.SelectionChanged and ScriptDocument:GetLine()

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

Sicurezza Plugin

Restituisce il numero di righe nel documento attivo.


Restituzioni

Campioni di codice

ScriptDocument:GetLineCount()

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
Sicurezza Plugin

Restituisce l'esempiosottostante LuaSourceContainer , se esiste, altrimenti nil .


Restituzioni

Campioni di codice

ScriptDocument:GetScript()

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

Sicurezza Plugin

Ottiene il testo selezionato nell'editor, o una stringa vuota se non c'è una selezione.


Restituzioni

Campioni di codice

ScriptDocument:HasSelectedText() and :GetSelectedText()

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

Sicurezza Plugin

Restituisce l'ultima selezione conosciuta dello Editor di Script nel formato: CursorLine, CursorChar, AnchorLine, AnchorChar . Se lo Editor di Script non ha una selezione, CursorLine == AnchorLine e CursorChar == AnchorChar .


Restituzioni

CursorLine, CursorChar, AncoraggioLinea, AncoraggioChar.

GetSelectionEnd

Sicurezza Plugin

Ottiene la posizione del cursore e l'ancoraggio più grande. Se l'editor non ha una selezione, sono lo stesso valore.


Restituzioni

Campioni di codice

ScriptDocument:GetSelectionStart() and :GetSelectionEnd()

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

Sicurezza Plugin

Ottiene la posizione del cursore e l'ancoraggio più piccola. Se l'editor non ha una selezione, sono lo stesso valore.


Restituzioni

Campioni di codice

ScriptDocument:GetSelectionStart() and :GetSelectionEnd()

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

Sicurezza Plugin

Restituisce il testo dall'editor aperto. Deve essere chiamato con 0, 2 o 4 argomenti:

  • Se chiamato con 0 argomenti, ottiene l'intero contenuto dell'editor aperto.
  • Se chiamato con 2 argomenti, ottiene il testo del documento a partire da ( startLine, startColumn ).
  • Se chiamato con 4 argomenti, ottiene il testo del documento a partire da ( startLine, startColumn ) e finisce a ( endLine, endColumn ).

Parametri

startLine: number
Valore predefinito: "nil"
startCharacter: number
Valore predefinito: "nil"
endLine: number
Valore predefinito: "nil"
endCharacter: number
Valore predefinito: "nil"

Restituzioni

Campioni di codice

ScriptDocument:GetText()

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

Sicurezza Plugin

Restituisce i numeri di linea attualmente visualizzati nella modifica dell'editor.L'editor visualizza le linee tra startLine e endLine, incluse.La prima e l'ultima linea potrebbero essere visualizzate solo parzialmente.Ad esempio, solo il pixel superiore dell'ultima riga potrebbe essere sullo schermo.Inoltre, il piegamento del codice potrebbe nascondere le linee tra startLine e endLine.


Restituzioni

Campioni di codice

ScriptDocument:GetViewport

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

Sicurezza Plugin

Restituisce se l'editor ha o meno del testo selezionato.


Restituzioni

Campioni di codice

ScriptDocument:HasSelectedText() and :GetSelectedText()

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

Sicurezza Plugin

Restituisce vero se il ScriptDocument rappresenta la barra dei comandi. La barra dei comandi ha regole e limitazioni speciali in questa API:

  • Studio crea la barra dei comandi prima di eseguire i plugin, quindi non sempre lancia l'evento aperto, anche se chiude e riapre durante le transizioni di Studio tra i modelli di dati.
  • Non puoi modificare la barra dei comandi con EditTextAsync per motivi di sicurezza.

Restituzioni

Campioni di codice

ScriptDocument:IsCommandBar()

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

Resa
Sicurezza Plugin

Richieste che l'editor associato a questo documento Chiudere.Rilascia il thread attuale fino a quando l'editor non risponde alla Richiesta.Se la funzione ha successo, restituisce (true, nil).Se la funzione fallisce, restituisce (false, Stringa) come descrizione del problema.

Questa funzione non può chiudere la barra dei comandi.


Restituzioni

Campioni di codice

ScriptDocument:CloseAsync

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

Resa
Sicurezza Plugin

Sostituisce il testo nella gamma specificata da ( startLine , startColumn ) a ( endLine , endColumn ) con newText .Se l'intervallo è vuoto, allora la funzione inserisce il testo a ( startLine, startColumn ).Se il cursore del testo è all'interno dell'intervallo specificato, il cursore si sposta alla posizione finale dell'Modificare.Altrimenti, il cursore del testo non si Sposta.Questa funzione restituisce il thread attuale fino a quando non riceve una risposta dall'editor sull'Modificare.

Se la funzione ha successo, restituisce ( true , nil ).

La funzione lancia un errore se:

  • Il range non è valido.
  • Il raggio taglierà un carattere Unicode, ad esempio sostituirà solo alcuni dei bytes del carattere Unicode.
  • Il newText stesso contiene UTF-8 non valido.

Se la funzione fallisce, restituisce (false, Stringa).La stringa è una descrizione del problema.Il tipo di fallimento più comune è una non corrispondenza della versione.Questo accade quando tenti di chiamare EditTextAsync durante il tempo in cui il ScriptDocument è fuori sincronia con i contenuti dell'editor.Se ciò accade, puoi riprovare l'Modificare.

Parametri

newText: string
Valore predefinito: ""
startLine: number
Valore predefinito: ""
startCharacter: number
Valore predefinito: ""
endLine: number
Valore predefinito: ""
endCharacter: number
Valore predefinito: ""

Restituzioni

ForceSetSelectionAsync

Resa
Sicurezza Plugin

Chiede all'editor di impostare la selezione del cursore ai valori degli argomenti.Entrambi gli argomenti dell'ancoraggio devono essere passati, o nessuno.Se nessuno viene passato, allora ognuno di essi predefinisce di essere lo stesso dell'argomento cursore corrispondente.L'editor potrebbe rifiutarsi di aggiornare il cursore se il contenuto del testo del documento è cambiato.A differenza di ScriptDocument:RequestSetSelectionAsync(), l'editor non rifiuterà di spostare il cursore se il cursore si è spostato da quando è stata fatta la richiesta.Restituisce (vero, nil) se il cursore è stato aggiornato, e (false, Stringa) con una stringa di spiegazione se non lo era.Produce il thread attuale fino a quando l'editor risponde.

Parametri

cursorLine: number
Valore predefinito: ""
cursorCharacter: number
Valore predefinito: ""
anchorLine: number
Valore predefinito: "nil"
anchorCharacter: number
Valore predefinito: "nil"

Restituzioni

Campioni di codice

ScriptDocument:ForceSetSelectionAsync()

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

Resa
Sicurezza Plugin

Parametri

edits: Array
Valore predefinito: ""

Restituzioni

RequestSetSelectionAsync

Resa
Sicurezza Plugin

Chiede all'editor di impostare la selezione del cursore ai valori degli argomenti.Entrambi gli argomenti dell'ancoraggio devono essere passati, o nessuno.Se nessuno viene passato, allora ognuno di essi predefinisce di essere lo stesso dell'argomento cursore corrispondente.L'editor potrebbe rifiutarsi di aggiornare il cursore se il contenuto del testo del documento è cambiato o il cursore si è spostato da quando è stata fatta la richiesta.Restituisce (vero, nil) se il cursore è stato aggiornato, e (false, Stringa) con una stringa di spiegazione se non lo era.Produce il thread attuale fino a quando l'editor risponde.

Parametri

cursorLine: number
Valore predefinito: ""
cursorCharacter: number
Valore predefinito: ""
anchorLine: number
Valore predefinito: "nil"
anchorCharacter: number
Valore predefinito: "nil"

Restituzioni

Campioni di codice

ScriptDocument:RequestSetSelectionAsync()

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

Eventi

SelectionChanged

Sicurezza Plugin

Si attiva quando il ScriptDocument cambia, incluso immediatamente dopo un cambio di testo.

Parametri

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

Campioni di codice

ScriptDocument.SelectionChanged and ScriptDocument:GetLine()

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

Sicurezza Plugin

Si accende quando i numeri di linea visualizzati nell'editor cambiano. Vedi ScriptDocument.GetViewport per i dettagli.

Parametri

startLine: number
endLine: number

Campioni di codice

Demonstrates using ScriptDocument.ViewportChanged to print the start and end line of the script's viewport when it changes.

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
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)