ScriptDocument

Hiển Thị Bản Đã Lỗi Thời

*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.

Không Thể Tạo
Không Sao Chép

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

  • GetLine(lineIndex : number?):string
    Bảo Mật Plugin

    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.

  • Bảo Mật Plugin

    Trả về số dòng trong tài liệu.

  • Bảo Mật Plugin

    Trả về ví dụ / trường hợpcơ sở LuaSourceContainer nếu có, nếu không có thì nil .

  • Bảo Mật Plugin

    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.

  • Bảo Mật Plugin

    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 == AnchorLineCursorChar == AnchorChar .

  • Bảo Mật Plugin

    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ự.

  • Bảo Mật Plugin

    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
    Bảo Mật Plugin

    Trả văn bản từ trình soạn thảo mở.

  • Bảo Mật Plugin

    Trả về số dòng hiển thị hiện tại trong thay đổi biên tập.

  • Bảo Mật Plugin

    Trả về xem có hay không có văn bản được chọn bởi biên tập viên.

  • Bảo Mật Plugin

    Trả về true nếu ScriptDocument đại diện cho thanh Command.

  • Sinh Lợi
    Bảo Mật Plugin

    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
    Sinh Lợi
    Bảo Mật Plugin

    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
    Sinh Lợi
    Bảo Mật Plugin

    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ố.

  • Sinh Lợi
    Bảo Mật Plugin
  • RequestSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
    Sinh Lợi
    Bảo Mật Plugin

    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

Thuộc Tính

Phương Pháp

GetLine

Bảo Mật Plugin

Tham Số

lineIndex: number
Giá Trị Mặc Định: "nil"

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

Bảo Mật Plugin

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
Bảo Mật Plugin

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

Bảo Mật Plugin

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

Bảo Mật Plugin

Lợi Nhuận

GetSelectionEnd

Bảo Mật Plugin

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

Bảo Mật Plugin

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

Bảo Mật Plugin

Tham Số

startLine: number
Giá Trị Mặc Định: "nil"
startCharacter: number
Giá Trị Mặc Định: "nil"
endLine: number
Giá Trị Mặc Định: "nil"
endCharacter: number
Giá Trị Mặc Định: "nil"

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

Bảo Mật Plugin

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

Bảo Mật Plugin

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

Bảo Mật Plugin

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

Sinh Lợi
Bảo Mật Plugin

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

Sinh Lợi
Bảo Mật Plugin

Tham Số

newText: string
Giá Trị Mặc Định: ""
startLine: number
Giá Trị Mặc Định: ""
startCharacter: number
Giá Trị Mặc Định: ""
endLine: number
Giá Trị Mặc Định: ""
endCharacter: number
Giá Trị Mặc Định: ""

Lợi Nhuận

ForceSetSelectionAsync

Sinh Lợi
Bảo Mật Plugin

Tham Số

cursorLine: number
Giá Trị Mặc Định: ""
cursorCharacter: number
Giá Trị Mặc Định: ""
anchorLine: number
Giá Trị Mặc Định: "nil"
anchorCharacter: number
Giá Trị Mặc Định: "nil"

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

MultiEditTextAsync

Sinh Lợi
Bảo Mật Plugin

Tham Số

edits: Array
Giá Trị Mặc Định: ""

Lợi Nhuận

RequestSetSelectionAsync

Sinh Lợi
Bảo Mật Plugin

Tham Số

cursorLine: number
Giá Trị Mặc Định: ""
cursorCharacter: number
Giá Trị Mặc Định: ""
anchorLine: number
Giá Trị Mặc Định: "nil"
anchorCharacter: number
Giá Trị Mặc Định: "nil"

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ảo Mật Plugin

Tham Số

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

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ảo Mật Plugin

Tham Số

startLine: number
endLine: number

Mẫu mã

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)