-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Update formatOnType handler to support formatting on NewLine #76876
Conversation
{ | ||
// When formatting after a newline is pressed, the cursor line will be blank and we do | ||
// not want to remove the whitespace indentation from it. | ||
textChanges = textChanges.WhereAsArray(change => !change.Span.Contains(position)); |
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'm not really getting the comment. in the case of a newline, you do remove some of the changes. could you bemore specific in the comment, perhaps with examples of the data we get back, what we want to filter out, and waht we want to keep?
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.
Updated!
src/LanguageServer/ProtocolUnitTests/Formatting/FormatDocumentOnTypeTests.cs
Outdated
Show resolved
Hide resolved
src/LanguageServer/Protocol/Handler/Formatting/FormatDocumentOnTypeHandler.cs
Outdated
Show resolved
Hide resolved
// Letting the second change go through would be a bad experience for the user as they | ||
// will now be responsible for adding back the proper indentation. | ||
|
||
textChanges = textChanges.WhereAsArray(static (change, position) => !change.Span.Contains(position), position); |
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.
great comment. very helpful. would it make sense to also add a condition that that final span of the change is empty? basically, it would b only filtering out the formatting change that deletes the code the caret it at.
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.
Looks like I was imprecise and will update the comment.
The second change runs from the start of the cursor line to the closing brace. It removes the whitespace from the cursor line and rewrites the indentation on the line with the closing brace. The 14 characters \r\n
is replaced with \r\n
.
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.
Enables format on type when the trigger character is a newline. Removes text changes that contain the cursor as we do not want to remove the indentation.
FormatOnType.NewLine.mp4
Resolves dotnet/vscode-csharp#6834