ScriptDocument
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Một ví dụ ScriptDocument instance là một proxy của tài liệu của Trình soạn thảo Tập lệnh Studio.Nó khác với LuaSourceContainer mở trong trình soạn thảo ở chỗ nó đại diện cho trạng thái thoáng qua của một tài liệu mở, và sự thể hiện của nó là trong một định dạng phù hợp hơn để đọc và chỉnh sửa mã hơn là thực hiện nó.Cụ thể, ScriptDocument phản ánh bất kỳ thay đổi nào đã được thực hiện cho mã mở trong chế độ Bản nháp, mà thuộc tính nguồn không có.
Chính Trình soạn thảo Scrip tồn tại và thay đổi trên một luồng khác hơn bất kỳ DataModel , vì vậy ScriptDocument sao lưu Trình soạn thảo Scrip mở, nhưng nó không phải là trình soạn thảo mở.Vì replication, đôi khi có một chút trễ giữa việc thay đổi văn bản trong editor và cập nhật ScriptDocument.Thời gian trễ thường xảy ra vì DataModel bận rộn, và nó gần như luôn rất nhỏ, nhưng nó vẫn tồn tại.
Sự hiện hữu của một ScriptDocument cho thấy rằng một tài liệu đang mở trong Trình soạn thảo Tập lệnh.Tất cả các ScriptDocument có ScriptEditorService làm cha của nó.Mỗi ví dụ tuân theo các tiêu chuẩn mã hóa sau:
- Tất cả văn bản trong ScriptDocument được mã hóa bằng UTF-8.
- Tất cả các chỉ mục dòng được đánh số 1.
- Tất cả các chỉ mục nhân vật được đánh số 1 và đếm các bayt UTF-8, không phải là các grapheme, vì vậy cảnh báo tương tự từ TextBox.CursorPosition áp dụng: nhiều nhân vật Unicode cần hơn một bayt.
- Tất cả các phạm vi bao gồm vị trí bắt đầu của chúng và loại trừ vị trí cuối cùng của chúng, vì vậy start == end có nghĩa là một phạm vi trống.
Tất cả các API cho ScriptDocument ở cấp độ bảo mật Plugin .
Tóm Tắt
Phương Pháp
Trả văn bản của dòng được chỉ định. Khi không cung cấp tham số, trả lại dòng của vị trí con trỏ hiện tại.
Trả về số dòng trong tài liệu.
Trả về ví dụ / trường hợpcơ sở LuaSourceContainer nếu có, nếu không có thì nil .
Nhận văn bản được chọn trong trình soạn thảo, hoặc một chuỗi trống nếu không có lựa chọn.
Trả về lựa chọn cuối cùng được biết đến của Trình biên tập Scrip trong định dạng: CursorLine, CursorChar, AnchorLine, AnchorChar . Nếu Trình biên tập Scrip không có lựa chọn, CursorLine == AnchorLine và CursorChar == AnchorChar .
Nhận vị trí con trỏ và neo lớn hơn. Nếu trình soạn thảo không có lựa chọn, chúng có giá trị tương tự.
Nhận vị trí con trỏ và neo nhỏ hơn. Nếu trình soạn thảo không có lựa chọn, chúng có giá trị tương tự.
- GetText(startLine : number?,startCharacter : number?,endLine : number?,endCharacter : number?):string
Trả văn bản từ trình soạn thảo mở.
Trả về số dòng hiển thị hiện tại trong thay đổi biên tập.
Trả về xem có hay không có văn bản được chọn bởi biên tập viên.
Trả về true nếu ScriptDocument đại diện cho thanh Command.
Các yêu cầu mà người biên tập liên kết với tài liệu này đóng. Tạo luồng hiện tại cho đến khi người biên tập trả lời yêu cầu.
- EditTextAsync(newText : string,startLine : number,startCharacter : number,endLine : number,endCharacter : number):Tuple
Thay thế văn bản trong phạm vi được chỉ định từ ( startLine, startColumn ) đến ( endLine, endColumn ) với văn bản mới.
- ForceSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
Yêu cầu người soạn thảo đặt lựa chọn con trỏ của nó vào các giá trị tham số.
- RequestSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
Yêu cầu người soạn thảo đặt lựa chọn con trỏ của nó vào các giá trị tham số.
Sự Kiện
- SelectionChanged(positionLine : number,positionCharacter : number,anchorLine : number,anchorCharacter : number):RBXScriptSignal
Bắt lửa khi ScriptDocument thay đổi, bao gồm ngay sau khi thay đổi văn bản.
Bắt lửa khi số dòng hiển thị trong editor thay đổi.
Thuộc Tính
Phương Pháp
GetLine
Tham Số
Lợi Nhuận
Mẫu mã
--!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
Lợi Nhuận
Mẫu mã
--!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
Lợi Nhuận
Mẫu mã
--!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
Lợi Nhuận
Mẫu mã
--!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
GetSelectionEnd
Lợi Nhuận
Mẫu mã
--!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
Lợi Nhuận
Mẫu mã
--!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
Tham Số
Lợi Nhuận
Mẫu mã
--!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
Lợi Nhuận
Mẫu mã
--!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
Lợi Nhuận
Mẫu mã
--!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
Lợi Nhuận
Mẫu mã
--!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
Lợi Nhuận
Mẫu mã
--!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
Tham Số
Lợi Nhuận
ForceSetSelectionAsync
Tham Số
Lợi Nhuận
Mẫu mã
--!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
Tham Số
Lợi Nhuận
Mẫu mã
--!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
Sự Kiện
SelectionChanged
Tham Số
Mẫu mã
--!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
Tham Số
Mẫu mã
--!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)