ScriptDocument

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建
未复制

一个 ScriptDocument 实例是 Studio 脚本编辑器的文档代理。它与在编辑器中打开的 LuaSourceContainer 不同,它代表打开文档的临时状态,其表示形式更适合阅读和编辑代码而不是执行它。特别是,ScriptDocument 反映了草稿模式中对开放脚本进行的任何更改,源属性没有。

脚本编辑器本身存在并发生在任何 DataModel 不同线程上,因此 ScriptDocument 复制打开的脚本编辑器,但它不是打开编辑器。由于复制,有时在更改编辑器中的文本和更新 ScriptDocument 之间会出现轻微延迟。延迟通常发生因为 DataModel 很忙,它几乎总是极小的,但仍然存在。

存在一个 ScriptDocument 表示文档在脚本编辑器中打开。所有 ScriptDocument 实例都有 ScriptEditorService 作为其父元素。每个实例遵守以下编码约定:

  • ScriptDocument 中的所有文本都采用了UTF-8编码。
  • 所有线索都是 1 索引。
  • 所有角色索引都是 1 索引的,计数 UTF-8 字节,而不是字母,因此适用相同的警告:许多Unicode字符需要超过一个字节。
  • 所有范围都包括其起始位置和专属于其末位置,因此 start == end 暗示一个空范围。

所有 API 对于 ScriptDocument 都处于 插件 等级安全。

概要

方法

活动

属性

方法

GetLine

插件安全性

返回指定行的文本。当没有提供参数时,返回当前鼠标位置的行。

参数

lineIndex: number
默认值:"nil"

返回

代码示例

ScriptDocument.SelectionChanged and ScriptDocument:GetLine()

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

插件安全性

返回激活文档中的线数。


返回

代码示例

ScriptDocument:GetLineCount()

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
插件安全性

返回底层 LuaSourceContainer 实例,如果存在,否则 nil .


返回

代码示例

ScriptDocument:GetScript()

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

插件安全性

获取编辑器中选择的文本或为空的字符串,如果没有选择。


返回

代码示例

ScriptDocument:HasSelectedText() and :GetSelectedText()

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

插件安全性

返回脚本编辑器最后一个已知选择的格式:CursorLine, CursorChar, AnchorLine, AnchorChar。如果脚本编辑器没有选择,CursorLine == AnchorLineCursorChar == AnchorChar


返回

CursorLine、CursorChar、AnchorLine、AnchorChar。

GetSelectionEnd

插件安全性

获取更大的鼠标位置和锚固。如果编辑器没有选择,它们是相同值。


返回

代码示例

ScriptDocument:GetSelectionStart() and :GetSelectionEnd()

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

插件安全性

获取更小的鼠标位置和锚固。如果编辑器没有选择,它们是相同的值。


返回

代码示例

ScriptDocument:GetSelectionStart() and :GetSelectionEnd()

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

插件安全性

从打开编辑器返回文本。必须使用 0、2 或 4 个参数调用:

  • 如果用 0 个参数调用,获取打开编辑器的全部内容。
  • 如果使用 2 个参数调用,将获得文档的文本,从 ( startLine , startColumn ) 开始。
  • 如果使用 4 个参数调用,将获得文档的文本开始于()、结束于(>、>)。

参数

startLine: number
默认值:"nil"
startCharacter: number
默认值:"nil"
endLine: number
默认值:"nil"
endCharacter: number
默认值:"nil"

返回

代码示例

ScriptDocument:GetText()

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

插件安全性

返回编辑器更改中显示的行号。编辑器显示从起始线到结束线之间的线,包括在内。第一行和最后行可能只显示部分内容。例如,最后一行的顶部像素可能只在屏幕上。此外,代码折叠可能会隐藏 startLine 和 endLine 之间的行。


返回

代码示例

ScriptDocument:GetViewport

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

插件安全性

返回编辑器是否有任何文本已选择。


返回

代码示例

ScriptDocument:HasSelectedText() and :GetSelectedText()

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

插件安全性

