Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Fix LF line-ending auto format bug #10802
Fix LF line-ending auto format bug #10802
Changes from 3 commits
e97ad73
7cca6bd
edea7fe
1c7e136
0ef7de5
7e8e056
c04f865
3dc6961
053f412
624e5d6
95d7a72
b445345
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: This method could just return a bool and be called
HasLFLineEndings
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a couple of thoughts/questions about this method:
SourceText.Lines
already been populated at this point? I'd be surprised if it isn't, since this occurs after all of the formatting passes. Given that, would it be more efficient to just walk through theLines
collection and check the line ending? That would give you the exact span of each line ending, so you wouldn't need to walk every character in theSourceText
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, this method does not access instance data and could be static. In fact, it's probably useful enough as a utility function to move it to
SourceTextExtensions
and make it an extension method onSourceText
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For an algorithm like this, I might do a few things differently to improve the efficiency. You can take these suggestions or leave them! Or, you can take an entirely different approach! I just wanted to share a couple of opportunities:
\r
.sourceText[i]
to a local variable or use a switch statement to avoid indexing extra times. This allows you to walk characters in pairs -- the previous and current character.As I mentioned above, it might just be simpler (and more efficient) to walk through
SourceText.Lines
. However, I wanted to provide some suggestions in case you keep this algorithm. Also, please note that I have not tested this code! I wrote it directly into the GitHub comment field. So, please take the accuracy with a grain of salt. I've only had one cup of coffee this morning. 😄There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests are great, and I was going to ask you to create a version of the
Formats_CodeBlockDirectiveWithMarkup_NonBraced
test as well (because it adds a newline with trailing whitespace, which these don't cover), but then I thought about it more, and I think it would be more interesting ifFormattingTestBase
was modified to replace\r\n
with\n
, and we run every test with both line endings.ie, duplicate these two lines again, but with
input
andexpected
having had their line endings replaced with LFhttps://github.com/dotnet/razor/blob/main/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs#L54
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense! That would be a better way to widen the test coverage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding test helpers to add the line breaks for you. Something like these would make it a little less error prone to write new tests.
Then, the tests can be written by passing in the line text without line endings like so:
Even better, since this is test code, efficiency is less worrisome than in production code. So, you could write a helper that uses a
SourceText
to parse the lines and use that to build up a new string with different line endings. Something like the following would do the trick (Note: untested code!).Then, the tests could continue using raw string literals like so:
Even, even better, the test helper could be extension method on string.