ScriptEditorService

顯示已棄用項目

*此內容是使用 AI(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,如果存在。

如果兩個 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 指定一個描述完成道具目詳情的字串。對於預設項目,這是其輸入型的字串表示。請注意,為了顯示說明 widget,必須存在 documentation,但 documentation.value 可能是空的。
  • Response.items[n].documentation 指定文檔的主體在其 value 欄中。documentation 如果值為空,即使文件是空白的,文件窗口仍會顯示詳情或超載指定。
  • Response.items[n].overloads 指定函數自動完成過載的數量。
  • Response.items[n].learnMoreLink 鏈接到創建者文件中的相關頁面。此 URL 必須是 https 要創建立、創作的.roblox.com 的請求;編輯器中沒有其他 URL 顯示。
  • Response.items[n].codeSample 指定完成道具目的樣本使用。documentation 必須不為空才能顯示此字段。
  • Response.items[n].preselect 如果真實,編輯器會在所有人之前排序此完成項目,並預設為用戶選擇它。如果為假或缺少,沒有影響。
  • 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 必須是 https 要求到 create.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、字串) 如果請求失敗,包含描述問題的字串。

如果腳本已開啟,此功能成功並切換標籤到相關編輯器。

參數

預設值:""

返回

範例程式碼

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)