Code intention for adding enum values as switch/when labels

Issue #1327 new
Justin Julicher created an issue

when using switch statements it would be good to create a live templates of sort that could generate the switch statement with all the valid when blocks. e.g. when an enum is used for the on.

e.g.

public enum TestEnum { TEST1, TEST2, TEST3};
TestEnum test = TEST3;
switch on test {
     when TEST1 {

     }
     when TEST2 {

     }
     when TEST3 {

     }
}

In the above code you could type ‘switch+tab’ it would prompt for the ‘test’ value for variables in scope and when you hit tab again (if it’s an Enum) it would generate the code above and then place you in the scope of the first block for the TEST1 enum.

if the ‘on’ was a string or integer then for each tab it would generate a new when block and you could type in the ‘1’,'2' + tab and it would do this:

String testString = 'test';

switch on testString {
       when [first tab] {

       }
       when [second tab] {
       }
}

when it detects that its an generic SObject it could do the above but then do the code completion for the SObjects and the next tab would then prompt for the name e.g.

SOBject mySObject = Trigger.new.get(0);
switch on [first tab] {
    when [second tab] [third tab] {

    }
    when [forth tab] [fifth tab] {

    }
}

so after 3 tabs it would look something like:

SOBject mySObject = Trigger.new.get(0);
switch on mySbject {
    when Account acct {

    }
    when [forth tab] [fifth tab] {

    }
}

I think you would hit enter and it would complete the code completion and put you back in the Account block for that last example.

thanks!

Comments (3)

  1. Scott Wells repo owner

    Hey, Justin. I'm planning to implement something like this as well. In the Java editor it's a code intention to insert known branches and/or a default branch:

    Switch_Code_Intention.png

    I'm planning to implement pretty much the same thing. In fact, I'm not sure if I have an open story for that work, so I'll just plan to use this one.

  2. Log in to comment