2

Inside a JavaScript switch instruction, different case(s) may contain much code. Therefore it could be useful to collapse (fold) the code for cases other than the one I am working on.

I use Netbeans. How do I do that?

Brice Coustillas
  • 2,363
  • 2
  • 13
  • 18

3 Answers3

1

Previously unknown to me, but very handy!

Put braces around the code that is inside the case: Netbeans will display the fold/unfold symbol then you can proceed as usual.

switch (kNumCell)
{
    case "0":
    {
      // code comes here
    }
    break;
     // …
}
Brice Coustillas
  • 2,363
  • 2
  • 13
  • 18
  • 1
    Just to add that you can also configure how you want code folding to work for JavaScript in general: **Tools > Options > Editor > Folding > Language >** then select **JavaScript** from the drop list. – skomisa Feb 06 '19 at 18:17
  • Hum… Actually you can specify to collapse code blocks, but as far as I have tried this rule does not apply to switch cases. Did you try it for yourself? – Brice Coustillas Feb 20 '19 at 10:07
1

The simple solution is adding curly braces around your case statement:

switch (num){
    case 1: {
        //Do something
        break;
    }
    default: {
        //Do something
        break;
    }
}

The more complex solution is to reconfigure code folding to for JavaScript in general: Tools > Options > Editor > Folding > Language >, then select JavaScript.

gbeegs
  • 11
  • 5
0

You can simply add a custom Surround with Code folding suggestion on code selection, so you can fold any part of code you want. Here is a tutorial.

Matteo Baldi
  • 5,613
  • 10
  • 39
  • 51