Indentation is wrong when creating new sobjects with fields preset

Issue #51 resolved
Nathen Drees created an issue

When using the syntax to set the fields of an sObject when you initialize it, the indentation is wrong when you enter a new line. Not sure if this is part of your plugin or not.

2015-07-08_11-13-58.png

Comments (11)

  1. Nathen Drees reporter

    Also, when you first start this syntax, it looks like the indentation is 2 tabs off, instead of 1:

    2015-07-08_11-24-40.png

  2. Scott Wells repo owner

    I believe that is driven by the continuation indent which defaults to 8 spaces vs. 4 spaces for the standard indent. Do you mind checking your Apex formatter settings (first tab) and seeing if that corrects the issue? It's also debatable whether that should use the continuation indent vs. the standard indent, but first let's make sure that's it.

  3. Nathen Drees reporter

    Yup, it looks like that's the setting. Changing it to 4 makes it work as expected. I personally would like it to be 4 by default, but as long as there's a setting to change this that should be fine too.

  4. Scott Wells repo owner

    Okay, good to hear. Yeah, I'm taking IDEA's own defaults for as many of those standard settings as possible, and that happens to be one of them. Best thing to do is to create a named code style scheme for Apex, Java, HTML, etc., and you can export that under File>Export Settings for your own or your team's reuse.

    I'm going to resolve this ticket since, once configured, it's working as you'd like. I think there's still an open question of whether that particular code section should be a continuation indent or a standard indent, but I think I'll wait for someone who REALLY uses continuation indent distinctly to tell me that's the case before tackling that!

  5. Vivek M. Chawla

    @RoseSilverSoftware I've experiencing some frustration that seems related to this issue.

    Both Eclipse and Sublime Text allow for preserving the indent of the previous line, instead of automatically using a "continuation indent". Using IDEA / IC, I can't seem to figure out how to get the Apex editor to allow me to do this.

    Here's a small screen shot that explains what I want to do.

    2015-12-05_13-43-52.png

    Right now, it feels like the only way to column-align my code is to repeatedly tab-tab-tab-tab-tab my way over to the right. Is there something I'm missing?

    Thanks, Vivek

  6. Scott Wells repo owner

    Vivek, I may be wrong, but I don't think the other IDEs you mentioned are using a truly configurable, lexical scope-aware formatter the way that IC is (try changing the formatter configuration and reformatting all of the code in your project); they're just using the same indent level as the previous line with perhaps a special case for braced blocks. There are settings in IntelliJ's Java formatter (but not yet IC's Apex formatter) to do things similar to this, e.g., align method formal parameters and arguments, but I don't think there's one for array initializers. There is a setting for Array initializer>Align when multiline, but it doesn't provide the alignment you're describing. I'm not sure that's possible even in Java right now.

    As you said, IC's formatter does properly align array initializer values, method formal params and arguments, etc., according to continuation indents based on the lexical scope. Also be careful trying to fight the formatter. If you do tab-tab-tab as you describe, any config-based reformatting of the code is going to move it back to where the formatter thinks it should go (try CTRL+ALT+L to reformat one or more files or CTRL+ALT+I to reformat the current line or selection). It'll also reformat automatically if you enclose that block in another block.

    If you'd really like to see this type of formatter option, definitely open a separate enhancement request and I'll see what I can do.

  7. Vivek M. Chawla

    Good advice on trying to not fight the formatter. I've been doing that for a couple hours now, and it's not fun. ;-)

    I use a lot of indented / aligned code, and it sounds like IDEA will chew up what I've got if I'm not careful. Is there any way to "dumb-down" IDEA's auto formatter so I don't have to worry about losing carefully aligned code if I enclose code in a new outer block? Pushing a currently formatted block of code another 2 spaces to the right is relatively easy, so I'm OK with IDEA not "helping" me automatically by taking it 20 spaces to the left. ;-)

    I also realize that this might be one of those differences that I just have to get used to while I focus on the cool things that IDEA and IC can do (instead of wishing for an Eclipse+ clone) ;-)

  8. Scott Wells repo owner

    Well, you can't disable or "dumb down" the formatter. That's a key feature of the IDE. For what it's worth, the same is true in Eclipse(/NetBeans/etc.) for fully-supported languages such as Java. They just never fully implemented that extension point in the Force.com IDE and only added simple indent/outdent for braced sections and indent retention from the previous line otherwise. There's an existing enhancement request for just such a formatter in the Force.com IDE:

    https://success.salesforce.com/ideaView?id=087300000006rPZAAY

    The best recommendation I can make right now is to take the code you want to format into Settings>Editor>Code Style>Apex, paste it into the editor on the right, and tinker with the settings until it's as close to your desired format as you can get it. For those things that still aren't working the way you'd like, let's get one or more enhancement requests going.

  9. Vivek M. Chawla

    I think I found an example of the formatting choice I'm looking for.

    Check out the options that are provided with the JavaScript formatter, namely these:

    2015-12-06_08-15-55.png

    It would be great if we had similar options for Apex (someday). :-) I'll create an enhancement request.

  10. Scott Wells repo owner

    Yeah, those options are the ones I referenced around alignment of method formal parameters and arguments. AFAIK, there's not anything similar for array initializer values as in your original example, though there's nothing special about those that prevents it. Let's get the enhancement request going and I should be able to add support pretty easily.

  11. Log in to comment