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

Improve 'convert regular to raw-string' for common cases (like test code). #67060

Merged
merged 13 commits into from
Feb 28, 2023

Conversation

CyrusNajmabadi
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi commented Feb 26, 2023

Fixed #61267

I was using this feature to update some of our tests to be raw-strings, and found the behavior suboptimal for common patterns we use. Specifically, we often left-align the original string to keep the quotes aligned with the left-aligned content. Keeping things that way when converting to raw strings forces everything to stay left-aligned, evne though it's no longer necessary (as raw strings already handle indentation for you). See tests for examples of the improvements.

You can see the results of this in: #67059

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner February 26, 2023 03:33
}

[Fact]
public async Task FixAllCommonRoslynTestPattern1()
Copy link
Member Author

Choose a reason for hiding this comment

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

these are the new behaviors.

class Y
{
}
""");
Copy link
Member Author

Choose a reason for hiding this comment

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

this output is much better than before. both arguments are now properly indented, while the contents of that argument stay appropriately non-indented.

var indentationOptions = new IndentationOptions(formattingOptions);

var tokenLine = parsedDocument.Text.Lines.GetLineFromPosition(token.SpanStart);
if (token.SpanStart == tokenLine.Start)
Copy link
Member Author

Choose a reason for hiding this comment

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

this is the new logic.

var parsedDocument = await ParsedDocument.CreateAsync(document, cancellationToken).ConfigureAwait(false);
var indentation = token.GetPreferredIndentation(parsedDocument, indentationOptions, cancellationToken);
return ConvertToMultiLineRawIndentedString(indentation, token, formattingOptions, characters);
SyntaxToken ConvertToSingleLineRawString()
Copy link
Member Author

Choose a reason for hiding this comment

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

this helper was inlined.


SyntaxToken ConvertToMultiLineRawIndentedString(
string indentation,
bool addIndentationToStart)
Copy link
Member Author

Choose a reason for hiding this comment

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

this helper was inlined. though the addIndentationToStart part is new. it represents the new behavior we want.

@@ -162,21 +163,30 @@ private static bool CanConvertStringLiteral(SyntaxToken token, out CanConvertPar
// This changes the contents of the literal, but that can be fine for the domain the user is working in.
// Offer this, but let the user know that this will change runtime semantics.
canBeMultiLineWithoutLeadingWhiteSpaces = token.IsVerbatimStringLiteral() &&
HasLeadingWhitespace(characters) &&
(HasLeadingWhitespace(characters) || HasTrailingWhitespace(characters)) &&
Copy link
Member Author

@CyrusNajmabadi CyrusNajmabadi Feb 26, 2023

Choose a reason for hiding this comment

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

discovered as we have a lots of tests that use:

@"class C
{
}
"

This allows the user to say they want to convert, while removing that trailing newline. So you get:

"""
class C
{
}
"""

not

"""
class C
{
}

"""

Copy link
Member

Choose a reason for hiding this comment

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

I agree that we want to offer this fix, but I find it exceedingly unfortunate that it was applied in bulk #67059. I would have expected that bulk refactoring to avoid any behavior changes.

Copy link
Member Author

Choose a reason for hiding this comment

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

I disagree. Applying in bulk without the behavior change just leads to horrendous outcomes with tests full of unnecessary whitespace that was not part of the original test's motivations.

That whitespace was there because of how c# literals work, not because the test author wanted them.

The conversion (which you have to opt into) does change the strict c# meaning of the strings. But it actually makes the tests reflect what the test author was intending.

@davidwengier
Copy link
Contributor

Does this fix #61267 ?

@CyrusNajmabadi
Copy link
Member Author

Why yes it does. how convenient :)

@CyrusNajmabadi CyrusNajmabadi merged commit 6757c56 into dotnet:main Feb 28, 2023
@ghost ghost added this to the Next milestone Feb 28, 2023
@CyrusNajmabadi CyrusNajmabadi deleted the convertRawFix branch February 28, 2023 03:04
@RikkiGibson RikkiGibson modified the milestones: Next, 17.6 P2 Mar 2, 2023
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 this pull request may close these issues.

Convert to raw string doesn't offer to change semantics if there is only whitespace at the end of the string
4 participants