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

Use new document formatter to format when a type is moved to a new document #58977

Closed
wants to merge 1 commit into from

Conversation

ryzngard
Copy link
Contributor

@ryzngard ryzngard commented Jan 20, 2022

Fixes #58660

// established for the existing type prior to being moved
var newDocumentOptions = (DocumentOptionSet)originalOptions.WithChangedOption(new Options.OptionKey(CodeStyleOptions2.RequireAccessibilityModifiers, unformattedDocumentWitMovedType.Project.Language), AccessibilityModifiersRequired.Never);
var newDocumentFormattingService = unformattedDocumentWitMovedType.GetRequiredLanguageService<INewDocumentFormattingService>();
var documentWithMovedType = await newDocumentFormattingService.FormatNewDocumentAsync(unformattedDocumentWitMovedType, newDocumentOptions, SemanticDocument.Document, default).ConfigureAwait(false);
Copy link
Member

@Youssef1313 Youssef1313 Jan 20, 2022

Choose a reason for hiding this comment

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

For some scenarios, @sharwell was suggesting that codefixes should never do a complete formatting. For example: #58078

I'm not sure this applies here as well, since we're moving to a completely new document. But wanted @sharwell to confirm.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unsure. I think whatever we decide should be same for "extract base" and "extract static" as well, since those are similar features that take existing code and move to a new document.

I believe this is the right approach, but happy to hear Sam's thoughts on the matter.

Copy link
Member

@sharwell sharwell Jan 21, 2022

Choose a reason for hiding this comment

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

For this feature, it's fine to apply formatting as long as there are absolutely no changes (not even whitespace changes) from the first line of the type that moved to its closing }. A test should be added showing that an incorrectly formatted type is not reformatted when it moves to a new file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(not even whitespace changes)

This could get tricky with namespace block changes for indentation.

Comment on lines +1668 to +1689
await TestMoveTypeToNewFileAsync(
originalCode: @"
using System;

class A
{
}

class [|B|]
{
}",
expectedSourceTextAfterRefactoring: @"
using System;

class A
{
}",
expectedDocumentName: "B.cs",
destinationDocumentText: @"
class B
{
}");
Copy link
Member

Choose a reason for hiding this comment

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

The linked issue mentions file headers, this is not tested currently.

It also needs some thought. f we completely removed the file header, then we're regressing a good behavior for many developers. I think a reasonable behavior is to consider whatever exists in file_header_template. If it's not set, then don't add any header.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Quick action "Move type to new file" does not fix header or remove unneeded using statements
3 participants