Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.CSharp.UseAutoProperty;
Expand Down Expand Up @@ -2999,4 +3000,62 @@ class Class
readonly ref int P => ref i;
}
""");

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/77011")]
public Task TestRemoveThisIfPreferredCodeStyle()
=> TestInRegularAndScriptAsync(
"""
class C
{
[|private readonly string a;|]

public C(string a)
{
this.a = a;
}

public string A => a;
}
""",
"""
class C
{
public C(string a)
{
A = a;
}

public string A { get; }
}
""",
options: Option(CodeStyleOptions2.QualifyPropertyAccess, false, NotificationOption2.Error));

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/77011")]
public Task TestKeepThisIfPreferredCodeStyle()
=> TestInRegularAndScriptAsync(
"""
class C
{
[|private readonly string a;|]

public C(string a)
{
this.a = a;
}

public string A => a;
}
""",
"""
class C
{
public C(string a)
{
this.A = a;
}

public string A { get; }
}
""",
options: Option(CodeStyleOptions2.QualifyPropertyAccess, true, NotificationOption2.Error));
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task<IInlineRenameReplacementInfo> GetReplacementsAsync(
CancellationToken cancellationToken)
{
var conflicts = await _renameLocationSet.ResolveConflictsAsync(
_renameInfo.RenameSymbol, _renameInfo.GetFinalSymbolName(replacementText), nonConflictSymbolKeys: default, cancellationToken).ConfigureAwait(false);
_renameInfo.RenameSymbol, _renameInfo.GetFinalSymbolName(replacementText), cancellationToken).ConfigureAwait(false);

return new InlineRenameReplacementInfo(conflicts);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ partial class C

public C()
{
this.P = 0;
P = 0;
}
}".Trim()

Expand Down
5 changes: 2 additions & 3 deletions src/EditorFeatures/Test2/Rename/RenameEngineResult.vb
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,13 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Rename
Dim locations = Renamer.FindRenameLocationsAsync(
solution, symbol, renameOptions, CancellationToken.None).GetAwaiter().GetResult()

Return locations.ResolveConflictsAsync(symbol, renameTo, nonConflictSymbolKeys:=Nothing, CancellationToken.None).GetAwaiter().GetResult()
Return locations.ResolveConflictsAsync(symbol, renameTo, CancellationToken.None).GetAwaiter().GetResult()
Else
' This tests that rename properly works when the entire call is remoted to OOP and the final result is
' marshaled back.

Return Renamer.RenameSymbolAsync(
solution, symbol, renameTo, renameOptions,
nonConflictSymbolKeys:=Nothing, CancellationToken.None).GetAwaiter().GetResult()
solution, symbol, renameTo, renameOptions, CancellationToken.None).GetAwaiter().GetResult()
End If
End Function

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Formatting.Rules;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Rename;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.UseAutoProperty;
using Roslyn.Utilities;
Expand Down Expand Up @@ -59,16 +59,15 @@ protected override SyntaxNode GetNodeToRemove(VariableDeclaratorSyntax declarato

protected override PropertyDeclarationSyntax RewriteFieldReferencesInProperty(
PropertyDeclarationSyntax property,
LightweightRenameLocations fieldLocations,
ImmutableArray<ReferencedSymbol> fieldLocations,
CancellationToken cancellationToken)
{
// We're going to walk this property body, converting most reference of the field to use the `field` keyword
// instead. However, not all reference can be updated. For example, reference through another instance. Those
// we update to point at the property instead. So we grab that property name here to use in the rewriter.
var propertyIdentifierName = IdentifierName(property.Identifier.WithoutTrivia());

var identifierNames = fieldLocations.Locations
.Select(loc => loc.Location.FindNode(getInnermostNodeForTie: true, cancellationToken) as IdentifierNameSyntax)
var identifierNames = fieldLocations.SelectMany(loc => loc.Locations.Select(loc => loc.Location.FindNode(getInnermostNodeForTie: true, cancellationToken) as IdentifierNameSyntax))
.WhereNotNull()
.ToSet();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private static async Task<Solution> RenameAsync(
// edit the files in the current project
var resolution = await initialLocations
.Filter((documentId, span) => !linkedProjectIds.Contains(documentId.ProjectId) && filter(documentId, span))
.ResolveConflictsAsync(field, finalName, nonConflictSymbolKeys: default, cancellationToken).ConfigureAwait(false);
.ResolveConflictsAsync(field, finalName, cancellationToken).ConfigureAwait(false);

Contract.ThrowIfFalse(resolution.IsSuccessful);

Expand Down
Loading
Loading