ScriptEditorService
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
บริการนี้ใช้สำหรับการโต้ตอบกับตัวอย่าง ScriptDocument
สรุป
วิธีการ
ลบคอลเลกชันที่ลงทะเบียนไว้ก่อนหน้านี้ที่มีชื่อ name
ลบคอลเลกชันที่ลงทะเบียนไว้ก่อนหน้านี้ที่มีชื่อ name
คืนค่าเปิด ScriptDocument ที่ตรงกับที่ให้ไว้ LuaSourceContainer หรือ nil หากสคริปต์ที่ให้ไว้ไม่เปิด
ส่งคืนแหล่งเวลาแก้ไขสำหรับสคริปต์ที่กำหนด
ส่งคืนคอลเลกชันของเอกสารสคริปต์ที่เปิดอยู่ในปัจจุบันรวมถึงแถบคําสั่ง
ลงทะเบียนการโทรกลับอัตโนมัติสำเร็จ callbackFunction ชื่อ name ด้วยลําดับความสําคัญ priority
ลงทะเบียนการโทรกลับการวิเคราะห์สคริปต์ callbackFunction ชื่อ name ด้วย priority
คำขอที่เครื่องมือตัวเขียนสคริปต์เปิดสคริปต์ที่ระบุไว้คืน (จริง, ไม่มี) หากคำขอสําเร็จคืน (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 หากสคริปต์ที่ให้ไว้ไม่เปิด
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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:
- Ensure Output view is open
- Run the below code in the Command Bar
- Scroll up and down in the opened Script window
--!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
ส่งคืนคอลเลกชันของเอกสารสคริปต์ที่เปิดอยู่ในปัจจุบันรวมถึงแถบคําสั่ง
ส่งค่ากลับ
ตัวอย่างโค้ด
Gets all script documents in the place with ScriptEditorService:GetScriptDocuments() and prints their names.
--!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 นี้ต้องเป็นคำขอ https ที่จะสร้าง create.roblox.com; ไม่มี URL อื่นแสดงในตัวแก้ไข
- Response.items[n].codeSample ไอเท็มdocumentation ต้องไม่เป็นว่างเปล่าเพื่อแสดงฟิลด์นี้
- Response.items[n].preselect หากเป็นจริง เครื่องแก้ไขจะจัดเรียงรายการสิ้นสุดนี้ก่อนทุกคนและเลือกมันสำหรับผู้ใช้โดยค่าเริ่มต้น ไม่มีผลหากเป็นเท็จหรือหายไป
- Response.items[n].textEdit หากมี การยอมรับการสรุปใช้ข้อความแก้ไขนี้ - ใส่หรือแทนที่ช่องว่างระหว่างตำแหน่งเริ่มต้นและสิ้นสุดด้วยข้อความใหม่
หากคอลเลกชันการเรียกคืนส่งผลลัพธ์ที่ไม่ถูกต้องหรือพบข้อผิดพลาด ตัวแก้ไขจะทิ้งตารางการตอบสนองที่แก้ไขแล้วและใช้รายการผลลัพธ์อัตโนมัติที่สร้างขึ้น
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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เมื่อการวิเคราะห์สคริปต์ในสตูดิโอดำเนินการแล้ว คำร้องขอทั้งหมดที่ลงทะเบียนจะโทรตามลําดับความสําคัญเพิ่มขึ้นแต่ละคอลเลกชันการโทรกลับมีจุดมุ่งหมายเพื่อส่งคืนตารางตอบสนองที่ตรงกับรูปแบบที่ระบุด้านล่างคอลเลกชันการโทรไม่ควรให้ผล
ตารางคำขอมีรูปแบบดังต่อไปนี้ซึ่ง 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 อื่นแสดงในตัวแก้ไข
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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
--!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 }
พารามิเตอร์
ตัวอย่างโค้ด
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 ไม่สามารถใช้ซ้ำได้แม้ว่าเครื่องมือตัวเขียนสคริปต์จะเปิดสคริปต์เดียวกันอีกครั้ง
พารามิเตอร์
ตัวอย่างโค้ด
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 ถูกสร้างและถูกผูกกับบริการซึ่งเกิดขึ้นทันทีหลังจากที่เครื่องมือตัวแก้ไขสคริปต์เปิด
พารามิเตอร์
ตัวอย่างโค้ด
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)