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
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.
Tham Số
Lợi Nhuận
Mẫu mã
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
Trả về số dòng trong tài liệu hoạt động.
Lợi Nhuận
Mẫu mã
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
Trả về ví dụ / trường hợpcơ sở LuaSourceContainer nếu có, nếu không có thì nil .
Lợi Nhuận
Mẫu mã
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
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.
Lợi Nhuận
Mẫu mã
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
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 .
Lợi Nhuận
CursorLine, CursorChar, Dòng neo, AnchorChar.
GetSelectionEnd
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ự.
Lợi Nhuận
Mẫu mã
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
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ự.
Lợi Nhuận
Mẫu mã
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
Trả văn bản từ trình soạn thảo mở. Phải được gọi với 0, 2 hoặc 4 tham số:
- Nếu được gọi với 0 tham số, nó nhận toàn bộ nội dung của editor mở.
- Nếu được gọi với 2 tham số, nhận được văn bản của tài liệu bắt đầu tại ( startLine, startColumn ).
- Nếu được gọi với 4 tham số, nó nhận được văn bản của tài liệu bắt đầu tại ( startLine , startColumn ) và kết thúc tại ( endLine , endColumn ).
Tham Số
Lợi Nhuận
Mẫu mã
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
Trả về số dòng hiển thị hiện tại trong thay đổi biên tập.Trình soạn thảo hiển thị các dòng giữa startLine và endLine, bao gồm.Dòng đầu tiên và cuối cùng có thể chỉ hiển thị một phần.Ví dụ, chỉ pixel trên cùng của dòng cuối cùng có thể ở trên màn hình.Ngoài ra, gấp mã có thể che đi các dòng giữa startLine và endLine.
Lợi Nhuận
Mẫu mã
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
Trả về xem có hay không có văn bản được chọn bởi biên tập viên.
Lợi Nhuận
Mẫu mã
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
Trả về true nếu ScriptDocument đại diện cho thanh lệnh. Thanh lệnh có các quy tắc và hạn chế đặc biệt trong API này:
- Studio tạo thanh Command trước khi chạy plugin, vì vậy nó không phải lúc nào cũng kích hoạt sự kiện được mở, mặc dù nó đóng và mở lại khi Studio chuyển đổi giữa DataModels.
- Bạn không thể chỉnh sửa thanh Command với EditTextAsync vì lý do an ninh.
Lợi Nhuận
Mẫu mã
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
Yêu cầu mà người biên tập liên kết với tài liệu này đóng.Sản xuất luồng hiện tại cho đến khi nhà biên tập trả lời yêu cầu.Nếu chức năng thành công, nó trả lại (true, nil).Nếu chức năng thất bại, nó trả về (false, chuỗi) như một mô tả về vấn đề.
Chức năng này không thể đóng thanh lệnh.
Lợi Nhuận
Mẫu mã
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
Thay thế văn bản trong phạm vi được chỉ định từ ( startLine, startColumn ) đến ( endLine, endColumn ) với newText .Nếu phạm vi là trống, thì chức năng sẽ chèn văn bản tại ( startLine , startColumn ).Nếu con trỏ văn bản nằm trong phạm vi được định, con trỏ di chuyển đến vị trí cuối cùng của việc chỉnh sửa.Nếu không, con trỏ văn bản không di chuyển.Chức năng này trả về luồng hiện tại cho đến khi nó nhận được phản hồi từ người soạn thảo về việc chỉnh sửa.
Nếu chức năng thành công, nó trả lại ( true, nil ).
Chức năng ném lỗi nếu:
- Phạm vi không hợp lệ.
- Phạm vi sẽ cắt một ký tự Unicode, ví dụ chỉ thay thế một số bayt của ký tự Unicode.
- Bản thân newText chứa UTF-8 không hợp lệ.
Nếu chức năng thất bại, nó trả về (false, chuỗi).Chuỗi là một mô tả về vấn đề.Loại lỗi phổ biến nhất là một trường hợp không phù hợp phiên bản.Điều này xảy ra khi bạn cố gọi EditTextAsync trong thời gian khi ScriptDocument bị mất đồng bộ với nội dung của editor.Nếu xảy ra, bạn có thể thử lại việc chỉnh sửa.
Tham Số
Lợi Nhuận
ForceSetSelectionAsync
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ố.Cả hai lập luận neo phải được truyền, hoặc không.Nếu không có được truyền, thì mỗi cái sẽ mặc định giống như các tham số con trỏ tương ứng.Người biên tập có thể từ chối cập nhật con trỏ nếu nội dung văn bản của tài liệu đã thay đổi.Không giống như ScriptDocument:RequestSetSelectionAsync(), trình soạn thảo sẽ không từ chối di chuyển con trỏ nếu con trỏ đã di chuyển kể từ khi yêu cầu được thực hiện.Trả về (đúng, nil) nếu con trỏ đã được cập nhật, và (giả mạo, chuỗi) với một chuỗi giải thích nếu nó không phải là vậy.Sản xuất luồng hiện tại cho đến khi nhà biên tập trả lời.
Tham Số
Lợi Nhuận
Mẫu mã
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
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ố.Cả hai lập luận neo phải được truyền, hoặc không.Nếu không có được truyền, thì mỗi cái sẽ mặc định giống như các tham số con trỏ tương ứng.Người biên tập có thể từ chối cập nhật con trỏ nếu nội dung văn bản của tài liệu đã thay đổi hoặc con trỏ đã di chuyển kể từ khi yêu cầu được đưa ra.Trả về (đúng, nil) nếu con trỏ đã được cập nhật, và (giả mạo, chuỗi) với một chuỗi giải thích nếu nó không phải là vậy.Sản xuất luồng hiện tại cho đến khi nhà biên tập trả lời.
Tham Số
Lợi Nhuận
Mẫu mã
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
Sự Kiện
SelectionChanged
Bắt lửa khi ScriptDocument thay đổi, bao gồm ngay sau khi thay đổi văn bản.
Tham Số
Mẫu mã
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
Bắt lửa khi số dòng hiển thị trong editor thay đổi. Xem ScriptDocument.GetViewport để biết chi tiết.
Tham Số
Mẫu mã
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)