Allow Registering Foldable Areas in the Script Editor
I would like to request an API for plugins and tools to register arbitrary regions of a script as foldable in the Script Editor.
This should build upon the ideas proposed in the existing feature request about preserving collapsed and expanded folds between sessions, while expanding the concept to allow plugins and developer tools to define their own foldable regions.
Proposed API
I propose adding RegisterFoldable (and DeregisterFoldable accordingly) to ScriptEditorService:
ScriptEditorService:RegisterFoldable(
script: LuaSourceContainer,
{
startLine: number,
startCharacter: number,
endLine: number,
endCharacter: number
},
Folded: boolean?
)
This would register an arbitrary range of source code as a foldable region.
The region would behave similarly to existing foldable constructs in the Script Editor, such as:
if condition then
-- ...
end
while condition do
-- ...
end
for i = 1, 10 do
-- ...
end
and block comments:
--[[
...
]]
The difference is that the foldable region would be defined externally by a plugin or other editor tooling rather than being inferred from the source syntax.
Retrieving Foldables
I would also propose:
ScriptEditorService:GetFoldables(script) -> {
[number]: {
startLine: number,
startCharacter: number,
endLine: number,
endCharacter: number,
Folded: boolean
}
}
This would allow plugins to inspect the foldable regions they have registered and determine their current collapsed/expanded state.
Calling RegisterFoldable for a region that is already registered would override its Folded state.
This would make it possible to use GetFoldables() to retrieve the current state and then re-register the region when necessary.
Why This Matters
This would be particularly useful for plugins that generate or insert code into the Script Editor.
For example, my #include plugin heavily utilizes AOT code-editor metaprogramming by inserting generated code snippets when the source is updated.
Consider:
--#include<code>
local a = "code here"
--\include
The generated section may become significantly larger than the original source.
Without the ability to create a custom foldable region, updating the include can quickly flood the Script Editor with generated code that the developer may not want to inspect every time.
I would instead like the plugin to register the generated region as foldable:
--#include<code> ┐
local a = "code here" │ Foldable
... │
... generated code ... │
--\include ┘
The developer could then collapse the entire generated section while still keeping the source available in the editor.
General Use Cases
This would not be limited to #include.
Plugins could use custom foldable regions for:
- Generated code
- Code generation systems
- Embedded DSLs
- Automatically inserted boilerplate
- Generated serialization code
- Metadata sections
- Plugin-generated declarations
- Large machine-generated sections
- Other editor metaprogramming systems
It would also allow plugins to create editor features that feel much more native to the Script Editor instead of having to rely on awkward source-code tricks.
Relationship With Existing Folding
The important distinction is that this would extend the existing folding system rather than replace it.
The Script Editor would continue to automatically recognize normal Luau constructs such as if, for, while, do, and block comments.
RegisterFoldable would simply provide an additional mechanism for external tools to tell the editor:
“This source range should also behave as a foldable region.”
This would give plugins access to a capability that the Script Editor already has internally, while allowing them to define regions that cannot be inferred from Luau syntax.
Conclusion
The Script Editor already has the concept of foldable regions.
I am proposing that this capability be exposed to editor tooling through ScriptEditorService.
The combination of:
ScriptEditorService:RegisterFoldable(...)
and:
ScriptEditorService:GetFoldables(...)
would allow plugins to create and manage their own foldable source regions while remaining compatible with the existing Script Editor folding system.
This would be especially valuable for code-generation and metaprogramming plugins, where generated sections can otherwise make scripts significantly harder to navigate.
Let plugins use the folding system that the Script Editor already provides.