Sublime Forum

C++ code folding stops with character in first column

#1

Code folding stops when there is non-whitespace in the first column. I noticed this in ST2 and figured it was a bug that would get fixed in ST3. Anybody else notice this and know of a work around – other than reindenting which isn’t convenient for working with an existing code base.

0 Likes

#2

It’s not a bug. The default folding is language agnostic, relying on tabs to determine where to fold. I believe there are some plugins that try to do this more intelligently, though I don’t know any off the top of my head.

0 Likes

#3

I might call it a bug, considering that the .tmLanguage files have built in folding start and stop markers.

0 Likes

#4

The folding markers are specified in the language file, so yes this is a bug.

The same happens with Lua syntax which is much simpler:

	<key>foldingStartMarker</key>
	<string>^\s*\b(function|local\s+function|if|for)\b|{ \t]*$|\\</string>
	<key>foldingStopMarker</key>
	<string>\bend\b|^\s*}|\]\]</string>

I don’t see anything in either of those rules which would identify something in column 1 as a start or stop folding marker.

0 Likes

#5

It’s not a bug: Jon simply hasn’t done it yet. I too wish for “proper” folding that uses the .tmLanguage definitions. It was high on my hope-list for ST3. The current folding works quite literally on the indent level: not great. It’s also a PITA that it doesn’t remember folds in your project, though I am aware that some plugins do circumvent this.

0 Likes

#6

I’m trying ST3 for the first time right now, and I’m a little confused about code folding. (Ironically, this thread was right at the top of the forum.)

I wrote a bit of Python:
def foo():
…larry()
…curly()
…moe()

(In the actual code, those four dots are a single tab). I placed the cursor in the header line (def foo():slight_smile: and hit the keyboard shortcut for Fold (opt-cmd-). It did nothing. So I selected the whole line, entered the keyboard cmd, and it folded the header, leaving the body—the exact opposite of what I wanted. So I dragged down the side and carefully selected the whole function, hit the keyboard cmd, and the whole thing folded up and disappeared, including the header line. So, now I can’t see what I’ve folded. So I selected all the lines in the function body, avoiding the header line, hit the command, and all the selected lines folded up, leaving the header, as I wanted.

Is this really how Sublime Text code folding is supposed to work (I have to manually select all the lines to fold), or am I doing it wrong or missing a preference setting or something? Or is the idea with ST that you should uncomment or specify some sort of package specific to the syntax of the file whose code you want to fold, then the keyboard shortcut triggers the command from that package?

EDIT: I found my own answer, in case it’s useful to others:
Instead of putting the cursor in the header line, put it on one of the indented lines in the group you want folded. You don’t have to select them all, just put the cursor in one of them.

0 Likes