Skip to content

Commit 3efcf4f

Browse files
Fixup
1 parent 574f7e7 commit 3efcf4f

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

src/Features/CSharp/Portable/GenerateDefaultConstructors/CSharpGenerateDefaultConstructorsCodeFixProvider.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Immutable;
77
using System.Composition;
88
using Microsoft.CodeAnalysis.CodeFixes;
9+
using Microsoft.CodeAnalysis.CSharp.Syntax;
910
using Microsoft.CodeAnalysis.GenerateDefaultConstructors;
1011
using Microsoft.CodeAnalysis.Host.Mef;
1112

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

2728
public override ImmutableArray<string> FixableDiagnosticIds { get; } =
2829
ImmutableArray.Create(CS1729, CS7036, CS8983);
30+
31+
protected override SyntaxToken? TryGetTypeName(SyntaxNode typeDeclaration)
32+
=> (typeDeclaration as BaseTypeDeclarationSyntax)?.Identifier;
2933
}
3034
}

src/Features/Core/Portable/GenerateDefaultConstructors/AbstractGenerateDefaultConstructorCodeFixProvider.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ internal abstract class AbstractGenerateDefaultConstructorCodeFixProvider : Code
1515
{
1616
public override FixAllProvider? GetFixAllProvider() => null;
1717

18+
protected abstract SyntaxToken? TryGetTypeName(SyntaxNode typeDeclaration);
19+
1820
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
1921
{
2022
var cancellationToken = context.CancellationToken;
@@ -23,17 +25,18 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
2325
if (diagnostic == null)
2426
return;
2527

26-
var syntaxFacts = document.GetRequiredLanguageService<ISyntaxFactsService>();
27-
var headerFacts = document.GetRequiredLanguageService<IHeaderFactsService>();
28-
2928
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
29+
var headerFacts = document.GetRequiredLanguageService<IHeaderFactsService>();
3030
if (!headerFacts.IsOnTypeHeader(root, diagnostic.Location.SourceSpan.Start, fullHeader: true, out var typeDecl))
3131
return;
3232

33-
var typeName = syntaxFacts.GetIdentifierOfTypeDeclaration(typeDecl);
33+
var typeName = TryGetTypeName(typeDecl);
34+
if (typeName == null)
35+
return;
36+
3437
var service = document.GetRequiredLanguageService<IGenerateDefaultConstructorsService>();
3538
var actions = await service.GenerateDefaultConstructorsAsync(
36-
document, new TextSpan(typeName.Span.Start, 0), forRefactoring: false, cancellationToken).ConfigureAwait(false);
39+
document, new TextSpan(typeName.Value.Span.Start, 0), forRefactoring: false, cancellationToken).ConfigureAwait(false);
3740
context.RegisterFixes(actions, diagnostic);
3841
}
3942
}

src/Features/VisualBasic/Portable/GenerateDefaultConstructors/VisualBasicGenerateDefaultConstructorsCodeFixProvider.vb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Imports System.Composition
77
Imports Microsoft.CodeAnalysis.CodeFixes
88
Imports Microsoft.CodeAnalysis.GenerateDefaultConstructors
99
Imports Microsoft.CodeAnalysis.Host.Mef
10+
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
1011

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

2425
Public Overrides ReadOnly Property FixableDiagnosticIds As Immutable.ImmutableArray(Of String) =
2526
ImmutableArray.Create(BC30387, BC40056)
27+
28+
Protected Overrides Function TryGetTypeName(typeDeclaration As SyntaxNode) As SyntaxToken?
29+
Return TryCast(typeDeclaration, TypeBlockSyntax)?.BlockStatement.Identifier
30+
End Function
2631
End Class
2732
End Namespace

0 commit comments

Comments
 (0)