Code Folding

Answered

I've landed in a shop with a ton of sloppy hard-to-read code. I mean a lot of it. And it's averse to refactoring for readability. So I fold code a lot. (Apologies in advance for the verbosity of this post.)

My problem: Code folding in IntelliJ folds one too many carriage returns. Is there a way to configure that behavior?

Consider this code:

public class CodeFolds {
     public void iffyMethod() {
     double x = Math.random();
     if (x > 10) {
         System.out.println("10");
         System.out.println("10");
         System.out.println("10");
         System.out.println("10");
     } else if (x > 9) {
         System.out.println("10");
         System.out.println("10");
         System.out.println("10");
         System.out.println("10");
     }

          [... and so on...]

}
}

In real life that "if" would have several long blocks that we'd like to hide by folding. It'd also have long conditions for the if and each else-if. If we were to fold those blocks, IntelliJ would show this:

public class CodeFolds {
    public void iffyMethod() {
     double x = Math.random();
     if (x > 10) {...} else if (x > 9) {...} else if (x > 8) {...} else if (x > 7) { //
         System.out.println("10");
         System.out.println("10");
         System.out.println("10");
         System.out.println("10");
     } else if (x > 6) {
         System.out.println("10");
         System.out.println("10");
         System.out.println("10");
         System.out.println("10");
    }
}
}

That's going to rapidly push the one long line off the screen. I suggest the folds should collapse like this instead:

public class CodeFolds { 
     public void iffyMethod() {
     double x = Math.random();
     if (x > 10) { ...
     } else if (x > 9) { ...
     } else if (x > 8) { ...
     } else if (x > 7) {
         System.out.println("10");
         System.out.println("10");
         System.out.println("10");
         System.out.println("10");
     } else if (x > 6) {
         System.out.println("10");
         System.out.println("10");
         System.out.println("10");
         System.out.println("10");
     }
}
}

That may not fold as compactly in the vertical, but it preserves the structure. More importantly, any number of folds can be made and the structure will still be evident. The value of code folding isn't as simple as hiding stuff. Code folding is useful because it lets us show something that's obscured - namely, the code's structure.

0
2 comments

Big thanks to whomever formatted the code in this post.

1

Hi Jerry,

Thank you for taking the time to contact IntelliJ IDEA Support.

Don't think it's possible at the moment, so I created a new feature request for this on our issue tracker: IDEA-259423.

Feel free to vote for it and leave additional comments there.

See this article if you are not familiar with YouTrack.

0

Please sign in to leave a comment.