ScriptEditorService

显示已弃用

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

无法创建
服务
未复制

该服务用于与 ScriptDocument 实例交互。

概要

方法

活动

属性

方法

DeregisterAutocompleteCallback

()
插件安全性

移除以名称 name 注册过的回调。

参数

name: string
默认值:""

返回

()

代码示例

ScriptEditorService:DeregisterAutocompleteCallback

game.ScriptEditorService:DeregisterAutocompleteCallback("foo")

DeregisterScriptAnalysisCallback

()
插件安全性

移除以名称 name 注册过的回调。

参数

name: string
默认值:""

返回

()

代码示例

ScriptEditorService:DeregisterScriptAnalysisCallback

local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService:DeregisterScriptAnalysisCallback("foo")

FindScriptDocument

插件安全性

返回与给定 ScriptDocument 相对应的打开 LuaSourceContainernil 如果给定的脚本未打开。

参数

默认值:""

返回

代码示例

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

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)

GetEditorSource

插件安全性

返回给定脚本的编辑时间源。

如果脚本在 脚本编辑器 中打开,该方法将返回正在显示在编辑器中的文本。如果脚本在编辑器中未打开,方法将返回编辑器打开时显示的文本。编辑时源并不总是与 Script.Source 属性一致。

参数

默认值:""

返回

GetScriptDocuments

Instances
插件安全性

返回当前打开的脚本文档阵列,包括命令栏。


返回

Instances

代码示例

Gets all script documents in the place with ScriptEditorService:GetScriptDocuments() and prints their names.

Print the name of every script

--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local scriptDocuments = ScriptEditorService:GetScriptDocuments()
for _, scriptDocument in scriptDocuments do
-- Prints the name of each script
if not scriptDocument:IsCommandBar() then
print(scriptDocument.Name)
end
end

RegisterAutocompleteCallback

()
插件安全性

注册一个自动完成回调 callbackFunction 名为 name 的优先级 priority

当脚本编辑器启用自动完成时,所有注册的自动完成回调按照优先级顺序调用自动完成请求和响应。多个回调可以共享优先级,但之后它们的调用顺序是不可预测的。每个回调都旨在返回与响应输入表格式相同的响应表。回调不应该返回。第一个调用的回调接收内部自动完成的响应作为其响应表,后续的回调接收前一个回调的输出作为其响应表。回调可以修改传递的表或返回相同格式的新表。

callbackFunction 必须具有以下输入:(Request: table, Response: table) -> table

请求表的格式如下:


type Request = {
position: {
line: number,
character: number
},
textDocument: {
document: ScriptDocument?,
script: LuaSourceContainer?
}
}
  • position 是你正在自动完成的一号索引位置。
  • textDocument.document 是你正在完成的开放 ScriptDocument,如果存在。
  • textDocument.script 是你正在完成的 LuaSourceContainer,如果存在。

如果 both textDocument.documenttextDocument.script 都存在,那么它们相互对应:req.textDocument.document:GetScript() == req.textDocument.script

响应表的格式如下:


type Response = {
items: {
{
label: string, -- The label
kind: Enum.CompletionItemKind?,
tags: {Enum.CompletionItemTag}?,
detail: string?,
documentation: {
value: string,
}?,
overloads: number?,
learnMoreLink: string?,
codeSample: string?,
preselect: boolean?,
textEdit: {
newText: string,
insert: { start: { line: number, character: number }, ["end"]: { line: number, character: number } },
replace: { start: { line: number, character: number }, ["end"]: { line: number, character: number } },
}?
}
}
}
  • Response.items 是完成项目的阵列。这个阵列的顺序不重要,它在编辑器中用于用户输入。
  • Response.items[n].label 是自动完成菜单中显示的项目标签。
  • Response.items[n].kind 指定这是什么类型的自动完成项目。主要控制编辑器中给予物品的图标。不是所有类型都有独特的标志。如果未指定,编辑器使用“文本”标志。未支持的类型默认显示“属性”标志。
  • Response.items[n].tags 指定描述此完成物品的标签阵列。请参阅Enum.CompletionItemTag了解其功能细节。
  • Response.items[n].details 指定描述完成物品目详情的字符串。对于默认项目,这是其输入的字符串表示。请注意,为了显示帮助文档插件,必须存在 documentation,但 documentation.value 可能为空。
  • Response.items[n].documentation 在其 value 字段中指定文档的主体。documentation 如果存在,即使值为空,文档窗口也会显示详情或超载被指定,因为如果指定了详情或超载,文档窗口将显示
  • Response.items[n].overloads 指定函数自动完成的过载数量。
  • Response.items[n].learnMoreLink 链接到创建者文档中的相关页面。此 URL 必须是 https 创创建 or 创作.roblox.com 的请求;编辑器中没有其他 URL 显示。
  • Response.items[n].codeSample 指定完成项物品的示例使用。documentation 必须不为空才能显示此字段。
  • Response.items[n].preselect 如果真实,编辑器将此完成项排在所有其他项之前,默认为用户选择它。如果为 false 或丢失,没有影响。
  • Response.items[n].textEdit 如果存在,接受完成适用这个文本编辑 - 插入或替换位置开始和结束之间的空格,用新文本替换。

