Code folding close bracket


#1

atom’s code folding reduces a block to one line instead of two, showing e.g. the open curly brace of a function but not the close curly brace. I’d like the option to show the close brace, because glancing over a line with an open brace but no matching close brace raises a red flag and I have to consciously dismiss it.

function foo(){...
}

var something = foo();

looks less suspect to me than

function foo(){...

var something = foo();

I think Sublime’s default is to show the close brace. What are other peoples’ opinions?


#2

If I’m folding code, I’d rather it would fold down to one line … like in the tree view. I see your point though about missing the close brace. Would this be an option that would work for you?

function foo(){...}

var something = foo();

#3

I think that would be better than how it is now, but I’d still prefer them on separate lines (since that’s how I wrote the function) (and also because that’s how Sublime does it and I’m used to that).


#4

A more compelling case: Consider that the line with the closing bracket also contains more than just the closing bracket. e.g. calling a js function that returns a Promise:

someLongRunningOp(
    param1,
    param2,
    param3,
    param4
).then(function(value){
    doSomethingWith(value);
});

Now collapse line 1 in Sublime:

someLongRunningOp(...
).then(function(value){
    doSomethingWith(value);
});

That makes sense. Compare to collapsing line 1 in atom:

someLongRunningOp(...
    doSomethingWith(value);
});

wtf?


#5

You are correct that the code folding behavior is a bug. Additionally, the two cases are different in more than just the way you mention. The first example is folding up the body of a function. The second example is folding up a parameter list of a function call. It would appear that the code folding system is misinterpreting one for the other.


#6

Similar to the second case is something like this that I’m running into:

Class.extend({
    init: function() {...
    other: function() {...
});

Here, the ending brace (and rest of the line) is also important. There is no way to know without expanding the folding whether there is a comma after the closing brace.

For example, this is valid:

Class.extend({
    init: function() {...},
    other: function() {...}
});

But this is invalid:

Class.extend({
    init: function() {...}
    other: function() {...}
});

This has tripped me up several times when wanting to add another function to the end of the object.

I don’t mind if the collapse is one line or two but it should only hide the code, not the end of the block.


#7

Was there any outcome to this?

I’ve just switched from Sublime and really don’t like the way Atom folds code.

Are there any configuration opens to change this to make it similar to Sublime or did somebody create a package in the end?

Thanks


#8

There has not been an update here as far as I know…I may look into writing a package to solve this but it really seems like that would a lot more work than getting Atom to fix this issue :frowning:


#9

Wouldn’t it be possible to fold the same way we are now but instead of folding and hiding the closing line we append the closing line to the fold?

someLongRunningOp( ... ).then(function(value){
    doSomethingWith(value);
});

This would also resolve the issue of if/else

if (someTest) {
    // do some code here
} else {
    // and some other code here
} 

Would collapse to:

if (someText) { ... } else {
    // and some other code here
}

Rather than

if (someText) {
    // and some other code here
}

Which is extremely confusing…


#10

But how would you fold both the if and the else block this way?


#11

That’s a good point and I suppose you would be unable to do that but you currently can’t do that anyway unless you close the else first.


#12

I’ve also now tested a few other text editors that follow my proposed method and it’s also not possible to close the if and the else when closing the if first.

Perhaps Sublime does have this the most correct and we should be sacrificing a line when we fold so we can still fold the else statement


#13

How about instead of moving the entire contents of the closing line, just move the closing bracket? A bit more complicated, but i think it would solve most issues


#14

Is there any solution for this?


#15

I agree with favoring Sublime’s approach. I think closing “blocks” rather than trying to intelligently close code based on syntax and function makes the most sense.

.a-thing {
    color: red;
    padding: 1rem;
}

becomes

.a-thing {
}

and

if (something) {
  console.log('do something');
  console.log('do something');
} else {
  console.log('do not do something');
  console.log('do not do something');
}

would become

if (something) {
} else {
}

#16

I agree with Aaron, I really wish there was a way to simply fold the code-block instead of using the current approach Atom is using.


#17

Yeah, it makes it really difficult to do things around that block. Indenting it or wrapping some HTML/JS conditional around it feels awkward. :pensive:

It also doesn’t look like the “folding” logic is in a package. Kind of a bummer because then it could be disabled and a different package could be installed in its place.


#18

Code folding of any level is always going to be intimately tied up with the grammar system. I’m hoping that as the grammar system in Atom improves, folding will be dragged along with it :grinning:


#19

Ah! Didn’t realize it was tightly coupled to the grammar part of Atom. Thanks for the heads up!


#20

I switched to Atom from Brackets recently, and this is one of the big things that bugs me. The lack of the closing bracket coupled with the fact that the folding is based on indentation makes reading folded code pretty much a mystery. I can never be sure that what I folded is actually what I meant to fold, and that’s really frustrating.

This especially applies to HTML for me. I’d really appreciate if
<div> <p><span> <div> </div> </span></p> </div>

collapsed into
<div>...</div>

rather than
<div>...

or worse yet, (because the indentation was off)
`


`