Skip to content

Commit

Permalink
Merge pull request #73424 from CyrusNajmabadi/diffMergeLOH
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored May 13, 2024
2 parents c2338d6 + 531a9d5 commit de0af4d
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,113 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.UnitTests.LinkedFiles
namespace Microsoft.CodeAnalysis.Editor.UnitTests.LinkedFiles;

public sealed class LinkedFileDiffMergingEditorTests : AbstractCodeActionTest
{
public partial class LinkedFileDiffMergingEditorTests : AbstractCodeActionTest
{
private const string WorkspaceXml = @"<Workspace>
<Project Language=""C#"" CommonReferences=""true"" AssemblyName=""CSProj"" PreprocessorSymbols=""Proj1"">
<Document FilePath = ""C.cs""><![CDATA[public class [|C|] { }]]></Document>
</Project>
<Project Language = ""C#"" CommonReferences=""true"" PreprocessorSymbols=""Proj2"">
<Document IsLinkFile = ""true"" LinkAssemblyName=""CSProj"" LinkFilePath=""C.cs""/>
</Project>
</Workspace>";

protected internal override string GetLanguage()
=> LanguageNames.CSharp;

protected override CodeRefactoringProvider CreateCodeRefactoringProvider(EditorTestWorkspace workspace, TestParameters parameters)
=> new TestCodeRefactoringProvider();

[WpfFact]
public async Task TestCodeActionPreviewAndApply()
private const string WorkspaceXml = """
<Workspace>
<Project Language="C#" CommonReferences="true" AssemblyName="CSProj" PreprocessorSymbols="Proj1">
<Document FilePath = "C.cs"><![CDATA[public class [|C|]
{
public class D
{
}
}]]></Document>
</Project>
<Project Language = "C#" CommonReferences="true" PreprocessorSymbols="Proj2">
<Document IsLinkFile = "true" LinkAssemblyName="CSProj" LinkFilePath="C.cs"/>
</Project>
</Workspace>
""";

private const string s_expectedCode = """
internal class C
{
// TODO: WPF required due to https://github.com/dotnet/roslyn/issues/46153
using var workspace = EditorTestWorkspace.Create(WorkspaceXml, composition: EditorTestCompositions.EditorFeaturesWpf);
var codeIssueOrRefactoring = await GetCodeRefactoringAsync(workspace, new TestParameters());
private class D
{
}
}
""";

var expectedCode = "private class D { }";
protected internal override string GetLanguage()
=> LanguageNames.CSharp;

await TestActionOnLinkedFiles(
workspace,
expectedText: expectedCode,
action: codeIssueOrRefactoring.CodeActions[0].action,
expectedPreviewContents: expectedCode);
}
protected override CodeRefactoringProvider CreateCodeRefactoringProvider(EditorTestWorkspace workspace, TestParameters parameters)
=> new TestCodeRefactoringProvider();

[Fact]
public async Task TestWorkspaceTryApplyChangesDirectCall()
{
using var workspace = EditorTestWorkspace.Create(WorkspaceXml);
var solution = workspace.CurrentSolution;
[WpfFact]
public async Task TestCodeActionPreviewAndApply()
{
// TODO: WPF required due to https://github.com/dotnet/roslyn/issues/46153
using var workspace = EditorTestWorkspace.Create(WorkspaceXml, composition: EditorTestCompositions.EditorFeaturesWpf);
var codeIssueOrRefactoring = await GetCodeRefactoringAsync(workspace, new TestParameters());

await TestActionOnLinkedFiles(
workspace,
expectedText: s_expectedCode,
action: codeIssueOrRefactoring.CodeActions[0].action,
expectedPreviewContents: """
internal class C
{
private class D
{
...
""");
}

var documentId = workspace.Documents.Single(d => !d.IsLinkFile).Id;
var text = await workspace.CurrentSolution.GetDocument(documentId).GetTextAsync();
[Fact]
public async Task TestWorkspaceTryApplyChangesDirectCall()
{
using var workspace = EditorTestWorkspace.Create(WorkspaceXml);
var solution = workspace.CurrentSolution;

var linkedDocumentId = workspace.Documents.Single(d => d.IsLinkFile).Id;
var linkedText = await workspace.CurrentSolution.GetDocument(linkedDocumentId).GetTextAsync();
var documentId = workspace.Documents.Single(d => !d.IsLinkFile).Id;
var text = await workspace.CurrentSolution.GetRequiredDocument(documentId).GetTextAsync();

var newSolution = solution
.WithDocumentText(documentId, text.Replace(13, 1, "D"))
.WithDocumentText(linkedDocumentId, linkedText.Replace(0, 6, "private"));
var linkedDocumentId = workspace.Documents.Single(d => d.IsLinkFile).Id;
var linkedText = await workspace.CurrentSolution.GetRequiredDocument(linkedDocumentId).GetTextAsync();

workspace.TryApplyChanges(newSolution);
var textString = linkedText.ToString();

var expectedMergedText = "private class D { }";
Assert.Equal(expectedMergedText, (await workspace.CurrentSolution.GetDocument(documentId).GetTextAsync()).ToString());
Assert.Equal(expectedMergedText, (await workspace.CurrentSolution.GetDocument(linkedDocumentId).GetTextAsync()).ToString());
}
var newSolution = solution
.WithDocumentText(documentId, text.Replace(textString.IndexOf("public"), "public".Length, "internal"))
.WithDocumentText(linkedDocumentId, linkedText.Replace(textString.LastIndexOf("public"), "public".Length, "private"));

workspace.TryApplyChanges(newSolution);

protected override ParseOptions GetScriptOptions()
=> throw new NotSupportedException();
Assert.Equal(s_expectedCode, (await workspace.CurrentSolution.GetRequiredDocument(documentId).GetTextAsync()).ToString());
Assert.Equal(s_expectedCode, (await workspace.CurrentSolution.GetRequiredDocument(linkedDocumentId).GetTextAsync()).ToString());
}

protected override ParseOptions GetScriptOptions()
=> throw new NotSupportedException();

private class TestCodeRefactoringProvider : CodeRefactorings.CodeRefactoringProvider
private sealed class TestCodeRefactoringProvider : CodeRefactoringProvider
{
public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
var document = context.Document;
var linkedDocument = document.Project.Solution.Projects.Single(p => p != document.Project).Documents.Single();
var document = context.Document;
var linkedDocument = document.Project.Solution.Projects.Single(p => p != document.Project).Documents.Single();
var sourceText = await linkedDocument.GetTextAsync();
var textString = sourceText.ToString();

var newSolution = document.Project.Solution
.WithDocumentText(document.Id, (await document.GetTextAsync()).Replace(13, 1, "D"))
.WithDocumentText(linkedDocument.Id, (await linkedDocument.GetTextAsync()).Replace(0, 6, "private"));
var newSolution = document.Project.Solution
.WithDocumentText(document.Id, (await document.GetTextAsync()).Replace(textString.IndexOf("public"), "public".Length, "internal"))
.WithDocumentText(linkedDocument.Id, sourceText.Replace(textString.LastIndexOf("public"), "public".Length, "private"));

#pragma warning disable RS0005
context.RegisterRefactoring(CodeAction.Create("Description", (ct) => Task.FromResult(newSolution)), context.Span);
#pragma warning restore RS0005
}
context.RegisterRefactoring(CodeAction.Create("Description", _ => Task.FromResult(newSolution)), context.Span);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,6 @@ protected static string MakeUnique(string baseName, INamedTypeSymbol containingT
return NameGenerator.GenerateUniqueName(baseName, containingTypeMemberNames.ToSet(), StringComparer.Ordinal);
}

internal override IEnumerable<SyntaxNode> GetConstructorNodes(INamedTypeSymbol containingType)
protected override IEnumerable<SyntaxNode> GetConstructorNodes(INamedTypeSymbol containingType)
=> containingType.Constructors.SelectMany(c => c.DeclaringSyntaxReferences.Select(d => d.GetSyntax()));
}
Loading

0 comments on commit de0af4d

Please sign in to comment.