如果回调返回了无效的结果或遇到错误,编辑器将丢弃修改的响应表并使用内置的自动完成结果列表。

参数

name: string
默认值:""
priority: number
默认值:""
callbackFunction: function
默认值:""

返回

()

代码示例

ScriptEditorService:RegisterAutocompleteCallback ScriptEditorService:DeregisterAutocompleteCallback

ScriptEditorService:RegisterAutocompleteCallback ScriptEditorService:DeregisterAutocompleteCallback

--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
type Request = {
position: {
line: number,
character: number,
},
textDocument: {
document: ScriptDocument?,
script: LuaSourceContainer?,
},
}
type Response = {
items: {
{
label: string,
kind: Enum.CompletionItemKind?,
tags: { Enum.CompletionItemTag }?,
detail: string?,
documentation: {
value: string,
}?,
overloads: number?,
learnMoreLink: string?,
codeSample: string?,
preselect: boolean?,
textEdit: {
newText: string,
replace: {
start: { line: number, character: number },
["end"]: { line: number, character: number },
},
}?,
}
},
}
local autocompleteCallback = function(request: Request, response: Response): Response
local item = {
label = "foo",
preselect = true,
}
table.insert(response.items, item)
return response
end
ScriptEditorService:RegisterAutocompleteCallback("foo", 1, autocompleteCallback)
-- To deregister the callback, run the following code in the Command Bar
ScriptEditorService:DeregisterAutocompleteCallback("foo")

RegisterScriptAnalysisCallback

()
插件安全性

注册一个名为 callbackFunction 的脚本分析回调 name ,使用 priority 。当 Studio 中运行脚本分析时,所有注册的回调按上升的优先级顺序调用。每个回调都应返回匹配下面指定格式的响应表。回调不应返回。

请求表的格式如下,其中 script 是要分析的 LuaSourceContainer


type Request = {
script: LuaSourceContainer?
}

响应表具有以下格式,其中 diagnostics 是一组诊断表。每个诊断表都有列出以下的条目。


type Response = {
diagnostics: {
{
range: {
start: {
line: number,
character: number,
},
["end"]: {
line: number,
character: number,
}
},
code: string?,
message: string,
severity: Enum.Severity?,
codeDescription: { href: string }?
}
}
}
  • range 代表需要由调试器突出显示的文本范围,提供需要开始突出显示的行/字符和需要停止突出显示的行/字符。
  • code 是消信息标签。
  • message 是行显示的警告消息。这也会出现在用户将鼠标悬停在脚本编辑器中的线上时的提示上。
  • severity 是诊断的 Enum.Severity 值。这决定了 Studio 中脚本分析工具中诊断类别以及脚本编辑器中文本如何突出显示。
  • codeDescription 链接到创建者文档中的相关页面。此 URL 必须是 httpscreate.roblox.com 发出的请求;编辑器中没有其他 URL 显示。

参数

name: string
默认值:""
priority: number
默认值:""
callbackFunction: function
默认值:""

返回

()

代码示例

ScriptEditorService:RegisterScriptAnalysisCallback

