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 @@ -1349,6 +1349,50 @@ public partial class Goo
</Workspace>
""");

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/76565")]
public Task TestCouldInitializeThrowingProperty_ButGeneratePropertyInstead()
=> TestInRegularAndScript1Async(
"""
using System;

class C([||]string s)
{
private string S => throw new NotImplementedException();
}
""",
"""
using System;

class C(string s)
{
public string S1 { get; } = s;

private string S => throw new NotImplementedException();
}
""", index: 1);

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/76565")]
public Task TestCouldInitializeThrowingProperty_ButGenerateFieldInstead()
=> TestInRegularAndScript1Async(
"""
using System;

class C([||]string s)
{
private string S => throw new NotImplementedException();
}
""",
"""
using System;

class C(string s)
{
private readonly string s = s;

private string S => throw new NotImplementedException();
}
""", index: 2);

[Fact]
public Task TestUpdateCodeToReferenceExistingField1()
=> TestInRegularAndScript1Async(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.InitializeParameter;
using Microsoft.CodeAnalysis.Shared.Collections;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Naming;
using Roslyn.Utilities;
Expand Down Expand Up @@ -73,11 +72,18 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);

var fieldOrProperty = TryFindMatchingUninitializedFieldOrPropertySymbol();
var refactorings = fieldOrProperty == null
? HandleNoExistingFieldOrProperty()
: HandleExistingFieldOrProperty();
using var refactorings = TemporaryArray<CodeAction>.Empty;
if (fieldOrProperty != null)
{
// Found a field/property that this parameter should be assigned to. Just offer the simple assignment to it.
refactorings.Add(CreateCodeAction(
string.Format(fieldOrProperty.Kind == SymbolKind.Field ? FeaturesResources.Initialize_field_0 : FeaturesResources.Initialize_property_0, fieldOrProperty.Name),
cancellationToken => AddAssignmentForPrimaryConstructorAsync(document, parameter, fieldOrProperty, cancellationToken)));
}

AddCreateFieldOrPropertyCodeActions();

context.RegisterRefactorings(refactorings.ToImmutableArray(), context.Span);
context.RegisterRefactorings(refactorings.ToImmutableAndClear(), context.Span);
return;

ISymbol? TryFindMatchingUninitializedFieldOrPropertySymbol()
Expand Down Expand Up @@ -133,15 +139,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
static CodeAction CreateCodeAction(string title, Func<CancellationToken, Task<Solution>> createSolution)
=> CodeAction.Create(title, createSolution, title);

IEnumerable<CodeAction> HandleExistingFieldOrProperty()
{
// Found a field/property that this parameter should be assigned to. Just offer the simple assignment to it.
yield return CreateCodeAction(
string.Format(fieldOrProperty.Kind == SymbolKind.Field ? FeaturesResources.Initialize_field_0 : FeaturesResources.Initialize_property_0, fieldOrProperty.Name),
cancellationToken => AddAssignmentForPrimaryConstructorAsync(document, parameter, fieldOrProperty, cancellationToken));
}

IEnumerable<CodeAction> HandleNoExistingFieldOrProperty()
void AddCreateFieldOrPropertyCodeActions()
{
// Didn't find a field/prop that this parameter could be assigned to. Offer to create new one and assign to that.

Expand All @@ -159,8 +157,8 @@ IEnumerable<CodeAction> HandleNoExistingFieldOrProperty()
string.Format(FeaturesResources.Create_and_assign_property_0, property.Name),
cancellationToken => AddMultipleMembersAsync(document, typeDeclaration, [parameter], [property], cancellationToken));

yield return siblingFieldOrProperty is IFieldSymbol ? fieldAction : propertyAction;
yield return siblingFieldOrProperty is IFieldSymbol ? propertyAction : fieldAction;
refactorings.Add(siblingFieldOrProperty is IFieldSymbol ? fieldAction : propertyAction);
refactorings.Add(siblingFieldOrProperty is IFieldSymbol ? propertyAction : fieldAction);

var parameters = GetParametersWithoutAssociatedMembers();
if (parameters.Length >= 2)
Expand All @@ -172,8 +170,8 @@ IEnumerable<CodeAction> HandleNoExistingFieldOrProperty()
FeaturesResources.Create_and_assign_remaining_as_properties,
cancellationToken => AddMultipleMembersAsync(document, typeDeclaration, parameters, parameters.SelectAsArray(CreateProperty), cancellationToken));

yield return siblingFieldOrProperty is IFieldSymbol ? allFieldsAction : allPropertiesAction;
yield return siblingFieldOrProperty is IFieldSymbol ? allPropertiesAction : allFieldsAction;
refactorings.Add(siblingFieldOrProperty is IFieldSymbol ? allFieldsAction : allPropertiesAction);
refactorings.Add(siblingFieldOrProperty is IFieldSymbol ? allPropertiesAction : allFieldsAction);
}
}

Expand Down
Loading