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

Reproduce and fix 'Use expression body' indenting new expression body oddly #76582

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -580,4 +580,48 @@ public long Length //N
""";
await TestWithUseExpressionBody(code, fixedCode);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/38057")]
public async Task TestUseExpressionBodyPreserveComments2()
{
var code = """
public class C
{
{|IDE0025:public long Length // cool prop
{
get { return 1 + 2; }
}|}
}
""";
var fixedCode = """
public class C
{
public long Length // cool prop
=> 1 + 2;
}
""";
await TestWithUseExpressionBody(code, fixedCode);
}

[Fact]
public async Task TestUseExpressionBodyPreserveComments3()
{
var code = """
public class C
{
{|IDE0025:public long Length // cool prop
{
/*a*/ get { return 1 + 2; }
}|}
}
""";
var fixedCode = """
public class C
{
public long Length // cool prop
/*a*/ => 1 + 2;
}
""";
await TestWithUseExpressionBody(code, fixedCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,13 @@ public static bool TryConvertToArrowExpressionBody(
var comments = parent.GetLeadingTrivia().Where(t => !t.IsWhitespaceOrEndOfLine());
if (!comments.IsEmpty())
{
arrowExpression = arrowExpression.WithLeadingTrivia(
parent.GetLeadingTrivia());
arrowExpression = arrowExpression.WithLeadingTrivia(parent.GetLeadingTrivia());
}
else if (parent.GetLeadingTrivia().All(t => t.IsWhitespace()))
{
// Manually copy indentation, but keep the elastic to erase the newline trivia after the property
// identifier token if there is no comment following it.
arrowExpression = arrowExpression.WithPrependedLeadingTrivia(parent.GetLeadingTrivia());
}
}

Expand Down
Loading