提交 7333c9a6 编写于 作者: G Gen Lu

Handle linked files in MoveToNamespace refactoring

上级 b33ec5a2
......@@ -7,6 +7,7 @@
using Microsoft.CodeAnalysis.Editor.UnitTests;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.MoveToNamespace;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Test.Utilities.MoveToNamespace;
using Microsoft.VisualStudio.Composition;
......@@ -1226,5 +1227,63 @@ partial class MyClass
{
}",
expectedSuccess: false);
[Fact, Trait(Traits.Feature, Traits.Features.MoveToNamespace)]
[WorkItem(39234, "https://github.com/dotnet/roslyn/issues/39234")]
public async Task TestMultiTargetingProject()
{
// Create two projects with same project file path and single linked document to simulate a multi-targeting project.
var input = @"<Workspace>
<Project Language=""C#"" CommonReferences=""true"" AssemblyName=""Proj1"" FilePath=""SharedProj.csproj"">
<Document FilePath=""CurrentDocument.cs"">
namespace A
{
public class Class1
{
}
public class Class2[||]
{
}
}
</Document>
</Project>
<Project Language=""C#"" CommonReferences=""true"" AssemblyName=""Proj2"" FilePath=""SharedProj.csproj"">
<Document IsLinkFile=""true"" LinkAssemblyName=""Proj1"" LinkFilePath=""CurrentDocument.cs""/>
</Project>
</Workspace>";
var expected =
@"namespace A
{
public class Class1
{
}
}
namespace B
{
public class Class2
{
}
}";
using var workspace = TestWorkspace.Create(System.Xml.Linq.XElement.Parse(input), exportProvider: ExportProviderFactory.CreateExportProvider());
// Set the target namespace to "B"
var testDocument = workspace.Projects.Single(p => p.Name == "Proj1").Documents.Single();
var document = workspace.CurrentSolution.GetDocument(testDocument.Id);
var movenamespaceService = document.GetLanguageService<IMoveToNamespaceService>();
var moveToNamespaceOptions = new MoveToNamespaceOptionsResult("B");
((TestMoveToNamespaceOptionsService)movenamespaceService.OptionsService).SetOptions(moveToNamespaceOptions);
var (_, action) = await GetCodeActionsAsync(workspace, default);
var operations = await VerifyActionAndGetOperationsAsync(workspace, action, default);
var result = ApplyOperationsAndGetSolution(workspace, operations);
var changedDocument = result.Item2.GetDocument(testDocument.Id);
var changedRoot = await changedDocument.GetSyntaxRootAsync();
var actualText = changedRoot.ToFullString();
Assert.Equal(expected, actualText);
}
}
}
......@@ -253,7 +253,13 @@ private static async Task<ImmutableArray<ISymbol>> GetMemberSymbolsAsync(Documen
MoveTypeOperationKind.MoveTypeNamespaceScope,
cancellationToken).ConfigureAwait(false);
var modifiedDocument = modifiedSolution.GetDocument(document.Id);
// Since MoveTypeService doesn't handle linked files, we need to merge the diff ourselves,
// otherwise, we will end up with multiple linked documents with different content.
var diffMergingSession = new LinkedFileDiffMergingSession(document.Project.Solution, modifiedSolution, modifiedSolution.GetChanges(document.Project.Solution), logSessionInfo: false);
var mergeResult = await diffMergingSession.MergeDiffsAsync(mergeConflictHandler: null, cancellationToken: cancellationToken).ConfigureAwait(false);
var mergedSolution = mergeResult.MergedSolution;
var modifiedDocument = mergedSolution.GetDocument(document.Id);
var syntaxRoot = await modifiedDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var syntaxNode = syntaxRoot.GetAnnotatedNodes(AbstractMoveTypeService.NamespaceScopeMovedAnnotation).SingleOrDefault();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册