ScriptDocument

Tampilkan yang Tidak Digunakan Lagi

*Konten ini diterjemahkan menggunakan AI (Beta) dan mungkin mengandung kesalahan. Untuk melihat halaman ini dalam bahasa Inggris, klik di sini.

Tidak Dapat Dibuat
Tidak Direplikasi

Sebuah instansi ScriptDocument adalah proxy dokumen dari Editor Skrip Studio.Ini berbeda dari LuaSourceContainer terbuka di editor dalam kenyataannya mewakili keadaan ephemeral dari dokumen terbuka, dan representasinya adalah dalam format yang lebih cocok untuk membaca dan mengedit kode daripada mengeksekusinya.Secara khusus, ScriptDocument mencerminkan setiap perubahan yang telah dilakukan pada skrip terbuka di Mode Rancangan, yang tidak dimiliki oleh properti sumber.

Editor Skrip itu sendiri ada dan berubah di thread yang berbeda dari setiap DataModel, jadi ScriptDocument mereplikasi Editor Skrip terbuka, tetapi itu bukan editor terbuka.Karena replicasi, terkadang ada sedikit kelambatan antara mengubah teks di editor dan memperbarui ScriptDocument.Penundaan biasanya terjadi karena DataModel sibuk, dan hampir selalu sangat kecil, tetapi masih ada.

Keberadaan ScriptDocument menunjukkan bahwa dokumen terbuka di Editor Skrip.Semua instansi ScriptDocument memiliki ScriptEditorService sebagai orangtuanya.Setiap instansi mematuhi konvensi enkode berikut:

  • Semua teks di ScriptDocument adalah dikodekan UTF-8.
  • Semua indeks baris diindeks 1.
  • Semua indeks karakter diindeks 1 dan menghitung bayangan UTF-8, bukan huruf, jadi peringatan yang sama dari TextBox.CursorPosition berlaku: banyak karakter Unicode mengambil lebih dari satu bayangan.
  • Semua rentang termasuk posisi awal mereka dan eksklusif dari posisi akhir mereka, jadi start == end berarti rentang kosong.

Semua API untuk ScriptDocument ada di tingkat keamanan Plugin .

Rangkuman

Metode

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

    Kembalikan teks baris yang ditentukan. Saat tidak ada argumen yang diberikan, kembalikan baris posisi kursor saat ini.

  • Keamanan Plugin

    Kembalikan jumlah baris di dokumen.

  • Keamanan Plugin

    Kembalikan kejadiandasar LuaSourceContainer , jika ada, jika tidak ada maka nil .

  • Keamanan Plugin

    Mendapatkan teks yang dipilih di editor, atau string kosong jika tidak ada pilihan.

  • Keamanan Plugin

    Kembalikan pilihan terakhir yang dikenal dari Editor Skrip dalam format: CursorLine, CursorChar, AnchorLine, AnchorChar . Jika Editor Skrip tidak memiliki pilihan, CursorLine == AnchorLine dan CursorChar == AnchorChar .

  • Keamanan Plugin

    Mendapatkan posisi kursor dan anker terbesar. Jika editor tidak memiliki pilihan, mereka adalah nilai yang sama.

  • Keamanan Plugin

    Mendapatkan posisi kursor dan anchor yang lebih kecil. Jika editor tidak memiliki pilihan, mereka adalah nilai yang sama.

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

    Kembalikan teks dari editor terbuka.

  • Keamanan Plugin

    Kembalikan nomor baris yang ditampilkan saat ini di perubahan editor.

  • Keamanan Plugin

    Kembalikan apakah atau tidak editor memiliki teks yang dipilih.

  • Keamanan Plugin

    Kembalikan benar jika ScriptDocument mewakili bilah Perintah.

  • Hasil
    Keamanan Plugin

    Permintaan bahwa editor yang terkait dengan dokumen ini ditutup. Menghasilkan thread saat ini sampai editor menanggapi permintaan.

  • EditTextAsync(newText : string,startLine : number,startCharacter : number,endLine : number,endCharacter : number):Tuple
    Hasil
    Keamanan Plugin

    Menggantikan teks dalam rentang yang ditentukan dari ( startLine , startColumn ) ke ( endLine , endColumn ) dengan teks baru.

  • ForceSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
    Hasil
    Keamanan Plugin

    Meminta editor untuk mengatur seleksi kurornya ke nilai argumen.

  • Hasil
    Keamanan Plugin
  • RequestSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
    Hasil
    Keamanan Plugin

    Meminta editor untuk mengatur seleksi kurornya ke nilai argumen.

Acara

Properti

Metode

GetLine

Keamanan Plugin

Parameter

lineIndex: number
Nilai Default: "nil"

Memberikan nilai

Contoh Kode

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

Keamanan Plugin

Memberikan nilai

Contoh Kode

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

Memberikan nilai

Contoh Kode

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

Keamanan Plugin

Memberikan nilai

Contoh Kode

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

Keamanan Plugin

Memberikan nilai

GetSelectionEnd

Keamanan Plugin

Memberikan nilai

Contoh Kode

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

Keamanan Plugin

Memberikan nilai

Contoh Kode

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

Keamanan Plugin

Parameter

startLine: number
Nilai Default: "nil"
startCharacter: number
Nilai Default: "nil"
endLine: number
Nilai Default: "nil"
endCharacter: number
Nilai Default: "nil"

Memberikan nilai

Contoh Kode

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

Keamanan Plugin

Memberikan nilai

Contoh Kode

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

Keamanan Plugin

Memberikan nilai

Contoh Kode

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

Keamanan Plugin

Memberikan nilai

Contoh Kode

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

Hasil
Keamanan Plugin

Memberikan nilai

Contoh Kode

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

Hasil
Keamanan Plugin

Parameter

newText: string
Nilai Default: ""
startLine: number
Nilai Default: ""
startCharacter: number
Nilai Default: ""
endLine: number
Nilai Default: ""
endCharacter: number
Nilai Default: ""

Memberikan nilai

ForceSetSelectionAsync

Hasil
Keamanan Plugin

Parameter

cursorLine: number
Nilai Default: ""
cursorCharacter: number
Nilai Default: ""
anchorLine: number
Nilai Default: "nil"
anchorCharacter: number
Nilai Default: "nil"

Memberikan nilai

Contoh Kode

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

Hasil
Keamanan Plugin

Parameter

edits: Array
Nilai Default: ""

Memberikan nilai

RequestSetSelectionAsync

Hasil
Keamanan Plugin

Parameter

cursorLine: number
Nilai Default: ""
cursorCharacter: number
Nilai Default: ""
anchorLine: number
Nilai Default: "nil"
anchorCharacter: number
Nilai Default: "nil"

Memberikan nilai

Contoh Kode

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

Acara

SelectionChanged

Keamanan Plugin

Parameter

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

Contoh Kode

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

Keamanan Plugin

Parameter

startLine: number
endLine: number

Contoh Kode

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)