Code folding prevented by non-indented line


#1

Hi all,

I want to fold a large code of block but the folding stops at the first line that is not indented.

For example, in the following C function:

void example()
{
    // Comment
    int a = 0;
#if 0
    // Commented block
#endif
    int b = 1;
}

I would expect the folding on the first bracket “{” to fold up to the closing bracket “}” but instead stops at the compiler directive “#if 0”.

Is this Atom’s expected behaviour? Is it possible to fold against matching brackets?

Thanks!


#2

This, too, is a problem for me.

It appears that the Folding function in Atom works off the assumed nesting of lines based on indents, rather than matching opening and closing braces or tags. This makes it completely useless when working with files which are a mix of HTML and PHP (where each has their own nesting levels) or where comments are marked at the start of a line rather than at the respective indent level.

This is making folding in Atom all but useless for me at the moment, and needs to be addressed.


#3

Neither HTML nor PHP are whitespace-sensitive. It’s possible to organize them in such a way that you have meaningful blocks to fold.

If you want to work toward Atom having better language-specific folding sense, the first step would be more public API methods for working with folds. Right now, you can fold a selection and that’s it, but a smart folding package would require something like a FoldRegistry where you can add and remove individual fold zones programmatically. That could be cool.

In the mean time, maybe this package can help fill the gap.


#4

I am aware that HTML & PHP are not (programmatically) whitespace-sensitive, but they certainly are when it comes to a monkey reading both the unexecuted and resulting code.

When working with HTML & PHP, you essentially end up with a file which has two intermingled indentation schemas. I indent the PHP to keep the logic organised and properly nested in my mind and the file, and between all that the HTML is also being organised, indented and nested to ensure that I don’t put the wrong element in the wrong element.

Other editors work their folding on matching tags, Atom is great, but this is really making life difficult for me…


#5

Atom currently has no awareness of any code languages built-in, so the rich editor features (highlighting and folding) have no way to know where the matching tags are.


#6

But it is worth pointing out tree-sitter grammars can (or at leat have potential to) do this.


#7

Atom currently has no awareness of any code languages built-in, so the rich editor features (highlighting and folding) have no way to know where the matching tags are.

Atom does highlight matching closing tags in HTML and PHP, so it has some awareness, so this is incorrect. If it can recognise matching tags for highlighting, it should be able to recognise them for folding.


#8

That’s done by the bracket-matcher package (which is inbuilt, so I guess it counts). It would be interesting to see if someone can write a package that can take the bracket pair information, and make a fold in that range.


#9

Like @Aerijo says, that’s the bracket-matcher package, which is a core package, but it can still be disabled. If you disable all packages, folding and highlighting still work (if you were to add a grammar to the registry).