Skip to content

Commit

Permalink
Merge pull request #59548 from CyrusNajmabadi/vbGenEquals
Browse files Browse the repository at this point in the history
Fix Generate-Equals for VB when invoked on type header.
  • Loading branch information
CyrusNajmabadi authored Feb 15, 2022
2 parents f767c70 + 3efcf4f commit a405d25
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ protected override async Task FixAllAsync(

var methodDecl = diagnostic.AdditionalLocations[1].FindNode(cancellationToken);
var method = semanticModel.GetDeclaredSymbol(methodDecl, cancellationToken);
if (method == null)
continue;

var methodBlock = declarationService.GetDeclarations(method)[0].GetSyntax(cancellationToken);

var (accessesBase, members, _) = analyzer.GetHashedMembers(method, operation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -818,5 +818,27 @@ End Class
",
index:=1)
End Function

<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEqualsAndGetHashCode)>
<WorkItem(45919, "https://github.com/dotnet/roslyn/issues/45919")>
Public Async Function TestWithDialogOnClassHeader() As Task
Await TestWithPickMembersDialogAsync(
"
Class [||]Program
Public Property F() As Integer

End Class",
"
Class Program
Public Property F() As Integer

Public Overrides Function Equals(obj As Object) As Boolean
Dim program = TryCast(obj, Program)
Return program IsNot Nothing AndAlso
F = program.F
End Function
End Class",
chosenSymbols:=Nothing)
End Function
End Class
End Namespace
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Immutable;
using System.Composition;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.GenerateDefaultConstructors;
using Microsoft.CodeAnalysis.Host.Mef;

Expand All @@ -26,5 +27,8 @@ public CSharpGenerateDefaultConstructorsCodeFixProvider()

public override ImmutableArray<string> FixableDiagnosticIds { get; } =
ImmutableArray.Create(CS1729, CS7036, CS8983);

protected override SyntaxToken? TryGetTypeName(SyntaxNode typeDeclaration)
=> (typeDeclaration as BaseTypeDeclarationSyntax)?.Identifier;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ internal abstract class AbstractGenerateDefaultConstructorCodeFixProvider : Code
{
public override FixAllProvider? GetFixAllProvider() => null;

protected abstract SyntaxToken? TryGetTypeName(SyntaxNode typeDeclaration);

public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var cancellationToken = context.CancellationToken;
Expand All @@ -23,17 +25,18 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
if (diagnostic == null)
return;

var syntaxFacts = document.GetRequiredLanguageService<ISyntaxFactsService>();
var headerFacts = document.GetRequiredLanguageService<IHeaderFactsService>();

var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var headerFacts = document.GetRequiredLanguageService<IHeaderFactsService>();
if (!headerFacts.IsOnTypeHeader(root, diagnostic.Location.SourceSpan.Start, fullHeader: true, out var typeDecl))
return;

var typeName = syntaxFacts.GetIdentifierOfTypeDeclaration(typeDecl);
var typeName = TryGetTypeName(typeDecl);
if (typeName == null)
return;

var service = document.GetRequiredLanguageService<IGenerateDefaultConstructorsService>();
var actions = await service.GenerateDefaultConstructorsAsync(
document, new TextSpan(typeName.Span.Start, 0), forRefactoring: false, cancellationToken).ConfigureAwait(false);
document, new TextSpan(typeName.Value.Span.Start, 0), forRefactoring: false, cancellationToken).ConfigureAwait(false);
context.RegisterFixes(actions, diagnostic);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,11 @@ private async Task HandleNonSelectionAsync(CodeRefactoringContext context)
// Only supported on classes/structs.
var containingType = semanticModel.GetDeclaredSymbol(typeDeclaration) as INamedTypeSymbol;
if (containingType?.TypeKind is not TypeKind.Class and not TypeKind.Struct)
{
return;
}

// No overrides in static classes.
if (containingType.IsStatic)
{
return;
}

// Find all the possible instance fields/properties. If there are any, then
// show a dialog to the user to select the ones they want.
Expand All @@ -108,9 +104,7 @@ private async Task HandleNonSelectionAsync(CodeRefactoringContext context)
.ToImmutableArray();

if (viableMembers.Length == 0)
{
return;
}

GetExistingMemberInfo(
containingType, out var hasEquals, out var hasGetHashCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Imports System.Composition
Imports Microsoft.CodeAnalysis.CodeFixes
Imports Microsoft.CodeAnalysis.GenerateDefaultConstructors
Imports Microsoft.CodeAnalysis.Host.Mef
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax

Namespace Microsoft.CodeAnalysis.VisualBasic.GenerateDefaultConstructors
<ExportCodeFixProvider(LanguageNames.VisualBasic, Name:=PredefinedCodeFixProviderNames.GenerateDefaultConstructors), [Shared]>
Expand All @@ -23,5 +24,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.GenerateDefaultConstructors

Public Overrides ReadOnly Property FixableDiagnosticIds As Immutable.ImmutableArray(Of String) =
ImmutableArray.Create(BC30387, BC40056)

Protected Overrides Function TryGetTypeName(typeDeclaration As SyntaxNode) As SyntaxToken?
Return TryCast(typeDeclaration, TypeBlockSyntax)?.BlockStatement.Identifier
End Function
End Class
End Namespace
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.LanguageServices
End If

Dim typeStatement = typeBlock.BlockStatement
typeDeclaration = typeStatement
typeDeclaration = typeBlock

Dim lastToken = If(typeStatement.TypeParameterList?.GetLastToken(), typeStatement.Identifier)
If fullHeader Then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Host;

Expand Down

0 comments on commit a405d25

Please sign in to comment.