Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line Breaks off of Static Queries in a SObject Constructor #319

Open
kacrouse opened this issue Dec 7, 2020 · 3 comments
Open

Line Breaks off of Static Queries in a SObject Constructor #319

kacrouse opened this issue Dec 7, 2020 · 3 comments

Comments

@kacrouse
Copy link

kacrouse commented Dec 7, 2020

# Prettier options (if any):
none

Input:

Contact newContact = new Contact(FirstName = 'test', LastName = 'contact', AccountId = [SELECT Id FROM Account LIMIT 1].Id);

Actual output:

Contact newContact = new Contact(
    FirstName = 'test',
    LastName = 'contact',
    AccountId = [SELECT Id FROM Account LIMIT 1]
    .Id
);

Expected output:

Contact newContact = new Contact(
    FirstName = 'test',
    LastName = 'contact',
    AccountId = [SELECT Id FROM Account LIMIT 1].Id
);

Even if the query was longer to where it needed to be split across multiple lines, I still think the .Id should not be on its own line.

Additional information (please fill this out):

  • OS: macOS 11.0.1
  • Version: 1.7.0
@dangmai
Copy link
Owner

dangmai commented Dec 7, 2020

Thanks for the bug report, I agree that the current format does not look right, but we need to think a bit more about the expected output. For example, what if the property chain after the SOQL query is really long, like this:

Contact newContact = new Contact(
    FirstName = 'test',
    LastName = 'contact',
    AccountId = [SELECT Another_Test_Object__r.Yet_Another_Test_Object__r.Account.Id FROM Test_Object__c LIMIT 1].Another_Test_Object__r.Yet_Another_Test_Object__r.Account.Id
);

In my mind it makes sense to format it this way:

Contact newContact = new Contact(
    FirstName = 'test',
    LastName = 'contact',
    AccountId = [SELECT Another_Test_Object__r.Yet_Another_Test_Object__r.Account.Id FROM Test_Object__c LIMIT 1]
      .Another_Test_Object__r
      .Yet_Another_Test_Object__r
      .Account
      .Id
);

Maybe we should allow breaking it up if there are more than 1 property in the chain, but keep it on the same line if there's only 1. This is very early thought from me though, I have to dive deep into the current implementation to see what it's doing, and how complicated it would be to add this type of behavior.

@kacrouse
Copy link
Author

kacrouse commented Dec 7, 2020

You're right, it gets tricky if there is a long query and a long chain of properties. I can't see a good way to break up both the query and the property chain across lines. This might be another option for formatting it. I think the query is likely to be longer than the property chain, so this approach might look better in a larger number of scenarios?

Contact newContact = new Contact(
    FirstName = 'test',
    LastName = 'contact',
    AccountId = [
        SELECT Another_Test_Object__r.Yet_Another_Test_Object__r.Account.Id 
        FROM Test_Object__c 
        LIMIT 1
    ].Another_Test_Object__r.Yet_Another_Test_Object__r.Account.Id
);

Or to get even more complex...

  • if the query can fit on one line, break up the property chain across lines
  • if the query cannot fit on one line, break it up, but don't break up the property chain

That sounds like it might be a pain to implement 😅

@kacrouse
Copy link
Author

kacrouse commented Dec 8, 2020

You may be aware already, but I just realized this issue applies to all static queries, not just those in an SObject constructor. Example below.

Input

[SELECT Another_Test_Object__r.Yet_Another_Test_Object__r.Account.Id FROM Test_Object__c LIMIT 1].Another_Test_Object__r.Yet_Another_Test_Object__r.Account.Id;

Output

[SELECT Another_Test_Object__r.Yet_Another_Test_Object__r.Account.Id FROM Test_Object__c LIMIT 1]
.Another_Test_Object__r.Yet_Another_Test_Object__r.Account.Id;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants