Explicit Folding
This extension lets you manually control how and where to fold your code.
Configuration
In your settings:
"explicitFolding.rules": {
"*": {
"begin": "{{{",
"end": "}}}"
},
"javascriptreact": [
{
"begin": "{/*",
"end": "*/}"
},
{
"begin": "<",
"end": "/>"
}
]
}
Rules
The property explicitFolding.rules
defines how to fold the code.
Here the list of possible rules:
Global Scope
When used in the global scope, the rules must be grouped by language.
"explicitFolding.rules": {
"cpp": [
{
"beginRegex": "#if(?:n?def)?",
"middleRegex": "#el(?:se|if)",
"endRegex": "#endif"
}
]
}
Language Scope
"[cpp]": {
"explicitFolding.rules": [
{
"beginRegex": "#if(?:n?def)?",
"middleRegex": "#el(?:se|if)",
"endRegex": "#endif"
}
]
}
Regex Syntax
Via VSCode's editor, the extension supports ES2018 regexes (except \n
).
The document parser is line-based. So \n
and multi-lines regexes aren't supported.
The end of a line can be matched with $
.
Additionally, the following aspects of PCRE2 syntax are supported:
(?i)x
:x
becomes case insensitive(?i:x)y
: onlyx
is case insensitive
Wildcard Exclusions
By default, the wildcard rule, like the following, are applied to all languages.
"explicitFolding.rules": {
"*": {
"begin": "{{{",
"end": "}}}"
}
}
But, for languages which are using the indentation to define foldable blocks of code (such as in Python syntax), the wildcard rule will prevent the use of the indentation provider.
To avoid that, you need to add an exclusion:
"explicitFolding.wildcardExclusions": ["python"]
Auto Fold
You can define the automatic folding of the ranges with the property explicitFolding.autoFold
(an enum, none
by default).
Each rule can overwrite that property with its own property autoFold
(a boolean, false
by default).
So you can auto fold only the imports with:
"[javascript]": {
"explicitFolding.rules": [
{
"beginRegex": "^import\\b",
"whileRegex": "^(?:import\\b|\\/\\/)",
"autoFold": true
}
],
"explicitFolding.autoFold": "none"
}
Debugging
If the property explicitFolding.debug
(false
by default) is true
, the extension will print out debug information into the channel Folding
of the panel Output
(menu: View
/ Output
).
Priority/Delay
VSCode is scoring each folding providers based on the scheme and language. When the scores are identical, the providers which have been registered the most recently, receive a higher priority.
When starting up, VSCode loads the installed extensions. When reading a file, VSCode will load the folding provider of the file's language (only once per language).
The property explicitFolding.delay
(measured in milliseconds, and set to 1000
by default) is used so that this extension's folding provider has a higher priority than that of the language provider.
Notification
The property explicitFolding.notification
(minor
by default) indicates when to show the update notification.
Usages
Language | Config |
---|---|
Emacs |
|
C/C++ |
|
HTML |
|
PHP |
|
Python |
|
SASS |
|
FAQ
Q: Why don't I see the foldings?
A: Firstly, make sure you have the setting "editor.showFoldingControls": "always"
defined, and that you don't have "editor.foldingStrategy": "indentation"
defined. Then, verify your configuration.
Q: Why doesn't \n
work?
A: The document parser is line-based. So in order to match the end of a line, you need to use $
.
Donations
Support this project by becoming a financial contributor, using any of the following methods:
ko-fi.com/daiyam | |
liberapay.com/daiyam/donate | |
paypal.me/daiyam99 |
Supported Editors
VSCode/VSCodium
VSCode uses the folding ranges provided:
- by the folding range provider defined by the setting
editor.foldingStrategy
(auto
orindentation
) - and, by the folding range provider defined by this extension if
editor.foldingStrategy
is set toauto
MrCode
MrCode is using the folding ranges provided:
- by the folding range provider defined by the setting
editor.foldingStrategy
(auto
orindentation
) - or by the folding range provider defined by this extension if
editor.foldingStrategy
is set toexplicit
The long-standing PR tries to bring this new behaviour to VSCode.
Enjoy!