There’s not a direct API related way to control the fold arrows in the gutter (other than by adjusting the settings that turn them on or off).
At a user level, folding is controlled by this setting:
// The style of code folding to use
// - "auto": Use whatever the default is for the syntax in use
// - "force_indentation": Always use indentation-based code folding
// - "scope_only": Only allow syntax-based code folding
"fold_style": "auto",
When the value of this is force_indentation
, or when it is auto
but the metadata that supplies scope based folding is missing, then the folding arrows that appear in the gutter appear automatically and are controlled by a change in indentation level.
Conversely, when fold_style
is scope_only
, or when it’s auto
and there is metadata to supply scope based folding rules, fold indicators will appear in the gutter at positions where folding can start.
The metadata that you want is something that would be stored in a tmPreferences
file. As an example, you can use View Package File
from the command palette and use the filter markdown fold
to find and open the Markdown/Fold.tmPreferences
file.
In a nutshell, the file contains:
- A
scope
that specifies to what syntax the metadata inside affects
- A
foldScopes
inside of settings
to indicate that the metadata to be added is fold scopes
- An array of 1 or more sets of fold scopes that include:
- The scope that will
begin
the fold area
- The scope that will
end
the fold area
- Whether or not the trailing newline on the line that contains the
end
scope will also be included in the fold
For your own syntax definition you would need to create such a file (the name of the file itself does not matter, only the tmPreferences
extension does, but the convention is to name it Fold
to remind you what it’s doing) that specifies your custom syntax scope and the locations where it should begin and end folding.
Generally speaking globals should work for this, though you have to be wary of the fact that whenever your plugin .py
file reloads, the value of the global will reset, but anything holding a reference to it that was not also reloaded may be holding onto the old value.
The other way to share state between different commands would be settings (for example view
settings or window
settings), which have the benefit of being persisted in the session file so that their values will remain in place even if you quit and restart Sublime (assuming you’re using hot_exit
). The caveat there is that you can only store basic types as settings (anything that can be converted to json
, essentially).
I don’t know of any threads off the top of my head about the topic in general, but if you provide an example of what you were trying to do that didn’t work, I’m sure we can work out what went wrong and get you onto the right track.