ScriptEditorService
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
このサービスは、ScriptDocument インスタンスと対話するために使用されます。
概要
方法
以前に登録されたコールバックを名前 name で削除します。
以前に登録されたコールバックを名前 name で削除します。
指定された ScriptDocument に対応する開いた LuaSourceContainer または nil を返し、指定されたスクリプトが開いていない場合。
指定のスクリプトの編集時間ソースを返します。
コマンドバーを含む現在開いているスクリプト文書のアレイを返します。
自動完了コールバック callbackFunction を優先順位 name で名前付き priority に登録します。
スクリプト分析コールバック callbackFunction を登録し、名前は name で priority です。
スクリプトエディタが指定されたスクリプトを開くリクエスト。リクエストが成功すると、返却 (true、nil) します。リクエストが失敗した場合、問題を説明する文字列を返します (false、string)。
古いスクリプトから新しいコンテンツを生成し、スクリプトエディタが開いている場合は更新し、スクリプトエディタが閉じている場合は Script インスタンスを更新します。
イベント
ScriptDocument 変更の直後に発火します。
スクリプトエディタが閉じた直後に、ScriptDocument オブジェクトが破壊される直前に炎が上がり、スクリプトエディタが閉じた直後に発生します。
スクリプトエディタが開いた直後に、ScriptDocument オブジェクトが作成されてサービスに親属し、それがスクリプトエディタが開いた直後に起こる
プロパティ
方法
DeregisterAutocompleteCallback
以前に登録されたコールバックを名前 name で削除します。
パラメータ
戻り値
コードサンプル
game.ScriptEditorService:DeregisterAutocompleteCallback("foo")
DeregisterScriptAnalysisCallback
以前に登録されたコールバックを名前 name で削除します。
パラメータ
戻り値
コードサンプル
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService:DeregisterScriptAnalysisCallback("foo")
FindScriptDocument
指定された ScriptDocument に対応する開いた LuaSourceContainer または nil を返し、指定されたスクリプトが開いていない場合。
パラメータ
戻り値
コードサンプル
--!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
--!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
コマンドバーを含む現在開いているスクリプト文書のアレイを返します。
戻り値
コードサンプル
--!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 に登録します。
スクリプトエディタが自動完了を呼び出すと、すべての登録された自動完了コールバックは、自動完了リクエストと応答の優先順位に従って呼び出されます。複数のコールバックは優先順位を共有できますが、その後の呼び出し順序は予測できません。各コールバックは、応答入力テーブルと同じ形式の応答テーブルを返すことを目的としています。コールバックは生成してはいけません。最初に呼び出されたコールバックは、内部の自動完了の応答を応答テーブルとして受け取り、後続のコールバックは前のコールバックの出力を応答テーブルとして受け取ります。コールバックは、パスされたテーブルを変更するか、同じ形式の新しいテーブルを返すかもしれません。
The 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.document と textDocument.script が存在する場合、それらはお互いに対応します:req.textDocument.document:GetScript() == req.textDocument.script
応答テーブルの形式は次のとおりです:
type Response = {items: {{label: string, -- The labelkind: 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 は、create.roblox.com を作成するための https リクエストでなければなりません。エディタに他の URL は表示されません。
- Response.items[n].codeSample 完了アイテムのサンプル使用を指定します。documentation このフィールドを表示するには、空白でない必要があります。
- Response.items[n].preselect 真である場合、エディタはこの完了アイテムをすべての他のものよりも先に並べ替え、デフォルトでユーザーに選択します。ファルスまたは欠落している場合は、効果がありません。
- Response.items[n].textEdit 現在存在する場合、完了の承認はこのテキスト編集を適用します - 位置の開始と終了の間のスペインを新しいテキストで挿入または置換します。
コールバックが不正な結果を返したり、エラーが発生した場合、エディタは修正されたレスポンステーブルを排除し、内蔵の自動完了結果リストを使用します。
パラメータ
戻り値
コードサンプル
--!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 です。スタジオでのスクリプト分析が実行されると、登録されたすべてのコールバックが優先順位の上昇順に呼び出されます。それぞれのコールバックは、指定された形式に一致する応答テーブルを返すことを目的としています。コールバックは生成してはいけません。
リクエストテーブルには、以下の形式があり、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 のスクリプト分析ツールで診断がカテゴリ化される方法、およびスクリプトエディタでテキストが強調される方法を決定します。
- クリエイタードキュメントの関連するページへのリンク。この URL は、エディタで表示される他の URL ではありません。
パラメータ
戻り値
コードサンプル
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
スクリプトエディタが指定されたスクリプトを開くリクエスト。リクエストが成功すると、返却 (true、nil) します。リクエストが失敗した場合、問題を説明する文字列を返します (false、string)。
スクリプトがすでに開いている場合、この関数は成功し、タブを関連するエディタに切り替えます。
パラメータ
戻り値
コードサンプル
--!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)
パラメータ
更新するスクリプトインスタンス。
新しいスクリプトコンテンツを返す機能。
戻り値
イベント
TextDocumentDidChange
変更後の ScriptDocument に火がつきます。textChanged はフォーマットの変更構造の配列です:
{ range : { start : { line : number, character : number }, end : { line : number, character : number } }, text: string }
パラメータ
コードサンプル
--!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 オブジェクトは再利用できません、スクリプトエディタが同じスクリプトを再開しても。
パラメータ
コードサンプル
--!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 オブジェクトが作成されてサービスに親属し、それがスクリプトエディタが開いた直後に起こる
パラメータ
コードサンプル
--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidOpen:Connect(function(scriptDocument)
print("Opened", scriptDocument)
end)