-
Notifications
You must be signed in to change notification settings - Fork 4k
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 make static to not drop trivia #54216
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,12 +18,12 @@ internal abstract class AbstractMakeMemberStaticCodeFixProvider : SyntaxEditorBa | |
{ | ||
internal sealed override CodeFixCategory CodeFixCategory => CodeFixCategory.Compile; | ||
|
||
protected abstract bool IsValidMemberNode([NotNullWhen(true)] SyntaxNode? node); | ||
protected abstract bool TryGetMemberDeclaration(SyntaxNode node, [NotNullWhen(true)] out SyntaxNode? memberDeclaration); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need for an abstract class here, only C# is implemented. But I didn't cleanup this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there's a lot of optimism that this could be in VB :) Fine to leave for now I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ryzngard The situation is the exact opposite in VB. For C#, a static class requires all members to be static There is a code fix for the VB situation: |
||
|
||
public sealed override Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
if (context.Diagnostics.Length == 1 && | ||
IsValidMemberNode(context.Diagnostics[0].Location?.FindNode(context.CancellationToken))) | ||
TryGetMemberDeclaration(context.Diagnostics[0].Location.FindNode(context.CancellationToken), out _)) | ||
{ | ||
context.RegisterCodeFix( | ||
new MyCodeAction(c => FixAsync(context.Document, context.Diagnostics[0], c)), | ||
|
@@ -38,12 +38,13 @@ protected sealed override Task FixAllAsync(Document document, ImmutableArray<Dia | |
{ | ||
for (var i = 0; i < diagnostics.Length; i++) | ||
{ | ||
var declaration = diagnostics[i].Location?.FindNode(cancellationToken); | ||
var declaration = diagnostics[i].Location.FindNode(cancellationToken); | ||
|
||
if (IsValidMemberNode(declaration)) | ||
if (TryGetMemberDeclaration(declaration, out var memberDeclaration)) | ||
{ | ||
editor.ReplaceNode(declaration, | ||
(currentDeclaration, generator) => generator.WithModifiers(currentDeclaration, DeclarationModifiers.Static)); | ||
var generator = SyntaxGenerator.GetGenerator(document); | ||
var newNode = generator.WithModifiers(memberDeclaration, generator.GetModifiers(declaration).WithIsStatic(true)); | ||
editor.ReplaceNode(declaration, newNode); | ||
} | ||
} | ||
|
||
|
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.
instead of walking arbitrarily high, we should check for exactly the shapes we expect here. that prevents thigns like matching on a variable inside a lambda inside a field initializer. In this case, we jsut want to match
node { Parent: VaraibleDeclaration { Parent: FieldDeclarationSyntax or EventFieldDeclarationSyntax } }