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 @@ -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 @@ -2940,4 +2941,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()
Copy link
Member Author

Choose a reason for hiding this comment

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

As part of this, use-auto-prop is no longer going through the 'rename' codepaths itself. That is because the space of what is needs to rename is so narrow that there isn't much benefit here, and because we used to have to hack up the rename engine itself to support the somewhat strange scenario that Use-Auto-Prop is following. Specifically, 'use auto prop' renames a field (with name 'f') to match the name of a property 'P'. This is, definitionally, a conflict. So we hacked up the rename engine to let the caller tell it to ignore that.

We no longer need this capability, so all that threading through of these values is gone.

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