Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

:'(


// 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
Copy link
Member Author

Choose a reason for hiding this comment

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

this it the fix. this VB helper doesn't match the rest (like "IsBetweenMembers") and returns teh type statement not the type block.


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