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

If Statement with long expression #34

Closed
belav opened this issue Mar 14, 2021 · 4 comments · Fixed by #75
Closed

If Statement with long expression #34

belav opened this issue Mar 14, 2021 · 4 comments · Fixed by #75

Comments

@belav
Copy link
Owner

belav commented Mar 14, 2021

Currently when an if statement has a long expression, it formats like

if (
    !this.model.TranslatableFields.TryGetValue(
        fieldName,
        out var dictionary
    )
)
{
    return;
}

I think it should do this

if (
    !this.model.TranslatableFields.TryGetValue(
        fieldName,
        out var dictionary
    )
) {
    return;
}

if (shortStatement)
{
    return;
}

or

if (
    !this.model.TranslatableFields.TryGetValue(
        fieldName,
        out var dictionary
    ))
{
    return;
}

if (shortStatement)
{
    return;
}

This applies to other syntax nodes, like for loops and while loops.

@belav belav added type:bug Something isn't working area:formatting labels Mar 14, 2021
@belav belav added this to the Beta milestone Mar 14, 2021
@belav belav added formatting-style and removed type:bug Something isn't working area:formatting labels Mar 22, 2021
@shocklateboy92
Copy link
Collaborator

Hmmm. I'm definitely not a fan of # 3. # 2 might make sense if we want to make it consistent with things like constructors or long method defintions:

public Constructor(
    Type arg1,
    Type arg2
) {
    // logic
}

@belav
Copy link
Owner Author

belav commented Apr 10, 2021

I made #2 work, which I'm going to call SpaceBrace (vs NewLineBrace).
I made WhileStatements, IfStatements, ForEachStatements and UsingStatements all use SpaceBrace.
There are a number of other node's that have a block syntax that could also use SpaceBrace.

I see two options

  1. Format everything that has a block syntax using SpaceBrace. Which means in the example below, the MethodDeclaration would be changing.
  2. Format everything that has the potential to be deeply nested using SpaceBrace, everything else using NewLineBrace. In the code below, the MethodDeclaration can only have parameters, it can't really be any deeper, so it would stay NewLineBrace.

I think I'm leaning towards option 1, because then things are consistent. But I'm not opposed to option 2 either.

    public void LongMethodNameForceLineBreak(
        string oneoneoneoneoneoneoneoneone,
        string twotwotwotwotwotwotwotwo,
        string threethreethreethreethreethree)
    {
        if (
            longStatementName
            && longerStatementName
            && evenLongerStatementName
            && superLongStatementName
        ) {
            return;
        }
    }

belav added a commit that referenced this issue Apr 10, 2021
changing if, while, foreach and using to use SpaceBrace formatting.
closes #34
@belav
Copy link
Owner Author

belav commented Apr 11, 2021

After working on #58 I'm thinking we go all in on SpaceBrace.
In order to print Lambda methods like this, we need a SoftLine before the close paren. If we wanted to use NewLineBrace we'd have to add extra logic to decide when that SoftLine should be added.

const doSomething = (
  jasdjklfasjkldflajskdflkasdlfjkasdf,
  jalsdfkasdlkfalksflksdf,
  kjladslkfasjfkasdfl
) => doSomethingElse();

belav added a commit that referenced this issue Apr 12, 2021
changing if, while, foreach and using to use SpaceBrace formatting.
closes #34
@belav belav closed this as completed in #75 Apr 12, 2021
@jrobertson-insite
Copy link

jrobertson-insite commented Apr 24, 2021

This is closed... but the spacebrace in all cases as seen in the screenshot below is seemingly not the way the vast majority of C# is formatted in my experience.

image

Edit: This is not specifically addressed in the MS C# coding conventions but samples basically never use space brace.

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions

@belav belav removed this from the Beta milestone Jul 19, 2021
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

Successfully merging a pull request may close this issue.

3 participants