type Request = {
["script"]: LuaSourceContainer,
}
type Response = {
diagnostics: {
{
range: {
start: {
line: number,
character: number,
},
["end"]: {
line: number,
character: number,
},
},
code: string?,
message: string,
severity: Enum.Severity?,
codeDescription: { href: string }?,
}
},
}
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService:RegisterScriptAnalysisCallback("foo", 1, function(Req: Request): Response
local response = {
diagnostics = {},
}
local lineNo = 1
-- Iterate line by line
for text, newline in Req.script.Source:gmatch("([^\r\n]*)([\r\n]*)") do
local startIndex, endIndex = string.find(text, "Foo")
if startIndex and endIndex then
table.insert(response.diagnostics, {
range = {
["start"] = {
line = lineNo,
character = startIndex,
},
["end"] = {
line = lineNo,
character = endIndex,
},
},
code = "FooFinder",
message = "Foo found here!",
severity = Enum.Severity.Warning,
})
end
lineNo = lineNo + #newline:gsub("\n+", "\0%0\0"):gsub(".%z.", "."):gsub("%z", "")
end
return response
end)

OpenScriptDocumentAsync

暂停
插件安全性

要求脚本编辑器打开指定的脚本的请求。如果请求成功,返回(真值,为零)。返回 (false, string) 如果请求失败,带有描述问题的字符串。

如果脚本已打开,该函数成功并切换选项卡到相关编辑器。

参数

默认值:""

返回

代码示例

ScriptEditorService:OpenScriptDocumentAsync

ScriptEditorService:OpenScriptDocumentAsync

--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local Workspace = game:GetService("Workspace")
local newScript = Instance.new("Script")
newScript.Parent = Workspace
local success, err = ScriptEditorService:OpenScriptDocumentAsync(newScript)
if success then
print("Opened script document")
else
print(`Failed to open script document: {err}`)
end

UpdateSourceAsync

()
暂停
插件安全性

返回给定脚本的编辑时间 Script.Source

这个函数使用脚本的旧内容调用传递的回调,计算脚本的新内容。

如果脚本在 脚本编辑器 中打开,那么它将向编辑器发出请求以更新其源。编辑器可能会拒绝此更新,如果 Script.Source 属性与用户的脚本版本不兼容,在这种情况下,回调将被重新调用,尝试将被重复。

回调可能无法返回。如果回调返回 nil,操作将被取消。此函数返回直到操作被取消或成功为止。

如果脚本在编辑器中未打开,新内容将更新到脚本源代码,这是编辑器会显示的文本,如果打开。


local ses = game:GetService('ScriptEditorService')
ses:UpdateSourceAsync(Workspace.Script, function(oldContent)
return oldContent .. " World!"
end)

参数

需要更新的脚本实例。

默认值:""
callback: function

返回新脚本内容的函数。

默认值:""

返回

()

活动

TextDocumentDidChange

插件安全性

ScriptDocument 发生变更之后立即发生火焰。 textChanged 是格式的更改结构阵列:

{ range : { start : { line : number, character : number }, end : { line : number, character : number } }, text: string }

参数

document: ScriptDocument
changesArray: Variant

代码示例

ScriptEditorService.TextDocumentDidChange

ScriptEditorService.TextDocumentDidChange

--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidChange:Connect(function(scriptDocument, changes)
print("Changed", scriptDocument, changes)
end)

TextDocumentDidClose

插件安全性

ScriptDocument 对象被摧毁之前发生火焰,这发生在脚本编辑器关闭后。在此事件发生后,ScriptDocument 进入“关闭”状态,尝试调用其方法会发生错误。ScriptDocument

参数

oldDocument: ScriptDocument

代码示例

ScriptEditorService.TextDocumentDidClose

ScriptEditorService.TextDocumentDidClose

--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidClose:Connect(function(scriptDocument)
print("Closed", scriptDocument)
end)

TextDocumentDidOpen

插件安全性

火焰发生在 ScriptDocument 对象创建并传给服务之后,这发生在脚本编辑器打开之后。

参数

newDocument: ScriptDocument

代码示例

ScriptEditorService.TextDocumentDidOpen

ScriptEditorService.TextDocumentDidOpen

--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidOpen:Connect(function(scriptDocument)
print("Opened", scriptDocument)
end)