如果 ScriptDocument 代表命令栏,返回 true;命令栏在此 API 中有特殊规则和限制:

  • 工作室在运行插件之前创建命令栏,因此它并不总是发射打开的事件,尽管它会关闭并重新打开,因为它在数据模型之间转换时。
  • 您无法因安全原因编辑命令栏 EditTextAsync

返回

代码示例

ScriptDocument:IsCommandBar()

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

暂停
插件安全性

要求编辑器关闭与此文档相关的请求。在编辑器回应请求之前,返回当前线程。如果函数成功,它返回 (true, nil)。如果函数失败,它返回 (false, strin字符串) 作为问题的描述。

此函数无法关闭命令栏。


返回

代码示例

ScriptDocument:CloseAsync

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

暂停
插件安全性

将指定范围内的文本(startLinestartColumn)替换为(endLineendColumn)的 newText 。如果范围为空,则函数将文本插入到(startLinestartColumn中。如果文本鼠标在指定范围内,鼠标将移动到编辑的最后位置。否则,文本鼠标不会移动工具。这个函数返回编辑器关于编辑的回复,直到它收到编辑器关于编辑的回复。

如果函数成功,它返回(truenil)。

如果函数发生错误,则:

  • 范围无效。
  • 范围将切割一个Unicode字符,例如仅替换Unicode字符的部分字节。
  • newText 本身包含无效的 UTF-8。

如果函数失败,它返回 (false, string)。字符串是问题的描述。最常见的失败类型是版本不匹配。这发生在你尝试在 期间调用 时,当 与编辑器内容不匹配时。如果发生这种情况,您可以重试编辑。

参数

newText: string
默认值:""
startLine: number
默认值:""
startCharacter: number
默认值:""
endLine: number
默认值:""
endCharacter: number
默认值:""

返回

ForceSetSelectionAsync

暂停
插件安全性

要求编辑器将鼠标选择设置为参数值。必须传递两个锚点参数,或者没有。如果没有传递任何一个,那么它们每个默认为与对应的鼠标参数相同。编辑器可能会拒绝更新其鼠标,如果文档的文本内容已更改。与 ScriptDocument:RequestSetSelectionAsync() 不同,编辑器不会拒绝移动鼠标,如果鼠标自请求以来已移动。返回 (true, nil) 如果滚动器已更新,否则 (字符串alse, string) 带有说明文本。在编辑器回应之前,产生当前线程。

参数

cursorLine: number
默认值:""
cursorCharacter: number
默认值:""
anchorLine: number
默认值:"nil"
anchorCharacter: number
默认值:"nil"

返回

代码示例

ScriptDocument:ForceSetSelectionAsync()

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

暂停
插件安全性

参数

edits: Array
默认值:""

返回

RequestSetSelectionAsync

暂停
插件安全性

要求编辑器将鼠标选择设置为参数值。必须传递两个锚点参数,或者没有。如果没有传递任何一个,那么它们每个默认为与对应的鼠标参数相同。编辑器可能拒绝更新其鼠标内容,如果文档的文本内容已更改,或鼠标自请求发出以来已移动。返回 (true, nil) 如果滚动器已更新,否则 (false, strin字符串) 带有说明文本。在编辑器回应之前,产生当前线程。

参数

cursorLine: number
默认值:""
cursorCharacter: number
默认值:""
anchorLine: number
默认值:"nil"
anchorCharacter: number
默认值:"nil"

返回

代码示例

ScriptDocument:RequestSetSelectionAsync()

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

活动

SelectionChanged

插件安全性

当脚本文档更改时发生火焰,包括在文本更改后立即。

参数

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

代码示例

ScriptDocument.SelectionChanged and ScriptDocument:GetLine()

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

插件安全性

当编辑器中显示的行号发生变更时,发生火焰。见 ScriptDocument.GetViewport 获取详细信息。

参数

startLine: number
endLine: number

代码示例

Demonstrates using ScriptDocument.ViewportChanged to print the start and end line of the script's viewport when it changes.

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