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

Don't format regions inside inactive conditionals #64551

Merged
merged 5 commits into from
Oct 18, 2022

Conversation

Youssef1313
Copy link
Member

Fixes #62612

@Youssef1313 Youssef1313 requested a review from a team as a code owner October 6, 2022 20:34
@ghost ghost added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Oct 6, 2022
else
{
return LineColumnRule.Preserve;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this likely warrants docs :)

// skip whitespace trivia.
}

if (!previous.IsKind(SyntaxKind.DisabledTextTrivia))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, consider inverting.

var previous = trivia2;
while ((previous = previous.GetPreviousTrivia(previous.SyntaxTree, CancellationToken.None)).IsKind(SyntaxKind.WhitespaceTrivia))
{
// skip whitespace trivia.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs more explanation :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about end-of-line trivia?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about end-of-line trivia?

The following test just passes:

        [Fact]
        public async Task TestRegion2()
        {
            var testCode = @"
class MyClass
{
#if true
    public void M()
    {
[||]#region ABC
        System.Console.WriteLine();
[||]#endregion
    }
#else
    public void M()
    {
#region ABC
        System.Console.WriteLine();
#endregion
    }
#endif
}
";
            var fixedCode = @"
class MyClass
{
#if true
    public void M()
    {
        #region ABC
        System.Console.WriteLine();
        #endregion
    }
#else
    public void M()
    {
#region ABC
        System.Console.WriteLine();
#endregion
    }
#endif
}
";
            await Verify.VerifyCodeFixAsync(testCode, fixedCode);
        }

(I'll definitely commit and push it)

'#region'.GetPreviousTrivia(..) is not an end of line trivia for this case though.

Do you have an interesting test case where the tree will have end of line trivia?

I found a niche case where my logic is incorrect apart from EOL trivia:

#region OUTER
        #region ABC
        System.Console.WriteLine();
        #endregion
#endregion

Here, after we go back consuming whitespace trivia, we don't find a disabled text, but we are in disabled text.

Is there a compiler API for the "Is this position conditionally excluded?" question?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does IsInInactiveRegion work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CyrusNajmabadi It doesn't 😕

public static bool IsInInactiveRegion(
this SyntaxTree syntaxTree, int position, CancellationToken cancellationToken)
{
Contract.ThrowIfNull(syntaxTree);
// cases:
// $ is EOF
// #if false
// |
// #if false
// |$
// #if false
// |
// #if false
// |$
if (syntaxTree.IsPreProcessorKeywordContext(position, cancellationToken))
{
return false;
}
// The latter two are the hard cases we don't actually have an
// DisabledTextTrivia yet.
var trivia = syntaxTree.GetRoot(cancellationToken).FindTrivia(position, findInsideTrivia: false);
if (trivia.Kind() == SyntaxKind.DisabledTextTrivia)
{
return true;
}
var token = syntaxTree.FindTokenOrEndToken(position, cancellationToken);
if (token.Kind() == SyntaxKind.EndOfFileToken)
{
var triviaList = token.LeadingTrivia;
foreach (var triviaTok in triviaList.Reverse())
{
if (triviaTok.Span.Contains(position))
{
return false;
}
if (triviaTok.Span.End < position)
{
if (!triviaTok.HasStructure)
{
return false;
}
var structure = triviaTok.GetStructure();
if (structure is BranchingDirectiveTriviaSyntax branch)
{
return !branch.IsActive || !branch.BranchTaken;
}
}
}
}
return false;
}

That seems a hard-to-answer question on the IDE side.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems a hard-to-answer question on the IDE side.

Not really. I think I see how I can do it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CyrusNajmabadi This is ready for another look now :)

@CyrusNajmabadi
Copy link
Member

Currently failing test: Microsoft.CodeAnalysis.CSharp.UnitTests.Formatting.FormattingEngineTriviaTests.MixAll

@Youssef1313
Copy link
Member Author

Currently failing test: Microsoft.CodeAnalysis.CSharp.UnitTests.Formatting.FormattingEngineTriviaTests.MixAll

Fixed. Not an ideal fix though, just falling back to the original behavior for edge cases that are harder to handle.

@Youssef1313
Copy link
Member Author

@CyrusNajmabadi Can you take a look please? Thanks!

Copy link
Member

@CyrusNajmabadi CyrusNajmabadi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tentative approval. but would like answer to question posed.

@CyrusNajmabadi CyrusNajmabadi merged commit f670952 into dotnet:main Oct 18, 2022
@ghost ghost added this to the Next milestone Oct 18, 2022
@Youssef1313 Youssef1313 deleted the issues/62612 branch October 18, 2022 14:36
@RikkiGibson RikkiGibson modified the milestones: Next, 17.5 P1 Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False IDE0055 (Fix formatting) reported when using multiple TargetFrameworks
3 participants