Skip to content

Commit

Permalink
Add elastic trivia to Inline Declaration if necessary (#47916)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwengier authored Sep 23, 2020
1 parent 8d87741 commit 81f358b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ private static DeclarationExpressionSyntax GetDeclarationExpression(
SourceText sourceText, IdentifierNameSyntax identifier,
TypeSyntax newType, VariableDeclaratorSyntax declaratorOpt)
{
newType = newType.WithoutTrivia().WithAdditionalAnnotations(Formatter.Annotation);
var designation = SyntaxFactory.SingleVariableDesignation(identifier.Identifier);

if (declaratorOpt != null)
Expand All @@ -310,6 +309,15 @@ private static DeclarationExpressionSyntax GetDeclarationExpression(
}
}

newType = newType.WithoutTrivia().WithAdditionalAnnotations(Formatter.Annotation);
// We need trivia between the type declaration and designation or this will generate
// "out inti", but we might have trivia in the form of comments etc from the original
// designation and in those cases adding elastic trivia will break formatting.
if (!designation.HasLeadingTrivia)
{
newType = newType.WithAppendedTrailingTrivia(SyntaxFactory.ElasticSpace);
}

return SyntaxFactory.DeclarationExpression(newType, designation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2292,5 +2292,26 @@ await TestMissingAsync(@"
{
}", new TestParameters(TestOptions.Regular));
}

[WorkItem(47041, "https://github.com/dotnet/roslyn/issues/47041")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineDeclaration)]
public async Task CollectionInitializer()
{
await TestInRegularAndScript1Async(
@"class C
{
private List<Func<string, bool>> _funcs2 = new List<Func<string, bool>>()
{
s => { int [|i|] = 0; return int.TryParse(s, out i); }
};
}",
@"class C
{
private List<Func<string, bool>> _funcs2 = new List<Func<string, bool>>()
{
s => { return int.TryParse(s, out int i); }
};
}");
}
}
}

0 comments on commit 81f358b

Please sign in to comment.