DotNetAnalyzers / StyleCopAnalyzers

An implementation of StyleCop rules using the .NET Compiler Platform
MIT License
2.66k stars 507 forks source link

"SA1649: File name must match first type name." fix "Rename file to match first type name" will delete and add new file instead of just renaming the file which breaks TFS version control server file history #2277

Open ramiabughazaleh opened 7 years ago

ramiabughazaleh commented 7 years ago

"SA1649: File name must match first type name." fix "Rename file to match first type name" will delete and add new file instead of just renaming the file which breaks TFS version control server file history.

Test.cs

namespace TestApp
{
    public class MyTest // SA1649: File name must match first type name.
    {
    }
}

Produces warning "SA1649: File name must match first type name." as expected.

However, if I let DotNetAnalyzers fix this by selecting "Rename file to match first type name" from the list of potential fixes, Test.cs shows up as deleted and MyTest.cs shows up as added in Visual Studio's "Pending Changes" window.

In contrast, if I just manually rename the file in the "Solution Explorer" window, the file shows up as rename in Visual Studio's "Pending Changes" window.

If I go ahead and check in the changes performed by DotNetAnalyzers, then I cannot view the history on the new file in TFS; whereas the history is maintained if I check in the changes performed when manually renaming the file.

Visual Studio 2015 Update 3 DotNetAnalyzers 1.0.0 TFS 2015 Update 3

Thank you.

vweijsters commented 7 years ago

Marking this blocked because there is no proper document rename API in Roslyn to achieve this behavior.

There already is an issue filed (dotnet/roslyn#6261) for this.

ClassTerr commented 3 years ago

Any news on this?

sharwell commented 3 years ago

@ClassTerr At this time I'm not aware of anyone pursuing this issue. If you find a solution please consider submitting a pull request for it.

bjornhellander commented 1 year ago

Looks like something like this might work (although I have not actually tried it):

Microsoft.CodeAnalysis.Rename.Renamer.RenameDocumentActionSet actionSet =
    await Microsoft.CodeAnalysis.Rename.Renamer.RenameDocumentAsync(
        document,
        newDocumentName,
        newDocumentFolders: null,
        optionSet: null,
        cancellationToken).ConfigureAwait(false);
solution = await actionSet.UpdateSolutionAsync(solution, cancellationToken).ConfigureAwait(false);

But this is only available in later Roslyn versions. I feel unsure about how to add "light up" support for this in a good way. Compiling expression trees does not sound fun, due to the return type Task<Microsoft.CodeAnalysis.Rename.Renamer.RenameDocumentActionSet>. Generate source code and compile it at runtime?

bjornhellander commented 4 months ago

This same problem also causes the file to be closed when applying the fix.