ScriptDocument
*Ta zawartość została przetłumaczona przy użyciu narzędzi AI (w wersji beta) i może zawierać błędy. Aby wyświetlić tę stronę w języku angielskim, kliknij tutaj.
Instancja ScriptDocument jest proxy dokumentu edytora skryptów Studio.Różni się od LuaSourceContainer otwartego w edytorze, ponieważ reprezentuje stan tymczasowy otwartego dokumentu i jego reprezentacja jest w formacie, który jest bardziej odpowiedni do czytania i edytowania kodu niż jego uruchamiania.W szczególności ScriptDocument odzwierciedla wszelkie zmiany, które zostały wprowadzone do otwartego skryptu w trybie projektów, których właściwość źródła nie ma.
Edytor skryptów sam w sobie istnieje i zmienia się na innym wątku niż jakikolwiek DataModel, więc ScriptDocument replikuje otwarty edytor skryptów, ale nie jest to otwarty edytor.Z powodu replikacji czasami występuje niewielka zwłoka między zmianą tekstu w edytorze a aktualizacją ScriptDocument.Opóźnienie zwykle występuje, ponieważ DataModel jest zajęty, a jest prawie zawsze bardzo mały, ale nadal istnieje.
Istnienie ScriptDocument wskazuje, że dokument jest otwarty w edytorze skryptów.Wszystkie instancje ScriptDocument mają ScriptEditorService jako swojego rodzica.Każda instancja przestrzega następujących konwencji kodowania:
- Cały tekst w ScriptDocument jest kodowany w UTF-8.
- Wszystkie indeksy linii są indeksowane 1.
- Wszystkie indeksy postaci są indeksowane 1 i liczą bajty UTF-8, a nie grafemy, więc ta sama ostrzeżenie z TextBox.CursorPosition ma zastosowanie: wiele znaków Unicode zajmuje więcej niż jeden bajt.
- Wszystkie zakresy obejmują pozycję startową i wyłączną pozycję końcową, więc start == end oznacza pusty zakres.
Wszystkie API dla ScriptDocument są na poziomie bezpieczeństwa Plugin .
Podsumowanie
Metody
Zwraca tekst określonej linii. Gdy nie zostanie podany żaden argument, zwraca linię pozycji obecnego kurora.
Zwraca liczbę linii w dokumencie.
Zwraca podstawową instancję LuaSourceContainer, jeśli istnieje, w przeciwnym razie nil.
Zwraca tekst wybrany w edytorze lub pustą строку, jeśli nie ma wyboru.
Zwraca ostatnią znaną selekcję edytora skryptów w formacie: CursorLine, CursorChar, AnchorLine, AnchorChar . Jeśli edytor skryptów nie ma wyboru, CursorLine == AnchorLine i CursorChar == AnchorChar .
Otrzymuje największą pozycję kurora i zakotwiczenia. Jeśli edytor nie ma wyboru, są taką samą wartością.
Otrzymuje najmniejszą pozycję kurora i zakotwiczenia. Jeśli edytor nie ma wyboru, są taką samą wartością.
- GetText(startLine : number?,startCharacter : number?,endLine : number?,endCharacter : number?):string
Zwraca tekst z otwartego edytora.
Zwraca obecnie wyświetlane numery linii w zmianie edytora.
Zwraca, czy edytor ma wybrany tekst, czy nie.
Wyświetla prawdę, jeśli ScriptDocument reprezentuje pasek poleceń.
Żądania, które edytor powiązany z tym dokumentem zamykać. Zapewnia obecny wątek, dopóki edytor nie odpowie na prośba.
- EditTextAsync(newText : string,startLine : number,startCharacter : number,endLine : number,endCharacter : number):Tuple
Zastępuje tekst w określonym zakresie od ( startLine, startColumn ) do ( endLine, endColumn ) nowym tekstem.
- ForceSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
Pyta edytora, aby ustawił wybór kurora na wartości argumentów.
- RequestSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
Pyta edytora, aby ustawił wybór kurora na wartości argumentów.
Zdarzenia
- SelectionChanged(positionLine : number,positionCharacter : number,anchorLine : number,anchorCharacter : number):RBXScriptSignal
Wystrzeliwuje się, gdy zmienia się dokument skryptowy, w tym bezpośrednio po zmianie tekstu.
Wystrzeliwuje, gdy zmienia się wyświetlany numer linii w edytorze
Właściwości
Metody
GetLine
Zwraca tekst określonej linii. Gdy nie zostanie podany żaden argument, zwraca linię pozycji obecnego kurora.
Parametry
Zwroty
Przykłady kodu
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
Zwraca liczbę linii w aktywnym dokumencie.
Zwroty
Przykłady kodu
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
GetScript
Zwraca podstawową instancję LuaSourceContainer, jeśli istnieje, w przeciwnym razie nil.
Zwroty
Przykłady kodu
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
Zwraca tekst wybrany w edytorze lub pustą строку, jeśli nie ma wyboru.
Zwroty
Przykłady kodu
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
Zwraca ostatnią znaną selekcję edytora skryptów w formacie: CursorLine, CursorChar, AnchorLine, AnchorChar . Jeśli edytor skryptów nie ma wyboru, CursorLine == AnchorLine i CursorChar == AnchorChar .
Zwroty
CursorLine, CursorChar, Ankorowa linia, Ankorowy charakter.
GetSelectionEnd
Otrzymuje największą pozycję kurora i zakotwiczenia. Jeśli edytor nie ma wyboru, są taką samą wartością.
Zwroty
Przykłady kodu
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
Otrzymuje najmniejszą pozycję kurora i zakotwiczenia. Jeśli edytor nie ma wyboru, są taką samą wartością.
Zwroty
Przykłady kodu
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
Wyświetla tekst z otwartego edytora. Musi być wywołany z 0, 2 lub 4 argumentami:
- Jeśli wezwano z argumentami 0, otrzymuje całą zawartość otwartego edytora.
- Jeśli wezwano z 2 argumentami, otrzymuje tekst dokumentu rozpoczynający się od ( startLine, startColumn ).
- Jeśli wezwany z 4 argumentami, otrzymuje tekst dokumentu rozpoczynający się od ( startLine, startColumn ) i kończący się na ( endLine, endColumn ).
Parametry
Zwroty
Przykłady kodu
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
Zwraca obecnie wyświetlane numery linii w zmianie edytora.Edytor wyświetla linie między startLine a endLine, włącznie.Pierwsza i ostatnia linia może wyświetlać tylko częściowo.Na przykład tylko najwyższy piksel ostatniej linii może być na ekranie.Ponadto składanie kodu może ukrywać linie między startLine a endLine.
Zwroty
Przykłady kodu
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
Zwraca, czy edytor ma wybrany tekst, czy nie.
Zwroty
Przykłady kodu
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
Wyświetla prawdę, jeśli ScriptDocument reprezentuje pasek poleceń. Pasek poleceń ma specjalne zasady i ograniczenia w tym API:
- Studio tworzy pasek poleceń przed uruchomieniem pluginów, więc nie zawsze uruchamia otwarte wydarzenie, choć zamyka i ponownie otwiera, gdy Studio przełącza się między modelami danych.
- Nie możesz edytować paska poleceń z EditTextAsync ze względów bezpieczeństwa.
Zwroty
Przykłady kodu
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
Prośby, aby edytor powiązany z tym dokumentem zamykać.Wydaje obecny wątek, aż edytor odpowie na prośba.Jeśli funkcja się powiedzie, zwraca (true, nil).Jeśli funkcja zawodzi, zwraca (false, ciąg) jako opis problemu.
Funkcja ta nie może zamknąć paska poleceń.
Zwroty
Przykłady kodu
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
Zastępuje tekst w określonym zakresie od ( startLine, startColumn ) do ( endLine, endColumn ) za pomocą newText .Jeśli zakres jest pusty, funkcja wstawia tekst na ( startLine , startColumn ).Jeśli kurser tekstowy znajduje się w określonym zakresie, kurser przesuwa się do końcowej pozycji edytowania.W przeciwnym razie kurser tekstu się nie porusza.Funkcja ta zwraca obecny wątek, dopóki nie otrzyma od edytora odpowiedzi o edycji.
Jeśli funkcja się powiedzie, zwraca (true, nil).
Funkcja rzuca błąd, jeśli:
- Zasięg jest nieprawidłowe.
- Zasięg obniżałby znak unikodowy, na przykład zastąpił tylko niektóre bajty znaku unikodowego.
- Sama newText zawiera nieprawidłowe UTF-8.
Jeśli funkcja nie powodzi się, zwraca (false, ciąg).Sznurek to opis problemu.Najczęstszy typ awarii to rozbieżność wersji.Występuje to, gdy próbujesz wezwać EditTextAsync podczas czasu, gdy ScriptDocument jest w niezgodności z zawartością edytora.Jeśli tak się stanie, możesz spróbować ponownie edytować.
Parametry
Zwroty
ForceSetSelectionAsync
Pyta edytora, aby ustawił wybór kurora na wartości argumentów.Muszą być przekazane oba argumenty kotwicy, albo żadne.Jeśli żadna z nich nie zostanie przekazana, każda z nich domyślnie będzie taka sama jak odpowiadający argument kurora.Edytor może odmówić aktualizacji kurora, jeśli treść dokumentu zmieniła się.W przeciwieństwie do ScriptDocument:RequestSetSelectionAsync(), edytor nie odrzuci przeniesienia kurora, jeśli kuror się poruszył od czasu złożenia żądania.Zwraca (true, nil), jeśli kurser został zaktualizowany, i (false, string) z wyjaśnieniem ciągu znaków, jeśli nie został.Wydaje obecny wątek, aż edytor odpowie.
Parametry
Zwroty
Przykłady kodu
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
RequestSetSelectionAsync
Pyta edytora, aby ustawił wybór kurora na wartości argumentów.Muszą być przekazane oba argumenty kotwicy, albo żadne.Jeśli żadna z nich nie zostanie przekazana, każda z nich domyślnie będzie taka sama jak odpowiadający argument kurora.Edytor może odmówić aktualizacji kurora, jeśli treść dokumentu zmieniła się lub kuror się przesunął od czasu złożenia żądania.Zwraca (true, nil), jeśli kurser został zaktualizowany, i (false, string) z wyjaśnieniem ciągu znaków, jeśli nie został.Wydaje obecny wątek, aż edytor odpowie.
Parametry
Zwroty
Przykłady kodu
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
Zdarzenia
SelectionChanged
Wystrzeliwuje się, gdy zmienia się dokument skryptowy, w tym bezpośrednio po zmianie tekstu.
Parametry
Przykłady kodu
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
Wystrzeliwuje, gdy zmienia się wyświetlana kolejność numerów linii w edytorze. Zobacz ScriptDocument.GetViewport dla szczegółów.
Parametry
Przykłady kodu
Demonstrates using ScriptDocument.ViewportChanged to print the start and end line of the script's viewport when it changes.
To run:
- Ensure Output view is open
- Run the below code in the Command Bar
- Scroll up and down in the opened Script window
--!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)